R:将光栅聚合到shapefile多边形

R:将光栅聚合到shapefile多边形,r,polygon,raster,aggregation,sf,R,Polygon,Raster,Aggregation,Sf,我想将光栅数据聚合到自定义形状文件中的每个多边形 在这种情况下,我想获得撒哈拉以南非洲国家以下区域的平均城市化程度 我的sf看起来像这样: > africa_map Simple feature collection with 543 features and 4 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: -25.36042 ymin: -46.96575 xmax: 63.49391

我想将光栅数据聚合到自定义形状文件中的每个多边形

在这种情况下,我想获得撒哈拉以南非洲国家以下区域的平均城市化程度

我的sf看起来像这样:

> africa_map
Simple feature collection with 543 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -25.36042 ymin: -46.96575 xmax: 63.49391 ymax: 27.66147
Geodetic CRS:  WGS 84
First 10 features:
    cname ccode        regname continent                       geometry
1  Angola    AO          Bengo    Africa MULTIPOLYGON (((13.371 -8.5...
2  Angola    AO       Benguela    Africa MULTIPOLYGON (((12.53336 -1...
3  Angola    AO            Bie    Africa MULTIPOLYGON (((16.61158 -1...
4  Angola    AO        Cabinda    Africa MULTIPOLYGON (((12.78266 -4...
5  Angola    AO Cuando Cubango    Africa MULTIPOLYGON (((21.9838 -16...
6  Angola    AO   Cuanza Norte    Africa MULTIPOLYGON (((15.40788 -7...
7  Angola    AO     Cuanza Sul    Africa MULTIPOLYGON (((13.7926 -11...
或绘制:

另一方面,光栅数据采用以下形式:

> imported_raster
class      : RasterLayer 
dimensions : 18000, 36082, 649476000  (nrow, ncol, ncell)
resolution : 1000, 1000  (x, y)
extent     : -18041000, 18041000, -9e+06, 9e+06  (xmin, xmax, ymin, ymax)
crs        : +proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
names      : GHS_BUILT_LDS1975_GLOBE_R2018A_54009_1K_V2_0 
values     : 0, 100  (min, max)
对于整个地球来说,这些都比需要的粒度细得多。为了加速计算,我首先聚合光栅,然后将其转换为一个shapefile,剩余的每个光栅像素都转换为shapefile中的点几何体。然后,这个形状文件可以聚合到我的区域边界。诚然,这不是很优雅(最终也行不通)

库(tidyverse)
图书馆(sf)
图书馆(光栅)
图书馆(星级)
图书馆(rgdal)
>#骨料(至25x25 km)
>进口光栅光栅光栅
>#转换为sp
>城市化=光栅拓扑多边形(进口光栅)
>#转换为sf
>城市化非洲=4。
>城市化固定=4。

我想在我的转化过程中,可能有什么东西被破坏了,或者有什么更根本的缺陷。将光栅数据聚合到shapefile多边形中,最重要的是,还有什么更优雅、功能更强大的工作流?

请查看
提取功能

就你的情况来说

africamap$urbanized <- extract(imported_raster, africamap, fun="mean")

africamap$urbanized非常好的建议,非常感谢!使用推荐的exactextractr包,聚合工作非常快,无需裁剪,我得到了期望的结果。对齐投影:
africa\u map\u mollweide=st\u transform(africa\u map,crs=“+proj=moll”)
并提取光栅数据:
africa\u map\u mollweide$urbanized
> urbanized_africa <- aggregate(urbanized_sf["urbanization"], by = africa_map$geometry, mean)
although coordinates are longitude/latitude, st_intersects assumes that they are planar
Error in CPL_geos_binop(st_geometry(x), st_geometry(y), op, par, pattern,  : 
  Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.

> urbanized_sf_fixed <- sf::st_make_valid(urbanized_sf)
Error in CPL_geos_make_valid(x) : 
  Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
africamap$urbanized <- extract(imported_raster, africamap, fun="mean")