R中从osmar对象到igraph的最短路径.尝试复制osmar文档示例 [英] Shortest path from osmar object to igraph in R. Trying to replicate an osmar documentation example
问题描述
我正在尝试从R中的openstreetmap开始,并尝试复制osmar软件包文档中给出的示例.
i am trying to get started with openstreetmap in R and try to replicate the example given in osmar package documentation.
我得到了一些慕尼黑的数据.
I get a bit data for munich.
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)
我得到了慕尼黑所有高速公路的一部分
I get a subset of all highways in munich
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"的节点和最近的高速公路:
Then I find a node with "Tor" in a name of it and a nearest highway:
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))
然后我选择一些随机点和最近的高速公路:
Then I pick some random point and a nearest highway to it:
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并卡住了
Now I convert osmar object to igraph and get stuck
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中找到或解决它们.
How do i dress the nodes in igraph correctly? From what I understand I use the node ids but it seems I can not find or address them in igraph.
非常感谢您.
推荐答案
有几个错误: 1.无法找到所选的GPS坐标. 2.仅当忽略一条定向道路时,才能到达所选的GPS坐标
There are several errors: 1. GPS coordinate chosen is not reachable. 2. GPS coordinate chosen can only be reached if you ignore one directional roads
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]
还将图形更改为无向图.
also change the graph to be undirected.
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]]
在进一步的示例中,作者犯了一个小错误,因此请注意使用此行代码从列表中选择一条路线:
further down in the exmple the Author makes a small mistake so be aware to use this line of code to select one route from a list:
route=route[[1]]
这篇关于R中从osmar对象到igraph的最短路径.尝试复制osmar文档示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!