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)
def parse_input_definition(param: QgsProcessingParameterDefinition, alg: QgsProcessingAlgorithm = None, context: MapContext = None) -> WPSInput: """ Create WPS input from QgsProcessingParamDefinition the description is used in QGIS UI as the title in WPS. see https://qgis.org/api/qgsprocessingparameters_8h_source.html#l01312 """ kwargs = { 'identifier': param.name(), 'title': param.description() or param.name().replace('_', ' '), 'abstract': param.help(), 'metadata': [ Metadata('processing:type', param.type()), ] } # Handle defaultValue # XXX In some case QVariant are # not converted to python object (SIP bug ?) # Problem stated in getting QgsProcessingParameterFeatureSource # from processing.core.parameters.getParameterFromString defaultValue = param.defaultValue() if isinstance(defaultValue, QVariant): defaultValue = None if defaultValue.isNull() else defaultValue.value() kwargs['default'] = defaultValue # Check for optional flags if _is_optional(param): kwargs['min_occurs'] = 0 inp = parse_literal_input(param,kwargs) \ or layersio.parse_input_definition(param, kwargs, context) \ or geometryio.parse_input_definition(param, kwargs, context) \ or filesio.parse_input_definition(param, kwargs) \ or datetimeio.parse_input_definition(param, kwargs) if inp is None: raise ProcessingInputTypeNotSupported("%s:'%s'" % (type(param), param.type())) parse_metadata(param, kwargs) return inp
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)