Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1872 from gioman/processing_fix_qgis_raster_tools
[processing] add support for rasters other than float32 in qgis/processing raster tools
  • Loading branch information
alexbruy committed Feb 1, 2015
2 parents 9a4612e + 8d7c2d7 commit d6017dd
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions python/plugins/processing/tools/raster.py
Expand Up @@ -32,19 +32,36 @@
from osgeo import osr
from PyQt4.QtCore import *
from qgis.core import *

from processing.core.GeoAlgorithmExecutionException import \
GeoAlgorithmExecutionException


def scanraster(layer, progress):
filename = unicode(layer.source())
dataset = gdal.Open(filename, GA_ReadOnly)
band = dataset.GetRasterBand(1)
nodata = band.GetNoDataValue()
bandtype = gdal.GetDataTypeName(band.DataType)
for y in xrange(band.YSize):
progress.setPercentage(y / float(band.YSize) * 100)
scanline = band.ReadRaster(0, y, band.XSize, 1, band.XSize, 1,
band.DataType)
values = struct.unpack('f' * band.XSize, scanline)
if bandtype == 'Byte':
values = struct.unpack('B' * band.XSize, scanline)
elif bandtype == 'Int16':
values = struct.unpack('h' * band.XSize, scanline)
elif bandtype == 'UInt16':
values = struct.unpack('H' * band.XSize, scanline)
elif bandtype == 'Int32':
values = struct.unpack('i' * band.XSize, scanline)
elif bandtype == 'UInt32':
values = struct.unpack('I' * band.XSize, scanline)
elif bandtype == 'Float32':
values = struct.unpack('f' * band.XSize, scanline)
elif bandtype == 'Float64':
values = struct.unpack('d' * band.XSize, scanline)
else:
raise GeoAlgorithmExecutionException('Raster format not supported')
for value in values:
if value == nodata:
value = None
Expand Down Expand Up @@ -99,4 +116,4 @@ def close(self):
dst_ds.SetGeoTransform([self.minx, self.cellsize, 0,
self.maxy, self.cellsize, 0])
dst_ds.GetRasterBand(1).WriteArray(self.matrix)
dst_ds = None
dst_ds = None

0 comments on commit d6017dd

Please sign in to comment.