R OSRM 计算 data.frame 内的行程距离
R OSRM calculate travel distance within data.frame
使用 osrm 包,我想计算存储在数据框的两个不同列中的两点之间的距离,并将其放入一个新列中。
但是,执行以下操作不起作用并引发错误:找不到对象 'lon'。这里出了什么问题?
pacman::p_load(
tidyverse,
sf,
osrm
)
test_1 <- data.frame(name_1 = c("a", "b"), long = c(13.371871012201932, 13.445856720780322), lat = c(52.5212875734526, 52.503693991115206)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326) %>%
rename(geom_1 = "geometry")
test_2 <- data.frame(name_2 = c("c", "d"), long = c(13.401224766110927, 13.349212418670756), lat = c(52.51946945798967, 52.5149481833963)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326) %>%
rename(geom_2 = "geometry")
comb <- expand_grid(test_1, test_2)
routes <- comb %>%
mutate(distances = osrmRoute(
src = geom_1,
dst = geom_2,
returnclass = "sf"
)$distance)
问题是一个 sf
对象一次只能激活一个 geom
列。要解决这个问题,只需调用 sf
对象 test1
和 test2
它应该可以工作。
请在下面找到修改后的代码和相应的输出。
- 代码
routes <- comb %>%
mutate(distances = osrmRoute(
src = test_1,
dst = test_2,
returnclass = "sf"
)$distance)
- 输出:
routes
# A tibble: 4 x 5
name_1 geom_1 name_2 geom_2 distances
<chr> <POINT [°]> <chr> <POINT [°]> <dbl>
1 a (13.37187 52.52129) c (13.40122 52.51947) 4.35
2 a (13.37187 52.52129) d (13.34921 52.51495) 4.35
3 b (13.44586 52.50369) c (13.40122 52.51947) 4.35
4 b (13.44586 52.50369) d (13.34921 52.51495) 4.35
或者,您可以在 sf
对象 comb
中指定哪个 geom
列处于活动状态,您将获得与上述相同的结果。
- 替代代码
routes <- comb %>% mutate(distances = osrmRoute(
src = st_sf(.,sf_column_name = "geom_1"),
dst = st_sf(.,sf_column_name = "geom_2"),
returnclass = "sf"
)$distance)
编辑
作为我与@Jindra Lacko 交流的后续行动(参见下面的评论),我提出了一个 computeDistances()
功能,它可以帮助您获得想要的东西。请在下方找到函数代码及其使用您的对象的输出 comb
- 函数代码
computeDistances
computeDistances <- function(x){
distances <- data.frame(Distances = numeric())
for (i in seq(x)){
distances[i,"Distances"] <- osrmRoute(
src = st_sf(x,sf_column_name = "geom_1")[i,],
dst = st_sf(x,sf_column_name = "geom_2")[i,],
returnclass = "sf"
)$distance
}
results <- st_sf(cbind(x, distances))
return(results)
}
- 使用您的
comb
对象测试函数
computeDistances(comb)
Simple feature collection with 4 features and 3 fields
Active geometry column: geom_1
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 13.37187 ymin: 52.50369 xmax: 13.44586 ymax: 52.52129
Geodetic CRS: WGS 84
name_1 name_2 Distances geom_1 geom_2
1 a c 4.3543 POINT (13.37187 52.52129) POINT (13.40122 52.51947)
2 a d 2.6576 POINT (13.37187 52.52129) POINT (13.34921 52.51495)
3 b c 4.4650 POINT (13.44586 52.50369) POINT (13.40122 52.51947)
4 b d 8.1611 POINT (13.44586 52.50369) POINT (13.34921 52.51495)
使用 osrm 包,我想计算存储在数据框的两个不同列中的两点之间的距离,并将其放入一个新列中。
但是,执行以下操作不起作用并引发错误:找不到对象 'lon'。这里出了什么问题?
pacman::p_load(
tidyverse,
sf,
osrm
)
test_1 <- data.frame(name_1 = c("a", "b"), long = c(13.371871012201932, 13.445856720780322), lat = c(52.5212875734526, 52.503693991115206)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326) %>%
rename(geom_1 = "geometry")
test_2 <- data.frame(name_2 = c("c", "d"), long = c(13.401224766110927, 13.349212418670756), lat = c(52.51946945798967, 52.5149481833963)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326) %>%
rename(geom_2 = "geometry")
comb <- expand_grid(test_1, test_2)
routes <- comb %>%
mutate(distances = osrmRoute(
src = geom_1,
dst = geom_2,
returnclass = "sf"
)$distance)
问题是一个 sf
对象一次只能激活一个 geom
列。要解决这个问题,只需调用 sf
对象 test1
和 test2
它应该可以工作。
请在下面找到修改后的代码和相应的输出。
- 代码
routes <- comb %>%
mutate(distances = osrmRoute(
src = test_1,
dst = test_2,
returnclass = "sf"
)$distance)
- 输出:
routes
# A tibble: 4 x 5
name_1 geom_1 name_2 geom_2 distances
<chr> <POINT [°]> <chr> <POINT [°]> <dbl>
1 a (13.37187 52.52129) c (13.40122 52.51947) 4.35
2 a (13.37187 52.52129) d (13.34921 52.51495) 4.35
3 b (13.44586 52.50369) c (13.40122 52.51947) 4.35
4 b (13.44586 52.50369) d (13.34921 52.51495) 4.35
或者,您可以在 sf
对象 comb
中指定哪个 geom
列处于活动状态,您将获得与上述相同的结果。
- 替代代码
routes <- comb %>% mutate(distances = osrmRoute(
src = st_sf(.,sf_column_name = "geom_1"),
dst = st_sf(.,sf_column_name = "geom_2"),
returnclass = "sf"
)$distance)
编辑
作为我与@Jindra Lacko 交流的后续行动(参见下面的评论),我提出了一个 computeDistances()
功能,它可以帮助您获得想要的东西。请在下方找到函数代码及其使用您的对象的输出 comb
- 函数代码
computeDistances
computeDistances <- function(x){
distances <- data.frame(Distances = numeric())
for (i in seq(x)){
distances[i,"Distances"] <- osrmRoute(
src = st_sf(x,sf_column_name = "geom_1")[i,],
dst = st_sf(x,sf_column_name = "geom_2")[i,],
returnclass = "sf"
)$distance
}
results <- st_sf(cbind(x, distances))
return(results)
}
- 使用您的
comb
对象测试函数
computeDistances(comb)
Simple feature collection with 4 features and 3 fields
Active geometry column: geom_1
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 13.37187 ymin: 52.50369 xmax: 13.44586 ymax: 52.52129
Geodetic CRS: WGS 84
name_1 name_2 Distances geom_1 geom_2
1 a c 4.3543 POINT (13.37187 52.52129) POINT (13.40122 52.51947)
2 a d 2.6576 POINT (13.37187 52.52129) POINT (13.34921 52.51495)
3 b c 4.4650 POINT (13.44586 52.50369) POINT (13.40122 52.51947)
4 b d 8.1611 POINT (13.44586 52.50369) POINT (13.34921 52.51495)