示例#1
0
def _set_output_layer_style(layerName: str, layer: QgsMapLayer,
                            alg: QgsProcessingAlgorithm,
                            details: 'QgsProcessingContext::LayerDetails',
                            context: QgsProcessingContext, parameters) -> None:
    """ Set layer style 

        Original code is from python/plugins/processing/gui/Postprocessing.py
    """
    '''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
    if parameters:
        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 == layerName:
                outputName = out.name()
                break

    style = None
    if outputName:
        # If a style with the same name as the outputName exists
        # in workdir then use it
        style = os.path.join(context.workdir, outputName + '.qml')
        if not os.path.exists(style):
            style = RenderingStyles.getStyle(alg.id(), outputName)
        LOGGER.debug("Getting style for %s: %s <%s>", alg.id(), outputName,
                     style)

    # Get defaults styles
    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:
        LOGGER.debug("Adding style '%s' to layer %s (outputName %s)", style,
                     details.name, outputName)
        layer.loadNamedStyle(style)

    LOGGER.debug("Layer name set to %s <details name: %s>", layer.name(),
                 details.name)
示例#2
0
# Import and initialize Processing framework
sys.path.append('/usr/share/qgis/python/plugins')

import warnings
# Silence the error "UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.", see https://github.com/matplotlib/matplotlib/issues/5836#issuecomment-179592427
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    from processing.core.Processing import Processing
    from processing.core.Processing import ProcessingConfig

Processing.initialize()
import processing

# Manually set the OTB path, see https://github.com/qgis/QGIS/blob/master/python/plugins/processing/core/ProcessingConfig.py and https://github.com/qgis/QGIS/blob/master/python/plugins/processing/algs/otb/OTBUtils.py
ProcessingConfig.setSettingValue("OTB_FOLDER", os.getenv('OTB_FOLDER', ''))
ProcessingConfig.setSettingValue("OTB_LIB_FOLDER",
                                 os.getenv('OTB_LIB_FOLDER', ''))

print("###### Algorithm help and options:")
processing.alghelp("modeler:docker")
processing.algoptions("modeler:docker")

# Helper function for creating output directory
import errno


def make_sure_path_exists(path):
    try:
        os.makedirs(path)
    except OSError as exception:
示例#3
0
print(QgsApplication.showSettings())

# Import and initialize Processing framework
sys.path.append('/usr/share/qgis/python/plugins')

import warnings; # Silence the error "UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.", see https://github.com/matplotlib/matplotlib/issues/5836#issuecomment-179592427
with warnings.catch_warnings():
    warnings.simplefilter("ignore"); 
    from processing.core.Processing import Processing
    from processing.core.Processing import ProcessingConfig

Processing.initialize()
import processing

# Manually set the OTB path, see https://github.com/qgis/QGIS/blob/master/python/plugins/processing/core/ProcessingConfig.py and https://github.com/qgis/QGIS/blob/master/python/plugins/processing/algs/otb/OTBUtils.py
ProcessingConfig.setSettingValue("OTB_FOLDER", os.getenv('OTB_FOLDER', ''))
ProcessingConfig.setSettingValue("OTB_LIB_FOLDER", os.getenv('OTB_LIB_FOLDER', ''))

print("###### Algorithm help and options:")
processing.alghelp("modeler:docker")
processing.algoptions("modeler:docker")

# Helper function for creating output directory
import errno
def make_sure_path_exists(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise