应用遮罩功能时如何计算遮罩面积的百分比?

How to calculate % of area masked when applying mask function?

我有两个栅格对象,将它们都裁剪到相同的范围并屏蔽栅格 1 中不在栅格 2 值范围内的所有值。

    suit_hab_45_50_Env <- futureEnv_45_50_cropped<maxValue(currentEnv_summer_masked)
    suit_hab_45_50_Env <- suit_hab_45_50_Env*futureEnv_45_50_cropped
    suit_hab_45_50_Env <- mask(futureEnv_45_50_cropped, suit_hab_45_50_Env, maskvalue=0)
    suit_hab_45_50_Env <- crop(suit_hab_45_50_Env, currentEnv_summer_masked)
    suit_hab_45_50_Env <- mask(suit_hab_45_50_Env, currentEnv_summer_masked)
    plot(suit_hab_45_50_Env)
    writeRaster(suit_hab_45_50_Env, filename = "suit_hab_45_50_Env", format = "GTiff", overwrite = TRUE)

有没有办法让 R 告诉我光栅 1 中有多少百分比的区域被屏蔽了?

或者换句话说:灰色多边形 = 100%,叠加栅格图层覆盖多边形的 x %。

由于在地理坐标系下工作,尤其是在高纬度地区工作,不能简单地比较非NA像素的数量,因为高纬度的像素比低纬度的像素小纬度。您必须使用纬度来计算每个像素的面积,然后将这些求和以获得每个栅格的面积。这是一个使用加拿大的裁剪栅格与蒙版栅格以及 raster::area 函数的示例,该函数在计算像素面积时考虑了每个像素的纬度:

require(raster)
require(maptools)

##get shapefile of canada
data("wrld_simpl")
can_shp=wrld_simpl[which(wrld_simpl$NAME=="Canada"),]

##create empty raster of the world
rs=raster()

##crop canada
rs_can=crop(rs,can_shp)

##calaculate area of each pixel
crop_area=area(rs_can)
plot(crop_area) ##note cells are smaller at higher latitudes
lines(can_shp)

##calculate crop area
crop_area_total=sum(crop_area[])

##mask canada
mask_area=mask(crop_area,can_shp)
plot(mask_area)
lines(can_shp)

##calculate mask area
mask_area_total=sum(mask_area[],na.rm=T)
mask_area_total
# [1] 9793736 
# which is not far from the wikipedia reported 9,984,670 km2 
# the under-estimate is due to the coarse resolution of the raster

##calculate percentage
mask_area_total/crop_area_total*100
# [1] 48.67837

N.B。你把纬度和经度标记错了:)

建议的答案工作正常,但是,这是我现在正在使用的代码。完美运行。

    suit_hab_45_50_temp_poly <- rasterToPolygons(suit_hab_45_50_temp, na.rm = TRUE)
    shapefile(suit_hab_45_50_temp_poly, filename="suit_hab_45_50_temp_poly.shp", 
    overwrite=TRUE)

    mosaic_summer_poly_arcmap <- spTransform(mosaic_summer_poly_arcmap, crs(suit_hab_45_50_temp_poly))

    mosaic_summer_poly_arcmap_area <- area(mosaic_summer_poly_arcmap)
    mosaic_summer_poly_arcmap_area_total <- sum(mosaic_summer_poly_arcmap_area[])
    mosaic_summer_poly_arcmap_area_total

    suit_hab_45_50_temp_area <- area(suit_hab_45_50_temp_poly)
    suit_hab_45_50_temp_area_total <- sum(suit_hab_45_50_temp_area[])
    suit_hab_45_50_temp_area_total

    hab_loss_45_50_temp_s <- 100- (suit_hab_45_50_temp_area_total/mosaic_summer_poly_arcmap_area_total*100)
    hab_loss_45_50_temp_s