如何在 st_intersect 之后从几何集合中选择某些几何? [英] How to select certain geometries from a geometrycollection after st_intersect?

查看:41
本文介绍了如何在 st_intersect 之后从几何集合中选择某些几何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用奇妙的新 sf 包运行两个多边形或其他 sf 对象的交集.类似这样:

I am running an intersect of two polygons or other sf objects using the fantastic new sf package. It's similar to this:

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 的等效项.

how do I only keep the POLYGON of the GEOMETRYCOLLECTION? Selecting different types in a feature collection is easy enough, but I can't seem to find the equivalent of ST_CollectionExtract in the sf package.

推荐答案

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

Output is a list so that you can extract with i[[3]] here.
If you a more standard way to find which element is a polygon use:

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来分隔特征类型,然后选择感兴趣的行:

If you have a sfc, you can use st_cast to separate feature types, and then select the lines of interest:

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

这篇关于如何在 st_intersect 之后从几何集合中选择某些几何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆