valhalla implementation changes

This commit is contained in:
Arnaud Delcasse 2025-03-05 00:45:21 +01:00
parent d03cd41008
commit faabcc54f5
2 changed files with 23 additions and 12 deletions

View File

@ -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
}

View File

@ -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
}