使用 OSM 数据时路径之间的离线路由断开问题

Offline Routing disconnection issue between ways while using OSM Data

我有我所在城市的 osm 文件,我正在使用 Libosmium 读取它,我将每条路的节点存储为我的图形的顶点,并在每条路的每 2 个相邻节点之间创建边,并计算 g他们之间的欧几里得距离。

问题是:

路径互不相连!我无法从我的目的地到达目的地 来源,即使 google 地图中有一条路线,但路线之间没有节点相交(没有公共节点)所以我无法到达我的目的地!我应该将哪些节点添加到我的图表中,以及如何正确桥接它们?这样我就可以从我的节点到达目的地了?
我用来创建边缘的代码如下

// Loop the Whole Map of Ways
  for ( it = MyWayMap.begin(); it != MyWayMap.end(); it++ ){
      WayCounter++;
      NodesOfWayIndex = 0; //reset Index
      //define a vector of nodes with size of Way
      vector<Vertex> WayNodes(it->second.nodeRefList.size());
      //======================================================
      // Loop The Nodes of Each way
      for(auto j = it->second.nodeRefList.begin(); j != it->second.nodeRefList.end(); ++j){

          VertexID = it->second.nodeRefList.at(NodesOfWayIndex);
          //declare Variable for Eucledean Distance
          double dist = 0;
          WayNodes[NodesOfWayIndex] = VertexID ;
          //---------------------------------------------------------------------
          // if the VertexId doesn't exist yet give back do the following
          if(BelalMap.find(VertexID) == BelalMap.end()){
              // assign idType values to the idmap
              //idmap[IdMapIndex] = VertexID;
              IdMapIndex++;
              // Fill BelalMap
              BelalMap.insert({VertexID,IdMapIndex});
              if(NodesOfWayIndex == 0) Node1 = IdMapIndex;
              else {
                  Node2 = IdMapIndex ;
                  dist = distance(WayNodes[NodesOfWayIndex - 1], WayNodes[NodesOfWayIndex]);
                  add_edge(Node1, Node2,dist,MyGraph);
                  add_edge(Node2, Node1,dist,MyGraph);
                  Node1 = Node2 ;

                } // end else
            }//end of outer if
          //--------------------------------------------------------------------
          //if the VertexId already exists give back it's Vertex number
          else {
              if(NodesOfWayIndex == 0) Node1 = BelalMap.find(VertexID)->second;
              else {
                  // Calculating Eucledan Distance Between Nodes
                  dist = distance(WayNodes[NodesOfWayIndex - 1], WayNodes[NodesOfWayIndex]);
                  Node2 = BelalMap.find(VertexID)->second;
                  add_edge(Node1, Node2,dist,MyGraph);
                  add_edge(Node2, Node1,dist,MyGraph);
                  Node1 = Node2 ;
                }// end of inner else
            }//end of outer else

          //======================================================

          // increase The indexs after iterating each node.
          NodesOfWayIndex++;
        }// end of Nodes of Way Loop
    }// end of Ways of Map Loop

如何从 OSM XML 构建路由图已在此处得到解答:https://help.openstreetmap.org/questions/19213/how-can-i-convert-an-osm-xml-file-into-a-graph-representation/19214。引用这个答案:

Assuming you want only roads, then a possible algorithm is this:

  1. parse all ways; throw away those that are not roads, and for the others, remember the node IDs they consist of, by incrementing a "link counter" for each node referenced.
  2. parse all ways a second time; a way will normally become one edge, but if any nodes apart from the first and the last have a link counter greater than one, then split the way into two edges at that point. Nodes with a link counter of one and which are neither first nor last can be thrown away unless you need to compute the length of the edge.
  3. (if you need geometry for your graph nodes) parse the nodes section of the XML now, recording coordinates for all nodes that you have retained.

If you are only working on a small data set you can of course simply read everything into memory and do the above analysis in memory.

根据您的描述,您忘记了在不同的方式之间创建边缘。如果路径共享一个节点,则它们会相互连接。您需要在由不止一种方式共享的每个节点处拆分一条方式,以便在您的路由图中创建适当的边。

另请参阅 OSM wiki 中的 Routing