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_layer_input(param: QgsProcessingParameterDefinition, kwargs, context: MapContext = None) -> LiteralInput: """ Layers input are passed as layer name We treat layer destination the same as input since they refer to layers ids in qgisProject """ if isinstance(param, INPUT_LAYER_TYPES): kwargs['data_type'] = 'string' parse_allowed_layers(param, kwargs, context) elif isinstance(param, QgsProcessingParameterRasterDestination): kwargs['data_type'] = 'string' kwargs['metadata'].append( Metadata('processing:extension', param.defaultFileExtension())) elif isinstance(param, (QgsProcessingParameterVectorDestination, QgsProcessingParameterFeatureSink)): kwargs['data_type'] = 'string' kwargs['metadata'].append( Metadata('processing:dataType', str(param.dataType()))) kwargs['metadata'].append( Metadata('processing:extension', param.defaultFileExtension())) else: return None return LiteralInput(**kwargs)
def parse_input_definition(param: QgsProcessingParameterDefinition, kwargs, context: MapContext = None) -> LiteralInput: """ Layers input may be passed in various forms: - For input layers and if a context is given: it will be a list of available layers from the source project as literal string. - If there is no context, the input will be defined as a complex input for valid gis data. We treat layer destination the same as input since they refer to layers ids in qgisProject """ if isinstance(param, INPUT_LAYER_TYPES): typ = param.type() if typ == 'multilayer': num_inputs = param.minimumNumberInputs() kwargs['min_occurs'] = num_inputs if num_inputs >= 1 else 0 kwargs['max_occurs'] = kwargs['min_occurs'] # Set metadata for geometry type datatypes = get_layers_type(param, kwargs) kwargs['data_type'] = 'string' if context is not None: # Retrieve the list of layers # Inputs will be a list of strings from the source # project get_layers_from_context(kwargs, context, datatypes) # Set max occurs accordingly if typ == 'multilayer': kwargs['max_occurs'] = len(kwargs['allowed_values']) return LiteralInput(**kwargs) elif isinstance(param, DESTINATION_LAYER_TYPES): # Since QgsProcessingOutputLayerDefinition may # be defined as default value, get extension # and layer name from it extension, defval = get_default_destination_values( param, kwargs['default']) kwargs['default'] = defval kwargs['data_type'] = 'string' # Used to retrieve extension when handling wps response kwargs['metadata'].append(Metadata('processing:extension', extension)) if isinstance(param, DESTINATION_VECTOR_LAYER_TYPES): kwargs['metadata'].append( Metadata('processing:dataType', str(param.dataType()))) return LiteralInput(**kwargs) else: return None
def get_processing_value(param: QgsProcessingParameterDefinition, inp: LiteralInput, context: ProcessingContext) -> Any: """ Return processing value from wps inputs Processes other inputs than layers """ typ = param.type() if typ != 'datetime': return None value = inp[0].data # Convert python datetime to appropriate object dtype = param.dataType() if dtype == QgsProcessingParameterDateTime.Date: value = QDate(value) elif dtype == QgsProcessingParameterDateTime.Time: value = QTime(value) else: value = QDateTime(value) return value
def parse_input_definition(param: QgsProcessingParameterDefinition, kwargs) -> LiteralInput: """ Convert processing input to File Input """ typ = param.type() if typ != 'datetime': return None def to_value(qdt): return qdt.toPyDateTime() if qdt.isValid() else None defval = kwargs['default'] dtype = param.dataType() if dtype == QgsProcessingParameterDateTime.Date: kwargs['data_type'] = 'date' maxval = (to_value(param.maximum()) or datetime.max).date() minval = (to_value(param.minimum()) or datetime.min).date() if defval: defval = QDateTime(defval).toPyDateTime().date() elif dtype == QgsProcessingParameterDateTime.Time: kwargs['data_type'] = 'time' maxval = (to_value(param.maximum()) or datetime.max).time() minval = (to_value(param.minimum()) or datetime.min).time() if defval: defval = convert_time(defval.toString(Qt.ISODate)) else: kwargs['data_type'] = 'dateTime' maxval = to_value(param.maximum()) or datetime.max minval = to_value(param.minimum()) or datetime.min if defval: defval = defval.toPyDateTime() kwargs['default'] = defval return LiteralInput(allowed_values=[(minval, maxval)], **kwargs)
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)