我使用这段代码:
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();
我无法创建工作结构。我已经查看了测试示例,例如https://github.com/graphhopper/graphhopper/blob/master/core/src/test/java/com/graphhopper/GraphHopperAPITest.java但 loadGraph 方法不能在外部调用包裹。
我想从 OSM 文件创建一个正确的图形数据库,这似乎有效。比我想找到最接近地理位置的边缘:
m_index.findClosest( p_position.getLatitude(), p_position.getLongitude(), EdgeFilter.ALL_EDGES );
但这会返回一个空指针异常,所以恕我直言,我的索引应该是错误的。如何创建正确的工作索引?
在此之后,我想通过图表创建一条快速/最短的路线
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);
但我无法创建一个有效的 graphhopper 实例来调用路由方法。