Add data to routes

This commit is contained in:
Arnaud Delcasse 2023-03-29 12:51:09 +02:00
parent fb30573934
commit a2dc346ed5
2 changed files with 20 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package routing
import ( import (
"fmt" "fmt"
"time"
"github.com/paulmach/orb" "github.com/paulmach/orb"
) )
@ -20,8 +21,15 @@ func NewRoutingService(service_type string, baseUrl string) (RoutingService, err
type Route struct { type Route struct {
Summary RouteSummary Summary RouteSummary
Legs []RouteLeg
} }
type RouteSummary struct { type RouteSummary struct {
Polyline string Polyline string
} }
type RouteLeg struct {
Distance float64
Duration time.Duration
Polyline string
}

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"time"
"git.coopgo.io/coopgo-platform/routing-service/encoding/polylines" "git.coopgo.io/coopgo-platform/routing-service/encoding/polylines"
"git.coopgo.io/coopgo-platform/routing-service/proto/valhalla" "git.coopgo.io/coopgo-platform/routing-service/proto/valhalla"
@ -55,16 +56,27 @@ func (v *ValhallaRouting) Route(locations []orb.Point) (route *Route, err error)
decodedLinestring := orb.LineString{} decodedLinestring := orb.LineString{}
legs := []RouteLeg{}
for _, leg := range resp.Directions.Routes[0].Legs { for _, leg := range resp.Directions.Routes[0].Legs {
shape := leg.Shape shape := leg.Shape
decodedShape := polylines.Decode(&shape, 6) decodedShape := polylines.Decode(&shape, 6)
decodedLinestring = append(decodedLinestring, decodedShape...) decodedLinestring = append(decodedLinestring, decodedShape...)
routeLeg := RouteLeg{
Distance: float64(leg.Summary.Length),
Duration: time.Duration(leg.Summary.Time) * time.Second,
Polyline: polylines.Encode(decodedShape),
}
legs = append(legs, routeLeg)
} }
return &Route{ return &Route{
Summary: RouteSummary{ Summary: RouteSummary{
Polyline: polylines.Encode(decodedLinestring), Polyline: polylines.Encode(decodedLinestring),
}, },
Legs: legs,
}, nil }, nil
} }