2013-08-06 19:55:18 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ZonalStatistics.py
|
|
|
|
---------------------
|
2017-06-29 09:49:46 +07:00
|
|
|
Date : September 2016
|
|
|
|
Copyright : (C) 2016 by Alexander Bruy
|
2013-08-06 19:55:18 +03:00
|
|
|
Email : alexander dot bruy at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Alexander Bruy'
|
2017-06-29 09:49:46 +07:00
|
|
|
__date__ = 'September 2016'
|
|
|
|
__copyright__ = '(C) 2016, Alexander Bruy'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-08-06 19:55:18 +03:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-08-06 19:55:18 +03:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2017-06-29 09:49:46 +07:00
|
|
|
import os
|
2017-06-30 19:29:05 +10:00
|
|
|
from collections import OrderedDict
|
2015-04-21 16:13:14 +03:00
|
|
|
|
2017-06-29 09:49:46 +07:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2015-04-21 16:13:14 +03:00
|
|
|
|
2017-06-29 09:49:46 +07:00
|
|
|
from qgis.analysis import QgsZonalStatistics
|
|
|
|
from qgis.core import (QgsFeatureSink,
|
|
|
|
QgsProcessingUtils,
|
|
|
|
QgsProcessingParameterDefinition,
|
|
|
|
QgsProcessingParameterVectorLayer,
|
|
|
|
QgsProcessingParameterRasterLayer,
|
|
|
|
QgsProcessingParameterString,
|
|
|
|
QgsProcessingParameterNumber,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingOutputVectorLayer)
|
2015-04-21 16:13:14 +03:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2017-06-29 09:49:46 +07:00
|
|
|
|
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-08-06 19:55:18 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class ZonalStatistics(QgisAlgorithm):
|
2013-08-06 19:55:18 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT_RASTER = 'INPUT_RASTER'
|
|
|
|
RASTER_BAND = 'RASTER_BAND'
|
|
|
|
INPUT_VECTOR = 'INPUT_VECTOR'
|
|
|
|
COLUMN_PREFIX = 'COLUMN_PREFIX'
|
2017-06-29 09:49:46 +07:00
|
|
|
STATISTICS = 'STATS'
|
|
|
|
|
|
|
|
def icon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'zonalstats.png'))
|
2013-08-06 19:55:18 +03:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-06-30 19:29:05 +10:00
|
|
|
self.STATS = OrderedDict([(self.tr('Count'), QgsZonalStatistics.Count),
|
|
|
|
(self.tr('Sum'), QgsZonalStatistics.Sum),
|
|
|
|
(self.tr('Mean'), QgsZonalStatistics.Mean),
|
|
|
|
(self.tr('Median'), QgsZonalStatistics.Median),
|
|
|
|
(self.tr('Std. dev.'), QgsZonalStatistics.StDev),
|
|
|
|
(self.tr('Min'), QgsZonalStatistics.Min),
|
|
|
|
(self.tr('Max'), QgsZonalStatistics.Max),
|
|
|
|
(self.tr('Range'), QgsZonalStatistics.Range),
|
|
|
|
(self.tr('Minority'), QgsZonalStatistics.Minority),
|
|
|
|
(self.tr('Majority (mode)'), QgsZonalStatistics.Majority),
|
|
|
|
(self.tr('Variety'), QgsZonalStatistics.Variety),
|
|
|
|
(self.tr('Variance'), QgsZonalStatistics.Variance),
|
|
|
|
(self.tr('All'), QgsZonalStatistics.All)])
|
2017-06-29 09:49:46 +07:00
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT_RASTER,
|
|
|
|
self.tr('Raster layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterNumber(self.RASTER_BAND,
|
|
|
|
self.tr('Raster band'),
|
|
|
|
minValue=1, maxValue=999, defaultValue=1))
|
|
|
|
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT_VECTOR,
|
|
|
|
self.tr('Vector layer containing zones'),
|
|
|
|
[QgsProcessingParameterDefinition.TypeVectorPolygon]))
|
|
|
|
self.addParameter(QgsProcessingParameterString(self.COLUMN_PREFIX,
|
|
|
|
self.tr('Output column prefix'), '_'))
|
2017-06-30 19:29:05 +10:00
|
|
|
keys = list(self.STATS.keys())
|
2017-06-29 09:49:46 +07:00
|
|
|
self.addParameter(QgsProcessingParameterEnum(self.STATISTICS,
|
|
|
|
self.tr('Statistics to calculate'),
|
2017-06-30 19:29:05 +10:00
|
|
|
keys,
|
2017-06-30 19:52:22 +10:00
|
|
|
allowMultiple=True, defaultValue=[0, 1, 2]))
|
2017-06-29 09:49:46 +07:00
|
|
|
self.addOutput(QgsProcessingOutputVectorLayer(self.INPUT_VECTOR,
|
|
|
|
self.tr('Zonal statistics'),
|
|
|
|
QgsProcessingParameterDefinition.TypeVectorPolygon))
|
2013-08-06 19:55:18 +03:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.bandNumber = None
|
|
|
|
self.columnPrefix = None
|
|
|
|
self.selectedStats = None
|
|
|
|
self.vectorLayer = None
|
|
|
|
self.rasterLayer = None
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'zonalstatistics'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Zonal Statistics')
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def prepareAlgorithm(self, parameters, context, feedback):
|
|
|
|
self.bandNumber = self.parameterAsInt(parameters, self.RASTER_BAND, context)
|
|
|
|
self.columnPrefix = self.parameterAsString(parameters, self.COLUMN_PREFIX, context)
|
2017-06-29 09:49:46 +07:00
|
|
|
st = self.parameterAsEnums(parameters, self.STATISTICS, context)
|
|
|
|
|
|
|
|
keys = list(self.STATS.keys())
|
2017-06-28 19:55:08 +10:00
|
|
|
self.selectedStats = 0
|
2017-06-29 09:49:46 +07:00
|
|
|
for i in st:
|
2017-06-28 19:55:08 +10:00
|
|
|
self.selectedStats |= self.STATS[keys[i]]
|
|
|
|
|
|
|
|
self.vectorLayer = self.parameterAsVectorLayer(parameters, self.INPUT_VECTOR, context)
|
|
|
|
self.rasterLayer = self.parameterAsRasterLayer(parameters, self.INPUT_RASTER, context)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def processAlgorithm(self, context, feedback):
|
|
|
|
zs = QgsZonalStatistics(self.vectorLayer,
|
|
|
|
self.rasterLayer,
|
|
|
|
self.columnPrefix,
|
|
|
|
self.bandNumber,
|
2017-06-30 19:28:39 +10:00
|
|
|
QgsZonalStatistics.Statistics(self.selectedStats))
|
2017-06-29 09:49:46 +07:00
|
|
|
zs.calculateStatistics(feedback)
|
2017-06-28 19:55:08 +10:00
|
|
|
return True
|
2017-06-29 09:49:46 +07:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def postProcessAlgorithm(self, context, feedback):
|
|
|
|
return {self.INPUT_VECTOR: self.vectorLayer}
|