我的数据集的网格分辨率为0.25 * 0.25度
lon <- c(-53.615449969, -53.365449969, -53.115449969, -53.365449969,
-53.115449969, -52.865449969, -53.365449969, -53.115449969, -52.865449969,
-52.615449969, -53.365449969, -53.115449969, -52.865449969, -52.615449969,
-52.365449969, -53.365449969, -53.115449969, -52.865449969, -52.615449969,
-52.365449969, -53.615449969, -53.365449969, -53.115449969, -52.865449969,
-52.615449969, -52.365449969, -52.115449969, -53.865449969, -53.615449969,
-53.365449969, -53.115449969, -52.865449969, -52.615449969, -52.365449969,
-52.115449969, -51.865449969, -54.365449969, -54.115449969, -53.865449969,
-53.615449969, -53.365449969, -53.115449969, -52.865449969, -52.615449969,
-52.365449969, -52.115449969, -51.865449969, -51.615449969, -54.615449969,
-54.365449969, -54.115449969, -53.865449969, -53.615449969, -53.365449969,
-53.115449969, -52.865449969, -52.615449969, -52.365449969, -52.115449969,
-51.615449969)
lat <- c(-33.627081271, -33.627081271, -33.627081271, -33.377081271,
-33.377081271, -33.377081271, -33.127081271, -33.127081271, -33.127081271,
-33.127081271, -32.877081271, -32.877081271, -32.877081271, -32.877081271,
-32.877081271, -32.627081271, -32.627081271, -32.627081271, -32.627081271,
-32.627081271, -32.377081271, -32.377081271, -32.377081271, -32.377081271,
-32.377081271, -32.377081271, -32.377081271, -32.127081271, -32.127081271,
-32.127081271, -32.127081271, -32.127081271, -32.127081271, -32.127081271,
-32.127081271, -32.127081271, -31.877081271, -31.877081271, -31.877081271,
-31.877081271, -31.877081271, -31.877081271, -31.877081271, -31.877081271,
-31.877081271, -31.877081271, -31.877081271, -31.877081271, -31.627081271,
-31.627081271, -31.627081271, -31.627081271, -31.627081271, -31.627081271,
-31.627081271, -31.627081271, -31.627081271, -31.627081271, -31.627081271,
-31.627081271)
df <- as.data.frame(cbind(lon, lat))
df$ID <- 1:nrow(df)
coordinates(df) <- c(1,2)
library(raster)
elev <- getData('alt', country='BRA')
plot(elev)
plot(df, add = T)
对于我的0.25 * 0.25度网格的每一个,我想计算平均高程。如何在R中做到这一点?
答案 0 :(得分:1)
鉴于res(elev)
为0.008333333
(即1/120
),您希望以因子.25 * 120 = 30
进行汇总。最简单的方法是
a <- aggregate(elev, 30, fun=mean, na.rm=TRUE)
现在,分辨率是所需的,但不是与您指定的栅格的对齐。图解:
r <- rasterFromXYZ(df)
s <- as(r, 'SpatialPolygons')
z <- crop(a, r, snap='out')
plot(z)
plot(s, add=TRUE)
相反,您可以这样做:
x <- resample(elev, r)
或(更精确,但更慢)
e <- extract(elev, s, fun=mean, na.rm=TRUE)
s$elev <- e
比较值
z <- rasterize(r, s)
plot(z, e)