QGIS/python/plugins/processing/algs/qgis/ZonalStatisticsQgis.py
Nyall Dawson f84d703234 Add method QgsProcessingUtils::mapLayerFromString
Algorithms and other processing code should use this method
(instead of dataobjects.getLayerFromString) to
retrieve layers from a string, as it considers the processing
context and allows resolving strings to temporarily stored layers.

This permits processing models to function correctly when
intermediate results are stored as memory layers. Subsequent
model algorithms can then access these temporary layers as inputs.
All temporary layers will be removed when the context object
is destroyed after the model algorithm is run.
2017-05-02 12:33:04 +10:00

124 lines
5.2 KiB
Python

# -*- coding: utf-8 -*-
"""
***************************************************************************
ZonalStatisticsQgis.py
---------------------
Date : September 2016
Copyright : (C) 2016 by Alexander Bruy
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'
__date__ = 'September 2016'
__copyright__ = '(C) 2016, Alexander Bruy'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
from qgis.PyQt.QtGui import QIcon
from qgis.analysis import QgsZonalStatistics
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
from processing.tools import dataobjects
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class ZonalStatisticsQgis(GeoAlgorithm):
INPUT_RASTER = 'INPUT_RASTER'
RASTER_BAND = 'RASTER_BAND'
INPUT_VECTOR = 'INPUT_VECTOR'
COLUMN_PREFIX = 'COLUMN_PREFIX'
STATISTICS = 'STATS'
OUTPUT_LAYER = 'OUTPUT_LAYER'
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'zonalstats.png'))
def group(self):
return self.tr('Raster tools')
def name(self):
return 'zonalstatisticsqgis'
def displayName(self):
return self.tr('Zonal Statistics (QGIS)')
def defineCharacteristics(self):
self.STATS = {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'): QgsZonalStatistics.Majority,
self.tr('Variety'): QgsZonalStatistics.Variety,
self.tr('All'): QgsZonalStatistics.All
}
self.addParameter(ParameterRaster(self.INPUT_RASTER,
self.tr('Raster layer')))
self.addParameter(ParameterNumber(self.RASTER_BAND,
self.tr('Raster band'),
1, 999, 1))
self.addParameter(ParameterVector(self.INPUT_VECTOR,
self.tr('Vector layer containing zones'),
[dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(ParameterString(self.COLUMN_PREFIX,
self.tr('Output column prefix'), '_'))
self.addParameter(ParameterSelection(self.STATISTICS,
self.tr('Statistics to calculate'),
list(self.STATS.keys()),
multiple=True))
self.addOutput(OutputVector(self.OUTPUT_LAYER,
self.tr('Zonal statistics'),
True,
datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
def processAlgorithm(self, context, feedback):
rasterPath = self.getParameterValue(self.INPUT_RASTER)
vectorPath = self.getParameterValue(self.INPUT_VECTOR)
bandNumber = self.getParameterValue(self.RASTER_BAND)
columnPrefix = self.getParameterValue(self.COLUMN_PREFIX)
st = self.getParameterValue(self.STATISTICS)
vectorLayer = dataobjects.QgsProcessingUtils.mapLayerFromString(vectorPath, context)
rasterLayer = dataobjects.QgsProcessingUtils.mapLayerFromString(rasterPath, context)
keys = list(self.STATS.keys())
selectedStats = 0
for i in st:
selectedStats |= self.STATS[keys[i]]
zs = QgsZonalStatistics(vectorLayer,
rasterLayer,
columnPrefix,
bandNumber,
selectedStats)
zs.calculateStatistics(None)
self.setOutputValue(self.OUTPUT_LAYER, vectorPath)