What I'm trying to do:
- Extract median by a single polygon from multiple GeoTIFF rasters stored in a folder (Solved)
- Add each median value to a point shapefile (Match/join by: id column for each point in shape = raster filename)
Basically the project has three sources:
One folder containing 558 GeoTIFF raster's which are viewshed results from different observer points. All raster's have the same extent and are named 1.tiff, 2.tiff, 3.tiff .... 558.tiff
One vector point layer with the above observer points. The ID field is numbered from 1 to 558 (which corresponds to the raster filenames)
One vector polygon layer which represents a planned new development area (much smaller extent then the raster's). This is the polygon that raster median is calculated from.
What I got so far:
Python console from within Qgis and
Qgs.ZonalStatistics I have, with help from @Joseph, been able to extract median values by polygon (3) from all TIFF's in the folder (1) with this code:
import glob, os from qgis.analysis import QgsZonalStatistics vectorlayer = iface.activeLayer() rasterfolder = 'D:/effect_test/' os.chdir(rasterfolder) for lyr in glob.glob("*.tiff"): QgsZonalStatistics(vectorlayer, lyr, attributePrefix=lyr, rasterBand=1, stats=QgsZonalStatistics.Statistics(QgsZonalStatistics.Median)).calculateStatistics(None)
Right now the statistics are added to a new column in the new planned development polygon layer I use to query the raster's with (3). The columns are named by filename with "median" added without speces behind the .tiff extention, see example below:
But this is not optimal since I now have to cut the data out of the attribute table, rearrange it in excel and then join it with the observer points vector layer, and on top of that I have to do it in two batches since 255 is the column limit in a shapefile.
How can I re-write the above code to take the median output I get from Qgs.ZonalStatistics and append it (join by id/filename) with the vector point layer containing all the observer points (2)?