@@ -63,34 +63,18 @@ class SagaAlgorithm(GeoAlgorithm):
6363 OUTPUT_EXTENT = 'OUTPUT_EXTENT'
6464
6565 def __init__ (self , descriptionfile ):
66- # True if it should resample
67- self .resample = True
66+ self .allowUnmatchingGridExtents = False
6867
6968 # In case several non-matching raster layers are used as input
7069 GeoAlgorithm .__init__ (self )
7170 self .descriptionFile = descriptionfile
7271 self .defineCharacteristicsFromFile ()
73- if self .resample :
74- # Reconsider resampling policy now that we know the input
75- # parameters
76- self .resample = self .setResamplingPolicy ()
7772
7873 def getCopy (self ):
7974 newone = SagaAlgorithm (self .descriptionFile )
8075 newone .provider = self .provider
8176 return newone
8277
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-
9478 def getIcon (self ):
9579 return QIcon (os .path .dirname (__file__ ) + '/../../images/saga.png' )
9680
@@ -116,8 +100,8 @@ def defineCharacteristicsFromFile(self):
116100 self .hardcodedStrings .append (line [len ('Harcoded|' ) + 1 :])
117101 elif line .startswith ('Parameter' ):
118102 self .addParameter (ParameterFactory .getFromString (line ))
119- elif line .startswith ('DontResample ' ):
120- self .resample = False
103+ elif line .startswith ('AllowUnmatching ' ):
104+ self .allowUnmatchingGridExtents = True
121105 elif line .startswith ('Extent' ):
122106 # An extent parameter that wraps 4 SAGA numerical parameters
123107 self .extentParamNames = line [6 :].strip ().split (' ' )
@@ -128,71 +112,6 @@ def defineCharacteristicsFromFile(self):
128112 line = lines .readline ().strip ('\n ' ).strip ()
129113 lines .close ()
130114
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
196115
197116 def processAlgorithm (self , progress ):
198117 if isWindows ():
@@ -208,8 +127,6 @@ def processAlgorithm(self, progress):
208127
209128 # 1: Export rasters to sgrd and vectors to shp
210129 # Tables must be in dbf format. We check that.
211- if self .resample :
212- self .calculateResamplingExtent ()
213130 for param in self .parameters :
214131 if isinstance (param , ParameterRaster ):
215132 if param .value is None :
@@ -219,8 +136,6 @@ def processAlgorithm(self, progress):
219136 exportCommand = self .exportRasterLayer (value )
220137 if exportCommand is not None :
221138 commands .append (exportCommand )
222- if self .resample :
223- commands .append (self .resampleRasterLayer (value ))
224139 if isinstance (param , ParameterVector ):
225140 if param .value is None :
226141 continue
@@ -253,9 +168,6 @@ def processAlgorithm(self, progress):
253168 exportCommand = self .exportRasterLayer (layerfile )
254169 if exportCommand is not None :
255170 commands .append (exportCommand )
256- if self .resample :
257- commands .append (
258- self .resampleRasterLayer (layerfile ))
259171 elif param .datatype == ParameterMultipleInput .TYPE_VECTOR_ANY :
260172 for layerfile in layers :
261173 layer = dataobjects .getObjectFromUri (layerfile , False )
@@ -421,34 +333,7 @@ def getOutputCellsize(self):
421333 cellsize = float (param .value )
422334 break
423335 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+
452337
453338 def exportRasterLayer (self , source ):
454339 if source in sessionExportedLayers :
@@ -485,22 +370,33 @@ def checkBeforeOpeningParametersDialog(self):
485370 html = '<p>This algorithm requires SAGA to be run.Unfortunately, \
486371 it seems that SAGA is not installed in your system, or it \
487372 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>'
489375 return html
490376
491377 def checkParameterValuesBeforeExecuting (self ):
492- """We check that there are no multiband layers, which are not
493- supported by SAGA.
494378 """
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
496383 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 :
501389 return 'Input layer ' + str (layer .name ()) \
502390 + ' has more than one band.\n ' \
503391 + '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+
504400
505401 def help (self ):
506402 name = self .cmdname .lower ()
0 commit comments