Skip to content

Commit 8ccae4f

Browse files
committed
[processing] add "mode" to ZonalStatistics output. Contributed by Anton
Biatov
1 parent 8c1ee0c commit 8ccae4f

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

python/plugins/processing/algs/qgis/ZonalStatistics.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@
2626
__revision__ = '$Format:%H$'
2727

2828
import numpy
29-
from qgis.core import QgsRectangle, QgsGeometry, QgsFeature
29+
30+
try:
31+
from scipy.stats.mstats import mode
32+
hasSciPy = True
33+
except:
34+
hasSciPy = False
35+
3036
from osgeo import gdal, ogr, osr
37+
from qgis.core import QgsRectangle, QgsGeometry, QgsFeature
38+
3139
from processing.core.GeoAlgorithm import GeoAlgorithm
3240
from processing.core.parameters import ParameterVector
3341
from processing.core.parameters import ParameterRaster
@@ -144,6 +152,9 @@ def processAlgorithm(self, progress):
144152
columnPrefix + 'var', 21, 6)
145153
(idxMedian, fields) = vector.findOrCreateField(layer, fields,
146154
columnPrefix + 'median', 21, 6)
155+
if hasSciPy:
156+
(idxMode, fields) = vector.findOrCreateField(layer, fields,
157+
columnPrefix + 'mode', 21, 6)
147158

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

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

0 commit comments

Comments
 (0)