【问题标题】:R How to extract the mean values of a raster for each polygon for an altitude greater than 600m?R如何为高度大于600m的每个多边形提取栅格的平均值?
【发布时间】:2017-10-01 09:34:00
【问题描述】:

首先,我设法使用以下程序提取了每个多边形的平均光栅温度值:

您可以在此链接上下载 GIS 图层:

https://depots.univ-perp.fr/get?k=iTzEDSUkdyZVw2st78G

## load packages 

library(raster); library(rgdal) 

## Read rasters

ras_temp<-raster("ras_temp.tif")
plot(ras_temp)
ras_alti<-raster("ras_alti.tif")

## read polygon

polygon <- readOGR(dsn = getwd(), layer = "polygon") 
plot(polygon,add=TRUE)

## extract mean value for each polygon

v1 <- extract( ras_temp, polygon, fun=mean, na.rm=TRUE)
nom <- sapply(polygon@polygons, slot, "ID")
v1 <- data.frame(ID = nom, Value = v1)
View(v1)

然后,我想提取每个多边形的温度平均值,但只提取海​​拔超过 600 m 的表面?

不幸的是,我做不到,我的问题是如何将海拔条件整合到我的“提取”函数中?

提前致谢

【问题讨论】:

    标签: r extract raster r-raster rgdal


    【解决方案1】:

    你可以像这样轻松地做到这一点:

    # first resample the altitude raster to the temperature one so that they are
    # "aligned"
    ras_alti.new = resample(ras_alti, ras_temp, "bilinear")
    
    # set to NA all data in ras_temp corresponding to cells in ras_alti.new below 600 
    # metre
    ras_temp.new = ras_temp
    ras_temp.new[ras_alti.new <= 600] = NA
    
    # extract the data
    v2 <- extract(ras_temp.new, polygon, fun=mean, na.rm=TRUE, sp = T)
    v2@data
    
    ID ras_temp
    0 417 64.11342
    1 433 68.53541
    

    【讨论】:

    • 为小于 600m 的表面设置 NA 值是一个非常好的主意。非常感谢 LoBu 这个技巧!
    猜你喜欢
    • 2020-07-16
    • 2020-03-10
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多