Exemplo n.º 1
0
def parse_literal_input(param: QgsProcessingParameterDefinition,
                        kwargs) -> LiteralInput:
    """
    """
    typ = param.type()

    if typ == 'string':
        kwargs['data_type'] = 'string'
    elif typ == 'boolean':
        kwargs['data_type'] = 'boolean'
    elif typ == 'enum':
        options = param.options()
        kwargs['data_type'] = 'string'
        kwargs['allowed_values'] = options
        kwargs['max_occurs'] = len(options) if param.allowMultiple() else 1
        default_value = param.defaultValue()
        if default_value is not None:
            # XXX Values for processing enum are indices
            if isinstance(default_value, int):
                kwargs['default'] = options[default_value]
            elif isinstance(default_value, list):
                kwargs['default'] = options[default_value[0]]
            else:
                raise InvalidParameterValue('Unsupported default value: %s' %
                                            default_value)
    elif typ == 'number':
        kwargs['data_type'] = {
            QgsProcessingParameterNumber.Double: 'float',
            QgsProcessingParameterNumber.Integer: 'integer'
        }[param.dataType()]
        kwargs['allowed_values'] = [(param.minimum(), param.maximum())]
    elif typ == 'field':
        kwargs['data_type'] = 'string'
        kwargs['metadata'].append(
            Metadata('processing:parentLayerParameterName',
                     param.parentLayerParameterName()))
        kwargs['metadata'].append(
            Metadata(
                'processing:dataType', {
                    QgsProcessingParameterField.Any: 'Any',
                    QgsProcessingParameterField.Numeric: 'Numeric',
                    QgsProcessingParameterField.String: 'String',
                    QgsProcessingParameterField.DateTime: 'DateTime'
                }[param.dataType()]))
    elif typ == 'crs':
        kwargs['data_type'] = 'string'
    elif typ == 'band':
        kwargs['data_type'] = 'nonNegativeInteger'
    else:
        return None

    return LiteralInput(**kwargs)
Exemplo n.º 2
0
def get_processing_value(param: QgsProcessingParameterDefinition,
                         inp: WPSInput, context: ProcessingContext) -> Any:
    """ Return processing value from wps inputs

        Processes other inputs than layers
    """
    typ = param.type()
    if typ == 'enum':
        # XXX Processing wants the index of the value in option list
        if param.allowMultiple() and len(inp) > 1:
            opts = param.options()
            value = [opts.index(d.data) for d in inp]
        else:
            value = param.options().index(inp[0].data)
    elif len(inp):
        # Return raw value
        value = inp[0].data
    else:
        # Return undefined value
        if not _is_optional(param):
            LOGGER.warning("Required input %s has no value", param.name())
        value = None

    return value
Exemplo n.º 3
0
def parse_literal_input(param: QgsProcessingParameterDefinition,
                        kwargs) -> LiteralInput:
    """ Convert processing input to Literal Input 
    """
    typ = param.type()

    if typ == 'string':
        kwargs['data_type'] = 'string'
    elif typ == 'boolean':
        kwargs['data_type'] = 'boolean'
    elif typ == 'enum':
        options = param.options()
        kwargs['data_type'] = 'string'
        kwargs['allowed_values'] = options
        kwargs['max_occurs'] = len(options) if param.allowMultiple() else 1
        default_value = param.defaultValue()
        if default_value is not None:
            # XXX Values for processing enum are indices
            if isinstance(default_value, list):
                default_value = default_value[0]
            if not isinstance(default_value, int):
                raise InvalidParameterValue(
                    'Unsupported default value for parameter %s: %s' %
                    (param.name(), default_value))
            if default_value < 0 or default_value >= len(options):
                LOGGER.error(
                    "Out of range default value for enum parameter %s: %s",
                    param.name(), default_value)
                default_value = 0

            kwargs['default'] = options[default_value]

    elif typ == 'number':
        kwargs['data_type'] = _number_data_type(param)
        kwargs['allowed_values'] = [(param.minimum(), param.maximum())]
    elif typ == 'distance':
        kwargs['data_type'] = 'length'
        kwargs['allowed_values'] = [(param.minimum(), param.maximum())]
        kwargs['metadata'].extend((
            Metadata('processing:parentParameterName',
                     param.parentParameterName()),
            Metadata('processing:defaultUnit',
                     QgsUnitTypes.toString(param.defaultUnit())),
        ))
    elif typ == 'scale':
        kwargs['data_type'] = 'scale'
        kwargs['allowed_values'] = [(param.minimum(), param.maximum())]
    elif typ == 'duration':
        # XXX OGC duration is defined as time dataType
        kwargs['data_type'] = 'time'
        kwargs['allowed_values'] = [(param.minimum(), param.maximum())]
        kwargs['metadata'].append(
            Metadata('processing:defaultUnit',
                     QgsUnitTypes.toString(param.defaultUnit())), )
    elif typ == 'field':
        kwargs['data_type'] = 'string'
        kwargs['metadata'].append(
            Metadata('processing:parentLayerParameterName',
                     param.parentLayerParameterName()))
        kwargs['metadata'].append(
            Metadata(
                'processing:dataType', {
                    QgsProcessingParameterField.Any: 'Any',
                    QgsProcessingParameterField.Numeric: 'Numeric',
                    QgsProcessingParameterField.String: 'String',
                    QgsProcessingParameterField.DateTime: 'DateTime',
                }[param.dataType()]))
    elif typ == 'band':
        kwargs['data_type'] = 'nonNegativeInteger'
    else:
        return None

    return LiteralInput(**kwargs)