2017-05-03 65 views
0

首先,我設法提取平均光柵溫度值爲每個多邊形,而下面的程序:R如何提取海拔高於600m的每個多邊形柵格的平均值?

您可以下載這個鏈接上的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([email protected], slot, "ID") 
v1 <- data.frame(ID = nom, Value = v1) 
View(v1) 

然後,我想提取每個多邊形的溫度平均值,但僅限於超過600米高度的表面。

不幸的是,我不能這樣做,我的問題如何在我的函數「提取」中整合高度條件?

在此先感謝

回答

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) 
[email protected] 

ID ras_temp 
0 417 64.11342 
1 433 68.53541 
+0

這是一個非常好的主意來設置NA爲面小於600米值。非常感謝LoBu爲這個技巧! – tazrart