我想知道它是否比下面使用 rstats sf 的示例更简单,以计算底层网格中多边形 shapefile 的(分数)面积覆盖率。到目前为止,我还没有找到可以为我完成这项工作的功能。这是我的例子:
library(sf)
## create a multipolygon
p1 <- rbind(c(7, 48), c(8, 52), c(11, 49), c(9, 48), c(7, 48))
p2 <- rbind(c(8, 50), c(8, 51), c(9, 50), c(8, 50))
mpol <- st_sfc(st_multipolygon(list(list(p1, p2))), crs=st_crs(4326))
## create a half degree grid covering the multipolygon
grid <- st_make_grid(mpol, cellsize=0.5)
## Create a sf object with the required attributes
## package 'lwgeom' reqiured
area <- st_area(grid)
grid <- st_as_sf(data.frame(ID=c(1:length(area)), area=area, geometry=grid))
## check 'map' elements
plot(grid['area'])
plot(mpol, col="#FF0000AA", add=TRUE)
现在开始计算,我首先通过从边界框('bpol')中减去'mpol'来反转多边形('ipol')并计算新的覆盖区域。
## create a rectangular bounding box around the polygon
bbox <- st_bbox(mpol)
bpol <- st_sfc(st_polygon(list(rbind(c(bbox['xmin'], bbox['ymin']),
c(bbox['xmax'], bbox['ymin']),
c(bbox['xmax'], bbox['ymax']),
c(bbox['xmin'], bbox['ymax']),
c(bbox['xmin'], bbox['ymin'])))), crs=st_crs(4326))
## invert the multipolygon, by substracting it from the bounding box
ipol <- st_difference(bpol, mpol)
## substract the inverted polygon from the grid
gridinpoly <- st_difference(grid, ipol)
## calculate new 'cropped' area
gridinpoly$croppedArea = st_area(gridinpoly)
plot(gridinpoly['area'])
plot(gridinpoly['croppedArea'])
## Finally the fractional coverage is calculated:
gridinpoly$frac = gridinpoly$croppedArea / gridinpoly$area
plot(gridinpoly['frac'])
如果一个网格单元被覆盖两次,然后从网格中断处裁剪倒多边形,它会变得更加复杂
p3 <- rbind(c(9.75, 50.5), c(10, 51), c(10.5, 50), c(9.75, 50.5))
mpol <- st_sfc(st_multipolygon(list(list(p1, p2, p3))), crs=st_crs(4326))
ipol <- st_difference(bpol, mpol)
gridinpoly <- st_difference(grid, ipol)
错误消息:“CPL_geos_op2(op, st_geometry(x), st_geometry(y)) 中的错误:评估错误:TopologyException:输入几何 1 无效:在 9.75 50.5 处或附近的嵌套壳在 9.75 50.5。
现在我的问题是,是否已经有一个功能可以完成这项工作?我错过了文档中的某些内容吗?当网格单元被裁剪多次时,如何解决错误?
谢谢和亲切的问候约尔格