def runalgIterating(alg, paramToIter, progress): #generate all single-feature layers settings = QSettings() systemEncoding = settings.value("/UI/encoding", "System").toString() layerfile = alg.getParameterValue(paramToIter) layer = QGisLayers.getObjectFromUri(layerfile, False) provider = layer.dataProvider() allAttrs = provider.attributeIndexes() provider.select(allAttrs) feat = QgsFeature() filelist = [] outputs = {} while provider.nextFeature(feat): output = SextanteUtils.getTempFilename("shp") filelist.append(output) writer = QgsVectorFileWriter(output, systemEncoding, provider.fields(), provider.geometryType(), layer.crs()) writer.addFeature(feat) del writer #store output values to use them later as basenames for all outputs for out in alg.outputs: outputs[out.name] = out.value #now run all the algorithms i = 1 for f in filelist: alg.setParameterValue(paramToIter, f) for out in alg.outputs: filename = outputs[out.name] if filename: filename = filename[:filename.rfind(".")] + "_" + str( i) + filename[filename.rfind("."):] out.value = filename progress.setText("Executing iteration " + str(i) + "/" + str(len(filelist)) + "...") progress.setPercentage((i * 100) / len(filelist)) if UnthreadedAlgorithmExecutor.runalg(alg, SilentProgress()): SextantePostprocessing.handleAlgorithmResults( alg, progress, False) i += 1 else: return False return True
def finish(): QApplication.restoreOverrideCursor() if onFinish is not None: onFinish(alg, SilentProgress()) progress.close()
def runAlgorithm(algOrName, onFinish, *args): if isinstance(algOrName, GeoAlgorithm): alg = algOrName else: alg = Sextante.getAlgorithm(algOrName) if alg == None: print("Error: Algorithm not found\n") return if len(args) != alg.getVisibleParametersCount() + alg.getVisibleOutputsCount(): print ("Error: Wrong number of parameters") sextante.alghelp(algOrName) return alg = alg.getCopy() if isinstance(args, dict): # set params by name for name, value in args.items(): if alg.getParameterFromName(name).setValue(value): continue; if alg.getOutputFromName(name).setValue(value): continue; print ("Error: Wrong parameter value %s for parameter %s." % (value, name)) return else: i = 0 for param in alg.parameters: if not param.hidden: if not param.setValue(args[i]): print ("Error: Wrong parameter value: " + unicode(args[i])) return i = i +1 for output in alg.outputs: if not output.hidden: if not output.setValue(args[i]): print ("Error: Wrong output value: " + unicode(args[i])) return i = i +1 msg = alg.checkParameterValuesBeforeExecuting() if msg: print ("Unable to execute algorithm\n" + msg) return if not alg.checkInputCRS(): print ("Warning: Not all input layers use the same CRS.\n" + "This can cause unexpected results.") SextanteLog.addToLog(SextanteLog.LOG_ALGORITHM, alg.getAsCommand()) # don't set the wait cursor twice, because then when you restore it # it will still be a wait cursor cursor = QApplication.overrideCursor() if cursor == None or cursor == 0: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) elif cursor.shape() != Qt.WaitCursor: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) useThreads = SextanteConfig.getSetting(SextanteConfig.USE_THREADS) #this is doing strange things, so temporarily the thread execution is disabled from the console useThreads = False if useThreads: algEx = AlgorithmExecutor(alg) progress = QProgressDialog() progress.setWindowTitle(alg.name) progress.setLabelText("Executing %s..." % alg.name) def finish(): QApplication.restoreOverrideCursor() if onFinish is not None: onFinish(alg, SilentProgress()) progress.close() def error(msg): QApplication.restoreOverrideCursor() print msg SextanteLog.addToLog(SextanteLog.LOG_ERROR, msg) def cancel(): try: algEx.finished.disconnect() algEx.terminate() QApplication.restoreOverrideCursor() progress.close() except: pass algEx.error.connect(error) algEx.finished.connect(finish) algEx.start() algEx.wait() else: progress = SilentProgress() ret = UnthreadedAlgorithmExecutor.runalg(alg, progress) if onFinish is not None and ret: onFinish(alg, progress) QApplication.restoreOverrideCursor() return alg