gpt4 book ai didi

r - 使用 `st_intersection(x)` 后如何获取所有相交多边形的属性?

转载 作者:行者123 更新时间:2023-12-03 07:54:01 28 4
gpt4 key购买 nike

我想创建一个数据集,其中包含不相交的多边形及其属性,以及两个/所有多边形相交时的属性。

为了演示问题,我创建了一些虚构的数据

set.seed(131)
library(sf)

#create example data
m = rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0))
p = st_polygon(list(m))
n = 20
l = vector("list", n)
for (i in 1:n)
l[[i]] = p + 10 * runif(2)
s = st_sfc(l)
plot(s, col = sf.colors(categorical = TRUE, alpha = .5))
title("overlapping squares")

#convert to sf object
s <- st_as_sf(s)

#add some random info
s$tree_type <- rep(c("oak", "pine", "fir"), length.out=nrow(s))

看起来像这样 enter image description here

数据如下所示:

Simple feature collection with 20 features and 1 field
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 0.3840939 ymin: 1.132567 xmax: 10.19807 ymax: 10.14945
CRS: NA
First 10 features:
x tree_type
1 POLYGON ((2.06437 1.249422,... oak
2 POLYGON ((2.932732 3.757797... pine
3 POLYGON ((8.463468 5.292048... fir
4 POLYGON ((5.186254 2.378545... oak
5 POLYGON ((3.263181 9.149452... pine
6 POLYGON ((4.162483 3.446414... fir
7 POLYGON ((8.651544 3.160404... oak
8 POLYGON ((5.231186 2.916041... pine
9 POLYGON ((0.3840939 6.87075... fir
10 POLYGON ((7.742229 4.721718... oak

然后我像这样进行自交

s_intersection <- st_intersection(s)

它创建这样的数据

Simple feature collection with 31 features and 3 fields
Geometry type: GEOMETRY
Dimension: XY
Bounding box: xmin: 0.3840939 ymin: 1.132567 xmax: 10.19807 ymax: 10.14945
CRS: NA
First 10 features:
tree_type n.overlaps origins x
1 oak 1 1 POLYGON ((2.06437 1.249422,...
2 pine 1 2 POLYGON ((3.932732 4.757797...
3 fir 1 3 POLYGON ((8.463468 6.292048...
4 oak 1 4 POLYGON ((6.186254 2.378545...
5 pine 1 5 POLYGON ((3.263181 9.149452...
6 fir 1 6 POLYGON ((4.162483 3.446414...
7 oak 1 7 POLYGON ((9.651544 3.160404...
4.1 oak 2 4, 8 POLYGON ((6.186254 2.916041...
8 pine 1 8 MULTIPOLYGON (((5.826989 3....
9 fir 1 9 POLYGON ((0.3840939 6.87075...

我想要做的是将 origins 列替换为两个多边形的属性。例如,多边形 4.1 将变成oak, pine。或者如果它是两列也可以。我考虑解决这个问题的方法是取消列出origins列中的观察结果,以便每个值都在自己的列中,然后将多边形的属性绑定(bind)到多个中的每个值起源列。但有没有更简单的方法呢?

最佳答案

st_join(s, s, st_intersects)
Simple feature collection with 42 features and 2 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 0.3840939 ymin: 1.132567 xmax: 10.19807 ymax: 10.14945
CRS: NA
First 10 features:
tree_type.x tree_type.y x
1 oak oak POLYGON ((2.06437 1.249422,...
2 pine pine POLYGON ((2.932732 3.757797...
2.1 pine oak POLYGON ((2.932732 3.757797...
3 fir fir POLYGON ((8.463468 5.292048...
3.1 fir oak POLYGON ((8.463468 5.292048...
4 oak oak POLYGON ((5.186254 2.378545...
4.1 oak pine POLYGON ((5.186254 2.378545...
4.2 oak oak POLYGON ((5.186254 2.378545...
5 pine pine POLYGON ((3.263181 9.149452...
6 fir fir POLYGON ((4.162483 3.446414...

您的自相交方向是正确的,但您需要将原始几何体与自身完全相交,因此您可以使用 st_intersects 进行连接来实现这一点。

关于r - 使用 `st_intersection(x)` 后如何获取所有相交多边形的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76491872/

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