如何识别空心几何图形的交点而忽略其他几何图形?

问题描述 投票:0回答:1

我正在使用 sf 包在 R 中处理空间数据,并且我需要识别完全位于较大几何图形内的几何图形(而不仅仅是相交或接触边界)。

换句话说,我需要红色方块(中心空心)与紫色合并,同时保持蓝色不受影响。

enter image description here

有人知道该怎么做吗?

这是一个数据示例:


# Geometry 1 (geom1) is a polygon with a hole (an outer boundary and an inner "empty" ring).
# Geometry 2 (geom2) is a simple polygon that aligns perfectly with the inner boundary of geom1 (it is completely inside the "hole").
# Geometry 3 (geom3) is a separate polygon that intersects the boundary of geom1 but is not fully contained by it.

library(sf)

# Create geometry 1 as a polygon with a hole
geom1 <- st_polygon(list(
  rbind(c(0, 0), c(0, 5), c(5, 5), c(5, 0), c(0, 0)),           # Outer boundary
  rbind(c(1, 1), c(1, 4), c(4, 4), c(4, 1), c(1, 1))            # Inner "hole"
))

# Create geometry 2 as a simple polygon
geom2 <- st_polygon(list(
  rbind(c(1, 1), c(1, 4), c(4, 4), c(4, 1), c(1, 1))            # Polygon inside the hole
))

# Create geometry 3 as a separate polygon
geom3 <- st_polygon(list(
  rbind(c(5, 5), c(5, 9), c(9, 9), c(9, 5), c(5, 5))            # Polygon outside geom1
))

# Combine geometries into a simple feature collection
sf_data <- st_sf(geometry = st_sfc(geom1, geom2, geom3))

请注意,

st_intersects
同时返回几何图形2(内部)和几何图形3。在这种情况下,有必要仅返回几何图形2。
st_contains
也不起作用,因为几何图形1有空格。

我希望找到一种方法来识别完全位于另一个几何图形外边界内的几何图形。

r spatial intersection r-sf
1个回答
0
投票

包含的示例表明您可以打孔并使用外环进行操作,以检查覆盖了什么(被什么覆盖)。

sfheaders::sf_remove_holes()

library(sf)

(exterior_rings <- sfheaders::sf_remove_holes(sf_data))
#> Simple feature collection with 3 features and 0 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 0 xmax: 9 ymax: 9
#> CRS:           NA
#>                         geometry
#> 1 POLYGON ((0 0, 0 5, 5 5, 5 ...
#> 2 POLYGON ((1 1, 1 4, 4 4, 4 ...
#> 3 POLYGON ((5 5, 5 9, 9 9, 9 ...

st_covered_by(exterior_rings)
#> Sparse geometry binary predicate list of length 3, where the predicate
#> was `covered_by'
#>  1: 1
#>  2: 1, 2
#>  3: 3

which(lengths(st_covered_by(exterior_rings)) > 1)
#> [1] 2
© www.soinside.com 2019 - 2024. All rights reserved.