Skip to content

Commit 4886b36

Browse files
committedJun 30, 2017
[Processing] Fixes for zonal statistics algorithm:
1. Mask NaN values instead of converting them to 0. 2. Handle rasters for which raster value offset and scale have not been set.
1 parent 6e52f1b commit 4886b36

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed
 

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ def processAlgorithm(self, progress):
8989
geoTransform = rasterDS.GetGeoTransform()
9090
rasterBand = rasterDS.GetRasterBand(bandNumber)
9191
noData = rasterBand.GetNoDataValue()
92+
scale = rasterBand.GetScale()
93+
if scale is None:
94+
scale = 1.0
95+
offset = rasterBand.GetOffset()
96+
if offset is None:
97+
offset = 0.0
9298

9399
cellXSize = abs(geoTransform[1])
94100
cellYSize = abs(geoTransform[5])
@@ -118,7 +124,7 @@ def processAlgorithm(self, progress):
118124

119125
srcOffset = (startColumn, startRow, width, height)
120126
srcArray = rasterBand.ReadAsArray(*srcOffset)
121-
srcArray = srcArray * rasterBand.GetScale() + rasterBand.GetOffset()
127+
srcArray = srcArray * scale + offset
122128

123129
newGeoTransform = (
124130
geoTransform[0] + srcOffset[0] * geoTransform[1],
@@ -192,7 +198,7 @@ def processAlgorithm(self, progress):
192198

193199
srcOffset = (startColumn, startRow, width, height)
194200
srcArray = rasterBand.ReadAsArray(*srcOffset)
195-
srcArray = srcArray * rasterBand.GetScale() + rasterBand.GetOffset()
201+
srcArray = srcArray * scale + offset
196202

197203
newGeoTransform = (
198204
geoTransform[0] + srcOffset[0] * geoTransform[1],
@@ -219,10 +225,11 @@ def processAlgorithm(self, progress):
219225
gdal.RasterizeLayer(rasterizedDS, [1], memLayer, burn_values=[1])
220226
rasterizedArray = rasterizedDS.ReadAsArray()
221227

222-
srcArray = numpy.nan_to_num(srcArray)
223228
masked = numpy.ma.MaskedArray(srcArray,
224-
mask=numpy.logical_or(srcArray == noData,
225-
numpy.logical_not(rasterizedArray)))
229+
mask=numpy.logical_or.reduce((
230+
srcArray == noData,
231+
numpy.logical_not(rasterizedArray),
232+
numpy.isnan(srcArray))))
226233

227234
outFeat.setGeometry(geom)
228235

0 commit comments

Comments
 (0)
Please sign in to comment.