[R-sig-Geo] Doubt about extract climate variables from raster.

Bede-Fazekas Ákos b|@|ev||@t @end|ng |rom gm@||@com
Fri Jan 8 07:36:20 CET 2021


Dear Pietro,

I think that, technically, your code is correct (although using a for 
loop is unnecessary, function extract() can deal with 
RasterStacks/RasterBricks and returns a matrix in this case). And, 
technically, also the following solutions would be correct:
- mean with na.rm,
- weighted mean without na.rm,
- weighted mean with na.rm.
Also you should consider using package 'exactextract', which can quickly 
and accurately extract values over polygons in a way that it handles 
grid cells that are partially covered by a polygon.
The "best" solution depends on your data and your aim.

HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences

2021.01.07. 23:51 keltezéssel, Pietro Andre Telatin Paschoalino írta:
> 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]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list