|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + ZonalStatisticsQgis.py |
| 6 | + --------------------- |
| 7 | + Date : September 2016 |
| 8 | + Copyright : (C) 2016 by Alexander Bruy |
| 9 | + Email : alexander dot bruy at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Alexander Bruy' |
| 21 | +__date__ = 'September 2016' |
| 22 | +__copyright__ = '(C) 2016, Alexander Bruy' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +from qgis.analysis import QgsZonalStatistics |
| 29 | + |
| 30 | +from processing.core.GeoAlgorithm import GeoAlgorithm |
| 31 | +from processing.core.parameters import ParameterVector |
| 32 | +from processing.core.parameters import ParameterRaster |
| 33 | +from processing.core.parameters import ParameterString |
| 34 | +from processing.core.parameters import ParameterNumber |
| 35 | +from processing.core.parameters import ParameterSelection |
| 36 | +from processing.core.outputs import OutputVector |
| 37 | +from processing.tools import dataobjects |
| 38 | + |
| 39 | + |
| 40 | +class ZonalStatisticsQgis(GeoAlgorithm): |
| 41 | + |
| 42 | + INPUT_RASTER = 'INPUT_RASTER' |
| 43 | + RASTER_BAND = 'RASTER_BAND' |
| 44 | + INPUT_VECTOR = 'INPUT_VECTOR' |
| 45 | + COLUMN_PREFIX = 'COLUMN_PREFIX' |
| 46 | + STATISTICS = 'STATS' |
| 47 | + OUTPUT_LAYER = 'OUTPUT_LAYER' |
| 48 | + |
| 49 | + def defineCharacteristics(self): |
| 50 | + self.STATS = {self.tr('Count'): QgsZonalStatistics.Count, |
| 51 | + self.tr('Sum'): QgsZonalStatistics.Count, |
| 52 | + self.tr('Mean'): QgsZonalStatistics.Count, |
| 53 | + self.tr('Median'): QgsZonalStatistics.Count, |
| 54 | + self.tr('Std. dev.'): QgsZonalStatistics.Count, |
| 55 | + self.tr('Min'): QgsZonalStatistics.Count, |
| 56 | + self.tr('Max'): QgsZonalStatistics.Count, |
| 57 | + self.tr('Range'): QgsZonalStatistics.Count, |
| 58 | + self.tr('Minority'): QgsZonalStatistics.Count, |
| 59 | + self.tr('Majority'): QgsZonalStatistics.Count, |
| 60 | + self.tr('Variety'): QgsZonalStatistics.Count, |
| 61 | + self.tr('All'): QgsZonalStatistics.All |
| 62 | + } |
| 63 | + |
| 64 | + self.name, self.i18n_name = self.trAlgorithm('Zonal Statistics (QGIS)') |
| 65 | + self.group, self.i18n_group = self.trAlgorithm('Raster tools') |
| 66 | + |
| 67 | + self.addParameter(ParameterRaster(self.INPUT_RASTER, |
| 68 | + self.tr('Raster layer'))) |
| 69 | + self.addParameter(ParameterNumber(self.RASTER_BAND, |
| 70 | + self.tr('Raster band'), 1, 999, 1)) |
| 71 | + self.addParameter(ParameterVector(self.INPUT_VECTOR, |
| 72 | + self.tr('Vector layer containing zones'), |
| 73 | + [dataobjects.TYPE_VECTOR_POLYGON])) |
| 74 | + self.addParameter(ParameterString(self.COLUMN_PREFIX, |
| 75 | + self.tr('Output column prefix'), '_')) |
| 76 | + self.addParameter(ParameterSelection(self.STATISTICS, |
| 77 | + self.tr('Statistics to calculate'), |
| 78 | + list(self.STATS.keys()), |
| 79 | + multiple=True)) |
| 80 | + self.addOutput(OutputVector(self.OUTPUT_LAYER, |
| 81 | + self.tr('Zonal statistics'), |
| 82 | + True, |
| 83 | + datatype=[dataobjects.TYPE_VECTOR_POLYGON])) |
| 84 | + |
| 85 | + def processAlgorithm(self, progress): |
| 86 | + rasterPath = self.getParameterValue(self.INPUT_RASTER) |
| 87 | + vectorPath = self.getParameterValue(self.INPUT_VECTOR) |
| 88 | + bandNumber = self.getParameterValue(self.RASTER_BAND) |
| 89 | + columnPrefix = self.getParameterValue(self.COLUMN_PREFIX) |
| 90 | + st = self.getParameterValue(self.STATISTICS) |
| 91 | + |
| 92 | + vectorLayer = dataobjects.getObjectFromUri(vectorPath) |
| 93 | + |
| 94 | + keys = list(self.STATS.keys()) |
| 95 | + selectedStats = 0 |
| 96 | + for i in st: |
| 97 | + selectedStats |= self.STATS[keys[i]] |
| 98 | + |
| 99 | + zs = QgsZonalStatistics(vectorLayer, rasterPath, columnPrefix, bandNumber, selectedStats) |
| 100 | + zs.calculateStatistics(None) |
| 101 | + |
| 102 | + self.setOutputValue(self.OUTPUT_LAYER, vectorPath) |
0 commit comments