select 如何在 st_intersect 之后从几何集合中 select 某些几何图形?

How to select certain geometries from a geometrycollection after st_intersect?

我是 运行 两个多边形或其他 sf 对象的交集,使用了很棒的新 sf 包。类似于:

a <- st_polygon(list(cbind(c(0,0,7.5,7.5,0),c(0,-1,-1,0,0))))
b <- st_polygon(list(cbind(c(0,1,2,3,4,5,6,7,7,0),c(1,0,.5,0,0,0.5,-0.5,-0.5,1,1))))
i <- st_intersection(a,b)
## GEOMETRYCOLLECTION(POINT(1 0), LINESTRING(4 0, 3 0), POLYGON((5.5 0, 7 0, 7 -0.5, 6 -0.5, 5.5 0)))

如何只保留 GEOMETRYCOLLECTIONPOLYGON?在特征集合中选择不同的类型很容易,但我似乎无法在 sf 包中找到 ST_CollectionExtract 的等价物。

输出是一个列表,因此您可以在此处使用 i[[3]] 提取。
如果您使用更标准的方法来查找哪个元素是多边形,请使用:

w.pol <- purrr::map_lgl(i, ~st_is(.x, c("POLYGON", "MULTIPOLYGON")))
pol <- i[[which(w.pol)]]

##> pol
## POLYGON((5.5 0, 7 0, 7 -0.5, 6 -0.5, 5.5 0))

编辑:如果你有 sfc,你可以使用 st_cast 来分隔特征类型,然后 select 兴趣线:

# Simple feature data frame of spatial collection
a1 <- st_sf(a=1, geom = st_sfc(i))
a2 <- st_sf(a=2, geom = st_sfc(i))

ii <- rbind(a1, a2)

# Use st_cast to separate all features types
st_cast(ii)[which(st_is(st_cast(ii), c("POLYGON", "MULTIPOLYGON"))),]

编辑:2017-12-23

您可以直接使用:

st_collection_extract(i, "POLYGON")