Skip to content
Permalink
Browse files

processing: add support for rasters other than float in qgis/processi…

…ng raster tools
  • Loading branch information
Giovanni Manghi
Giovanni Manghi committed Jan 31, 2015
1 parent fe27b63 commit 8d7c2d7a8db88232f37b974c98170a1ff4c3ffa9
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 8d7c2d7

Please sign in to comment.
You can’t perform that action at this time.