您可以使用 raster::extract() 根据您的多边形汇总栅格数据。
我经常发现有必要指定要提取的包以确保您获取正确的版本。
library(sp) # I'm assuming that's the source for st_read
library(raster)
my_shape <- st_read("multipolygon shapfile with 196 provinces inside Peru")
r <- raster("raster file of temp with 1km2 grid")
## not necessary to crop source raster
my_shape$mean <- raster::extract(r, my_shape, fun = mean, na.rm = TRUE)
这应该会在您的省份多边形图层中添加一个属性列。你可以用 fun = min 和 fun = max 做同样的事情。
如果您的栅格图层很大,或者您想多次执行此操作,则使用 velox 等替代包可能会获得更好的性能。
library(velox)
vs <- velox("raster file of temp with 1km2 grid")
my_shape$mean2 <- vs$extract(my_shape, fun = function(x){mean(x, na.rm= TRUE)})
velox 方法要快得多(见下文)
另一种选择是使用 stars 包,并使用aggregate() 来汇总最小和最大数据。见How to extract values from a raster using polygons with the R stars package?
library(stars)
library(microbenchmark)
rstar = read_stars(raster_file)
st_crs(rstar) <- st_crs(my_shape)
f1 = function(){
my_shape$mean = raster::extract(r, my_shape, fun = mean, na.rm = TRUE)
}
f2 = function(){
vs <- velox(raster_file)
my_shape$mean2 <- vs$extract(my_shape, fun = function(x){mean(x, na.rm= TRUE)})
}
f3 = function(){
zones = rasterize(my_shape, r)
raster::zonal(r, zones)
}
f4 = function(){
x = aggregate(rstar, my_shape, mean, na.rm = T)
st_as_sf(x)
}
res = microbenchmark(f1(), f2(), f3(), f4(), times = 3)
res
# Unit: seconds
# expr min lq mean median uq max neval
# f1() 172.283024 174.644299 178.920625 177.005575 182.2394 187.473276 3
# f2() 3.339221 3.374647 3.396874 3.410072 3.4257 3.441327 3
# f3() 170.981220 179.481681 182.520730 187.982142 188.2905 188.598829 3
# f4() 167.076081 173.427232 179.332300 179.778383 185.4604 191.142437 3
velox$extract 显然是性能赢家。其他三个版本看起来非常相似。
zonal 的处理时间主要用于对多边形图层进行栅格化,如果您用于多个指标,它会超过提取和聚合。
从文件中读取栅格包含在 velox 表达式中,但不包含在其他版本中。