应用遮罩功能时如何计算被遮掩区域的百分比?

问题描述 投票:0回答:2

我有两个栅格对象,以相同的程度裁切并屏蔽栅格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%。

r crop raster mask
2个回答
1
投票

当您在地理坐标系中工作时,尤其是在高纬度区域中工作时,您不能简单地比较非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)

enter image description here

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

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

enter image description here

##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。您误贴了纬度和经度:)


0
投票

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

    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
© www.soinside.com 2019 - 2024. All rights reserved.