示例#1
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, e:
                wrongLayers.append(out)
        elif isinstance(out, OutputHTML):
            ProcessingResults.addResult(out.description, out.value)
            htmlResults = True
示例#2
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(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
示例#3
0
def runalg(alg, progress=None):
    """Executes a given algorithm, showing its progress in the
    progress object passed along.

    Return true if everything went OK, false if the algorithm
    could not be completed.
    """
    if progress is None:
        progress = SilentProgress()
    try:
        alg.execute(progress)
        return True
    except GeoAlgorithmExecutionException as e:
        ProcessingLog.addToLog(sys.exc_info()[0], ProcessingLog.LOG_ERROR)
        progress.error(e.msg)
        return False
示例#4
0
def runalg(alg, progress=None):
    """Executes a given algorithm, showing its progress in the
    progress object passed along.

    Return true if everything went OK, false if the algorithm
    could not be completed.
    """
    if progress is None:
        progress = SilentProgress()
    try:
        alg.execute(progress)
        return True
    except GeoAlgorithmExecutionException as e:
        ProcessingLog.addToLog(sys.exc_info()[0], ProcessingLog.LOG_ERROR)
        progress.error(e.msg)
        return False
示例#5
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
示例#6
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, e:
                wrongLayers.append(out.description)
        elif isinstance(out, OutputHTML):
            ProcessingResults.addResult(out.description, out.value)
            htmlResults = True
示例#7
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 AlgorithmExecutor.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 AlgorithmExecutor.runalg(alg, SilentProgress()):
                        return load(alg.outputs[0].value)
                    return layer
            except:
                return layer
示例#8
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
示例#9
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
示例#10
0
文件: OTBUtils.py 项目: ljx0305/QGIS
def getInstalledVersion(runOtb=False):
    global _installedVersion
    global _installedVersionFound

    if _installedVersionFound and not runOtb:
        return _installedVersion

    if otbPath() is None or otbLibPath() is None:
        _installedVersionFound = False
        return None
    commands = [os.path.join(otbPath(), "otbcli_Smoothing")]
    progress = SilentProgress()
    out = executeOtb(commands, progress, False)
    for line in out:
        if "version" in line:
            _installedVersionFound = True
            _installedVersion = line.split("version")[-1].strip()
            break
    return _installedVersion