【问题标题】:create GraphHopper API for Desktop为桌面创建 GraphHopper API
【发布时间】:2014-03-07 17:23:06
【问题描述】:

我想使用 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 类、重载方法并使用上面的代码加载数据并可以调用路由是否正确?

非常感谢 菲尔

【问题讨论】:

  • 我将其读作“Grace Hopper API”

标签: java graphhopper


【解决方案1】:

您只需要一次 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...)根据您的需要对其进行自定义?

【讨论】:

    【解决方案2】:

    我使用这个代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多