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

核能气质少年 提交于 2020-07-10 07:47:51

问题


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)

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"))) })

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

I get this error:

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

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.

Thank you very much in advance.


回答1:


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.Change this line :

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

to

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

Change this line:

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

to

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


来源:https://stackoverflow.com/questions/62246073/shortest-path-from-osmar-object-to-igraph-in-r-trying-to-replicate-an-osmar-doc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!