From faabcc54f53649c5e61b6d6b5f800b88659d4895 Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Wed, 5 Mar 2025 00:45:21 +0100 Subject: [PATCH] valhalla implementation changes --- routing.go | 12 ++++++------ valhalla.go | 23 +++++++++++++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/routing.go b/routing.go index 7804dcd..f02eda5 100644 --- a/routing.go +++ b/routing.go @@ -20,16 +20,16 @@ func NewRoutingService(service_type string, baseUrl string) (RoutingService, err } type Route struct { - Summary RouteSummary + Summary Legs []RouteLeg } -type RouteSummary struct { - Polyline string -} - -type RouteLeg struct { +type Summary struct { Distance float64 Duration time.Duration Polyline string } + +type RouteLeg struct { + Summary +} diff --git a/valhalla.go b/valhalla.go index 6f8479c..b987829 100644 --- a/valhalla.go +++ b/valhalla.go @@ -25,7 +25,6 @@ func NewValhallaRouting(baseURL string) (*ValhallaRouting, error) { } func (v *ValhallaRouting) Route(locations []orb.Point) (route *Route, err error) { - valhalla_locations := []*valhalla.Location{} for _, loc := range locations { @@ -57,6 +56,8 @@ func (v *ValhallaRouting) Route(locations []orb.Point) (route *Route, err error) } decodedLinestring := orb.LineString{} + routeDuration := time.Duration(0) + routeLength := 0.0 legs := []RouteLeg{} @@ -65,17 +66,27 @@ func (v *ValhallaRouting) Route(locations []orb.Point) (route *Route, err error) decodedShape := polylines.Decode(&shape, 6) decodedLinestring = append(decodedLinestring, decodedShape...) + length := float64(leg.Summary.Length) + routeLength = routeLength + length + + duration := time.Duration(leg.Summary.Time) * time.Second + routeDuration = routeDuration + duration + routeLeg := RouteLeg{ - Distance: float64(leg.Summary.Length), - Duration: time.Duration(leg.Summary.Time) * time.Second, - Polyline: polylines.Encode(decodedShape, 5), + Summary: Summary{ + Distance: float64(length), + Duration: duration, + Polyline: polylines.Encode(decodedShape, 5), + }, } legs = append(legs, routeLeg) } return &Route{ - Summary: RouteSummary{ + Summary: Summary{ + Distance: routeLength, + Duration: routeDuration, Polyline: polylines.Encode(decodedLinestring, 5), }, Legs: legs, @@ -96,7 +107,7 @@ func (v *ValhallaRouting) protocolBufferRequest(api *valhalla.Api, path string) client := http.Client{} resp, err := client.Do(req) - //resp, err := http.Post(v.BaseURL+path, "application/x-protobuf", bytes.NewBuffer(data)) + // resp, err := http.Post(v.BaseURL+path, "application/x-protobuf", bytes.NewBuffer(data)) if err != nil { return nil, err }