directions-api-java-client icon indicating copy to clipboard operation
directions-api-java-client copied to clipboard

InstructionList.createGPX fails to null Translation

Open devemux86 opened this issue 8 years ago • 3 comments

Using the alternative API client to perform online routing and then produce gpx from the result, fails with:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String com.graphhopper.util.Translation.tr(java.lang.String, java.lang.Object[])' on a null object reference
    at com.graphhopper.util.FinishInstruction.getTurnDescription(FinishInstruction.java:55)
    at com.graphhopper.util.InstructionList.createWayPointBlock(InstructionList.java:168)
    at com.graphhopper.util.InstructionList.createGPX(InstructionList.java:200)

This is because in GraphHopperWeb.createPathWrapper a null Translation is used:

InstructionList il = new InstructionList(null);

Since we have the online routing result, can we support GPX creation on the client? e.g. enhance InstructionList to allow external Translation?

devemux86 avatar Jun 30 '17 15:06 devemux86

I just had a more detailed look here. It depends how you call the API.

If you add req.getHints().put("turn_description", false);, then it fails, if you don't add this it works. I created a PR here containing a test that shows this.

enhance InstructionList to allow external Translation?

The easiest way as a workaround if you want to have req.getHints().put("turn_description", false);, would be to create a new instance of the InstructionList with the added TranslationMap. You can extract all instruction from the old InstructionList and add it to the new InstructionList. However, I think it would not hurt to provide a way to set the TranslationMap of the InstructionList, WDYT @karussell?

boldtrn avatar Jul 01 '17 09:07 boldtrn

If you add req.getHints().put("turn_description", false);, then it fails, if you don't add this it works.

Hmm the request hints are that way because they need to be for the wished routing result. If need to be different for the create gpx to succeed, then a 2nd request would be required, not very convenient. :slightly_smiling_face:

devemux86 avatar Jul 01 '17 09:07 devemux86

If need to be different for the create gpx to succeed, then a 2nd request would be required, not very convenient.

As I said, there is a workaround possible. In your code you could do something like:

req.getHints().put("turn_description", false);
ghResponse = gh.route(req);
InstructionList newIL = new InstructionList(tr);
for (int i = 0; i < ghResponse.getBest().getInstructions().size(); i++) {
    newIL.add(i, ghResponse.getBest().getInstructions().get(i));
}

I haven't tested it, but I think this should work. The newIL contains your translations now and creating GPX should work without any problems.

Anyway, I think we should make it possible to set the TranslationMap for an InstructionList, as this would solve the issue and would not hurt us I think.

boldtrn avatar Jul 01 '17 09:07 boldtrn