0

我想使用 GraphHopper 创建自己的路由 API。我查看了 GraphHopper.java 来创建自己的类。我将一个 OSM 文件放入 API 中,我得到一个包含边、节点等的目录。这似乎运作良好。

我的问题是,如何加载这些数据,以便调用路由方法?我尝试理解 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 类,重载方法并使用上面的代码加载数据并可以调用路由是否正确?

非常感谢菲尔

4

2 回答 2

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 实例来调用路由方法。

于 2014-03-07T09:23:06.070 回答
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...)来根据您的需要对其进行自定义呢?

于 2014-03-05T23:15:49.900 回答