Skip to content
Permalink
Browse files

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 d6017dd88fe20660b4556b1059c691fa7ed8daad
Showing with 20 additions and 3 deletions.
  1. +20 −3 python/plugins/processing/tools/raster.py
@@ -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
@@ -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.
You can’t perform that action at this time.