[R-sig-Geo] Doubt about extract climate variables from raster.
Pietro Andre Telatin Paschoalino
P|etro_te|@to @end|ng |rom hotm@||@com
Thu Jan 7 23:51:06 CET 2021
Hello everyone, I'm extracting climate variables (precipitation and temperature) from a grid (CRU data) to my shapefile.
To do this I'm using the function extract from the raster package.
In the extraction, I saw if the CRS are the same in the raster and in the shape (I change the crs of shape for the same as raster).
Initially, I tried to extract considering the weighted average of pixels with centroid inside the polygon, as I got more missings values, I ended up opting for the simple average. Still, I got less than 1% of the values as missings (5 of 557 regions) that I completed using the value of the nearest neighbor.
I decided this because as I am not from this area I am trying to keep it as simple as possible so as not to make mistakes. I believe that to get the weighted average of some form I would have to change the resolution of the raster, or something like that.
I asked the opinion of a geographer and he said that the way I did it is correct.
In this way I am putting my code for extraction if someone can verify that it is correct, I would also like an opinion if the way that I extract is correct. This is very important for me and I'll appreciate if anyone can take a look.
If anyone can help, I can make the data available directly.
In general my code is:
setwd("mypath")
shape <- readOGR(dsn = ".", layer = "microfinal")
fn<-file.path("mypath\\cru_ts4.04.2011.2019.tmp.dat.nc")
ncfile <- nc_open(fn)
rasbrick <- stack(fn)
To get only the layers that I want:
rasbrick <- rasbrick[[70:81]]
changing to the same crs
shape2<- spTransform(shape, crs(rasbrick))
weath_dt = as.data.frame(matrix(NA,nrow(shape2 using data),12))
First I got the ID regions of shape:
weath_dt[,1] = shape2 using data[,1]
and extract:
for (i in 1:length(rasbrick using layers)) {
weath_dt[,1+i] = raster::extract(rasbrick[[i]], shape2, mean)
}
After that I only pick the values for the missings by near neighbour.
Thank you.
Pietro Andre Telatin Paschoalino
Doutorando em Ciências Econômicas da Universidade Estadual de Maringá - PCE.
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list