R:将 igraph 属性获取到边缘是:运行时间慢。有没有办法让它更快?
R: getting igraph atributes to an edge id: slow runtime. Is there a way to make it faster?
我正在计算 sfnetwork 中两点之间的最短路径 my_sfn.
计算出的路径由保存在变量 paths 中的图形 ID 和我组成
并希望获得连接到这些 ID 的几何图形(GPS 值)。
这是通过以下方式完成的:
coordinates_of_the_path<-mclapply(paths,function(x) cbind(as.data.frame(st_coordinates(igraph::get.edge.attribute(graph = my_sfn, name = "geometry", index = x[1]))),id=x),mc.preschedule=TRUE, mc.cores = n.cores)
整块:
首先我打开街道地图数据:
library(sf)
library(tidygraph)
library(sfnetworks)
library(osmdata)
library(parallel)
library(data.table)
library(tictoc)
if ((n.cores <- detectCores()) > 4) n.cores <- 4
setDTthreads(threads = n.cores)
my_osm_data <- 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_sf(quiet = FALSE)
my_osm_data<-osmdata::osm_poly2line(my_osm_data)
my_roads <- my_osm_data$osm_lines[, c("osm_id","name")]
#my_roads <- st_geometry(my_osm_data$osm_lines)
my_sfn <- as_sfnetwork(my_roads, directed = FALSE, length_as_weight = TRUE)
然后我设置起点和终点:
start_point =st_sfc(st_point(c(11.829831, 48.110075)))
st_crs(start_point) = st_crs(my_sfn)
dest_point =st_sfc(st_point(c(12.20747, y = 47.83937)))
st_crs(dest_point) = st_crs(my_sfn)
创建路径:
paths = st_network_paths(my_sfn, from = start_point, to = dest_point)
paths = paths %>%
slice(1) %>%
pull(edge_paths) %>%
unlist()
在这里我想得到这条路径的几何形状。
coordinates_of_the_path<-mclapply(paths,function(x) cbind(as.data.frame(st_coordinates(igraph::get.edge.attribute(graph = my_sfn, name = "geometry", index = x[1]))),id=x),mc.preschedule=TRUE, mc.cores = n.cores)
但是最后一次调用需要很长时间。
有没有办法让它更快?
我通过使用 data.frame 最后一列中的索引作为路径向量的索引解决了这个问题
coordinates_of_the_path<-as.data.frame(st_coordinates(igraph::get.edge.attribute(graph = my_sfn, name = "geometry", index = paths)))
coordinates_of_the_path<-cbind(coordinates_of_the_path,id=paths[coordinates_of_the_path$L1])
我正在计算 sfnetwork 中两点之间的最短路径 my_sfn.
计算出的路径由保存在变量 paths 中的图形 ID 和我组成 并希望获得连接到这些 ID 的几何图形(GPS 值)。
这是通过以下方式完成的:
coordinates_of_the_path<-mclapply(paths,function(x) cbind(as.data.frame(st_coordinates(igraph::get.edge.attribute(graph = my_sfn, name = "geometry", index = x[1]))),id=x),mc.preschedule=TRUE, mc.cores = n.cores)
整块:
首先我打开街道地图数据:
library(sf)
library(tidygraph)
library(sfnetworks)
library(osmdata)
library(parallel)
library(data.table)
library(tictoc)
if ((n.cores <- detectCores()) > 4) n.cores <- 4
setDTthreads(threads = n.cores)
my_osm_data <- 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_sf(quiet = FALSE)
my_osm_data<-osmdata::osm_poly2line(my_osm_data)
my_roads <- my_osm_data$osm_lines[, c("osm_id","name")]
#my_roads <- st_geometry(my_osm_data$osm_lines)
my_sfn <- as_sfnetwork(my_roads, directed = FALSE, length_as_weight = TRUE)
然后我设置起点和终点:
start_point =st_sfc(st_point(c(11.829831, 48.110075)))
st_crs(start_point) = st_crs(my_sfn)
dest_point =st_sfc(st_point(c(12.20747, y = 47.83937)))
st_crs(dest_point) = st_crs(my_sfn)
创建路径:
paths = st_network_paths(my_sfn, from = start_point, to = dest_point)
paths = paths %>%
slice(1) %>%
pull(edge_paths) %>%
unlist()
在这里我想得到这条路径的几何形状。
coordinates_of_the_path<-mclapply(paths,function(x) cbind(as.data.frame(st_coordinates(igraph::get.edge.attribute(graph = my_sfn, name = "geometry", index = x[1]))),id=x),mc.preschedule=TRUE, mc.cores = n.cores)
但是最后一次调用需要很长时间。 有没有办法让它更快?
我通过使用 data.frame 最后一列中的索引作为路径向量的索引解决了这个问题
coordinates_of_the_path<-as.data.frame(st_coordinates(igraph::get.edge.attribute(graph = my_sfn, name = "geometry", index = paths)))
coordinates_of_the_path<-cbind(coordinates_of_the_path,id=paths[coordinates_of_the_path$L1])