add directions endpoint to routing service

This commit is contained in:
Asim Aslam
2021-04-27 09:56:56 +01:00
parent 5a329ac461
commit ae069ef031
6 changed files with 796 additions and 85 deletions

View File

@@ -18,6 +18,121 @@ type OSRM struct {
Client *osrm.OSRM
}
func (o *OSRM) Directions(ctx context.Context, req *pb.DirectionsRequest, rsp *pb.DirectionsResponse) error {
// validate the request
if req.Origin == nil {
return ErrMissingOrigin
}
if req.Destination == nil {
return ErrMissingDestination
}
if err := validatePoint(req.Origin); err != nil {
return err
}
if err := validatePoint(req.Destination); err != nil {
return err
}
orig := req.Origin
dest := req.Destination
if o.Client == nil {
u, _ := url.Parse(o.Address)
if u.Scheme == "" {
u.Scheme = "http"
}
o.Client = osrm.NewFromURL(u.String())
}
ctx, cancelFn := context.WithTimeout(context.Background(), time.Second)
defer cancelFn()
resp, err := o.Client.Route(ctx, osrm.RouteRequest{
Profile: "car",
Coordinates: osrm.NewGeometryFromPointSet(geo.PointSet{
{orig.Longitude, orig.Latitude},
{dest.Longitude, dest.Latitude},
}),
Steps: osrm.StepsTrue,
Annotations: osrm.AnnotationsFalse,
Overview: osrm.OverviewFalse,
})
if err != nil {
return errors.InternalServerError("routing.eta", "failed to get route: %v", err.Error())
}
if len(resp.Routes) == 0 {
return nil
}
// set the estimated duration and distance
rsp.Duration = float64(resp.Routes[0].Duration)
rsp.Distance = float64(resp.Routes[0].Distance)
for _, leg := range resp.Routes[0].Legs {
for _, step := range leg.Steps {
// set the waypoints for the route
for _, intersect := range step.Intersections {
rsp.Waypoints = append(rsp.Waypoints, &pb.Waypoint{
Name: step.Name,
Location: &pb.Point{
Latitude: intersect.Location.Lat(),
Longitude: intersect.Location.Lng(),
},
})
}
instruction := step.Maneuver.Modifier
switch step.Maneuver.Type {
case "new name":
instruction = "go " + step.Maneuver.Modifier
default:
instruction = step.Maneuver.Type + " " + step.Maneuver.Modifier
}
var intersections []*pb.Intersection
for _, is := range step.Intersections {
var bearings []float64
for _, bearing := range is.Bearings {
bearings = append(bearings, float64(bearing))
}
intersections = append(intersections, &pb.Intersection{
Bearings: bearings,
Location: &pb.Point{
Latitude: is.Location.Lat(),
Longitude: is.Location.Lng(),
},
})
}
// set the directions for the route
rsp.Directions = append(rsp.Directions, &pb.Direction{
Name: step.Name,
Instruction: instruction,
Distance: float64(step.Distance),
Duration: float64(step.Duration),
Maneuver: &pb.Maneuver{
BearingBefore: float64(step.Maneuver.BearingBefore),
BearingAfter: float64(step.Maneuver.BearingAfter),
Location: &pb.Point{
Latitude: float64(step.Maneuver.Location.Lat()),
Longitude: float64(step.Maneuver.Location.Lng()),
},
Action: step.Maneuver.Type,
Direction: step.Maneuver.Modifier,
},
Intersections: intersections,
})
}
}
return nil
}
func (o *OSRM) Eta(ctx context.Context, req *pb.EtaRequest, rsp *pb.EtaResponse) error {
// validate the request
if req.Origin == nil {
@@ -126,18 +241,24 @@ func (o *OSRM) Route(ctx context.Context, req *pb.RouteRequest, rsp *pb.RouteRes
return errors.InternalServerError("routing.route", "failed to get route: %v", err.Error())
}
for _, routes := range resp.Routes {
for _, leg := range routes.Legs {
for _, step := range leg.Steps {
for _, intersect := range step.Intersections {
rsp.Waypoints = append(rsp.Waypoints, &pb.Waypoint{
Name: step.Name,
Location: &pb.Point{
Latitude: intersect.Location.Lat(),
Longitude: intersect.Location.Lng(),
},
})
}
if len(resp.Routes) == 0 {
return nil
}
// set distance and duration
rsp.Duration = float64(resp.Routes[0].Duration)
rsp.Distance = float64(resp.Routes[0].Distance)
for _, leg := range resp.Routes[0].Legs {
for _, step := range leg.Steps {
for _, intersect := range step.Intersections {
rsp.Waypoints = append(rsp.Waypoints, &pb.Waypoint{
Name: step.Name,
Location: &pb.Point{
Latitude: intersect.Location.Lat(),
Longitude: intersect.Location.Lng(),
},
})
}
}
}