我有两个具有多个多边形特征的sf对象.这两个对象具有我希望使用多边形将其相互连接的属性.我已经用st_join()个做到了这一点,但不是以我预期的方式.

当使用st_join()时,我最初预计,如果我指定参数st_join(x, y, left = TRUE),那么如果x中的所有几何图形都包含在y中,那么生成的对象将具有与x相同的特征数量.例如:

library("sf")
library("tidyverse")

map1 <- st_as_sfc(c("POLYGON((0 0 , 0 1 , -1 1 , -1 0, 0 0))",
                    "POLYGON((0 0 , 0 2 , 2 2 , 2 0, 0 0 ))", 
                    "POLYGON((0 0 , 0 -0.5 , -0.5 -0.5 , -0.5 0, 0 0))")) %>% 
  st_sf(ID = paste0("poly", 1:3),
        att1 = c(letters[1:3]))

map2 <- st_as_sfc(c("POLYGON((0 0 , 0 0.5 , -0.5 .5 , -0.5 0, 0 0))",
                    "POLYGON((-0.5 0 , -0.5 1 , -1 1 , -1 0 , -0.5 0))",
                    "POLYGON((0 0 , 0 1 , 1 1 , 1 0 , 0 0))",
                    "POLYGON((1 1 , 1 2 , 2 2 , 2 1 , 1 1))",
                    "POLYGON((0 0 , 0 -0.25 , -0.25 -0.25 , -0.25 0 , 0 0))")) %>% 
  st_sf(ID = paste0("poly", 4:8)) %>%
  mutate(att2 = c(LETTERS[1:5]))

map3 <- st_join(map2,
                map1,
                left = TRUE)

我的期望是map3将具有5个特征,因为map2中的所有多边形都唯一地包含在map1的特征中.然而,事实并非如此,不重叠的多边形似乎已经相互连接:

Simple feature collection with 12 features and 4 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -1 ymin: -0.25 xmax: 2 ymax: 2
CRS:           NA
First 10 features:
     ID.x att2  ID.y att1                              .
1   poly4    A poly1    a POLYGON ((0 0, 0 0.5, -0.5 ...
1.1 poly4    A poly2    b POLYGON ((0 0, 0 0.5, -0.5 ...
1.2 poly4    A poly3    c POLYGON ((0 0, 0 0.5, -0.5 ...
2   poly5    B poly1    a POLYGON ((-0.5 0, -0.5 1, -...
2.1 poly5    B poly3    c POLYGON ((-0.5 0, -0.5 1, -...
3   poly6    C poly1    a POLYGON ((0 0, 0 1, 1 1, 1 ...
3.1 poly6    C poly2    b POLYGON ((0 0, 0 1, 1 1, 1 ...
3.2 poly6    C poly3    c POLYGON ((0 0, 0 1, 1 1, 1 ...
4   poly7    D poly2    b POLYGON ((1 1, 1 2, 2 2, 2 ...
5   poly8    E poly1    a POLYGON ((0 0, 0 -0.25, -0....

我通过使用参数largest = TRUE成功获得了预期结果,如下所示:

map4 <- st_join(map2,
                map1,
                left = TRUE,
                largest = TRUE)

然而,我不明白为什么这会给我想要的输出,但省略largest = TRUE却不能.

感谢您抽出时间提供帮助!

推荐答案

st_join()的默认判定为st_intersects,对于touch 几何形状,也是TRUE.

intersects - A and B are not disjoint
disjoint - A and B have no points in common
( https://r-spatial.org/book/03-Geometries.html#sec-de9im )

就你的情况而言,你似乎是st_within之后.

within B的点都不在A之外

首先让我们可视化这些功能的关联:

library("sf")
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library("tidyverse")

map1 <- st_as_sfc(c("POLYGON((0 0 , 0 1 , -1 1 , -1 0, 0 0))",
                    "POLYGON((0 0 , 0 2 , 2 2 , 2 0, 0 0 ))", 
                    "POLYGON((0 0 , 0 -0.5 , -0.5 -0.5 , -0.5 0, 0 0))")) %>% 
  st_sf(ID = paste0("poly", 1:3),
        att1 = c(letters[1:3]))

map2 <- st_as_sfc(c("POLYGON((0 0 , 0 0.5 , -0.5 .5 , -0.5 0, 0 0))",
                    "POLYGON((-0.5 0 , -0.5 1 , -1 1 , -1 0 , -0.5 0))",
                    "POLYGON((0 0 , 0 1 , 1 1 , 1 0 , 0 0))",
                    "POLYGON((1 1 , 1 2 , 2 2 , 2 1 , 1 1))",
                    "POLYGON((0 0 , 0 -0.25 , -0.25 -0.25 , -0.25 0 , 0 0))")) %>% 
  st_sf(ID = paste0("poly", 4:8)) %>%
  mutate(att2 = c(LETTERS[1:5]))

ggplot() +
  geom_sf(data = map1, aes(fill = "map1"), linewidth = 5,) +
  geom_sf(data = map2, aes(fill = "map2"), color = "white", , linewidth = 1, alpha = .2) +
  geom_sf_label(data = map1, aes(label = ID, fill = "map1")) +
  geom_sf_label(data = map2, aes(label = ID, fill = "map2"), color = "white") +
  theme_minimal()

st_join(... , join = st_intersects)(默认):


st_join(map2, map1, left = TRUE, join = st_intersects)
#> Simple feature collection with 12 features and 4 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -1 ymin: -0.25 xmax: 2 ymax: 2
#> CRS:           NA
#> First 10 features:
#>      ID.x att2  ID.y att1                              .
#> 1   poly4    A poly1    a POLYGON ((0 0, 0 0.5, -0.5 ...
#> 1.1 poly4    A poly2    b POLYGON ((0 0, 0 0.5, -0.5 ...
#> 1.2 poly4    A poly3    c POLYGON ((0 0, 0 0.5, -0.5 ...
#> 2   poly5    B poly1    a POLYGON ((-0.5 0, -0.5 1, -...
#> 2.1 poly5    B poly3    c POLYGON ((-0.5 0, -0.5 1, -...
#> 3   poly6    C poly1    a POLYGON ((0 0, 0 1, 1 1, 1 ...
#> 3.1 poly6    C poly2    b POLYGON ((0 0, 0 1, 1 1, 1 ...
#> 3.2 poly6    C poly3    c POLYGON ((0 0, 0 1, 1 1, 1 ...
#> 4   poly7    D poly2    b POLYGON ((1 1, 1 2, 2 2, 2 ...
#> 5   poly8    E poly1    a POLYGON ((0 0, 0 -0.25, -0....

st_join(... , join = st_within):

st_join(map2, map1, left = TRUE, join = st_within)
#> Simple feature collection with 5 features and 4 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -1 ymin: -0.25 xmax: 2 ymax: 2
#> CRS:           NA
#>    ID.x att2  ID.y att1                              .
#> 1 poly4    A poly1    a POLYGON ((0 0, 0 0.5, -0.5 ...
#> 2 poly5    B poly1    a POLYGON ((-0.5 0, -0.5 1, -...
#> 3 poly6    C poly2    b POLYGON ((0 0, 0 1, 1 1, 1 ...
#> 4 poly7    D poly2    b POLYGON ((1 1, 1 2, 2 2, 2 ...
#> 5 poly8    E poly3    c POLYGON ((0 0, 0 -0.25, -0....

创建于2024年4月15日,共有reprex v2.1.0

R相关问答推荐

R中的枢轴/转置

更改绘图上的x轴断点,而不影响风险?

derrr mutate case_when grepl不能在R中正确返回值

如何在emmeans中计算连续变量的对比度

RStudio中相关数据的分组箱形图

如何在一次运行中使用count进行多列计数

如何根据嵌套元素的名称高效而优雅地确定它属于哪个列表?

即使硬币没有被抛出,也要保持对其的跟踪

从多个线性回归模型中提取系数

当我们有多个反斜杠和/特殊字符时使用Gsubing

用两种 colored颜色 填充方框图

以NA为通配符的R中的FULL_JOIN以匹配其他数据中的任何值.Frame

减go R中列表的所有唯一元素对

WRS2包中带有bwtrim的简单ANOVA抛出错误

'使用`purrr::pwalk`从嵌套的嵌套框架中的列表列保存ggplots时出现未使用的参数错误

创建列并对大型数据集中的特定条件进行成对比较的更高效程序

是否可以将线性模型的p值添加到tbl_summary中

条形图中的条形图没有try 赋予它们的 colored颜色

基于R中的引用将向量值替换为数据框列的值

在shiny 表格中输入的文本在第一次后未更新