[R-sig-Geo] Extracting Temperature by exactextractr

Pietro Andre Telatin Paschoalino P|etro_te|@to @end|ng |rom hotm@||@com
Mon Oct 24 15:52:53 CEST 2022


Hello everyone, I am getting the year 2016 monthly average temperature for my shapefile polygons.

My data is cru_ts4.05.2011.2020.tmp.dat.nc

Follow my code. And at the end of the code my questions, if you can help.

Rasterdata <- cruts2raster("cru_ts4.05.2011.2020.tmp.dat.nc",
                      timeRange=c("2016-01-01","2017-01-01"))

rasterdata

# # checking raster crs
cat(wkt(crs(rasterdata)), "\n")

# getting shapefile

datasets <- list_geobr()

print(datasets)

micro <- read_micro_region(year=2013)

options(max.print = 9999)

print(micro[3:4],n=558)

sf::sf_use_s2(TRUE)
sf::st_is_longlat(micro)

#  checking CRS Shapefile.

st_crs(micro)

#switching to the same CRS as the raster to do extraction.

shape<-st_transform(micro, crs(rasterdata))

st_crs(shape)
#same.

#doing extraction through exact_extract.

weath_dt = as.data.frame(matrix(NA,nrow(micro),14))
weath_dt[,1] = shape$code_micro
weath_dt[,2] = shape$name_micro

for (i in 1:length(rasterdata using layers)) {
  weath_dt[,2+i] = exactextractr::exact_extract(rasterdata[[i]], shape, 'mean')
}

#generating average

#checking for Na

apply(weath_dt, 2, function(x) {sum(is.na(x))})

#all very well.

library(dplyr)

weath_dt[,15] <- rowMeans(weath_dt[,3:14])

shape$avetemp <- weath_dt[,15]

print(shape[6:7],n=558)

st_write(shape, dsn = ".", layer = "SHAPE.shp", driver = "ESRI Shapefile")

My questions:

#Code is OK?
#Any problem with the resolution or size of the polygons to the result of the extraction?
# From what I've seen the algorithm already considers the weighted average by the fraction of the cell that covers the polygon. I think it would be better than the simple average, right?
#result generated are in shapefile ID order?
#how to put raster CRS in 4326 before extraction? So, the shapefile's CRS would also end up in 4326. I believe they are in WGS 84 would this generate the same result for my extractions if they were in EPSG 4326?

Thanks.

Pietro Andre Telatin Paschoalino
Doutor em Ci�ncias Econ�micas pela Universidade Estadual de Maring� - PCE.
Professor tempor�rio do departamento de Economia da UEM.

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list