32
32
from osgeo import osr
33
33
from PyQt4 .QtCore import *
34
34
from qgis .core import *
35
-
35
+ from processing .core .GeoAlgorithmExecutionException import \
36
+ GeoAlgorithmExecutionException
36
37
37
38
38
39
def scanraster (layer , progress ):
39
40
filename = unicode (layer .source ())
40
41
dataset = gdal .Open (filename , GA_ReadOnly )
41
42
band = dataset .GetRasterBand (1 )
42
43
nodata = band .GetNoDataValue ()
44
+ bandtype = gdal .GetDataTypeName (band .DataType )
43
45
for y in xrange (band .YSize ):
44
46
progress .setPercentage (y / float (band .YSize ) * 100 )
45
47
scanline = band .ReadRaster (0 , y , band .XSize , 1 , band .XSize , 1 ,
46
48
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' )
48
65
for value in values :
49
66
if value == nodata :
50
67
value = None
@@ -99,4 +116,4 @@ def close(self):
99
116
dst_ds .SetGeoTransform ([self .minx , self .cellsize , 0 ,
100
117
self .maxy , self .cellsize , 0 ])
101
118
dst_ds .GetRasterBand (1 ).WriteArray (self .matrix )
102
- dst_ds = None
119
+ dst_ds = None
0 commit comments