【发布时间】:2021-06-19 23:13:36
【问题描述】:
是否可以减少以下代码的运行时间?
我的目标是从框边界指定的开放街道数据区域中获取加权 igraph 对象。
目前我正在尝试使用 overpass api 来减轻内存负载,因此我不必在内存中保留大的 osm 文件。
首先我得到一个由 bbox(仅街道)指定的 osm 数据作为 xml 结构
library(osmdata)
library(osmar)
install.packages("remotes")
remotes::install_github("hypertidy/scgraph")
library(scgraph)
dat <- opq(bbox = c(11.68771, 47.75233, 12.35058, 48.19743 )) %>%
add_osm_feature(key = 'highway',value = c("trunk", "trunk_link", "primary","primary_link", "secondary", "secondary_link", "tertiary","tertiary_link", "residential", "unclassified" ))%>%
osmdata_xml ()
然后我将生成的 xml 对象 dat 转换为 osmar 对象 dat_osmar,最后转换为 igraph 对象:
dat_osmar <-as_osmar(xmlParse(dat))
dat_graoh <- as_igraph(dat_osmar)
如何优化这些例程?
也许可以将 dat (XML) 对象分成块并并行解析?
我经过几个步骤才最终得到一个加权无向图。
目前整个过程在我的机器上需要 89.555 秒。
如果我可以缩短这两个步骤的运行时间:
dat_osmar <-as_osmar(xmlParse(dat))
dat_graoh <- as_igraph(dat_osmar)
这已经有帮助了。
我尝试的一种方法是使用 osmdata_sc() 而不是 osmdata_xml()。
这提供了一个硅酸盐对象,我可以将其转换为:
scgraph::sc_as_igraph(dat)
到 igraph。
它相当快,但遗憾的是重量正在丢失,所以它不是一个解决方案。
原因是:如果我使用从 osmar 对象到具有函数osmar::as_igraph() 的 igraph 对象的转换,则权重是根据两者之间的距离计算的两条边并添加到 igraph:
edges <- lapply(dat, function(x) {
n <- nrow(x)
from <- 1:(n - 1)
to <- 2:n
weights <- distHaversine(x[from, c("lon", "lat")], x[to,
c("lon", "lat")])
cbind(from_node_id = x[from, "ref"], to_node_id = x[to,
"ref"], way_id = x[1, "id"], weights = weights)
})
scgraph::sc_as_igraph(dat) 中缺少此内容
如果这可以添加到 硅酸盐 到 igraph 转换
我可以跳过dat_osmar <-as_osmar(xmlParse(dat)) 步骤
然后走overpass->silicate->igraph 路线,它比overpass->xml->osmar->igraph 快得多。
osmdata 包还通过 osmdata_sf()
提供 sf 响应所以也许overpass->sf->igraph 的工作流程更快,但在使用这种方式时,我需要根据边的距离将权重合并到图表中,而我目前还不够好,非常感谢任何帮助。
此外,在使用 sf 和生成的 igraph 对象时,openstreetmap gps 点与其 ID 之间的连接不应丢失。这意味着我应该能够从生成的 Igraph 中找到一个 ID 的 gps 位置。一个查找表就足够了。如果我去overpass->silicate->igraph 或overpass->xml->osmar->igraph 路线,这是可能的。我不确定overpass->sf->igraph 路由是否仍然可行。
【问题讨论】:
-
嗨!如果您对
osmar的替代方法感兴趣,我可以尝试提供一个基于名为sfnetworks的R 包的解决方案。sfnetworks是基于tidygraph,这意味着sfnetwork返回的对象也是igraph对象。 -
您好,是的。我需要能够做两件事:1. 根据 bbox 区域从立交桥获取数据,2. 从该区域获取 igraph 3. 能够将 igraph 连接/查找到 openstreetmap ID 到 gps 位置。例如,现在我使用 igraph 进行路由/图形处理,并在 osmar 对象中查找 ID 和 gps 值以及 openstreetmap 数据的其他键。如果可以通过 sfnetworks 和快速实现相同的效果(如果不超过 10 秒,不包括访问立交桥会很棒),我会非常乐意使用这个解决方案!
标签: r openstreetmap igraph sf overpass-api