Skip to content

Commit

Permalink
Merge pull request #4800 from radosuav/release-2_18
Browse files Browse the repository at this point in the history
[processing] fixes for zonal statistics algorithm
  • Loading branch information
alexbruy authored Jul 7, 2017
2 parents eb1a1d5 + 4886b36 commit 3d5124c
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions python/plugins/processing/algs/qgis/ZonalStatistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def processAlgorithm(self, progress):
geoTransform = rasterDS.GetGeoTransform()
rasterBand = rasterDS.GetRasterBand(bandNumber)
noData = rasterBand.GetNoDataValue()
scale = rasterBand.GetScale()
if scale is None:
scale = 1.0
offset = rasterBand.GetOffset()
if offset is None:
offset = 0.0

cellXSize = abs(geoTransform[1])
cellYSize = abs(geoTransform[5])
Expand Down Expand Up @@ -118,7 +124,7 @@ def processAlgorithm(self, progress):

srcOffset = (startColumn, startRow, width, height)
srcArray = rasterBand.ReadAsArray(*srcOffset)
srcArray = srcArray * rasterBand.GetScale() + rasterBand.GetOffset()
srcArray = srcArray * scale + offset

newGeoTransform = (
geoTransform[0] + srcOffset[0] * geoTransform[1],
Expand Down Expand Up @@ -192,7 +198,7 @@ def processAlgorithm(self, progress):

srcOffset = (startColumn, startRow, width, height)
srcArray = rasterBand.ReadAsArray(*srcOffset)
srcArray = srcArray * rasterBand.GetScale() + rasterBand.GetOffset()
srcArray = srcArray * scale + offset

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

srcArray = numpy.nan_to_num(srcArray)
masked = numpy.ma.MaskedArray(srcArray,
mask=numpy.logical_or(srcArray == noData,
numpy.logical_not(rasterizedArray)))
mask=numpy.logical_or.reduce((
srcArray == noData,
numpy.logical_not(rasterizedArray),
numpy.isnan(srcArray))))

outFeat.setGeometry(geom)

Expand Down

0 comments on commit 3d5124c

Please sign in to comment.