示例#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
def parse_input_definitions(
        alg: QgsProcessingAlgorithm,
        context: MapContext) -> Generator[WPSInput, None, None]:
    """ Parse algorithm inputs definitions 
    """
    for param in alg.parameterDefinitions():
        try:
            if not _is_hidden(param):
                yield parse_input_definition(param, alg, context=context)
            else:
                LOGGER.info("%s: dropping hidden param: %s", alg.id(),
                            param.name())
        except ProcessingTypeParseError as e:
            LOGGER.error("%s: unsupported param %s", alg.id(), e)
示例#3
0
def parse_output_definitions(
        alg: QgsProcessingAlgorithm,
        context: MapContext) -> Generator[WPSOutput, None, None]:
    """ Parse algorithm inputs definitions 
    """
    for param in alg.outputDefinitions():
        try:
            yield parse_output_definition(param, alg, context=context)
        except ProcessingTypeParseError as e:
            LOGGER.error("%s: unsupported output param %s", alg.id(), e)