【问题标题】:How can I do a spatial join with the sf package using st_join()如何使用 st_join() 与 sf 包进行空间连接
【发布时间】:2017-10-03 08:16:06
【问题描述】:

这是我一直在努力的一个玩具示例

# Make points
point1 <- c(.5, .5)
point2 <- c(.6, .6)
point3 <- c(3, 3)
mpt <- st_multipoint(rbind(point1, point2, point3))  # create multipoint

# Make polygons
square1 <- rbind(c(0, 0), c(1, 0), c(1,1), c(0, 1), c(0, 0))
square2 <- rbind(c(0, 0), c(2, 0), c(2,2), c(0, 2), c(0, 0))
square3 <- rbind(c(0, 0), c(-1, 0), c(-1,-1), c(0, -1), c(0, 0))
mpol <- st_multipolygon(list(list(square1), list(square2), list(square2)))  # create multipolygon

# Convert to class 'sf'
pts <- st_sf(st_sfc(mpt))
polys <- st_sf(st_sfc(mpol))

# Determine which points fall inside which polygons
st_join(pts, polys, join = st_contains)

最后一行产生

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class "c("sfc_MULTIPOINT", "sfc")" to a data.frame

如何进行空间连接以确定哪些点位于哪些多边形内?

【问题讨论】:

  • 您能澄清一下“空间连接”的含义吗?预期的结果是什么?
  • 给定一组多边形和一组点,创建映射 (PointId, PolygonId) 来说明哪些点包含在哪些多边形中。
  • 我最近为sf package 写了this tutorial,以帮助自己和其他人理解基本概念。了解基本原理是解决像我在这里遇到的特定问题的关键。

标签: r spatial sf


【解决方案1】:

我也在努力解决sf 包的功能,所以如果这不正确,我们深表歉意或有更好的方法。我认为这里的一个问题是,如果像在您的示例中那样构建几何图形,您将无法获得您的想法:

> pts
Simple feature collection with 1 feature and 0 fields
geometry type:  MULTIPOINT
dimension:      XY
bbox:           xmin: 0.5 ymin: 0.5 xmax: 3 ymax: 3
epsg (SRID):    NA
proj4string:    NA
                     st_sfc.mpt.
1 MULTIPOINT(0.5 0.5, 0.6 0.6...

> polys
Simple feature collection with 1 feature and 0 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 0 ymin: 0 xmax: 2 ymax: 2
epsg (SRID):    NA
proj4string:    NA
                    st_sfc.mpol.
1 MULTIPOLYGON(((0 0, 1 0, 1 ...

您可以看到ptspolys 中都只有一个“功能”。这意味着您正在构建一个“多面体”特征(即由 3 个部分构成的面),而不是三个不同的面。积分也是一样。

经过一番挖掘,我发现使用 WKT 表示法构建几何图形的这种不同(并且在我看来更容易)方式:

polys <- 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 -1 , -1 -1 , -1 0, 0 0))")) %>% 
  st_sf(ID = paste0("poly", 1:3))    

pts <- st_as_sfc(c("POINT(0.5 0.5)",
                   "POINT(0.6 0.6)",
                   "POINT(3 3)")) %>%
  st_sf(ID = paste0("point", 1:3))

> polys
Simple feature collection with 3 features and 1 field
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: -1 ymin: -1 xmax: 2 ymax: 2
epsg (SRID):    NA
proj4string:    NA
     ID                              .
1 poly1 POLYGON((0 0, 0 1, 1 1, 1 0...
2 poly2 POLYGON((0 0, 0 2, 2 2, 2 0...
3 poly3 POLYGON((0 0, 0 -1, -1 -1, ...

> pts
Simple feature collection with 3 features and 1 field
geometry type:  POINT
dimension:      XY
bbox:           xmin: 0.5 ymin: 0.5 xmax: 3 ymax: 3
epsg (SRID):    NA
proj4string:    NA
      ID              .
1 point1 POINT(0.5 0.5)
2 point2 POINT(0.6 0.6)
3 point3     POINT(3 3)

您可以看到现在polyspts 都具有三个特征。

我们现在可以使用以下方法找到“交集矩阵”:

# Determine which points fall inside which polygons
pi <- st_contains(polys,pts, sparse = F) %>% 
  as.data.frame() %>% 
  mutate(polys = polys$ID) %>% 
  select(dim(pi)[2],1:dim(pi)[1])
colnames(pi)[2:dim(pi)[2]] = levels(pts$ID)

> pi
  polys point1 point2 point3
1 poly1   TRUE   TRUE  FALSE
2 poly2   TRUE   TRUE  FALSE
3 poly3  FALSE  FALSE  FALSE

意思是(正如 cmets 中的 @symbolixau 所指出的那样)多边形 1 和 2 包含点 1 和 2,而多边形 3 不包含任何点。相反,点 3 不包含在任何多边形中。

HTH。

【讨论】:

  • 非常有趣。希望其他人对此表示赞同。我希望sf 文档有更好的st_join 示例并从头开始构建简单的功能。
  • 我也发现将功能“构建”为列表列表有点麻烦。但是,确实很少需要从头构建“几何”,相对于sp 的速度提升真的很棒!
  • 认为 结果是说点 1 和 2 在多边形 1 和 2 中(它们重叠),点 3 不在任何多边形中,并且多边形 3不包含任何点。 (我使用sf 以外的其他方法尝试了相同的问题来帮助解释结果)
  • 如果您正在寻找有关空间功能的更好文档,您可以查看此处postgis.net/docs/reference.html
【解决方案2】:

我看到了不同的输出:

> # Determine which points fall inside which polygons
> st_join(pts, polys, join = st_contains)
Simple feature collection with 1 feature and 0 fields
geometry type:  MULTIPOINT
dimension:      XY
bbox:           xmin: 0.5 ymin: 0.5 xmax: 3 ymax: 3
epsg (SRID):    NA
proj4string:    NA
                        geometry
1 MULTIPOINT(0.5 0.5, 0.6 0.6...

这是最新的 CRAN 版本的 sf 吗?

【讨论】:

  • 感谢您的回复和很棒的包裹。在我写这个问题的时候,它是 CRAN 上的最新版本。现在我在 0.4-3 上,但我仍然在同一个地方收到错误(尽管消息不同)。罪魁祸首似乎是这个电话pts &lt;- st_sf(st_sfc(mpt)),我猜这是因为一个简单的特征不能只有一个几何图形——它也必须有数据。这就是为什么像pts &lt;- st_sf(Dummy=1, st_sfc(mpt)) 这样的东西可以防止错误。我最近详细浏览了这些小插曲,这消除了我的很多困惑。
  • 谢谢 - 这已在不久前的开发版本中得到纠正。
【解决方案3】:

请注意,原始的多点和多多边形集可以“投射”为点和多边形,而无需创建新对象:

st_contains(polys %>% st_cast("POLYGON"), pts %>% st_cast("POINT"), sparse = F)
#      [,1]  [,2]  [,3]
#[1,]  TRUE  TRUE FALSE
#[2,]  TRUE  TRUE FALSE
#[3,] FALSE FALSE FALSE

【讨论】:

    猜你喜欢
    • 2019-04-18
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2021-01-07
    • 2017-07-01
    相关资源
    最近更新 更多