2

I have the NetCDF file that contains monthly gridded temperature from 1900 to 2014 (air.mon.mean.v501.nc). The goal is to extract monthly time series for every polygon in my shapefile. Using the US county map as an example, the resulting shapefile should contain (2014-1900)x12 = 1368 columns, with every row containing the monthly average temperature for a given county for the period between 1900 and 2014.

My departing point was the use and abuse of a combination of Zonal Statistics and the Batch Processing function in QGIS. However, it seems that I have to manually select 1368 bands when specifying the raster layer for every row.

Is there a more time-efficient (and less prone to human error) way to do deal with the issue in question?

4
  • This should be possible to do using some scripting, but also, my immideate gut feeling when someone mentiones a table with 1368 columns is that this is not a good data model.... Are you going to do any kind of time series of the extracted data, if so, have you thought about how to get the data out? Aug 13, 2021 at 8:39
  • @MortenSickel Thanks for your quick response. Well, if it is hard to pull 1368 columns out of QGIS, I will be happy to export multiple csv tables with, let's say, 50 columns per each. The goal is to construct a panel dataset, where panelvar is a polygon, and timevar -- month-year.
    – Andrey
    Aug 13, 2021 at 12:14
  • THe number of columns should not be any problem for QGIS, I was thinking more generally, it may just work fine for your application Aug 13, 2021 at 13:48
  • I recommend you to use R packages to perform this process. If you are interested I can post an answer using R.
    – sermomon
    Jul 4, 2022 at 10:29

1 Answer 1

0

Here an example using R. I used raster, rgdal, and exactextractr packages. However you can adapt the code and use much modern packages such as terra and sf.

# Input patches
# @IMG_PATH: absolute path of your RasterStack containing 1900 a 2014 monthly composites
# @SHP_PATH: ESRI Shapefile of EEUU
IMG_PATH <- "D:/Image.nc"
SHP_PATH <- "D:/Roi.shp"

# Read data
imgCube <- raster::stack(IMG_PATH)
shpFeatures <- rgdal::readOGR(SHP_PATH)

# Extract mean values by ROI
df <- exact_extract(imgCube, shpFeatures, 'mean')

# Export extracted data (optional) to csv
out_path <- "D:/Extracted.csv"
write.csv(df, out_path)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.