Skip to content

Basic support for Mapbox / OSRM endpoint - #1422

Closed
boldtrn wants to merge 29 commits into
graphhopper:masterfrom
boldtrn:mapbox_api
Closed

boldtrn wants to merge 29 commits into
graphhopper:masterfrom
boldtrn:mapbox_api

Conversation

@boldtrn

@boldtrn boldtrn commented Jul 11, 2018 •

Copy link
Copy Markdown
Member

This PR approaches #1265.

So far the implementation is still quite limited, but works well in my tests with the Mapbox SDK. This PR tries not to change anything in the GraphHopper core.

Some of the current limitations are:

  • Some of the response data is faked, including, driving_side, maneuver bearings, intersection data
  • The available request parameters are very limited
  • Voice instructions generation is pretty simple, every instruction is only spoken once
  • Banner instruction generation is pretty simple

Let me know what you think about the current state :).

return encodePolyline(poly, includeElevation, 1e5);
}

public static String encodePolyline(PointList poly, boolean includeElevation, double precision) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passing the precision is required to support the request parameter polyline6

@karussell karussell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and straightforward if I understand this all correctly :) Nice!

public class MapboxResponseConverter {

// TODO: this is already in GraphHopper, but getting it from there is not possible easily?
private static final TranslationMap trMap = new TranslationMap().doImport();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean hopper.getTranslationMap?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but in the web-bundle we only have a reference to the GraphHopperAPI interface. We could probably check if the object is instance of GraphHopper and then get the TranslationMap? I thought that this would be too much overhead, but we could try?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we bind

bind(translationMap).to(TranslationMap.class);

We could maybe inject it in the resource?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks, that's a good idea. I tried to inject it and it seems to work. Could you check if this is what you meant?

ObjectNode json = JsonNodeFactory.instance.objectNode();

if (ghResponse.hasErrors())
throw new IllegalStateException("Cannot convert an errornous ghResponse");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be possible. I mean, what if the route is not found or you want to go to the US from Europe. We have to support error JSON too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is :). It's just a different method, see MapboxResponseConverter#convertFromGHResponseError. Since we have to change the http response status to 422 in MapboxResource (at line ~137), I thought it would be a bit cleaner to have the two methods separated. It would be easily mergeable though. WDYT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Then the error message needs to be changed to "should be handled before in MapboxResource"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sure :)


json.put("code", "Ok");
// TODO: Maybe we need a different format... uuid: "cji4ja4f8004o6xrsta8w4p4h"
json.put("uuid", UUID.randomUUID().toString().replaceAll("-", ""));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this uuid? Maybe we have to hash the response somehow for error reporting?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose is not documented (or I could not find it). I haven't checked how it is used in the code. I think this is used internally to match error reports with requests. I tried sending multiple requests with the same parameters, and the UUID was different every time (but only at the end, the first characters were similar, so there seems to be some logic behind this).

{
distanceAlongGeometry: 40.9,
announcement: "Exit the traffic circle",
ssmlAnnouncement: "<speak><amazon:effect name="drc"><prosody rate="1.08">Exit the traffic circle</prosody></amazon:effect></speak>",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this xml style inside JSON :D

@boldtrn boldtrn Jul 12, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually called SSML which is used to change the way TTS speaks the instructions. So the idea is that voice instructions are generated by the server, also the frequency and distance of the voice instructions is generated by the server.

* @author Robin Boldt
*/
@Path("mapbox/directions/v5/mapbox")
public class MapboxResource {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use OSRM for the Mapbox SDK? If yes, can we rename to OSRMResource? If no, this is perfect already :)

@boldtrn boldtrn Jul 12, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I don't think that this is possible easily. The problem is that the OSRM endpoint does not generate voice instructions nor banner instructions which are both important for the MapboxSDK.

}

private void throwIllegalArgumentException(String exceptionMessage) {
logger.error(exceptionMessage);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this method? An exception should be logged from dropwizard automatically

@boldtrn boldtrn Jul 12, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will try this again, but IIRC in my tests so far, errors have not been logged by dropwizard, that's why I added this :). But maybe this is only true for the test environment during the IT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, IllegalArgEx are supposed to avoid extra logging. But still it should print the request and info about a 'bad request'. If not, then we need to fix it globally for all IllegalArgEx

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand you correct, as a user the result would look like:

{
message: "Voice units only support metric right now",
hints: [
{
message: "Voice units only support metric right now",
details: "java.lang.IllegalArgumentException",
}
],
}

And yes, this is currently the GH style and not Mapbox style for error responses :). Not sure if we should and can change that for a single resource like using a custom exception mapper?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception should not be logged separately (please just throw an IllegalArgumentException or if not a bad request but indicating an error throw RuntimeException or IllegalState or whatever).

If there is a bad request and it is not logged at all, we have to fix it for all resources, not sure here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will simply change this :)


} catch (Exception e) {
logger.error("Exception while processing: ", e);
throw new MultiException(e);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: I think logging + throwing an exception seems duplicated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, removed it :)

private List<GHPoint> getPointsFromRequest(PathSegment pathSegment) {

// TODO: Could the map at some point break the order?
Set<String> pointParams = pathSegment.getMatrixParameters().keySet();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be indeed a bit risky. What is the parameter form? Isn't it possible to use the same approach like in RouteResource? @QueryParam("point") List<GHPoint> requestPoints

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the points in the Mapbox SDK are so called "MatrixParameters" and not as QueryParameters.

An example would look like this: ...directions/v5/mapbox/driving-traffic/9.453778,48.70906;9.450624,48.706879

I did some research, but only found this option to read this parameter form. As noted in the TODO, I am not especially happy with this either :).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And did you try it for multiple points and does it work?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked. It does not work, it seems that it can change the order.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, according to SO, this is not easily fixable with JAX-RS. I switched to parsing the URL string manually. This seems to work well enough :).

ObjectNode json = JsonNodeFactory.instance.objectNode();

if (ghResponse.hasErrors())
throw new IllegalStateException("Cannot convert an errornous ghResponse");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Then the error message needs to be changed to "should be handled before in MapboxResource"?

ArrayNode location = node.putArray("location");
// GeoJson lon,lat
location.add(Helper.round6(lon));
location.add(Helper.round6(lat));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is also dependent on the precision asked from outside? (I think I saw something for the simplification)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is only for the polyline. The request parameter is geometries=polyline6 and only changes the precision of the Google Polyline like encoding.


double minPathPrecision = 1;
if (overview.equals("full"))
minPathPrecision = 0.1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why simplification is disabled? Does it not work or do we avoid a second request for the details?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why simplification is disabled?

Yes, the overview parameter means: "Type of returned overview geometry. Can be full (the most detailed geometry available), simplified (a simplified version of the full geometry), or false (no overview geometry). The default is simplified".

I found that the Mapbox SDK works better, when more points are in the geometry. That's why I disabled simplification. Even with disabled simplification, the Mapbox API seems to return a more detailed geometry than GH.

Regarding PathDetails: This is not yet implemented for the Mapbox endpoint. This seems to work different to the way we do PathDetails and might require a bit of time and planning (haven't checked in detail yet).

@boldtrn

boldtrn commented Jul 19, 2018 •

Copy link
Copy Markdown
Member Author

I updated this PR, improved the error logging and changed the way the points are parsed.

Let me know if we can merge this or if there is anything else?

}

return points;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the following work?

@Path("/{profile}/{coordinatesString}")

and then String[] pointStrings = coordinatesString.split(";");

This way at least the first three lines can be avoided.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this was my first idea as well :).

The problem is that the path parameter coordinate string would only contain the first coordinate up to the first ;, everything else is parsed as matrix parameter, which is unsorted.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ugly. And this means {coordinatesArray: .*} is no longer in use?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the parameter {coordinatesArray: .*} is no longer used. I could rename it or see if I can remove it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it like this:

@Path("/{profile}/{coordinatesArray : .+}")
...
@PathParam("coordinatesArray") String coordinates

I think this is what you meant? It does not work. The variable coordinates only contains the first coordinate up to the first ;, everything after that seems to be read as matrix parameter.

@boldtrn boldtrn mentioned this pull request Jul 20, 2018
@karussell

Copy link
Copy Markdown
Member

@karussell karussell closed this Aug 5, 2018
karussell pushed a commit that referenced this pull request Jun 23, 2020
karussell added a commit that referenced this pull request Jul 10, 2020
* Initial commit, moving relevant files from #1422 over to the new repository

* Remove debugging output

* Update README.md

* Rename to graphhopper-navigation-mapbox

* Add Tests (#3)

* Make MapboxResponseConverter testable

* Added Travis.yml

* Mavenbuild (#4)

* try maven build

* packaging pom

* Corrected conflict resolution

* Add Travis build status image

* Minor test improvement

* Fix indentation issue

* Remove WebHelper, fixes #1

* added a few properties required for deploying to maven central

* let's configure executable from settings

* Allow multiple waypoints (#5)

* First approach at allowing multiple waypoints

* Fix resetting time and distance

* First improvement to repeat instructions regularly

* Add a very far distance instruction to instruct users to continue on the road

* Moved the very far message a bit

* Reduce initial delay

* Improve roundabout banner instructions

* Add the very close instruction fall back

* Added a simple translation map

* Upper case the first letter of turn descriptions in bannerInstructions

* Update README.md

* Use GH translation map

* Move translations to resources

* Use Helper.firstBig

* Have TranslationMap as singleton

* TranslationMap as static variable

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* move to stable dep of GH core 0.11.0

* set explicit version to maven-compiler-plugin

* Add bearings (#17)

* Inigial approach of using bearings in the request

* Disable CH

* log test

* Added Tests for MapboxResource#getBearing

* Sett pass through to false

* Add alternative Routes to Navigation Endpoint (#18)

* Allow the addition of multiple routes when using headings

* Use alternative routes by default

* Add test for alternative routes

* Remove alternative route calculation

* upgrade GH core dependency

* Add then voice and banner instruction (#19)

Add comment and minor rename

* Change Endpoint name and Maven Package (#20)

* Rename maven package, endpoint, and files

* Fix tmp issue with openjdk

* Remove tmp fix for jdk issue

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* use jdk-12 instead of 10

* Add points

* updated GH core version

* upgraded to 0.12.0-pre3

* updated GH core to 0.12.0-pre6

* upgrade to 0.13-SNAPSHOT and using GH core 0.12.0

* upgrade GH core to 0.13.0-pre1

* upgrade GH core to 0.13.0-pre2

* Upgrade gh core to 0.13.0-pre3

* Init imperial voice unit support

* Move VoiceInstructionConfig to seperate file

* Add imperial unit translations

* Add voice instruction tests

* Set right inital voice instruction key

* Remove redundant init from unit

* Remove voiceUnit metric check

* Set imperial as default if no voiceUnit is set

* Refactor voice instruction configuration creation

* Upgrade gh core to 0.13.0-pre4

* upgrade GH core to 0.13.0-pre5

* upgrade GH core to 0.13.0-pre6

* upgrade GH core to 0.13.0-pre7

* upgrade GH core to 0.13.0-pre8

* upgrade GH core to 0.13.0-pre12

* Fallback to the original profile (#26)

* Load the correct translation map

* upgrade GH core to 0.13.0-pre15

* Upgrade GH core to 0.13.0-pre16

* Upgrade GH core to 0.13.0-pre17

* Upgrade GH core to 0.13.0-pre18

* Upgrade GH core to 0.13.0-pre19

* Upgrade GH core to 0.13.0

* Upgrade GH core to 0.14.0-pre1

* Upgrade GH core to 1.0-pre1

* Upgrade GH core to 1.0-pre2

* Upgrade GH core to 1.0-pre3

* Upgrade GH core to 1.0-pre4

* Upgrade GH core to 1.0-pre5

* Update README.md

* Fix continue instruction translation (#27)

* Fix issues with Continue instruction and fixed some typos

* Use GH translation map instead

* Upgrade GH core to 1.0-pre7 (#28)

* Upgrade GH core to 1.0-pre9

* Update pom.xml

* Upgrade GH core to 1.0-pre11

* Upgrade GH core to 1.0-pre12

* Upgrade GH core to 1.0-pre14

* Upgrade GH core to 1.0-pre16

* Upgrade GH core to 1.0-pre17

* Upgrade GH core to 1.0-pre18

* Upgrade GH core to 1.0-pre21

* Upgrade GH core to 1.0-pre22

* Upgrade GH core to 1.0-pre23

* Upgrade GH core to 1.0-pre24

* Upgrade GH core to 1.0-pre25

* Upgrade GH core to 1.0-pre27

* Upgrade GH core to 1.0-pre28

* Upgrade GH core to 1.0-pre29

* Upgrade GH core to 1.0-pre30

* Fix compiler error, tests still fail

* fix tests #31

* Upgrade GH core to 1.0-pre31

* update to jdk14

* Upgrade core to 1.0-pre32

* Upgrade core to 1.0-pre33

* Upgrade core to 1.0-pre33.2

* Upgrade core to 1.0-pre33.3

* Upgrade core to 1.0-pre33.4

* Upgrade core to 1.0-pre35

* fix resource

* Upgrade core to 1.0-pre37

* Upgrade core to 1.0-pre38

* for now we need the legacy resolver also here

* add logging

* make profile mapping between mapbox and graphhopper configurable; remove legacy resolver

* Update README.md

* Update README.md

* Update README.md

* updated GH core

* Set versions to 1.0-SNAPSHOT in pom.xml, keep dependency hashes in separate file

* Move gh_dependencies into pom.xml instead

* Rename: PathWrapper -> ResponsePath

* Use core dependency from jitpack

* Remove hash

* Upgrade to 1.0-pre42, revert to old build process

* Upgrade GH core to 1.0-pre43

* Upgrade GH core to 1.0

* Upgrade version to 2.0-SNAPSHOT

* add development profile

* Update core version, use GitHub packages, remove sonatype snapshot repository

* Add GitHub workflow to publish packages, see core/#n2049

* Add missing distributionManagement

* Add access token setup for travis with github packages

* travis job: include access also for github packages

* Update core version

* Include sources for GitHub Packages deployments

* Update core to 'remove unused methods encoder.defineNodeBits...'

* Update core to 'Set default algo for edge-based CH back to Dijkstra'

* Update core to 'Use Envelope in API'

* Update core to 'Add comment about module GpxFromInstructions belongs to'

* Update core to 'Add creator method for LMApproximator'

* move into navigation subfolder

* remove duplicate files

* removed andorra osm

* added apache license header to 2 files

* added note in changelog

Co-authored-by: Robin <[email protected]>
Co-authored-by: easbar <[email protected]>
Co-authored-by: Stefan Kienzl <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants