Skip to content

Commit d6017dd

Browse files
committedFeb 1, 2015
Merge pull request #1872 from gioman/processing_fix_qgis_raster_tools
[processing] add support for rasters other than float32 in qgis/processing raster tools
2 parents 9a4612e + 8d7c2d7 commit d6017dd

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed
 

‎python/plugins/processing/tools/raster.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,36 @@
3232
from osgeo import osr
3333
from PyQt4.QtCore import *
3434
from qgis.core import *
35-
35+
from processing.core.GeoAlgorithmExecutionException import \
36+
GeoAlgorithmExecutionException
3637

3738

3839
def scanraster(layer, progress):
3940
filename = unicode(layer.source())
4041
dataset = gdal.Open(filename, GA_ReadOnly)
4142
band = dataset.GetRasterBand(1)
4243
nodata = band.GetNoDataValue()
44+
bandtype = gdal.GetDataTypeName(band.DataType)
4345
for y in xrange(band.YSize):
4446
progress.setPercentage(y / float(band.YSize) * 100)
4547
scanline = band.ReadRaster(0, y, band.XSize, 1, band.XSize, 1,
4648
band.DataType)
47-
values = struct.unpack('f' * band.XSize, scanline)
49+
if bandtype == 'Byte':
50+
values = struct.unpack('B' * band.XSize, scanline)
51+
elif bandtype == 'Int16':
52+
values = struct.unpack('h' * band.XSize, scanline)
53+
elif bandtype == 'UInt16':
54+
values = struct.unpack('H' * band.XSize, scanline)
55+
elif bandtype == 'Int32':
56+
values = struct.unpack('i' * band.XSize, scanline)
57+
elif bandtype == 'UInt32':
58+
values = struct.unpack('I' * band.XSize, scanline)
59+
elif bandtype == 'Float32':
60+
values = struct.unpack('f' * band.XSize, scanline)
61+
elif bandtype == 'Float64':
62+
values = struct.unpack('d' * band.XSize, scanline)
63+
else:
64+
raise GeoAlgorithmExecutionException('Raster format not supported')
4865
for value in values:
4966
if value == nodata:
5067
value = None
@@ -99,4 +116,4 @@ def close(self):
99116
dst_ds.SetGeoTransform([self.minx, self.cellsize, 0,
100117
self.maxy, self.cellsize, 0])
101118
dst_ds.GetRasterBand(1).WriteArray(self.matrix)
102-
dst_ds = None
119+
dst_ds = None

0 commit comments

Comments
 (0)
Please sign in to comment.