def handleAlgorithmResults(alg, progress, showResults = True): wrongLayers = [] htmlResults = False; progress.setText("Loading resulting layers") i = 0 for out in alg.outputs: progress.setPercentage(100 * i / float(len(alg.outputs))) if out.hidden or not out.open: continue if isinstance(out, (OutputRaster, OutputVector, OutputTable)): try: if out.value.startswith("memory:"): layer = out.memoryLayer QgsMapLayerRegistry.instance().addMapLayers([layer]) else: if ProcessingConfig.getSetting(ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description dataobjects.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(),out.name)) except Exception, e: wrongLayers.append(out) elif isinstance(out, OutputHTML): ProcessingResults.addResult(out.description, out.value) htmlResults = True
def setTableContent(self): numOutputs = 0 for output in self.alg.outputs: if isinstance(output, (OutputVector, OutputRaster)): if not output.flags() & QgsProcessingParameterDefinition.FlagHidden: numOutputs += 1 self.tblStyles.setRowCount(numOutputs) i = 0 for output in self.alg.outputs: if isinstance(output, (OutputVector, OutputRaster)): if not output.flags() & QgsProcessingParameterDefinition.FlagHidden: item = QTableWidgetItem(output.description() + '<' + output.__class__.__name__ + '>') item.setFlags(Qt.ItemIsEnabled) self.tblStyles.setItem(i, 0, item) item = RenderingStyleFilePanel() style = \ RenderingStyles.getStyle(self.alg.id(), output.name()) if style: item.setText(str(style)) self.valueItems[output.name()] = item self.tblStyles.setCellWidget(i, 1, item) self.tblStyles.setRowHeight(i, 22) i += 1
def setTableContent(self): numOutputs = 0 for output in self.alg.outputs: if isinstance(output, (OutputVector, OutputRaster)): if not output.hidden: numOutputs += 1 self.tblStyles.setRowCount(numOutputs) i = 0 for output in self.alg.outputs: if isinstance(output, (OutputVector, OutputRaster)): if not output.hidden: item = QTableWidgetItem(output.description + '<' + output.__class__.__name__ + '>') item.setFlags(Qt.ItemIsEnabled) self.tblStyles.setItem(i, 0, item) item = RenderingStyleFilePanel() style = \ RenderingStyles.getStyle(self.alg.commandLineName(), output.name) if style: item.setText(unicode(style)) self.valueItems[output.name] = item self.tblStyles.setCellWidget(i, 1, item) self.tblStyles.setRowHeight(i, 22) i += 1
def handleAlgorithmResults(alg, context, feedback=None, showResults=True): wrongLayers = [] if feedback is None: feedback = QgsProcessingFeedback() feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for l, details in context.layersToLoadOnCompletion().items(): if feedback.isCanceled(): return False if len(context.layersToLoadOnCompletion()) > 2: # only show progress feedback if we're loading a bunch of layers feedback.setProgress(100 * i / float(len(context.layersToLoadOnCompletion()))) try: layer = QgsProcessingUtils.mapLayerFromString(l, context, typeHint=details.layerTypeHint) if layer is not None: set_layer_name(layer, details) style = None if details.outputName: style = RenderingStyles.getStyle(alg.id(), details.outputName) if style is None: if layer.type() == QgsMapLayer.RasterLayer: style = ProcessingConfig.getSetting(ProcessingConfig.RASTER_STYLE) else: if layer.geometryType() == QgsWkbTypes.PointGeometry: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POINT_STYLE) elif layer.geometryType() == QgsWkbTypes.LineGeometry: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_LINE_STYLE) else: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POLYGON_STYLE) if style: layer.loadNamedStyle(style) details.project.addMapLayer(context.temporaryLayerStore().takeMapLayer(layer)) if details.postProcessor(): details.postProcessor().postProcessLayer(layer, context, feedback) else: wrongLayers.append(str(l)) except Exception: QgsMessageLog.logMessage(QCoreApplication.translate('Postprocessing', "Error loading result layer:") + "\n" + traceback.format_exc(), 'Processing', Qgis.Critical) wrongLayers.append(str(l)) i += 1 feedback.setProgress(100) if wrongLayers: msg = QCoreApplication.translate('Postprocessing', "The following layers were not correctly generated.") msg += "<ul>" + "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += QCoreApplication.translate('Postprocessing', "You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm.") feedback.reportError(msg) return len(wrongLayers) == 0
def handleAlgorithmResults(alg, progress=None, showResults=True): wrongLayers = [] htmlResults = False if progress is None: progress = SilentProgress() progress.setText( QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for out in alg.outputs: progress.setPercentage(100 * i / float(len(alg.outputs))) if out.hidden or not out.open: continue if isinstance(out, (OutputRaster, OutputVector, OutputTable)): try: if hasattr(out, "layer") and out.layer is not None: out.layer.setLayerName(out.description) QgsMapLayerRegistry.instance().addMapLayers([out.layer]) else: if ProcessingConfig.getSetting( ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description dataobjects.load( out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(), out.name)) except Exception: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, "Error loading result layer:\n" + traceback.format_exc()) wrongLayers.append(out.description) elif isinstance(out, OutputHTML): ProcessingResults.addResult(out.description, out.value) htmlResults = True i += 1 QApplication.restoreOverrideCursor() if wrongLayers: msg = "The following layers were not correctly generated.<ul>" msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += "You can check the log messages to find more information about the execution of the algorithm" progress.error(msg) if showResults and htmlResults and not wrongLayers: dlg = ResultsDialog() dlg.exec_() return len(wrongLayers) == 0
def handleAlgorithmResults(alg, context, feedback=None, showResults=True): wrongLayers = [] if feedback is None: feedback = QgsProcessingFeedback() feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for l, details in context.layersToLoadOnCompletion().items(): if feedback.isCanceled(): return False if len(context.layersToLoadOnCompletion()) > 2: # only show progress feedback if we're loading a bunch of layers feedback.setProgress(100 * i / float(len(context.layersToLoadOnCompletion()))) try: layer = QgsProcessingUtils.mapLayerFromString(l, context) if layer is not None: if not ProcessingConfig.getSetting(ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): layer.setName(details.name) style = None if details.outputName: style = RenderingStyles.getStyle(alg.id(), details.outputName) if style is None: if layer.type() == QgsMapLayer.RasterLayer: style = ProcessingConfig.getSetting(ProcessingConfig.RASTER_STYLE) else: if layer.geometryType() == QgsWkbTypes.PointGeometry: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POINT_STYLE) elif layer.geometryType() == QgsWkbTypes.LineGeometry: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_LINE_STYLE) else: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POLYGON_STYLE) if style: layer.loadNamedStyle(style) details.project.addMapLayer(context.temporaryLayerStore().takeMapLayer(layer)) except Exception: QgsMessageLog.logMessage("Error loading result layer:\n" + traceback.format_exc(), 'Processing', QgsMessageLog.CRITICAL) wrongLayers.append(str(l)) i += 1 QApplication.restoreOverrideCursor() if wrongLayers: msg = "The following layers were not correctly generated.<ul>" msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += "You can check the log messages to find more information about the execution of the algorithm" feedback.reportError(msg) return len(wrongLayers) == 0
def handleAlgorithmResults(alg, context, feedback=None, showResults=True): wrongLayers = [] if feedback is None: feedback = QgsProcessingFeedback() feedback.setProgressText( QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for out in alg.outputs: feedback.setProgress(100 * i / float(len(alg.outputs))) if out.hidden or not out.open: continue if isinstance(out, (OutputRaster, OutputVector, OutputTable)): try: layer = QgsProcessingUtils.mapLayerFromString( out.value, context) if layer: layer.setName(out.description) QgsProject.instance().addMapLayer( context.temporaryLayerStore().takeMapLayer(layer)) else: if ProcessingConfig.getSetting( ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description isRaster = True if isinstance(out, OutputRaster) else False dataobjects.load( out.value, name, alg.crs, RenderingStyles.getStyle(alg.id(), out.name), isRaster) except Exception: QgsMessageLog.logMessage( "Error loading result layer:\n" + traceback.format_exc(), 'Processing', QgsMessageLog.CRITICAL) wrongLayers.append(out.description) elif isinstance(out, OutputHTML): resultsList.addResult(alg.icon(), out.description, out.value) i += 1 QApplication.restoreOverrideCursor() if wrongLayers: msg = "The following layers were not correctly generated.<ul>" msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += "You can check the log messages to find more information about the execution of the algorithm" feedback.reportError(msg) return len(wrongLayers) == 0
def handleAlgorithmResults(alg, progress=None, showResults=True): wrongLayers = [] htmlResults = False if progress is None: progress = SilentProgress() progress.setText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for out in alg.outputs: progress.setPercentage(100 * i / float(len(alg.outputs))) if out.hidden or not out.open: continue if isinstance(out, (OutputRaster, OutputVector, OutputTable)): try: if hasattr(out, "layer") and out.layer is not None: out.layer.setLayerName(out.description) QgsMapLayerRegistry.instance().addMapLayers([out.layer]) else: if ProcessingConfig.getSetting( ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description isRaster = True if isinstance(out, OutputRaster) else False dataobjects.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(), out.name), isRaster) except Exception: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "Error loading result layer:\n" + traceback.format_exc()) wrongLayers.append(out.description) elif isinstance(out, OutputHTML): ProcessingResults.addResult(out.description, out.value) htmlResults = True i += 1 QApplication.restoreOverrideCursor() if wrongLayers: msg = "The following layers were not correctly generated.<ul>" msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += "You can check the log messages to find more information about the execution of the algorithm" progress.error(msg) if showResults and htmlResults and not wrongLayers: dlg = ResultsDialog() dlg.exec_() return len(wrongLayers) == 0
def handleAlgorithmResults(alg, progress=None, showResults=True): wrongLayers = [] htmlResults = False if progress is None: progress = SilentProgress() progress.setText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for out in alg.outputs: progress.setPercentage(100 * i / float(len(alg.outputs))) if out.hidden or not out.open: continue if isinstance(out, (OutputRaster, OutputVector, OutputTable)): try: if out.value.startswith('memory:'): layer = out.memoryLayer QgsMapLayerRegistry.instance().addMapLayers([layer]) else: if ProcessingConfig.getSetting( ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description dataobjects.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(), out.name)) except Exception as e: wrongLayers.append(out.description) elif isinstance(out, OutputHTML): ProcessingResults.addResult(out.description, out.value) htmlResults = True i += 1 if wrongLayers: QApplication.restoreOverrideCursor() dlg = MessageDialog() dlg.setTitle(QCoreApplication.translate('Postprocessing', 'Problem loading output layers')) msg = "The following layers were not correctly generated.<ul>" msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += "You can check the <a href='log'>log messages</a> to find more information about the execution of the algorithm" dlg.setMessage(msg) dlg.exec_() if showResults and htmlResults and not wrongLayers: QApplication.restoreOverrideCursor() dlg = ResultsDialog() dlg.exec_() return len(wrongLayers) == 0
def handleAlgorithmResults(alg, feedback=None, showResults=True): wrongLayers = [] if feedback is None: feedback = QgsProcessingFeedback() feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for out in alg.outputs: feedback.setProgress(100 * i / float(len(alg.outputs))) if out.hidden or not out.open: continue if isinstance(out, (OutputRaster, OutputVector, OutputTable)): try: if hasattr(out, "layer") and out.layer is not None: out.layer.setName(out.description) QgsProject.instance().addMapLayers([out.layer]) else: if ProcessingConfig.getSetting( ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description dataobjects.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.id(), out.name)) except Exception: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "Error loading result layer:\n" + traceback.format_exc()) wrongLayers.append(out.description) elif isinstance(out, OutputHTML): resultsList.addResult(alg.icon(), out.description, out.value) i += 1 QApplication.restoreOverrideCursor() if wrongLayers: msg = "The following layers were not correctly generated.<ul>" msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += "You can check the log messages to find more information about the execution of the algorithm" feedback.reportError(msg) return len(wrongLayers) == 0
def setTableContent(self): numOutputs = 0 for output in self.alg.outputs: if isinstance(output, (OutputVector, OutputRaster)): if not output.hidden: numOutputs += 1 self.tableWidget.setRowCount(numOutputs) i = 0 for output in self.alg.outputs: if isinstance(output, (OutputVector, OutputRaster)): if not output.hidden: item = QtGui.QTableWidgetItem(output.description + "<" + output.__module__.split(".")[-1] + ">") item.setFlags(QtCore.Qt.ItemIsEnabled) self.tableWidget.setItem(i, 0, item) item = RenderingStyleFilePanel() style = RenderingStyles.getStyle(self.alg.commandLineName(), output.name) if style: item.setText(str(style)) self.valueItems[output.name] = item self.tableWidget.setCellWidget(i, 1, item) self.tableWidget.setRowHeight(i, 22) i += 1
def handleAlgorithmResults(alg, progress=None, showResults=True): wrongLayers = [] reslayers = [] if progress is None: progress = SilentProgress() progress.setText(QCoreApplication.translate('ArpaGeoprocessing', 'Loading resulting layer')) i = 0 for out in alg.outputs: progress.setPercentage(100 * i / float(len(alg.outputs))) #if out.hidden or not out.open: #continue if isinstance(out, (OutputVector)): try: if alg.outputType == 'Postgis': outUri = QgsDataSourceURI() psql = out.value['PSQL'] outUri.setConnection(psql.PSQLHost, psql.PSQLPort, psql.PSQLDatabase, psql.PSQLUsername, psql.PSQLPassword) outUri.setDataSource(out.value['schema'],out.value['table'],out.value['geoColumn']) layer = QgsVectorLayer(outUri.uri(), out.value['table'], "postgres") QgsMapLayerRegistry.instance().addMapLayers([layer]) else: if ProcessingConfig.getSetting( ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) name = name.split('.')[0] else: name = out.description # removing .ext from name if exixst layer = dataobjects.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(), out.name)) reslayers.append(layer) except Exception, e: wrongLayers.append(out) i += 1
def setTableContent(self): numOutputs = 0 for output in self.alg.outputDefinitions(): if isinstance(output, (QgsProcessingOutputVectorLayer, QgsProcessingOutputRasterLayer)): numOutputs += 1 self.tblStyles.setRowCount(numOutputs) i = 0 for output in self.alg.outputDefinitions(): if isinstance(output, (QgsProcessingOutputVectorLayer, QgsProcessingOutputRasterLayer)): item = QTableWidgetItem(output.description() + '<' + output.__class__.__name__ + '>') item.setFlags(Qt.ItemIsEnabled) self.tblStyles.setItem(i, 0, item) item = RenderingStyleFilePanel() style = \ RenderingStyles.getStyle(self.alg.id(), output.name()) if style: item.setText(str(style)) self.valueItems[output.name()] = item self.tblStyles.setCellWidget(i, 1, item) self.tblStyles.setRowHeight(i, 22) i += 1
def handleAlgorithmResults(alg, context, feedback=None, showResults=True, parameters={}): wrongLayers = [] if feedback is None: feedback = QgsProcessingFeedback() feedback.setProgressText( QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for l, details in context.layersToLoadOnCompletion().items(): if feedback.isCanceled(): return False if len(context.layersToLoadOnCompletion()) > 2: # only show progress feedback if we're loading a bunch of layers feedback.setProgress( 100 * i / float(len(context.layersToLoadOnCompletion()))) try: layer = QgsProcessingUtils.mapLayerFromString( l, context, typeHint=details.layerTypeHint) if layer is not None: set_layer_name(layer, details) '''If running a model, the execution will arrive here when an algorithm that is part of that model is executed. We check if its output is a final otuput of the model, and adapt the output name accordingly''' outputName = details.outputName expcontext = QgsExpressionContext() scope = QgsExpressionContextScope() expcontext.appendScope(scope) for out in alg.outputDefinitions(): if out.name() not in parameters: continue outValue = parameters[out.name()] if hasattr(outValue, "sink"): outValue = outValue.sink.valueAsString(expcontext)[0] else: outValue = str(outValue) if outValue == l: outputName = out.name() break style = None if outputName: style = RenderingStyles.getStyle(alg.id(), outputName) if style is None: if layer.type() == QgsMapLayerType.RasterLayer: style = ProcessingConfig.getSetting( ProcessingConfig.RASTER_STYLE) else: if layer.geometryType() == QgsWkbTypes.PointGeometry: style = ProcessingConfig.getSetting( ProcessingConfig.VECTOR_POINT_STYLE) elif layer.geometryType() == QgsWkbTypes.LineGeometry: style = ProcessingConfig.getSetting( ProcessingConfig.VECTOR_LINE_STYLE) else: style = ProcessingConfig.getSetting( ProcessingConfig.VECTOR_POLYGON_STYLE) if style: layer.loadNamedStyle(style) details.project.addMapLayer( context.temporaryLayerStore().takeMapLayer(layer)) if details.postProcessor(): details.postProcessor().postProcessLayer( layer, context, feedback) else: wrongLayers.append(str(l)) except Exception: QgsMessageLog.logMessage( QCoreApplication.translate( 'Postprocessing', "Error loading result layer:") + "\n" + traceback.format_exc(), 'Processing', Qgis.Critical) wrongLayers.append(str(l)) i += 1 feedback.setProgress(100) if wrongLayers: msg = QCoreApplication.translate( 'Postprocessing', "The following layers were not correctly generated.") msg += "<ul>" + "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += QCoreApplication.translate( 'Postprocessing', "You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm." ) feedback.reportError(msg) return len(wrongLayers) == 0
def ChloehandleAlgorithmResults(alg, context, feedback=None, showResults=True): # Overload feedback.setProgressText('ChloehandleAlgorithmResults') wrongLayers = [] if feedback is None: feedback = QgsProcessingFeedback() feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for l, details in context.layersToLoadOnCompletion().items(): # Overload QgsMessageLog.logMessage( l,'Processing', Qgis.Info) if feedback.isCanceled(): return False if len(context.layersToLoadOnCompletion()) > 2: # only show progress feedback if we're loading a bunch of layers feedback.setProgress(100 * i / float(len(context.layersToLoadOnCompletion()))) try: layer = QgsProcessingUtils.mapLayerFromString(l, context, typeHint=details.layerTypeHint) if layer is not None: #set_layer_name(layer, details) style = None if details.outputName: style = RenderingStyles.getStyle(alg.id(), details.outputName) if style is None: if layer.type() == QgsMapLayer.RasterLayer: style = ProcessingConfig.getSetting(ProcessingConfig.RASTER_STYLE) else: if layer.geometryType() == QgsWkbTypes.PointGeometry: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POINT_STYLE) elif layer.geometryType() == QgsWkbTypes.LineGeometry: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_LINE_STYLE) else: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POLYGON_STYLE) if style: layer.loadNamedStyle(style) details.project.addMapLayer(context.temporaryLayerStore().takeMapLayer(layer)) if details.postProcessor(): details.postProcessor().postProcessLayer(layer, context, feedback) else: wrongLayers.append(str(l)) except Exception: QgsMessageLog.logMessage(QCoreApplication.translate('Postprocessing', "Error loading result layer:") + "\n" + traceback.format_exc(), 'Processing', Qgis.Critical) wrongLayers.append(str(l)) i += 1 feedback.setProgress(100) if wrongLayers: msg = QCoreApplication.translate('Postprocessing', "The following layers were not correctly generated.") msg += "<ul>" + "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += QCoreApplication.translate('Postprocessing', "You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm.") feedback.reportError(msg) return len(wrongLayers) == 0
def handleAlgorithmResults(alg, context, feedback=None, showResults=True, parameters={}): wrongLayers = [] if feedback is None: feedback = QgsProcessingFeedback() feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for l, details in context.layersToLoadOnCompletion().items(): if feedback.isCanceled(): return False if len(context.layersToLoadOnCompletion()) > 2: # only show progress feedback if we're loading a bunch of layers feedback.setProgress(100 * i / float(len(context.layersToLoadOnCompletion()))) try: layer = QgsProcessingUtils.mapLayerFromString(l, context, typeHint=details.layerTypeHint) if layer is not None: set_layer_name(layer, details) '''If running a model, the execution will arrive here when an algorithm that is part of that model is executed. We check if its output is a final otuput of the model, and adapt the output name accordingly''' outputName = details.outputName expcontext = QgsExpressionContext() scope = QgsExpressionContextScope() expcontext.appendScope(scope) for out in alg.outputDefinitions(): if out.name() not in parameters: continue outValue = parameters[out.name()] if hasattr(outValue, "sink"): outValue = outValue.sink.valueAsString(expcontext)[0] else: outValue = str(outValue) if outValue == l: outputName = out.name() break style = None if outputName: style = RenderingStyles.getStyle(alg.id(), outputName) if style is None: if layer.type() == QgsMapLayer.RasterLayer: style = ProcessingConfig.getSetting(ProcessingConfig.RASTER_STYLE) else: if layer.geometryType() == QgsWkbTypes.PointGeometry: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POINT_STYLE) elif layer.geometryType() == QgsWkbTypes.LineGeometry: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_LINE_STYLE) else: style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POLYGON_STYLE) if style: layer.loadNamedStyle(style) details.project.addMapLayer(context.temporaryLayerStore().takeMapLayer(layer)) if details.postProcessor(): details.postProcessor().postProcessLayer(layer, context, feedback) else: wrongLayers.append(str(l)) except Exception: QgsMessageLog.logMessage(QCoreApplication.translate('Postprocessing', "Error loading result layer:") + "\n" + traceback.format_exc(), 'Processing', Qgis.Critical) wrongLayers.append(str(l)) i += 1 feedback.setProgress(100) if wrongLayers: msg = QCoreApplication.translate('Postprocessing', "The following layers were not correctly generated.") msg += "<ul>" + "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += QCoreApplication.translate('Postprocessing', "You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm.") feedback.reportError(msg) return len(wrongLayers) == 0
def handleAlgorithmResults(alg, context, feedback=None, showResults=True): wrongLayers = [] if feedback is None: feedback = QgsProcessingFeedback() feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for l, details in context.layersToLoadOnCompletion().items(): if feedback.isCanceled(): return False if len(context.layersToLoadOnCompletion()) > 2: # only show progress feedback if we're loading a bunch of layers feedback.setProgress(100 * i / float(len(context.layersToLoadOnCompletion()))) try: layer = QgsProcessingUtils.mapLayerFromString(l, context) if layer is not None: layer.setName(details.name) details.project.addMapLayer(context.temporaryLayerStore().takeMapLayer(layer)) else: name = details.name if ProcessingConfig.getSetting( ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(l) dataobjects.load(l, name, alg.crs, RenderingStyles.getStyle(alg.id(), l)) except Exception: QgsMessageLog.logMessage("Error loading result layer:\n" + traceback.format_exc(), 'Processing', QgsMessageLog.CRITICAL) #wrongLayers.append(out.description()) wrongLayers.append(l) # for out in alg.outputs: # feedback.setProgress(100 * i / float(len(alg.outputs))) # if out.flags() & QgsProcessingParameterDefinition.FlagHidden or not out.open: # continue # if isinstance(out, (OutputRaster, OutputVector, OutputTable)): # try: # layer = QgsProcessingUtils.mapLayerFromString(out.value, context) # if layer: # layer.setName(out.description) # QgsProject.instance().addMapLayer(context.temporaryLayerStore().takeMapLayer(layer)) # else: # if ProcessingConfig.getSetting( # ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): # name = os.path.basename(out.value) # else: # name = out.description # # isRaster = True if isinstance(out, OutputRaster) else False # dataobjects.load(out.value, name, alg.crs, # RenderingStyles.getStyle(alg.id(), out.name), # isRaster) # except Exception: # QgsMessageLog.logMessage("Error loading result layer:\n" + traceback.format_exc(), 'Processing', QgsMessageLog.CRITICAL) # wrongLayers.append(out.description) # elif isinstance(out, OutputHTML): # resultsList.addResult(alg.icon(), out.description, out.value) i += 1 QApplication.restoreOverrideCursor() if wrongLayers: msg = "The following layers were not correctly generated.<ul>" msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += "You can check the log messages to find more information about the execution of the algorithm" feedback.reportError(msg) return len(wrongLayers) == 0
def handleAlgorithmResults(alg, context, feedback=None, showResults=True): wrongLayers = [] if feedback is None: feedback = QgsProcessingFeedback() feedback.setProgressText( QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for l, details in context.layersToLoadOnCompletion().items(): if feedback.isCanceled(): return False if len(context.layersToLoadOnCompletion()) > 2: # only show progress feedback if we're loading a bunch of layers feedback.setProgress( 100 * i / float(len(context.layersToLoadOnCompletion()))) try: layer = QgsProcessingUtils.mapLayerFromString(l, context) if layer is not None: layer.setName(details.name) details.project.addMapLayer( context.temporaryLayerStore().takeMapLayer(layer)) else: name = details.name if ProcessingConfig.getSetting( ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(l) dataobjects.load(l, name, alg.crs, RenderingStyles.getStyle(alg.id(), l)) except Exception: QgsMessageLog.logMessage( "Error loading result layer:\n" + traceback.format_exc(), 'Processing', QgsMessageLog.CRITICAL) #wrongLayers.append(out.description()) wrongLayers.append(l) # for out in alg.outputs: # feedback.setProgress(100 * i / float(len(alg.outputs))) # if out.flags() & QgsProcessingParameterDefinition.FlagHidden or not out.open: # continue # if isinstance(out, (OutputRaster, OutputVector, OutputTable)): # try: # layer = QgsProcessingUtils.mapLayerFromString(out.value, context) # if layer: # layer.setName(out.description) # QgsProject.instance().addMapLayer(context.temporaryLayerStore().takeMapLayer(layer)) # else: # if ProcessingConfig.getSetting( # ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): # name = os.path.basename(out.value) # else: # name = out.description # # isRaster = True if isinstance(out, OutputRaster) else False # dataobjects.load(out.value, name, alg.crs, # RenderingStyles.getStyle(alg.id(), out.name), # isRaster) # except Exception: # QgsMessageLog.logMessage("Error loading result layer:\n" + traceback.format_exc(), 'Processing', QgsMessageLog.CRITICAL) # wrongLayers.append(out.description) # elif isinstance(out, OutputHTML): # resultsList.addResult(alg.icon(), out.description, out.value) i += 1 QApplication.restoreOverrideCursor() if wrongLayers: msg = "The following layers were not correctly generated.<ul>" msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += "You can check the log messages to find more information about the execution of the algorithm" feedback.reportError(msg) return len(wrongLayers) == 0