def preprocess(self, layer): ''' Preprocesses the layer with the corresponding preprocess hook and returns the path to the resulting layer. If no preprocessing is performed, it returns the input layer itself ''' if not processingOk: return layer if layer.type() == layer.RasterLayer: try: hookFile = str(QtCore.QSettings().value("/GeoServer/Settings/GeoServer/PreuploadRasterHook", "")) alg = self.getAlgorithmFromHookFile(hookFile) if (len(alg.parameters) == 1 and isinstance(alg.parameters[0], ParameterRaster) and len(alg.outputs) == 1 and isinstance(alg.outputs[0], OutputRaster)): alg.parameters[0].setValue(layer) if runalg(alg, SilentProgress()): return load(alg.outputs[0].value) return layer except: return layer elif layer.type() == layer.VectorLayer: try: hookFile = str(QtCore.QSettings().value("/GeoServer/Settings/GeoServer/PreuploadVectorHook", "")) alg = self.getAlgorithmFromHookFile(hookFile) if (len(alg.parameters) == 1 and isinstance(alg.parameters[0], ParameterVector) and len(alg.outputs) == 1 and isinstance(alg.outputs[0], OutputVector)): alg.parameters[0].setValue(layer) if runalg(alg, SilentProgress()): return load(alg.outputs[0].value) return layer except: QgsMessageLog.logMessage("Could not apply hook to layer upload. Wrong Hook", QgsMessageLog.WARNING) return layer
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, e: wrongLayers.append(out.description) elif isinstance(out, OutputHTML): ProcessingResults.addResult(out.description, out.value) htmlResults = True
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 preprocess(self, layer): ''' Preprocesses the layer with the corresponding preprocess hook and returns the path to the resulting layer. If no preprocessing is performed, it returns the input layer itself ''' if not processingOk: return layer if layer.type() == layer.RasterLayer: try: hookFile = str(QSettings().value("/OpenGeo/Settings/GeoServer/PreuploadRasterHook", "")) alg = self.getAlgorithmFromHookFile(hookFile) if (len(alg.parameters) == 1 and isinstance(alg.parameters[0], ParameterRaster) and len(alg.outputs) == 1 and isinstance(alg.outputs[0], OutputRaster)): alg.parameters[0].setValue(layer) if UnthreadedAlgorithmExecutor.runalg(alg, SilentProgress()): return load(alg.outputs[0].value) return layer except: return layer elif layer.type() == layer.VectorLayer: try: hookFile = str(QSettings().value("/OpenGeo/Settings/GeoServer/PreuploadVectorHook", "")) alg = self.getAlgorithmFromHookFile(hookFile) if (len(alg.parameters) == 1 and isinstance(alg.parameters[0], ParameterVector) and len(alg.outputs) == 1 and isinstance(alg.outputs[0], OutputVector)): alg.parameters[0].setValue(layer) if UnthreadedAlgorithmExecutor.runalg(alg, SilentProgress()): return load(alg.outputs[0].value) return layer except: return layer
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, OutputFile) and out.ext == 'csv': try: 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 processAlgorithm(self, parameters, context, feedback): filename = self.getParameterValue(self.INPUT) style = self.getParameterValue(self.STYLE) layer = QgsProcessingUtils.mapLayerFromString(filename, context, False) if layer is None: dataobjects.load(filename, os.path.basename(filename), style=style) else: layer.loadNamedStyle(style) context.addLayerToLoadOnCompletion(layer.id()) layer.triggerRepaint()
def processAlgorithm(self, progress): filename = self.getParameterValue(self.INPUT) style = self.getParameterValue(self.STYLE) layer = dataobjects.getObjectFromUri(filename, False) if layer is None: dataobjects.load(filename, os.path.basename(filename), style=style) self.getOutputFromName(self.OUTPUT).open = False else: layer.loadNamedStyle(style) iface.mapCanvas().refresh() layer.triggerRepaint()
def processAlgorithm(self, feedback): filename = self.getParameterValue(self.INPUT) style = self.getParameterValue(self.STYLE) layer = dataobjects.getObjectFromUri(filename, False) if layer is None: dataobjects.load(filename, os.path.basename(filename), style=style) self.getOutputFromName(self.OUTPUT).open = False else: layer.loadNamedStyle(style) iface.mapCanvas().refresh() layer.triggerRepaint()
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 processAlgorithm(self, progress): filename = self.getParameterValue(self.INPUT) style = self.getParameterValue(self.STYLE) dataobjects.resetLoadedLayers() layer = dataobjects.getObjectFromUri(filename, False) if layer is None: dataobjects.load(filename, os.path.basename(filename), style=style) else: layer.loadNamedStyle(style) layer.triggerRepaint() iface.legendInterface().refreshLayerSymbology(layer) self.setOutputValue(self.OUTPUT, filename)
def processAlgorithm(self, progress): filename = self.getParameterValue(self.INPUT) layer = dataobjects.getObjectFromUri(filename) style = self.getParameterValue(self.STYLE) layer = dataobjects.getObjectFromUri(filename, False) if layer is None: dataobjects.load(filename, os.path.basename(filename), style=style) self.getOutputFromName(self.OUTPUT).open = False else: layer.loadNamedStyle(style) iface.mapCanvas().refresh() iface.legendInterface().refreshLayerSymbology(layer)
def preprocess(self, layer): ''' Preprocesses the layer with the corresponding preprocess hook and returns the path to the resulting layer. If no preprocessing is performed, it returns the input layer itself ''' if not processingOk: return layer if layer.type() == layer.RasterLayer: try: hookFile = str(QtCore.QSettings().value( "/GeoServer/Settings/GeoServer/PreuploadRasterHook", "")) if hookFile: alg = self.getAlgorithmFromHookFile(hookFile) if (len(alg.parameters) == 1 and isinstance(alg.parameters[0], ParameterRaster) and len(alg.outputs) == 1 and isinstance(alg.outputs[0], OutputRaster)): alg.parameters[0].setValue(layer) if runalg(alg, SilentProgress()): return load(alg.outputs[0].value) return layer else: return layer except: QgsMessageLog.logMessage( "Could not apply hook to layer upload. Wrong Hook", level=QgsMessageLog.WARNING) return layer elif layer.type() == layer.VectorLayer: try: hookFile = str(QtCore.QSettings().value( "/GeoServer/Settings/GeoServer/PreuploadVectorHook", "")) if hookFile: alg = self.getAlgorithmFromHookFile(hookFile) if (len(alg.parameters) == 1 and isinstance(alg.parameters[0], ParameterVector) and len(alg.outputs) == 1 and isinstance(alg.outputs[0], OutputVector)): alg.parameters[0].setValue(layer) if runalg(alg, SilentProgress()): return load(alg.outputs[0].value) return layer else: return layer except: QgsMessageLog.logMessage( "Could not apply hook to layer upload. Wrong Hook", level=QgsMessageLog.WARNING) return layer
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 openProjectFromMapboxFile(mapboxFile): iface.newProject() layers = {} labels = [] with open(mapboxFile) as f: project = json.load(f) if "sprite" in project: sprites = os.path.join(os.path.dirname(mapboxFile), project["sprite"]) else: sprites = None for layer in project["layers"]: layerType = project["sources"][layer["source"]]["type"] if layerType.lower() == "geojson": source = project["sources"][layer["source"]]["data"] path = os.path.join(os.path.dirname(mapboxFile), source) if layer["id"].startswith("txt"): labels.append(layer) else: add = True if layer["source"] not in layers: add = False layers[layer["source"]] = dataobjects.load( path, layer["id"]) setLayerSymbologyFromMapboxStyle(layers[layer["source"]], layer, sprites, add) elif layerType.lower() == "raster": url = project["sources"][layer["source"]]["tiles"][0] url = url.replace("bbox={bbox-epsg-3857}", "") url = url.replace("&&", "&") wmsLayer = QgsRasterLayer(url, layer["id"], "wms") QgsMapLayerRegistry.instance().addMapLayer(wmsLayer) for labelLayer in labels: setLayerLabelingFromMapboxStyle(layers[labelLayer["source"]], labelLayer)
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 processAlgorithm(self, parameters, context, feedback): filename = self.getParameterValue(self.INPUT) layer = QgsProcessingUtils.mapLayerFromString(filename, context) style = self.getParameterValue(self.STYLE) if layer is None: dataobjects.load(filename, os.path.basename(filename), style=style) else: with open(style) as f: xml = "".join(f.readlines()) d = QDomDocument() d.setContent(xml) n = d.firstChild() layer.readSymbology(n, '') context.addLayerToLoadOnCompletion(self.getOutputFromName(self.OUTPUT).value) self.setOutputValue(self.OUTPUT, filename) layer.triggerRepaint()
def processAlgorithm(self, progress): filename = self.getParameterValue(self.INPUT) layer = dataobjects.getObjectFromUri(filename) style = self.getParameterValue(self.STYLE) if layer is None: dataobjects.load(filename, os.path.basename(filename), style=style) self.getOutputFromName(self.OUTPUT).open = False else: with open(style) as f: xml = "".join(f.readlines()) d = QDomDocument() d.setContent(xml) n = d.firstChild() layer.readSymbology(n, '') self.setOutputValue(self.OUTPUT, filename) iface.mapCanvas().refresh()
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.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): resultsList.addResult(alg.getIcon(), 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, 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 preprocess(self, layer): ''' Preprocesses the layer with the corresponding preprocess hook and returns the path to the resulting layer. If no preprocessing is performed, it returns the input layer itself ''' if not processingOk: return layer if layer.type() == layer.RasterLayer: try: hookFile = str(QSettings().value( "/OpenGeo/Settings/GeoServer/PreuploadRasterHook", "")) alg = self.getAlgorithmFromHookFile(hookFile) if (len(alg.parameters) == 1 and isinstance(alg.parameters[0], ParameterRaster) and len(alg.outputs) == 1 and isinstance(alg.outputs[0], OutputRaster)): alg.parameters[0].setValue(layer) if UnthreadedAlgorithmExecutor.runalg( alg, SilentProgress()): return load(alg.outputs[0].value) return layer except: return layer elif layer.type() == layer.VectorLayer: try: hookFile = str(QSettings().value( "/OpenGeo/Settings/GeoServer/PreuploadVectorHook", "")) alg = self.getAlgorithmFromHookFile(hookFile) if (len(alg.parameters) == 1 and isinstance(alg.parameters[0], ParameterVector) and len(alg.outputs) == 1 and isinstance(alg.outputs[0], OutputVector)): alg.parameters[0].setValue(layer) if UnthreadedAlgorithmExecutor.runalg( alg, SilentProgress()): return load(alg.outputs[0].value) return layer except: return layer
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
#Definition of inputs and outputs #================================== ##Landsat Indices=name ##Landsat Tools=group ##ParameterRaster|input|Input Reflectance Stack|False ##OutputDirectory|outputDirectory|Folder to save the stack of Indices #OutputRaster|output|Name for Index Stack # Call the function for Landsat index calculation #================================== import os import sys here = os.path.dirname(scriptDescriptionFile) if here not in sys.path: sys.path.append(here) import landsat_indices from processing.tools import dataobjects print 'Starting index calculation...' out_file = landsat_indices.landsat_indices(input, outputDirectory) dataobjects.load(out_file, isRaster=True) print 'Finished writing to disk...'
# Read watermask as nparray watermask_opt, geotrans, proj = rsu.raster2array(os.path.join( path_watermasks_opt, wm_file), AOI_extent=extent_AOI) # Perform fusion fused_watermask = fuse_watermasks(watermask_opt, watermask_opt_max, watermask_sar) # Write to file path_output_file = os.path.join(path_output_watermasks, wm_file[:-4] + "_sar.tif") res = rsu.array2raster(fused_watermask, geotrans, proj, path_output_file, gdal.GDT_Float32, 255) if res != True: if not DEBUG: raise GeoAlgorithmExecutionException(res) else: print(res) sys.exit(1) else: if not DEBUG: try: dataobjects.load(path_output_file, os.path.basename(path_output_file), isRaster=True) except: dataobjects.load(path_output_file, os.path.basename(path_output_file))
def fmasklandsat(instance, parameters, context, feedback, inputs): """ landsat indicies """ def standard_index(band1, band2): """Function for standard index calculation""" with np.errstate(divide='warn'): idx = (band1 - band2) / (band1 + band2) * 10000 return idx def extract_band(stack, bnd_num): """Function to extract single bands from stack; stack = input stack, bnd_num = the band number to extract""" b = stack.GetRasterBand(bnd_num) band = b.ReadAsArray().astype(np.float32) return band def calc_index(stack, bnd_num1, bnd_num2): """ Function to calculate an index; stack = input stack, bnd_numx = the band number in the stack""" band1 = extract_band(stack, bnd_num1) band2 = extract_band(stack, bnd_num2) any_index = standard_index(band1, band2) return any_index def landsat_indices(inRst, outDir): """ Main function for calculating a selction of Sentinel 2 Spectral indices """ stk = gdal.Open(inRst) # get raster specs xsize = stk.RasterXSize ysize = stk.RasterYSize proj = stk.GetProjection() geotransform = stk.GetGeoTransform() # calculate indices: these indices were chosen based on variable importance ranking from Random Forest classification # calc ndvi ndvi = calc_index(stk, 4, 3) # calc ndvi using 8a ndmi = calc_index(stk, 4, 5) # calc red edge ndi b8a_b5 nbr = calc_index(stk, 4, 6) # calc red edge ndi b8a_b6 nbr2 = calc_index(stk, 5, 6) # Stack and write to disk # get base filename and combine with outpath sName = os.path.splitext(os.path.basename(inRst))[-2] stkPath = os.path.join(outDir, sName + "_indices.tif") drv = gdal.GetDriverByName("GTiff") outTif = drv.Create(stkPath, xsize, ysize, 4, gdal.GDT_Int16) outTif.SetProjection(proj) outTif.SetGeoTransform(geotransform) outTif.GetRasterBand(1).WriteArray(ndvi) outTif.GetRasterBand(2).WriteArray(ndmi) outTif.GetRasterBand(3).WriteArray(nbr) outTif.GetRasterBand(4).WriteArray(nbr2) outTif = None return stkPath feedback.pushConsoleInfo("Starting index calculation...") out_file = landsat_indices( instance.parameterAsString(parameters, "input", context), instance.parameterAsString(parameters, "outputDirectory", context), ) dataobjects.load(out_file, isRaster=True) feedback.pushConsoleInfo("Finished writing to disk...")
import os import sys from processing.tools import dataobjects here = os.path.dirname(scriptDescriptionFile) if here not in sys.path: sys.path.append(here) import download_swi from logs import set_qgis_logger set_qgis_logger(progress) product = ['SWI', 'SWI10'][product_param] outfiles = download_swi.download(outdir=outdir, progress=progress, product=product, username=username, password=password, startdate=startdate, enddate=enddate, extent=extent, delete_downloaded=delete_downloaded) if load_to_canvas: for outfile in outfiles: dataobjects.load(outfile, os.path.basename(outfile), style=None) # Release file pointers held by QGIS Processing dataobjects.resetLoadedLayers()
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
-9999) if res != True: if not DEBUG: raise GeoAlgorithmExecutionException(res) else: print(res) sys.exit(1) # Legend file outfile_name = os.path.join(path_output_classification, file_name + ".qml") copyfile(os.path.join(qmlDir, "water_wet_frequency.qml"), outfile_name) # Load water frequency to canvas if not DEBUG: try: dataobjects.load(dest, os.path.basename(dest), isRaster=True) except: dataobjects.load(dest, os.path.basename(dest)) # Maximum water extent file_name = title + "_maximum_water_extent" dest = os.path.join(path_output_classification, file_name + ".tif") res = rsu.array2raster(max_water_extent, geotrans, proj, dest, gdal.GDT_Byte, 255) if res != True: if not DEBUG: raise GeoAlgorithmExecutionException(res) else: print(res) sys.exit(1)
if not os.path.exists(path_output_extents): os.mkdir(path_output_extents) extent_name = "userDefinedExtent_" + dt.datetime.today().strftime("%Y%m%d-h%Hm%M") # Write to shapefile res = extent_AOI.save_as_shp(path_output_extents, extent_name) if res != True: if not DEBUG: raise GeoAlgorithmExecutionException(res) else: print(res) sys.exit(1) # Load shapefile with user defined extent to canvas if not DEBUG: print(os.path.join(path_output_extents, extent_name + ".shp")) dataobjects.load(os.path.join(path_output_extents, extent_name+".shp"), extent_name) else: # 3. minimum joint extent of all scenes exBands = [sce.files[-1] for sce in scenes] # use swir2 bands as reference for geotransformation extent_AOI = rsu.getJointExtent(exBands) if extent_AOI is None: if not DEBUG: progress.setText("Invalid input data: Extent of AOI is invalid. " "Check if all scenes overlap each other.") else: print("Invalid input data: Extent of AOI is invalid. Check if all scenes overlap each other.") sys.exit(1) # Check AOI size ------------------------------------------------------------------------------------------------- AOI_size = extent_AOI.ncol * extent_AOI.nrow
def untarimagery(instance, parameters, context, feedback, inputs): """ untarimagery """ processing_path = instance.parameterAsString(parameters, "processing_path", context) infile = instance.parameterAsString(parameters, "infile", context) def untar(infile, processing_path): tar = tarfile.open(infile) tarfiles = tar.getnames() feedback.pushConsoleInfo("Unpacking data:") feedback.pushConsoleInfo(str(tarfiles)) tar.extractall(processing_path) tar.close() return tarfiles feedback.pushConsoleInfo("Starting untar") tarfiles = untar(infile, processing_path) feedback.pushConsoleInfo("Untar finished...") lbase = os.path.basename(infile) ltype = lbase[:4] if ltype in "LC08": bandlist = [ "band2", "band3", "band4", "band5", "band6", "band7", "B2", "B3", "B4", "B5", "B6", "B7", ] elif ltype in ["LE07", "LT05"]: bandlist = [ "band1", "band2", "band3", "band4", "band5", "band7", "B1", "B2", "B3", "B4", "B5", "B7", ] else: feedback.pushConsoleInfo("Cannot detect if it is Landsat 5, 7, or 8. Stopping") exit() # paths to the .tif files fls = [os.path.join(processing_path, f) for f in tarfiles] feedback.pushConsoleInfo(str(fls)) bandfile_list = [] type(bandfile_list) for item in bandlist: for cont in fls: extension = os.path.splitext(cont)[1] if extension == ".tif": if ( cont.replace(extension, "").split("_")[ (len(cont.replace(extension, "").split("_")) - 1) ] == item ): bandfile_list.append(cont) for layer in bandfile_list: dataobjects.load(layer, isRaster=True) return { "outfolder": instance.parameterAsString(parameters, "processing_path", context) }
def loadTestData(): dataobjects.load(points(), "points"); dataobjects.load(points2(), "points2"); dataobjects.load(polygons(), "polygons"); dataobjects.load(polygons2(), "polygons2"); dataobjects.load(polygonsGeoJson(), "polygonsGeoJson"); dataobjects.load(lines(), "lines"); dataobjects.load(raster(), "raster"); dataobjects.load(table(), "table"); dataobjects.load(union(), "union");
bands.sort(key=_sortfunc) return bands set_progress_logger(name='s2_export', progress=progress) kwargs = {} bands_param = bands_param.replace(' ', '') if bands_param: kwargs['bands'] = bands_param.split(',') else: kwargs['bands'] = flags_to_bandlist( B1=B1, B2=B2, B3=B3, B4=B4, B5=B5, B6=B6, B7=B7, B8=B8, B8A=B8A, B9=B9, B10=B10, B11=B11, B12=B12, allVISNIR=allVISNIR) kwargs['tgt_res'] = ["10m", "20m", "60m"][out_res] if granules.strip(): kwargs['granules'] = granules.split(',') kwargs['infile'] = infile kwargs['outdir'] = outdir outfiles = s2_export.export(**kwargs) for outfile in outfiles: dataobjects.load(outfile)
def loadTestData(): dataobjects.load(points(), 'points') dataobjects.load(points2(), 'points2') dataobjects.load(polygons(), 'polygons') dataobjects.load(polygons2(), 'polygons2') dataobjects.load(polygonsGeoJson(), 'polygonsGeoJson') dataobjects.load(lines(), 'lines') dataobjects.load(raster(), 'raster') dataobjects.load(table(), 'table') dataobjects.load(union(), 'union')
print("%s not within AOI." % sce.ID) if not debug: progress.setText("%s not within AOI." % sce.ID) continue # Check whether scene has enough valid pixels within AOI #sce.calcCloudCoverage() if sce.cloudy > maxCloudCover: print "Cloud coverage too high. Scene is skipped." if not debug: progress.setText("Cloud coverage too high. Scene is skipped.") continue scenesWithinExtent.append(sce) sce.createVRT(path_vrt) dataobjects.load(sce.VRTfile, os.path.basename(sce.VRTfile)) scenes = scenesWithinExtent if len(scenes) == 0: if not debug: raise GeoAlgorithmExecutionException( "Invalid input data: No %s scenes found that match the selection criteria." % sensor) sys.exit(1) else: print "Invalid input data: No %s scenes found that match the selection criteria." % sensor if not debug: progress.setPercentage(10) progress.setText("\n%s %s scenes selected." % (len(scenes), sensor))
TWI_binary = np.where(TWI > 0.4, 0, 1) #Sieve mask TWI_binary = rsu.removeNoise(TWI_binary, 2) TWI_binary = rsu.binaryBuffer_negative(TWI_binary, 3) #Export to file x = rsu.array2raster(TWI_binary, geotrans, proj, outfile_binary, gdal.GDT_Byte, 255) if x == False: raise RuntimeError("Exporting TWI mask failed.") del TWI_binary, TWI try: dataobjects.load(outfile_binary, os.path.basename(outfile_binary), isRaster=True) except: dataobjects.load(outfile_binary, os.path.basename(outfile_binary)) try: dataobjects.load(path_TWI_filtered, os.path.basename(path_TWI_filtered), isRaster=True) except: dataobjects.load(path_TWI_filtered, os.path.basename(path_TWI_filtered)) if not DEBUG: progress.setText("TWI binary mask done.")
with np.errstate(invalid="ignore"): mainRoutine_angles(cmdargs_angles) progress.setConsoleInfo("Done.") cmdargs = Namespace( toa=tempvrt, anglesfile=anglesfile, output=output, verbose=verbose, tempdir=tempdir, keepintermediates=False, mincloudsize=mincloudsize, cloudbufferdistance=cloudbufferdistance, shadowbufferdistance=shadowbufferdistance, cloudprobthreshold=cloudprobthreshold, nirsnowthreshold=nirsnowthreshold, greensnowthreshold=greensnowthreshold, ) progress.setConsoleInfo("Running FMask (this may take a while) ...") with redirect_print(progress): mainRoutine(cmdargs) finally: try: shutil.rmtree(tempdir) except OSError: pass progress.setConsoleInfo("Done.") dataobjects.load(output, os.path.basename(output))
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
# Export maps to file =============================================================================== # WATER FREQUENCY file_name = "WI_total_NUMOB" dest = os.path.join(pathOUT_class, file_name + '.tif') rsu.array2raster(validObs, geotrans, proj, dest, gdal.GDT_Byte, 255) file_name = "WI_water_frequency" dest = os.path.join(pathOUT_class, file_name + '.tif') rsu.array2raster(waterFreq_all, geotrans, proj, dest, gdal.GDT_Float32, -9999) # qml file outfile_name = os.path.join(pathOUT_class, file_name + '.qml') copyfile(os.path.join(qmlDir, "water_wet_frequency.qml"), outfile_name) dataobjects.load(dest, os.path.basename(dest)) file_name = "WI_dry_frequency" dest = os.path.join(pathOUT_class, file_name + '.tif') rsu.array2raster(dryFreq_all, geotrans, proj, dest, gdal.GDT_Float32, -9999) # qml file outfile_name = os.path.join(pathOUT_class, file_name + '.qml') copyfile(os.path.join(qmlDir, "water_wet_frequency.qml"), outfile_name) # Wet frequency file_name = "WI_wetness_frequency" dest = os.path.join(pathOUT_class, file_name + '.tif') rsu.array2raster(wetFreq_all, geotrans, proj, dest, gdal.GDT_Float32, -9999) # qml file
outfile=refimg, imagename='ref', landsatkey=landsatkey) progress.setConsoleInfo('Done.') # create angles file anglesfile = anglesfile or os.path.join(tempdir, 'angles.img') if not os.path.isfile(anglesfile): progress.setConsoleInfo('Creating angles file ...') with np.errstate(invalid='ignore'): mainRoutine_angles( Namespace(mtl=mtl, templateimg=refimg, outfile=anglesfile)) progress.setConsoleInfo('Done.') progress.setConsoleInfo('Creating TOA image ...') cmdargs = Namespace(infile=refimg, mtl=mtl, anglesfile=anglesfile, output=outfile) with np.errstate(invalid='ignore'): mainRoutine(cmdargs) progress.setConsoleInfo('Done.') finally: try: shutil.rmtree(tempdir) except OSError: pass dataobjects.load(outfile, os.path.basename(outfile))
water_mask_sar) # Write to file path_output_file = os.path.join(path_output_masks, wm_file[:-4] + "_sar.tif") res = rsu.array2raster(fused_watermask, geotrans, proj, path_output_file, gdal.GDT_Float32, 255) if res != True: if not DEBUG: raise GeoAlgorithmExecutionException(res) else: print(res) sys.exit(1) else: if not DEBUG: dataobjects.load(path_output_file, os.path.basename(path_output_file)) if wetness_fusion: # Dense vegetation wetness mask wet_files_soil = [ os.path.join(path_masks_opt, f) for f in fnmatch.filter( os.listdir(path_masks_opt), "*_d" + wm_file[idx_date:idx_date + 8] + "*_soil_mask.tif", ) ] # Dense vegetation wetness mask wet_files_sveg = [ os.path.join(path_masks_opt, f) for f in fnmatch.filter( os.listdir(path_masks_opt),