Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 应用遮罩功能时,如何计算遮罩面积的百分比?_R_Crop_Raster_Mask - Fatal编程技术网

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

R 应用遮罩功能时,如何计算遮罩面积的百分比?,r,crop,raster,mask,R,Crop,Raster,Mask,我有两个光栅对象,裁剪到相同的程度,并遮罩光栅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_5

我有两个光栅对象,裁剪到相同的程度,并遮罩光栅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)

suit_hab_45_50_Env当您在地理坐标系中工作时,尤其是在高纬度地区工作时,您不能简单地比较非NA像素的数量,因为高纬度的像素比低纬度的像素小。您必须使用纬度来计算每个像素的面积,然后将其相加以获得每个光栅的面积。下面是一个使用加拿大裁剪光栅与遮罩光栅的示例,以及计算像素面积时考虑每个像素纬度的
graster::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)


注意:您错误地标记了lat和long:)

建议的答案很好,但是,这就是我现在使用的代码。工作完美

    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

suit_hab_45_50_temp_poly非常感谢您的建议!我刚刚发布了我问题的答案,效果很好!哦,谢谢你对lat/long的提示。完全忽略了这一点。在我的博士论文里看起来不太好
##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
    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