Skip to content
Permalink
Browse files

[processing] add "mode" to ZonalStatistics output. Contributed by Anton

Biatov
  • Loading branch information
alexbruy committed Apr 21, 2015
1 parent 8c1ee0c commit 8ccae4fef11de5c3de8116ff9e39f2d05be6e956
Showing with 14 additions and 1 deletion.
  1. +14 −1 python/plugins/processing/algs/qgis/ZonalStatistics.py
@@ -26,8 +26,16 @@
__revision__ = '$Format:%H$'

import numpy
from qgis.core import QgsRectangle, QgsGeometry, QgsFeature

try:
from scipy.stats.mstats import mode
hasSciPy = True
except:
hasSciPy = False

from osgeo import gdal, ogr, osr
from qgis.core import QgsRectangle, QgsGeometry, QgsFeature

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterRaster
@@ -144,6 +152,9 @@ def processAlgorithm(self, progress):
columnPrefix + 'var', 21, 6)
(idxMedian, fields) = vector.findOrCreateField(layer, fields,
columnPrefix + 'median', 21, 6)
if hasSciPy:
(idxMode, fields) = vector.findOrCreateField(layer, fields,
columnPrefix + 'mode', 21, 6)

writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(
fields.toList(), layer.dataProvider().geometryType(), layer.crs())
@@ -225,6 +236,8 @@ def processAlgorithm(self, progress):
attrs.insert(idxRange, float(masked.max()) - float(masked.min()))
attrs.insert(idxVar, float(masked.var()))
attrs.insert(idxMedian, float(numpy.ma.median(masked)))
if hasSciPy:
attrs.insert(idxMode, float(mode(masked, axis=None)[0][0]))

outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

0 comments on commit 8ccae4f

Please sign in to comment.
You can’t perform that action at this time.