在sf对象的几何列上使用dplyr full_join

问题描述

我试图根据两个sf对象的几何形状相等来执行完全连接。我知道这两个对象具有相同的 geometry类型和相同的 crs

用于空间联接的st_join函数可以使用st_equals谓词,但仅允许左联接和内部联接(我认为有充分的理由)。但是,当将sf对象转换为小对象时(同时保持几何列表列存在),我可以使用full_join执行常规的dplyr,并且它完全按照我想要的方式工作,至少有一小段时间玩具示例:

suppresspackageStartupMessages({
  library(sf)
  library(dplyr)
  library(tibble)
})

# Create some points
p1 = st_sfc(st_point(c(0,0)),crs = 4326)
p2 = st_sfc(st_point(c(1,crs = 4326)
p3 = st_sfc(st_point(c(0,1)),crs = 4326)

# Create two sf objects.
# Both contain two points,of which one is shared between the two objects.
sf1 = st_as_sf(data.frame(foo = c(1,2),geometry = c(p1,p2)))
sf2 = st_as_sf(data.frame(bar = c("a","b"),p3)))

sf1
#> Simple feature collection with 2 features and 1 field
#> geometry type:  POINT
#> dimension:      XY
#> bBox:           xmin: 0 ymin: 0 xmax: 1 ymax: 0
#> CRS:            epsg:4326
#>   foo    geometry
#> 1   1 POINT (0 0)
#> 2   2 POINT (1 0)
sf2
#> Simple feature collection with 2 features and 1 field
#> geometry type:  POINT
#> dimension:      XY
#> bBox:           xmin: 0 ymin: 0 xmax: 0 ymax: 1
#> CRS:            epsg:4326
#>   bar    geometry
#> 1   a POINT (0 0)
#> 2   b POINT (0 1)

# Convert to tibble for a dplyr full join on the geometry column.
tbl_joined = full_join(as_tibble(sf1),as_tibble(sf2),by = "geometry")

# Convert back to sf.
sf_joined = st_as_sf(tbl_joined)

sf_joined
#> Simple feature collection with 3 features and 2 fields
#> geometry type:  POINT
#> dimension:      XY
#> bBox:           xmin: 0 ymin: 0 xmax: 1 ymax: 0
#> CRS:            epsg:4326
#> # A tibble: 3 x 3
#>     foo    geometry bar  
#>   <dbl> <POINT [°]> <fct>
#> 1     1       (0 0) a    
#> 2     2       (1 0) <NA> 
#> 3    NA       (0 1) b

reprex package(v0.3.0)于2020-10-15创建

那太好了,但是这确实不是一种“正确”的方式。因此,我的问题是:对于这个玩具示例,它可以很好地工作,但是我没有想到的这种方法的缺点(在没有真正的空间连接功能的情况下连接几何体列)会没有呢?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)