@@ -163,11 +163,18 @@ def processAlgorithm(self, progress):
163
163
commands = []
164
164
self .exportedLayers = {}
165
165
166
+ # if GRASS session has been created outside of this algorithm then get the list of layers loaded in GRASS
167
+ # otherwise start a new session
168
+ existingSession = GrassUtils .sessionRunning
169
+ if existingSession :
170
+ self .exportedLayers = GrassUtils .getSessionLayers ()
171
+ else :
172
+ GrassUtils .startGrassSession ()
173
+
174
+
166
175
#self.calculateRegion()
167
176
region = str (self .getParameterValue (self .GRASS_REGION_EXTENT_PARAMETER ))
168
177
regionCoords = region .split ("," )
169
- GrassUtils .createTempMapset ();
170
-
171
178
command = "g.region"
172
179
command += " n=" + str (regionCoords [3 ])
173
180
command += " s=" + str (regionCoords [2 ])
@@ -178,19 +185,26 @@ def processAlgorithm(self, progress):
178
185
else :
179
186
command += " res=" + str (self .getParameterValue (self .GRASS_REGION_CELLSIZE_PARAMETER ));
180
187
commands .append (command )
181
-
188
+
182
189
#1: Export layer to grass mapset
183
190
for param in self .parameters :
184
191
if isinstance (param , ParameterRaster ):
185
192
if param .value == None :
186
193
continue
187
194
value = param .value
188
- commands .append (self .exportRasterLayer (value ))
195
+ # check if the layer hasn't already been exported in, for example, previous GRASS calls in this session
196
+ if value in self .exportedLayers .keys ():
197
+ continue
198
+ else :
199
+ commands .append (self .exportRasterLayer (value ))
189
200
if isinstance (param , ParameterVector ):
190
201
if param .value == None :
191
202
continue
192
203
value = param .value
193
- commands .append (self .exportVectorLayer (value ))
204
+ if value in self .exportedLayers .keys ():
205
+ continue
206
+ else :
207
+ commands .append (self .exportVectorLayer (value ))
194
208
if isinstance (param , ParameterTable ):
195
209
pass
196
210
if isinstance (param , ParameterMultipleInput ):
@@ -201,15 +215,21 @@ def processAlgorithm(self, progress):
201
215
continue
202
216
if param .datatype == ParameterMultipleInput .TYPE_RASTER :
203
217
for layer in layers :
204
- commands .append (self .exportRasterLayer (layer ))
218
+ if layer in self .exportedLayers .keys ():
219
+ continue
220
+ else :
221
+ commands .append (self .exportRasterLayer (layer ))
205
222
elif param .datatype == ParameterMultipleInput .TYPE_VECTOR_ANY :
206
223
for layer in layers :
207
- commands .append (self .exportVectorLayer (layer ))
224
+ if layer in self .exportedLayers .keys ():
225
+ continue
226
+ else :
227
+ commands .append (self .exportVectorLayer (layer ))
208
228
209
229
#2: set parameters and outputs
210
230
command = self .grassName
211
231
for param in self .parameters :
212
- if param .value == None :
232
+ if param .value == None or param . value == "" :
213
233
continue
214
234
if param .name == self .GRASS_REGION_CELLSIZE_PARAMETER or param .name == self .GRASS_REGION_EXTENT_PARAMETER :
215
235
continue
@@ -238,7 +258,9 @@ def processAlgorithm(self, progress):
238
258
if isinstance (out , OutputFile ):
239
259
command += (" " + out .name + "=\" " + out .value + "\" " );
240
260
else :
241
- command += (" " + out .name + "=" + out .name );
261
+ command += (" " + out .name )
262
+ out .name += ("_" + str (len (self .exportedLayers ))) # make sure output is unique within a session
263
+ command += ("=" + out .name )
242
264
243
265
command += " --overwrite"
244
266
commands .append (command )
@@ -254,14 +276,17 @@ def processAlgorithm(self, progress):
254
276
command += out .name
255
277
command += " output=\" " + filename + "\" "
256
278
commands .append (command )
279
+ # add output file to exported layers, to indicate that they are present in GRASS
280
+ self .exportedLayers [filename ]= out .name
257
281
if isinstance (out , OutputVector ):
258
282
command = "v.out.ogr -ce input=" + out .name
259
283
command += " dsn=\" " + os .path .dirname (out .value ) + "\" "
260
284
command += " format=ESRI_Shapefile"
261
285
command += " olayer=" + os .path .basename (out .value )[:- 4 ]
262
286
command += " type=auto"
263
287
commands .append (command )
264
-
288
+ self .exportedLayers [filename ]= out .name
289
+
265
290
#4 Run GRASS
266
291
loglines = []
267
292
loglines .append ("GRASS execution commands" )
@@ -271,7 +296,12 @@ def processAlgorithm(self, progress):
271
296
if SextanteConfig .getSetting (GrassUtils .GRASS_LOG_COMMANDS ):
272
297
SextanteLog .addToLog (SextanteLog .LOG_INFO , loglines )
273
298
GrassUtils .executeGrass (commands , progress );
274
-
299
+ # if the session has been created outside of this algorithm, add the new GRASS layers to it
300
+ # otherwise finish the session
301
+ if existingSession :
302
+ GrassUtils .addSessionLayers (self .exportedLayers )
303
+ else :
304
+ GrassUtils .endGrassSession ()
275
305
276
306
def exportVectorLayer (self , orgFilename ):
277
307
#only export to an intermediate shp if the layer is not file-based.
@@ -319,3 +349,4 @@ def getTempFilename(self):
319
349
320
350
def commandLineName (self ):
321
351
return "grass:" + self .name [:self .name .find (" " )]
352
+
0 commit comments