I have been encountering difficulties trying to extract the raster pixel values from a polygon shapefile that has been assigned the same CRS.
The problem under inspection is how to get a list or a table with coordinates and pixel values from a polygon shapefile over a singleband raster for further investigation of values.
What is the easiest and most efficient way to perform this otherwise seemingly very simple task?
Best Answer
I would prefer using R to do this, is really easy with
extract()
function. In QGIS is a long process, but result:Suppose you have both layers:
Clip (or crop) raster layer to have a small input to work:
Create an empty raster with same extent/resolution:
Convert vector to raster using a unique ID by geometry:
Merge both raster layers:
Convert each layer to a csv file or use
gdal2xyz -band 1 -band 2 -csv /path/to/file.csv
in cmd/terminal windows:You'll obtain two .csv files (or one using command line):
Filter table by polygon ID:
R approach:
The result is a list with n slots, each slot represents a polygon feature used to extract.
Saving output in a csv file:
The output file:![enter image description here](https://i.stack.imgur.com/qCVLU.png)