gpt4 book ai didi

r - 如何在 st_intersect 之后从几何集合中选择某些几何图形?

转载 作者:行者123 更新时间:2023-12-02 19:35:28 25 4
gpt4 key购买 nike

我正在使用奇妙的新 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来分隔要素类型,然后选择感兴趣的行:

# 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") 

关于r - 如何在 st_intersect 之后从几何集合中选择某些几何图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45723775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com