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.
The geoprocessesing tool "Zonal statistics" currently implemented in QGIS have an issues (1, 2) that yield wrong results in certain situations. Exactextract can remedy it.
- 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) and weighted statistics (statistics withweighted_
name). - Define own, custom functions : Write custom Python code to define extra, additional features for raster zonal statistics. Custom functions should accept raster
values
andcoverage
attributes.
Example: Calculate 90th percentile of raster values:
import numpy as np def 90th_perc(values, cov): # values is a masked array; calling "compressed" will remove nodata values return np.percentile(values.compressed(), 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 earlier. In order to load the code of existing function and modify it the function name should be checked in Custom Function combo box. Custom functions defined in this plugin are removed when plugin is reloaded or qgis is restarted. User should save custom functions for later usage outside of the plugin.
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 parameter). 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 multiple values rasters: Selection of multiple rasters will process each raster separately and append to the result.
- Support multiband rasters: In case there's a multiband raster - each band is processed during calculations and is output as separate set of columns.
- Support weights raster
- Move all heavy operations to QGIS
QgsTask
;
- 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)
- Geospatial layer - Geospatial formats are supported with every OGR supported driver.
Warning: Support for array output may vary. Use with caution.
- Attribute layer - CSV format
In current version of the plugin there is 1 required and 1 optional package:
- [REQUIRED] From PyPi repository:
pip install exactextract
- [OPTIONAL] Installed through OSGeo4W setup utility (or
pip install pandas
):- pandas
Note: pandas is required only if user wants to export result to CSV (non-geospatial output) If
exactextract
package is not installed, plugin will try to install it by itself.
- pandas
This plugin comes with automatic installation module which installs exactextract
to QGIS user profile
directory.
User might also install library by himself using OSGeo4W Shell
command: pip install exactextract
Similar to Manual
installation step input command: pip install exactextract --upgrade
- Close QGIS.
- Go to
C:\Users\<Username>\AppData\Roaming\QGIS\QGIS3\profiles\<profile name>\python\plugins\python3.9
and remove directories withexactextract
in name. - Open QGIS and let plugin install the package using
Automatic
method.