我想使用GraphHopper创建自己的路由API。我已经看了一下GraphHopper.java来创建我自己的类。我把一个OSM文件放到API中我得到一个带边,节点等的目录。这似乎运作良好。
我的问题是,如何加载这些数据,以便我可以调用route-Method?我尝试理解GraphHopper.java类,但我的示例不起作用。我尝试使用
加载图表GHDirectory l_dir = new GHDirectory( m_graphlocation.getAbsolutePath(), DAType.RAM);
m_graph = new LevelGraphStorage( l_dir, m_EncodingManager );
我是否需要再次使用OSM文件进行路由,还是只能使用带边和节点的目录? 恕我直言,我需要一个电话
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();
创建我的图形,所以创建我的GraphHopperAPI类是正确的,重载方法并使用上面的代码加载数据并且可以调用路由吗?
非常感谢 菲尔
答案 0 :(得分:0)
您只需要输入一次OSM。在GraphHopper类中,实例化一个新存储,然后调用loadExisting。如果失败,您就知道需要导入OSM文件并创建新的graphhopper文件。例如。在我的脑海里,它应该与此类似:
g = new LevelGraphStorage(dir, encodingManager);
if(!g.loadExisting()) {
reader = new OSMReader(g).setLotsOfThings
reader.doOSM2Graph..
}
问题是如果你禁用CH,你可以使用GraphHopperStorage。然后你需要在正确的位置正确加载或创建LocationIndex等。看看现有的单元测试,我也只使用原始的东西而不是GraphHopper包装类。
但为什么不只是创建GraphHopper的子类并使用现有的钩子(postProcessing,createWeighting,...)来根据您的需要进行自定义?
答案 1 :(得分:0)
我使用此代码:
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实例来调用route-method。