Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] add "mode" to ZonalStatistics output. Contributed by Anton
Biatov
  • Loading branch information
alexbruy committed Apr 21, 2015
1 parent 8c1ee0c commit 8ccae4f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion python/plugins/processing/algs/qgis/ZonalStatistics.py
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8ccae4f

Please sign in to comment.