【问题标题】:How to calculate % of area masked when applying mask function?应用遮罩功能时如何计算遮罩面积的百分比?
【发布时间】:2020-08-21 04:31:33
【问题描述】:

我有两个栅格对象,将两者裁剪到相同的程度,并掩盖栅格 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


    【解决方案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)
    

    ##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
    

    注意你错误地标记了纬度和经度:)

    【讨论】:

    • 非常感谢您的建议!我刚刚发布了我的问题的答案,效果很好!哦,感谢您对纬度/经度的提示。完全忽略了这一点。在我的硕士论文中看起来不会很好。 ;)
    【解决方案2】:

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

        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多