找到总计与网格单元相交的sf多边形

时间:2020-05-18 10:16:30

标签: r sf

我有两个多边形和一个等距分布的网格。我的目标是让每个单元格计算每个单元格内多边形的总面积。

library(sf)
library(dplyr)

# Create two polygons of equal size area
pol_A = st_polygon(list(rbind(c(0,0), c(1,1), c(0,1), c(0,0))))
polys = st_sfc(pol_A, pol_A + c(-0.97, .05)) %>% 
  st_as_sf() %>%
  mutate(poly_ID = c("A","B"))

# Create a grid as an sfc and add ID for each cell
gr <- st_make_grid(polys, cellsize = .1, square = T) %>% 
  st_as_sf() %>%
  mutate(cell_ID = row_number())

这给出了以下情节:

# Plot the grid and polys
plot(gr)
plot(polys,add=T, col = "red")

enter image description here

我想找到每个网格单元所有多边形的总面积。从上图可能不明显,但每个多边形相交有三个像元。所以当我这样计算交点时:

# Generate the intersection
i <- st_intersection(gr, polys) %>%
  mutate(x = st_union(x),
         area_in_cell = st_area(x))

我得到:

Simple feature collection with 142 features and 3 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -0.97 ymin: 0 xmax: 1 ymax: 1.05
epsg (SRID):    NA
proj4string:    NA
First 10 features:
   cell_ID poly_ID                              x area_in_cell
1       10       A MULTIPOLYGON (((0 1, 0.03 1...            1
2       11       A MULTIPOLYGON (((0 1, 0.03 1...            1
3       30       A MULTIPOLYGON (((0 1, 0.03 1...            1
4       31       A MULTIPOLYGON (((0 1, 0.03 1...            1
5       32       A MULTIPOLYGON (((0 1, 0.03 1...            1
6       50       A MULTIPOLYGON (((0 1, 0.03 1...            1
7       51       A MULTIPOLYGON (((0 1, 0.03 1...            1
8       52       A MULTIPOLYGON (((0 1, 0.03 1...            1
9       53       A MULTIPOLYGON (((0 1, 0.03 1...            1
10      70       A MULTIPOLYGON (((0 1, 0.03 1...            1

但是当我使用以下方法查看i中每个单元格是否只有一行时:

overlap_count <- i %>% group_by(cell_ID) %>% summarise(count = n()) %>% arrange(desc(count))

我仍然有三个单元格,每个单元格都有两个条目:

Simple feature collection with 139 features and 2 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -0.97 ymin: 0 xmax: 1 ymax: 1.05
epsg (SRID):    NA
proj4string:    NA
# A tibble: 139 x 3
   cell_ID count                                                                                       x
     <int> <int>                                                                          <MULTIPOLYGON>
 1     190     2 (((0 1, 0.03 1, 0.13 1, 0.23 1, 0.33 1, 0.43 1, 0.53 1, 0.63 1, 0.73 1, 0.83 1, 0.93...
 2     210     2 (((0 1, 0.03 1, 0.13 1, 0.23 1, 0.33 1, 0.43 1, 0.53 1, 0.63 1, 0.73 1, 0.83 1, 0.93...
 3     211     2 (((0 1, 0.03 1, 0.13 1, 0.23 1, 0.33 1, 0.43 1, 0.53 1, 0.63 1, 0.73 1, 0.83 1, 0.93...
 4       1     1 (((0 1, 0.03 1, 0.13 1, 0.23 1, 0.33 1, 0.43 1, 0.53 1, 0.63 1, 0.73 1, 0.83 1, 0.93...
 5      10     1 (((0 1, 0.03 1, 0.13 1, 0.23 1, 0.33 1, 0.43 1, 0.53 1, 0.63 1, 0.73 1, 0.83 1, 0.93...
 6      11     1 (((0 1, 0.03 1, 0.13 1, 0.23 1, 0.33 1, 0.43 1, 0.53 1, 0.63 1, 0.73 1, 0.83 1, 0.93...
 7      21     1 (((0 1, 0.03 1, 0.13 1, 0.23 1, 0.33 1, 0.43 1, 0.53 1, 0.63 1, 0.73 1, 0.83 1, 0.93...
 8      22     1 (((0 1, 0.03 1, 0.13 1, 0.23 1, 0.33 1, 0.43 1, 0.53 1, 0.63 1, 0.73 1, 0.83 1, 0.93...
 9      30     1 (((0 1, 0.03 1, 0.13 1, 0.23 1, 0.33 1, 0.43 1, 0.53 1, 0.63 1, 0.73 1, 0.83 1, 0.93...
10      31     1 (((0 1, 0.03 1, 0.13 1, 0.23 1, 0.33 1, 0.43 1, 0.53 1, 0.63 1, 0.73 1, 0.83 1, 0.93...

1 个答案:

答案 0 :(得分:1)

事实证明我快到了:

st_intersection(gr,polys) %>% 
  group_by(cell_ID) %>% 
  summarise(geom = st_union(x)) %>%
  mutate(geom = st_sfc(geom),
         area = st_area(geom)) 

做到了!