ClassCastException when querying the graph

Hello,

First of all, million times sorry if I post in the wrong section.

I am currently upgrading my application using Graphhopper from 0.3 to 0.5.0.
Thanks to the release note and the “Low level API” explanations I could find my way through the API changes or almost.

Following the low-level-api (whith few tweaks because some methods’ signatures have changed) I ended up loading my graph and query it. But during the query the following exception raised :

java.lang.ClassCastException: com.graphhopper.storage.BaseGraph$EdgeIterable cannot be cast to com.graphhopper.util.CHEdgeIteratorState
    at com.graphhopper.routing.util.LevelEdgeFilter.accept(LevelEdgeFilter.java:50)
    at com.graphhopper.routing.AbstractRoutingAlgorithm.accept(AbstractRoutingAlgorithm.java:78)
    at com.graphhopper.routing.DijkstraBidirectionRef.fillEdges(DijkstraBidirectionRef.java:199)
    at com.graphhopper.routing.DijkstraBidirectionRef.fillEdgesTo(DijkstraBidirectionRef.java:169)
    at com.graphhopper.routing.AbstractBidirAlgo.runAlgo(AbstractBidirAlgo.java:76)
    at com.graphhopper.routing.AbstractBidirAlgo.calcPath(AbstractBidirAlgo.java:63)
    at com.miprouting._live.LoadAndTest.main(LoadAndTest.java:65)

I can’t find why this occurs.
Here is the code creating the graph :

args = new String[]{
        "graph.dataaccess=RAM_STORE",
        "prepare.chShortcuts=fastest",
        "osmreader.wayPointMaxDistance=1",
        "graph.flagEncoders=car",
        "osmreader.osm=/belgium-latest.osm.pbf",
        "graph.location=/bel-car-fastest-gh"
};

long start = System.currentTimeMillis();
CmdArgs cmd = new CmdArgs();
String[] keyVal;
for (String arg : args) {
    keyVal = arg.split("=");
    cmd.put(keyVal[0].trim(), keyVal[1].trim());
}

GraphHopper hopper = new GraphHopper();
hopper.init(cmd).forServer().importOrLoad();
System.out.println("Graph generated in " + (System.currentTimeMillis() - start) + "ms");

This seems to work fine as my graph is created, contracted and flushed.
(I deleted the previous graph, so it’s always created new)
Then here is how I try to query it :

String GRAPH_LOCATION = "/bel-car-fastest-gh";

FlagEncoder encoder = new CarFlagEncoder();
EncodingManager encodingManager = new EncodingManager(encoder);
Weighting weightCalc = new FastestWeighting(encoder);
GraphBuilder gb = new GraphBuilder(encodingManager)
    .setLocation(GRAPH_LOCATION)
    .setStore(true)
    .setCHGraph(weightCalc)
    .set3D(false);
GraphHopperStorage graph = gb.build();
graph.loadExisting();
LocationIndex index = new LocationIndexTree(graph, graph.getDirectory());
index.loadExisting();

AlgorithmOptions options = AlgorithmOptions.start()
    .algorithm(AlgorithmOptions.DIJKSTRA_BI)
    .traversalMode(TraversalMode.NODE_BASED)
    .flagEncoder(encoder)
    .weighting(weightCalc)
    .build();
PrepareContractionHierarchies prepare = new PrepareContractionHierarchies(graph.getDirectory(), graph, graph.getGraph(CHGraph.class), encoder, weightCalc, TraversalMode.NODE_BASED);
QueryResult from = index.findClosest(51.005227, 3.885238, EdgeFilter.ALL_EDGES);
QueryResult to = index.findClosest(50.401629, 4.739425, EdgeFilter.ALL_EDGES);
QueryGraph queryGraph = new QueryGraph(graph);
queryGraph.lookup(from, to);
RoutingAlgorithm algo = prepare.createAlgo(queryGraph, options);

Path path = algo.calcPath(from.getClosestNode(), to.getClosestNode()); // <---- error occurs from here

System.out.println(path.getTime());

index.close();

What did I miss ?
Thank you already for the time you took reading, and I hope, helping me :smile:

Ok I got my mistake.

Pretty logical when I see the solution but not obvious at first glance.

The error is how I initialize the queryGraph.

It should rather be

QueryGraph queryGraph = new QueryGraph(graph.getGraph(CHGraph.class, weightCalc));

This works ! :smile:

Sorry for bothering you I was fighting this issue for a day :sweat_smile:

1 Like

At the first glance I was unsure too - but this now makes completely sense. We should probably throw an exception if we can detect this earlier … hmmh but not that easy as one can route on the normal graph too …

Thanks for question & solution!

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.