gis-ops / routingjs

Library for consistent access to various vehicle routing APIs
https://gis-ops.github.io/routingjs/
MIT License
44 stars 4 forks source link

Valhalla: avoid locations/polygons incorrectly formatted in Directions request #31

Open nospi opened 1 year ago

nospi commented 1 year ago

The avoidLocations and avoidPolygons DirectionsOptions are not formatted as Valhalla expects them to be.

image

When running with dryRun: true in order to see what's going on:

image

I dug into the built code; the issue is here:

image

The Valhalla API expects the avoid locations parameters to be formatted identically to the directions location parameters (object with lat/lon properties). https://github.com/valhalla/valhalla-docs/blob/master/turn-by-turn/api-reference.md

I am passing an array, so I've edited only the top portion of this segment of code:

image

Or perhaps something more general:

params.exclude_locations = avoidLocations.map(([lat, lon]) => ({ lat, lon }));

That fixed the issue for me (for avoidLocations only; would also need the same in avoidPolygons). Dry run result:

image

And the final success from the valhalla public server:

image

I wanted to write a PR but I'm not sure how this would be handled from the typescript/interfaces perspective; but please let me me know if you'd like me to have a go at it.

Cheers!

EDIT: just saw the _buildLocations function.

nilsnolde commented 1 year ago

Oops, yeah that’s the „kids diseases“:) We’d be happy with a PR, as you wish. FWIW, avoid polygons does not take location objects but a plain array of coordinates (essentially a open/closed ring), as that’s not supposed to snap to road segments but describe polygons.

nospi commented 1 year ago

I've made the changes on a local branch but I'm afraid I've not submitted a PR before. Do I need to fork the repo in order to push to a branch for comparison and PR creation?

EDIT: all done, submitted a PR from a fork.