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

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

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

which looks like this 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

然后我像这样做一个self 交叉点

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列中取消列出观测,以便每个值都在自己的列中,然后将面的属性绑定到多个origins列中的每个值.但有没有更简单的方法呢?

推荐答案

一种可能性是,为了方便起见使用{dplyr}:

edit个 多个属性列的通用解决方案:

  • 添加一些属性并重新相交:
s$temperature <- rep(c("low", "medium", "high"), length.out=nrow(s))
s$region = rep(c("Oak county", "Pine state", "Fir territory"), length.out=nrow(s))

s_intersection <- st_intersection(s)
  • 以逗号分隔的字符串形式添加属性(如果使用List-Column可以跳过):
 s_intersection |>
 rowwise() |>
 mutate(across(-c(x, origins), ## leave out x (geometry col) and origins
               ~ list(as.data.frame(s)[origins, cur_column()]) |>
                 unlist() |> paste(collapse = ', ')
               )
        )
Simple feature collection with 31 features and 5 fields
Geometry type: GEOMETRY
Dimension:     XY
Bounding box:  xmin: 0.3840939 ymin: 1.132567 xmax: 10.19807 ymax: 10.14945
CRS:           NA
# A tibble: 31 x 6
# Rowwise: 
   tree_type temperature region     n.overlaps origins                         x
 * <chr>     <chr>       <chr>      <chr>      <list>                 <GEOMETRY>
 1 oak       low         Oak county ""         <int>   POLYGON ((2.06437 1.2494~
# ...
 8 oak, pine low, medium Oak count~ ""         <int>   POLYGON ((6.186254 2.916~
# ...

或者,如果您想要将多边形复制到每个树类型的一行中,那么从{tidyr}中 Select separate_rows可能很方便.

R相关问答推荐

多重插补后如何按组汇总平均值?

如何确保模块化lme4接受控制论点?

在图内移动y轴上的标签

R数据帧中的布尔加法会产生布尔值而不是整值

将模拟变量乘以多个观测结果中的模拟变量

如何在四进制仪表板值框中显示值(使用shiny 的服务器计算)

ggplot 2中的地块底图(basemaps_gglayer()不起作用)

非线性混合效应模型(NLME)预测变量的置信区间

用关联字符串替换列名的元素

即使硬币没有被抛出,也要保持对其的跟踪

如何读取CSV的特定列时,给定标题作为向量

使用rest从header(h2,h3,table)提取分层信息

将选定的索引范围与阈值进行比较

列名具有特殊字符时的循环回归

来自程序包AFEX和amp;的类/函数和NICE_TABLE&冲突

如何将一些单元格的内容随机 Select 到一个数据框中?

将摘要图添加到facet_WRAP gglot的末尾

有没有办法将不等长的列表转换为R中的数据帧

避免在图例中显示VLINS组

如果满足条件,则替换列的前一个值和后续值