簡體   English   中英

R:將柵格聚合為 shapefile 多邊形

[英]R: Aggregating raster to shapefile polygons

我想將柵格數據聚合到自定義 shapefile 中的每個多邊形。

在這種情況下,我想獲得撒哈拉以南非洲次國家區域內的平均城市化程度。

我的科幻是這樣的:

> 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 中的點幾何圖形。 然后,這個 shapefile 可以聚合到我的區域邊界。 誠然,這不是很優雅(並且最終不起作用)。

library(tidyverse)
library(sf)
library(raster)
library(stars)
library(rgdal)

> # aggregate (to 25x25 km)
> imported_raster_ag <- aggregate(imported_raster, fact=25)
> 
> # convert to sp
> urbanized = rasterToPolygons(imported_raster_ag) 
> # convert to sf
> urbanized_sf <- st_as_sf(urbanized)

# compare projection
st_crs(africa_map)==st_crs(urbanized_sf)
# align projection
urbanized_sf <- st_transform(urbanized_sf, st_crs(africa_map))

urbanized_sf <- urbanized_sf %>% rename(urbanization = GHS_BUILT_LDS1975_GLOBE_R2018A_54009_1K_V2_0)

> urbanized_sf
Simple feature collection with 398872 features and 1 field (with 500 geometries empty)
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -179.9999 ymin: -57.40086 xmax: 179.9963 ymax: 82.33738
Geodetic CRS:  WGS 84
First 10 features:
   urbanization                       geometry
1             0 POLYGON ((-117.1367 82.3373...
2             0 POLYGON ((-116.2261 82.3373...
3             0 POLYGON ((-115.3156 82.3373...
4             0 POLYGON ((-114.405 82.33738...
5             0 POLYGON ((-113.4944 82.3373...
6             0 POLYGON ((-112.5838 82.3373...
7             0 POLYGON ((-111.6732 82.3373...
8             0 POLYGON ((-110.7627 82.3373...
9             0 POLYGON ((-109.8521 82.3373...
10            0 POLYGON ((-108.9415 82.3373...

我的想法是,到目前為止,我可以將這些點聚合到另一個 SF 中的區域邊界上。 但是,我收到一條錯誤消息,並且我發現的唯一推薦修復只是重復它。

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

我想在我的轉換過程中的某個地方可能會損壞或者其他一些更根本的缺陷。 將柵格數據聚合到 shapefile 多邊形中有哪些更優雅、更重要的是功能更強大的工作流?

看看extract function

在你的情況下,像

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

africamap仍應首先投影到柵格投影。 如果您有nodata值,那么在調用中添加一個na.rm=TRUE參數應該是明智的。

為了縮短處理時間,您還可以將柵格裁剪到非洲。

exactextractr package 中還有一個更快的extract版本。 您還可以利用terra而不是raster進行柵格處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM