Plugin for QGIS to calculate zonal statistics using exactextract library
This is a repository for the QGIS plugin that allows to aggregate/summarize values of the raster over polygonal (vector) areas using exactextract
library. This library allows to treat pixel cells as partially covered by polygons using weighting according to the extent of cover. If you want to know more about the library working in the backend of this plugin it's highly recommend to visit its github repository.
"Zonal statistics" tool in QGIS currently have an issues (1, 2) that yield wrong results in certain situations.
- Multiple supported statistics: Every statistic given by
exactextract
is supported by plugin (statistics), including array result type (usage of these statistics might slow down calculation and saving stage). - Define own, custom functions : Write custom Python code to define how features should be calculated. Custom functions should accept raster
values
andcoverage
attributes.
Example: Calculate 90th percentile of raster values:
import numpy as np def 90th_perc(values, cov): return np.percentile(values, 90)If given statistic is checked in Custom Function combo box there will be new column
90th_perc
added.
There is also option to modify custom functions defined by user before. In order to load the code of existing function and modify it the function name should be checked in Custom Function combo box.
Warning: If there's an error during processing of custom function code whole processing will be stopped. Wrong function may also block QGIS or make it crash.
- Usage of QGIS parallel engine: There is an option to process statistics calculation in multiple parts (subtasks/batch option). Calculation of statistics in this case is done in parallel manner using
QgsTaskManager
engine. To configure number of parallel cores it will use you should configureMax Threads
option in QGIS settings. - Support for multiband rasters: In case there's a multiband raster - each band is processed during calculations and is output as separate set of columns.
- Support for weighting raster;
- Move all heavy operations to QGIS
QgsTask
; - Ability to output geospatial layer instead of CSV/Parquet only - It might be difficult due to lack of performant way to join
GeoDataFrame
toQgsVectorLayer
;
- Polygon layer (anything that is read by QGIS)
- Raster layer (it has to be locally accessible layer. Database raster layers do not work for now)
- Attribute layer - CSV format is supported by default and Parquet format is supported if
fastparquet
library is installed.
It's beta version of the plugin. It is not recommended for production usage yet!
It will be out of beta when:
- unit and integration tests are added;
- features are considered completed;
- documentation is provided with the plugin;
In current version of the plugin there are 4 packages required:
- [REQUIRED] Installed through OSGeo4W setup utility (or
pip install pandas
):- pandas
- [REQUIRED] You may use wheel delivered in this repository (tested with QGIS versions 3.34 and 3.36) or you can compile and install it manually from GitHub repository (
exactextract
library author is working on uploading it to PyPi) and thenpip install
wheel (.whl) file:- exactextract (zonal statistics computation engine)
- [OPTIONAL] It's advised to install
fastparquet
(pip install fastparquet
) to allow saving results as a parquet file format;