GraphHopper.com | Forum | GitHub | Maps | Blog

Issues trying to route using GeoJSON


#1

Hi,

I am trying to use the GraphHopper core and JSON APIs(0.10.0 for both) to create a graph using GeoJSON and then do a routing. However I run into a couple of exceptions. Here is the relevant part of my code:

    gh = new GraphHopper().forMobile();

    ... other code (omitted) ...

    // This code runs in a background thread
    GHJsonFactory fac = new GHJsonFactory();
    GHJson json = fac.create();
    Reader reader = new FileReader(Environment.getExternalStorageDirectory().getAbsolutePath() + "/data.json");
    JsonFeatureCollection features = json.fromJson(reader, JsonFeatureCollection.class);

    gh.changeGraph(features.getFeatures());

    GHRequest request = new GHRequest(51.05, -0.72, 51.0291, -0.7621).
                            setAlgorithm(Parameters.Algorithms.DIJKSTRA_BI).setVehicle("foot");

    GHResponse resp = gh.route(request);
    PathWrapper pw = resp.getBest();

Initially I got this error:

IllegalArgumentException: To use the changeGraph API you need to turn off CH

I then added, immediately before changeGraph():

gh.setCHEnabled(false);

but now get:

NullPointerException: Attempt to invoke virtual method 'java.util.List com.graphhopper.routing.util.EncodingManager.fetchEdgeEncoders() on a null object reference

Looks like I’ve missed a step somewhere. The above code was based on my own examination of the GraphHopper source code. Any suggestions on this?

Thanks,
Nick


#2

Can you send us minimal code that reproduces this NPE? An NPE should not be thrown even if the API is used in the ‘wrong’ order.

Disabling CH should happen before you call importOrLoad.


#3

It’s on github:

https://github.com/nickw1/GHJsonTest/blob/master/app/src/main/java/freemap/ghjsontest/MainActivity.java

(whole project https://github.com/nickw1/GHJsonTest)

I don’t actually call importOrLoad() as I presumed the changeGraph() would import JSON. However if I do add it in I get a new exception (IllegalStateException: GraphHopperLocation not specified) - presumably because I haven’t loaded a .ghz file.

Thanks.


#4

changeGraph changes an existing, previously imported graph it does not import GeoJSON. GeoJSON would be hard to handle, probably topojson would be a better possibility but it is not implemented.

What is the stacktrace of the NullPointerException? (update: ok, have identified&fixed this, the reason is the wrong assumption of the functionality and the missing import)


#5

Ah right ok… sorry, my misunderstanding.

Thanks,
Nick