7

我正在使用奇妙的新 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)))

我如何只保留POLYGONGEOMETRYCOLLECTION?在功能集合中选择不同的类型很容易,但我似乎无法在 sf 包中找到与 ST_CollectionExtract 等效的内容。

4

1 回答 1

11

输出是一个列表,您可以在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来分隔特征类型,然后选择感兴趣的行:

# 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") 
于 2017-08-22T12:43:04.267 回答