processing: add support for rasters other than float in qgis/processing raster tools

This commit is contained in:
Giovanni Manghi 2015-01-31 21:08:29 +00:00
parent fe27b63576
commit 8d7c2d7a8d

View File

@ -32,7 +32,8 @@ from osgeo.gdalconst import *
from osgeo import osr
from PyQt4.QtCore import *
from qgis.core import *
from processing.core.GeoAlgorithmExecutionException import \
GeoAlgorithmExecutionException
def scanraster(layer, progress):
@ -40,11 +41,27 @@ def scanraster(layer, progress):
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
@ -99,4 +116,4 @@ class RasterWriter:
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