如何计算另一个栅格的土地覆盖值?
How to compute land cover values from another raster?
我有两个光栅:
raster1
class : RasterLayer
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : memory
names : npp
values : -0.345, 1.723 (min, max)
raster2
class : RasterLayer
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : C:/a/b/Documents/c/landuse.tif
names : landuse
values : 1, 12 (min, max)
attributes :
ID zn
from: 1 evergreen needleleaf forest
to : 12 croplands
raster2
是特定区域的土地覆盖类型,raster1
是净初级生产力值。我想计算 raster 2
中每个土地覆盖 class 的净初级生产力是多少。我如何在 R 中实现这一点?
为 r2 中的每个土地覆盖代码求出 r1 中像元值的总和,输出以列表形式返回。可以改为使用 lapply.
library(raster)
#Generate example data
raster1 <- raster(matrix(c(1,1,1,2,3,4,5,6,7),ncol =3))
raster2 <- raster(matrix(c(1,1,1,2,2,2,3,3,3),ncol =3))
raster2 <- as.factor(raster2)
rat <- levels(raster2 )[[1]]
rat[["landcover"]] <- c("land","ocean/lake", "rivers")
levels(raster2 ) <- rat
#Extract sum of values of raster 1 for each level of raster 2
Val <- list()
for(lc in levels(raster2)[[1]]$ID){
Values <- raster1[raster2[]==lc]
Val[[lc]] <- sum(Values)
}
我有两个光栅:
raster1
class : RasterLayer
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : memory
names : npp
values : -0.345, 1.723 (min, max)
raster2
class : RasterLayer
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : C:/a/b/Documents/c/landuse.tif
names : landuse
values : 1, 12 (min, max)
attributes :
ID zn
from: 1 evergreen needleleaf forest
to : 12 croplands
raster2
是特定区域的土地覆盖类型,raster1
是净初级生产力值。我想计算 raster 2
中每个土地覆盖 class 的净初级生产力是多少。我如何在 R 中实现这一点?
为 r2 中的每个土地覆盖代码求出 r1 中像元值的总和,输出以列表形式返回。可以改为使用 lapply.
library(raster)
#Generate example data
raster1 <- raster(matrix(c(1,1,1,2,3,4,5,6,7),ncol =3))
raster2 <- raster(matrix(c(1,1,1,2,2,2,3,3,3),ncol =3))
raster2 <- as.factor(raster2)
rat <- levels(raster2 )[[1]]
rat[["landcover"]] <- c("land","ocean/lake", "rivers")
levels(raster2 ) <- rat
#Extract sum of values of raster 1 for each level of raster 2
Val <- list()
for(lc in levels(raster2)[[1]]$ID){
Values <- raster1[raster2[]==lc]
Val[[lc]] <- sum(Values)
}