行之间点的距离与sf库相关

12

我有多个轨迹被保存在简单要素(sf)中,类型为POINT。 我想计算相邻位置之间(即行)的欧几里得距离。 到目前为止,我已使用勾股定理计算二维空间中的欧几里得距离来“手动”计算距离。我想知道是否可以使用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)

我想使用sf::st_distance()计算dist。 我应该如何进行?

2个回答

29

关于sf的第一件事是,几何列(sfc类)存储为数据框内的列表列。通常使用列表列的关键是要使用purrr::map及相关函数或使用接受列表列作为参数的函数。在st_distance的情况下,它的参数可以是sf对象(一个数据框)、sfc(几何列),甚至还可以是sfg(单个geom行),因此不需要map及其相关函数。解决方案应该类似于:

df %>%
  group_by(gr) %>%
  mutate(
    dist = st_distance(geometry)
  )

然而,这样不起作用。经过一些调查,我们发现有两个问题。首先,st_distance 返回距离矩阵而不是单个值。为了解决这个问题,我们利用 st_distanceby_element = T 参数。

其次,我们不能仅仅做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]>

6
感谢您不仅分享了解决方案,还分享了背后的思考过程。这使得答案更具价值! - Ratnanil
我不知道什么时候发生的,但是lead()lag()现在可以用于列表。因此,在mutate()中,你可以只写dist = st_distance(geometry, lead(geometry), by_element = T) - Ratnanil

1

这里是一个基于R的解决方案。关键在于:

  1. 通过组列对数据框进行split()
  2. 对于每个组
    • 使用head()tail()计算相邻位置之间的距离
    • NA附加到结果中(因为最后一个位置没有下一个位置)
  3. 使用do.call()中的rbind将生成的sf对象绑定回来。
split(df, df$gr) |> 
  lapply(\(x){
    x$dist <- c(st_distance(head(x,-1), tail(x,-1),by_element = TRUE), NA)
    x
  }) |> 
  do.call(rbind, args = _)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接