R 中从 osmar 对象到 igraph 的最短路径。尝试复制 osmar 文档示例

Shortest path from osmar object to igraph in R. Trying to replicate an osmar documentation example

我正在尝试开始使用 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 中找到或解决它们。

非常感谢您。

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

1.Change 这一行:

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]]

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

route=route[[1]]