3232from osgeo import osr
3333from PyQt4 .QtCore import *
3434from qgis .core import *
35-
35+ from processing .core .GeoAlgorithmExecutionException import \
36+ GeoAlgorithmExecutionException
3637
3738
3839def 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