【问题标题】:Shortest path from osmar object to igraph in R. Trying to replicate an osmar documentation exampleR 中从 osmar 对象到 igraph 的最短路径。尝试复制 osmar 文档示例
【发布时间】:2020-09-26 11:23:00
【问题描述】:

我正在尝试在 R 中开始使用 openstreetmap,并尝试复制 osmar 包文档中给出的示例。

我得到了一些慕尼黑的数据。

src <- osmsource_api(url = "https://api.openstreetmap.org/api/0.6/")

muc_bbox <- center_bbox(11.575278, 48.137222, 1000, 1000)
muc <- get_osm(muc_bbox, src)

我得到了慕尼黑所有高速公路的一个子集

    hways_muc <- subset(muc, way_ids = find(muc, way(tags(k == "highway"))))
hways <- find(hways_muc, way(tags(k == "name")))
hways <- find_down(muc, way(hways))
hways_muc <- subset(muc, ids = hways)

然后我找到一个名称中带有“Tor”的节点和最近的高速公路:

  hway_start_node <- local({
  id<-find(muc, node(tags(v %agrep% "tor")))[1]
  find_nearest_node(muc, id, way(tags(k == "highway"))) })

  hway_start <- subset(muc, node(hway_start_node))

然后我选择一些随机点和最近的高速公路:

hway_end_node <- local({
  id <- find(muc, node(attrs(lon > 11.58 & lat > 47.150)))[1]
  find_nearest_node(muc, id, way(tags(k == "highway"))) })

现在我将 osmar 对象转换为 igraph 并卡住了

    gr_muc <- as_igraph(hways_muc)
route <- get.shortest.paths(gr_muc, from = as.character(hway_start_node), to =    as.character(hway_end_node))[[1]]

我收到此错误:

Fehler in as.igraph.vs(graph, from) : Invalid vertex names

如何正确修饰 igraph 中的节点? 据我了解,我使用节点 ID,但似乎在 igraph 中找不到或解决它们。

非常感谢您。

【问题讨论】:

    标签: r openstreetmap igraph shortest-path osmar


    【解决方案1】:

    有几个错误: 1. 选择的 GPS 坐标不可达。 2. 选择的GPS坐标只有忽略一个方向的道路才能到达

    1.改变这一行:

    id<-find(muc, node(tags(v %agrep% "tor")))[1]
    

    id<-find(muc, node(tags(v %agrep% "Sendlinger Tor")))[1]
    

    改变这一行:

    id <- find(muc, node(attrs(lon > 11.58 & lat > 47.150)))[1]
    

      id <- find(muc, node(attrs(lon > 11.58 & lat > 48.150)))[1]
    

    还将图形更改为无向。

    gr_muc<-as.undirected(gr_muc)
    
    route <- get.shortest.paths(gr_muc, from = as.character(hway_start_node), to =    as.character(hway_end_node))[[1]]
    

    在示例的更下方,作者犯了一个小错误,因此请注意使用这行代码从列表中选择一条路线:

    route=route[[1]]
    

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      相关资源
      最近更新 更多