我sf在类型的简单feature()中保存了多个轨迹POINT.我想计算后续位置(即行)之间的欧几里德距离.到目前为止,我使用毕达哥拉斯公式 "手动"计算距离,用于计算2D空间中的欧几里德距离.我想知道我是否可以使用该功能做同样的事情sf::st_distance().这是一个简单的例子:
library(sf)
library(dplyr)
set.seed(1)
df <- data.frame(
gr = c(rep("a",5),rep("b",5)),
x = rnorm(10),
y = rnorm(10)
)
df <- st_as_sf(df,coords = c("x","y"),remove = F)
df %>%
group_by(gr) %>%
mutate(
dist = sqrt((lead(x)-x)^2+(lead(y)-y)^2)
)
#> Simple feature collection with 10 features and 4 fields
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: -0.8356286 ymin: -2.2147 xmax: 1.595281 ymax: 1.511781
#> epsg (SRID): NA
#> proj4string: NA
#> # A tibble: 10 x 5
#> # Groups: gr [2]
#> gr x y dist geometry
#> <fct> <dbl> <dbl> <dbl> <POINT>
#> 1 a -0.626 1.51 1.38 (-0.6264538 1.511781)
#> 2 a 0.184 0.390 1.44 (0.1836433 0.3898432)
#> 3 a -0.836 -0.621 2.91 (-0.8356286 -0.6212406)
#> 4 a 1.60 -2.21 3.57 (1.595281 -2.2147)
#> 5 a 0.330 1.12 NA (0.3295078 1.124931)
#> 6 b -0.820 -0.0449 1.31 (-0.8204684 -0.04493361)
#> 7 b 0.487 -0.0162 0.992 (0.4874291 -0.01619026)
#> 8 b 0.738 0.944 0.204 (0.7383247 0.9438362)
#> 9 b 0.576 0.821 0.910 (0.5757814 0.8212212)
#> 10 b -0.305 0.594 NA (-0.3053884 0.5939013)
Run Code Online (Sandbox Code Playgroud)
我想计算dist用sf::st_distance().我该怎么做?
首先要知道的sf是几何列(类之一sfc)作为列表列存储在数据帧内.通常对列表列执行任何操作的关键是使用purrr::map和朋友或使用接受list-cols作为参数的函数.在st_distance它的参数可以是sf(一个数据帧),sfc(几何列),甚至一个sfg(一个geom行)的对象的情况下,所以不需要map和朋友.解决方案看起来应该是这样的:
df %>%
group_by(gr) %>%
mutate(
dist = st_distance(geometry)
)
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.经过一番调查,我们发现了两个问题.首先,st_distance返回距离矩阵而不是单个值.为了解决这个问题,我们利用了by_element = T论证st_distance.
接下来,我们不能这样做,dist = st_distance(geometry, lead(geometry), by_element = T)因为lead只适用于矢量列,而不是列表列.
为解决第二个问题,我们自己使用创建了铅柱geometry[row_number() + 1].
这是完整的解决方案:
library(sf)
library(dplyr)
df %>%
group_by(gr) %>%
mutate(
lead = geometry[row_number() + 1],
dist = st_distance(geometry, lead, by_element = T),
)
#> Simple feature collection with 10 features and 4 fields
#> Active geometry column: geometry
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: -0.8356286 ymin: -2.2147 xmax: 1.595281 ymax: 1.511781
#> epsg (SRID): 4326
#> proj4string: +proj=longlat +datum=WGS84 +no_defs
#> # A tibble: 10 x 6
#> # Groups: gr [2]
#> gr x y dist geometry
#> <fct> <dbl> <dbl> <dbl> <sf_geometry [degree]>
#> 1 a -0.626 1.51 1.38 POINT (-0.6264538 1.511781)
#> 2 a 0.184 0.390 1.44 POINT (0.1836433 0.3898432)
#> 3 a -0.836 -0.621 2.91 POINT (-0.8356286 -0.6212406)
#> 4 a 1.60 -2.21 3.57 POINT (1.595281 -2.2147)
#> 5 a 0.330 1.12 0 POINT (0.3295078 1.124931)
#> 6 b -0.820 -0.0449 1.31 POINT (-0.8204684 -0.04493361)
#> 7 b 0.487 -0.0162 0.992 POINT (0.4874291 -0.01619026)
#> 8 b 0.738 0.944 0.204 POINT (0.7383247 0.9438362)
#> 9 b 0.576 0.821 0.910 POINT (0.5757814 0.8212212)
#> 10 b -0.305 0.594 0 POINT (-0.3053884 0.5939013)
#> # ... with 1 more variable: lead <sf_geometry [degree]>
Run Code Online (Sandbox Code Playgroud)