0

I would like to use GraphHopper to create my own API for routing. I have taken a look into the GraphHopper.java to create my own class. I put a OSM file into the API I get a directory with edges, nodes etc. This seems to work well.

My question is, how can I load this data, so that I can call the route-Method? I try to understand the GraphHopper.java class, but my example does not work. I try to load the graph with

GHDirectory l_dir = new GHDirectory( m_graphlocation.getAbsolutePath(), DAType.RAM);
m_graph = new LevelGraphStorage( l_dir, m_EncodingManager );

Do I need the OSM file again for routing or can I use the directory with edges and nodes only? IMHO I need a call

OSMReader l_reader = new OSMReader( l_graph, CConfiguration.getInstance().get().ExpectedCapacity).setWorkerThreads(-1).setEncodingManager(m_EncodingManager).setWayPointMaxDistance(CConfiguration.getInstance().get().WaypointMaxDistance).setEnableInstructions(false);
l_reader.doOSM2Graph(p_osm);
l_graph.optimize();

to create my graph, so is it correct to create my GraphHopperAPI class, overload the methods and on load the data with the code above and can call route?

Thanks a lot Phil

flashpixx
  • 11
  • 3

2 Answers2

0

You only need the OSM import once. In the GraphHopper class a new storage is instantiated and then loadExisting is called. If that fails you know that you need to import the OSM file and create new graphhopper files. E.g. out of my head it should be similar to this:

g = new LevelGraphStorage(dir, encodingManager);
if(!g.loadExisting()) {
    reader = new OSMReader(g).setLotsOfThings
    reader.doOSM2Graph..
}

The problem is that if you disable CH you can just use the GraphHopperStorage. Then you need the LocationIndex properly loaded or created at the correct place etc. Have a look into the existing unit tests where I also just use the raw stuff instead of the GraphHopper wrapper class.

But why not just create a subclass of GraphHopper and use the existing hooks (postProcessing, createWeighting, ...) to customize it to your needs?

Karussell
  • 17,085
  • 16
  • 97
  • 197
0

I use this code:

GHDirectory l_dir    = new GHDirectory( l_graphlocation.getAbsolutePath(), DAType.RAM_STORE);
m_graph              = new LevelGraphStorage( l_dir, m_EncodingManager );

m_graph.setSegmentSize( CConfiguration.getInstance().get().SegmentSize );

if (!m_graph.loadExisting())
{
    File l_osm = this.downloadOSMData();

    OSMReader l_reader = new OSMReader( m_graph, CConfiguration.getInstance().get().ExpectedCapacity).setWorkerThreads(-1).setEncodingManager(m_EncodingManager).setWayPointMaxDistance(CConfiguration.getInstance().get().WaypointMaxDistance).setEnableInstructions(false);
    l_reader.doOSM2Graph(l_osm);

    m_graph.optimize();
    m_graph.flush();

    // do I need this?
    PrepareRoutingSubnetworks l_preparation = new PrepareRoutingSubnetworks(m_graph, m_EncodingManager);
    l_preparation.setMinNetworkSize( CConfiguration.getInstance().get().MinNetworkSize );
    l_preparation.doWork();
}

// is this correct?
m_index = new LocationIndexTree(m_graph, l_dir);
m_index.setResolution( CConfiguration.getInstance().get().IndexResolution );
m_index.setSearchRegion(true);
if (!m_index.loadExisting())
    throw new IOException("unable to load graph index file");

// does not work without the graph
m_graphhopper = new GraphHopper().setEncodingManager(m_EncodingManager).forDesktop();

I cannot create a working structure. I have taken a look into the test examples eg https://github.com/graphhopper/graphhopper/blob/master/core/src/test/java/com/graphhopper/GraphHopperAPITest.java but the loadGraph method cannot be called outside the package.

I would like to create a correct graph database from the OSM file, this seems to be working. Than I would like to finde the closest edge to a geo location with:

m_index.findClosest( p_position.getLatitude(), p_position.getLongitude(), EdgeFilter.ALL_EDGES );

but this returns a null pointer exception, so imho my index should be wrong. How can I create a correct working index?

After this I would like to create a fast / shortest route through the graph with

GHRequest l_request = new GHRequest( p_start.getLatitude(), p_start.getLongitude(), p_end.getLatitude(), p_end.getLongitude() );
l_request.setAlgorithm( CConfiguration.getInstance().get().RoutingAlgorithm );

return m_graphhopper.route(l_request);

but I cannot create a working graphhopper instance for call the route-method.

flashpixx
  • 11
  • 3