@@ -63,34 +63,18 @@ class SagaAlgorithm(GeoAlgorithm):
63
63
OUTPUT_EXTENT = 'OUTPUT_EXTENT'
64
64
65
65
def __init__ (self , descriptionfile ):
66
- # True if it should resample
67
- self .resample = True
66
+ self .allowUnmatchingGridExtents = False
68
67
69
68
# In case several non-matching raster layers are used as input
70
69
GeoAlgorithm .__init__ (self )
71
70
self .descriptionFile = descriptionfile
72
71
self .defineCharacteristicsFromFile ()
73
- if self .resample :
74
- # Reconsider resampling policy now that we know the input
75
- # parameters
76
- self .resample = self .setResamplingPolicy ()
77
72
78
73
def getCopy (self ):
79
74
newone = SagaAlgorithm (self .descriptionFile )
80
75
newone .provider = self .provider
81
76
return newone
82
77
83
- def setResamplingPolicy (self ):
84
- count = 0
85
- for param in self .parameters :
86
- if isinstance (param , ParameterRaster ):
87
- count += 1
88
- if isinstance (param , ParameterMultipleInput ):
89
- if param .datatype == ParameterMultipleInput .TYPE_RASTER :
90
- return True
91
-
92
- return count > 1
93
-
94
78
def getIcon (self ):
95
79
return QIcon (os .path .dirname (__file__ ) + '/../../images/saga.png' )
96
80
@@ -116,8 +100,8 @@ def defineCharacteristicsFromFile(self):
116
100
self .hardcodedStrings .append (line [len ('Harcoded|' ) + 1 :])
117
101
elif line .startswith ('Parameter' ):
118
102
self .addParameter (ParameterFactory .getFromString (line ))
119
- elif line .startswith ('DontResample ' ):
120
- self .resample = False
103
+ elif line .startswith ('AllowUnmatching ' ):
104
+ self .allowUnmatchingGridExtents = True
121
105
elif line .startswith ('Extent' ):
122
106
# An extent parameter that wraps 4 SAGA numerical parameters
123
107
self .extentParamNames = line [6 :].strip ().split (' ' )
@@ -128,71 +112,6 @@ def defineCharacteristicsFromFile(self):
128
112
line = lines .readline ().strip ('\n ' ).strip ()
129
113
lines .close ()
130
114
131
- def calculateResamplingExtent (self ):
132
- """This method calculates the resampling extent, but it might
133
- set self.resample to False if, with the current layers, there
134
- is no need to resample.
135
- """
136
-
137
- auto = ProcessingConfig .getSetting (SagaUtils .SAGA_AUTO_RESAMPLING )
138
- if auto :
139
- first = True
140
- self .inputExtentsCount = 0
141
- for param in self .parameters :
142
- if param .value :
143
- if isinstance (param , ParameterRaster ):
144
- if isinstance (param .value , QgsRasterLayer ):
145
- layer = param .value
146
- else :
147
- layer = dataobjects .getObjectFromUri (param .value )
148
- self .addToResamplingExtent (layer , first )
149
- first = False
150
- if isinstance (param , ParameterMultipleInput ):
151
- if param .datatype \
152
- == ParameterMultipleInput .TYPE_RASTER :
153
- layers = param .value .split (';' )
154
- for layername in layers :
155
- layer = dataobjects .getObjectFromUri (layername )
156
- self .addToResamplingExtent (layer , first )
157
- first = False
158
- if self .inputExtentsCount < 2 :
159
- self .resample = False
160
- else :
161
- self .xmin = ProcessingConfig .getSetting (
162
- SagaUtils .SAGA_RESAMPLING_REGION_XMIN )
163
- self .xmax = ProcessingConfig .getSetting (
164
- SagaUtils .SAGA_RESAMPLING_REGION_XMAX )
165
- self .ymin = ProcessingConfig .getSetting (
166
- SagaUtils .SAGA_RESAMPLING_REGION_YMIN )
167
- self .ymax = ProcessingConfig .getSetting (
168
- SagaUtils .SAGA_RESAMPLING_REGION_YMAX )
169
- self .cellsize = ProcessingConfig .getSetting (
170
- SagaUtils .SAGA_RESAMPLING_REGION_CELLSIZE )
171
-
172
- def addToResamplingExtent (self , layer , first ):
173
- if layer is None :
174
- return
175
- if first :
176
- self .inputExtentsCount = 1
177
- self .xmin = layer .extent ().xMinimum ()
178
- self .xmax = layer .extent ().xMaximum ()
179
- self .ymin = layer .extent ().yMinimum ()
180
- self .ymax = layer .extent ().yMaximum ()
181
- self .cellsize = (layer .extent ().xMaximum ()
182
- - layer .extent ().xMinimum ()) / layer .width ()
183
- else :
184
- cellsize = (layer .extent ().xMaximum () -
185
- layer .extent ().xMinimum ()) / layer .width ()
186
- if self .xmin != layer .extent ().xMinimum () or self .xmax \
187
- != layer .extent ().xMaximum () or self .ymin \
188
- != layer .extent ().yMinimum () or self .ymax \
189
- != layer .extent ().yMaximum () or self .cellsize != cellsize :
190
- self .xmin = min (self .xmin , layer .extent ().xMinimum ())
191
- self .xmax = max (self .xmax , layer .extent ().xMaximum ())
192
- self .ymin = min (self .ymin , layer .extent ().yMinimum ())
193
- self .ymax = max (self .ymax , layer .extent ().yMaximum ())
194
- self .cellsize = min (self .cellsize , cellsize )
195
- self .inputExtentsCount += 1
196
115
197
116
def processAlgorithm (self , progress ):
198
117
if isWindows ():
@@ -208,8 +127,6 @@ def processAlgorithm(self, progress):
208
127
209
128
# 1: Export rasters to sgrd and vectors to shp
210
129
# Tables must be in dbf format. We check that.
211
- if self .resample :
212
- self .calculateResamplingExtent ()
213
130
for param in self .parameters :
214
131
if isinstance (param , ParameterRaster ):
215
132
if param .value is None :
@@ -219,8 +136,6 @@ def processAlgorithm(self, progress):
219
136
exportCommand = self .exportRasterLayer (value )
220
137
if exportCommand is not None :
221
138
commands .append (exportCommand )
222
- if self .resample :
223
- commands .append (self .resampleRasterLayer (value ))
224
139
if isinstance (param , ParameterVector ):
225
140
if param .value is None :
226
141
continue
@@ -253,9 +168,6 @@ def processAlgorithm(self, progress):
253
168
exportCommand = self .exportRasterLayer (layerfile )
254
169
if exportCommand is not None :
255
170
commands .append (exportCommand )
256
- if self .resample :
257
- commands .append (
258
- self .resampleRasterLayer (layerfile ))
259
171
elif param .datatype == ParameterMultipleInput .TYPE_VECTOR_ANY :
260
172
for layerfile in layers :
261
173
layer = dataobjects .getObjectFromUri (layerfile , False )
@@ -421,34 +333,7 @@ def getOutputCellsize(self):
421
333
cellsize = float (param .value )
422
334
break
423
335
return cellsize
424
-
425
- def resampleRasterLayer (self , layer ):
426
- """This is supposed to be run after having exported all raster
427
- layers.
428
- """
429
-
430
- if layer in self .exportedLayers .keys ():
431
- inputFilename = self .exportedLayers [layer ]
432
- else :
433
- inputFilename = layer
434
- destFilename = getTempFilename ('sgrd' )
435
- self .exportedLayers [layer ] = destFilename
436
- saga208 = ProcessingConfig .getSetting (SagaUtils .SAGA_208 )
437
- if isWindows () or isMac () or not saga208 :
438
- s = 'grid_tools "Resampling" -INPUT "' + inputFilename \
439
- + '" -TARGET 0 -SCALE_UP_METHOD 0 -SCALE_DOWN_METHOD 0 -USER_XMIN ' \
440
- + str (self .xmin ) + ' -USER_XMAX ' + str (self .xmax ) \
441
- + ' -USER_YMIN ' + str (self .ymin ) + ' -USER_YMAX ' \
442
- + str (self .ymax ) + ' -USER_SIZE ' + str (self .cellsize ) \
443
- + ' -USER_GRID "' + destFilename + '"'
444
- else :
445
- s = 'libgrid_tools "Resampling" -INPUT "' + inputFilename \
446
- + '" -TARGET 0 -SCALE_UP_METHOD 0 -SCALE_DOWN_METHOD 0 -USER_XMIN ' \
447
- + str (self .xmin ) + ' -USER_XMAX ' + str (self .xmax ) \
448
- + ' -USER_YMIN ' + str (self .ymin ) + ' -USER_YMAX ' \
449
- + str (self .ymax ) + ' -USER_SIZE ' + str (self .cellsize ) \
450
- + ' -USER_GRID "' + destFilename + '"'
451
- return s
336
+
452
337
453
338
def exportRasterLayer (self , source ):
454
339
if source in sessionExportedLayers :
@@ -485,22 +370,33 @@ def checkBeforeOpeningParametersDialog(self):
485
370
html = '<p>This algorithm requires SAGA to be run.Unfortunately, \
486
371
it seems that SAGA is not installed in your system, or it \
487
372
is not correctly configured to be used from QGIS</p>'
488
- html += '<p><a href= "http://docs.qgis.org/2.0/en/docs/user_manual/processing/3rdParty.html">Click here</a> to know more about how to install and configure SAGA to be used with QGIS</p>'
373
+ html += '<p><a href= "http://docs.qgis.org/2.0/en/docs/user_manual/processing/3rdParty.html">\
374
+ Click here</a> to know more about how to install and configure SAGA to be used with QGIS</p>'
489
375
return html
490
376
491
377
def checkParameterValuesBeforeExecuting (self ):
492
- """We check that there are no multiband layers, which are not
493
- supported by SAGA.
494
378
"""
495
-
379
+ We check that there are no multiband layers, which are not
380
+ supported by SAGA, and that raster layers have the same grid extent
381
+ """
382
+ extent = None
496
383
for param in self .parameters :
497
- if isinstance (param , ParameterRaster ):
498
- value = param .value
499
- layer = dataobjects .getObjectFromUri (value )
500
- if layer is not None and layer .bandCount () > 1 :
384
+ if isinstance (param , ParameterRaster ):
385
+ layer = dataobjects .getObjectFromUri (param .value )
386
+ if layer is None :
387
+ continue
388
+ if layer .bandCount () > 1 :
501
389
return 'Input layer ' + str (layer .name ()) \
502
390
+ ' has more than one band.\n ' \
503
391
+ 'Multiband layers are not supported by SAGA'
392
+ if extent is None :
393
+ extent = (layer .extent (), layer .height (), layer .width ())
394
+ else :
395
+ extent2 = (layer .extent (), layer .height (), layer .width ())
396
+ if extent != extent2 :
397
+ return "Input layers do not have the same grid extent."
398
+
399
+
504
400
505
401
def help (self ):
506
402
name = self .cmdname .lower ()
0 commit comments