def parse_allowed_layers(param: QgsProcessingParameterDefinition, kwargs, context: MapContext) -> None: """ Find candidate layers according to datatypes """ typ = param.type() if typ == 'multilayer': num_inputs = param.minimumNumberInputs() kwargs['min_occurs'] = num_inputs if num_inputs >= 1 else 0 kwargs['max_occurs'] = 20 # XXX arbitrary number datatypes = [] if isinstance(param, QgsProcessingParameterLimitedDataTypes): datatypes = param.dataTypes() if not datatypes: if isinstance(param, INPUT_VECTOR_LAYER_TYPES): datatypes = [QgsProcessing.TypeVector] elif isinstance(param, INPUT_RASTER_LAYER_TYPES): datatypes = [QgsProcessing.TypeRaster] elif isinstance(param, QgsProcessingParameterMultipleLayers): datatypes = [param.layerType()] else: datatypes = [QgsProcessing.TypeMapLayer] kwargs['metadata'].append( Metadata('processing:dataTypes', ','.join(SourceTypes[dtyp] for dtyp in datatypes))) # Nothing to do is there is no context if context is None: return project = context.project() def _is_allowed(lyr): if lyr.type() == QgsMapLayer.VectorLayer: geomtype = lyr.geometryType() return (geomtype == QgsWkbTypes.PointGeometry and QgsProcessing.TypeVectorPoint in datatypes) \ or (geomtype == QgsWkbTypes.LineGeometry and QgsProcessing.TypeVectorLine in datatypes) \ or (geomtype == QgsWkbTypes.PolygonGeometry and QgsProcessing.TypeVectorPolygon in datatypes) \ or QgsProcessing.TypeVectorAnyGeometry in datatypes \ or QgsProcessing.TypeVector in datatypes \ or QgsProcessing.TypeMapLayer in datatypes elif lyr.type() == QgsMapLayer.RasterLayer: return QgsProcessing.TypeRaster in datatypes \ or QgsProcessing.TypeMapLayer in datatypes return False kwargs['allowed_values'] = [ lyr.name() for lyr in project.mapLayers().values() if _is_allowed(lyr) ] # Set max occurs accordingly to if typ == 'multilayer': kwargs['max_occurs'] = len(kwargs['allowed_values'])
def get_layers_type(param: QgsProcessingParameterDefinition, kwargs) -> None: """ Set datatype as metadata """ datatypes = [] if isinstance(param, QgsProcessingParameterLimitedDataTypes): datatypes = param.dataTypes() if not datatypes: if isinstance(param, INPUT_VECTOR_LAYER_TYPES): datatypes = [QgsProcessing.TypeVector] elif isinstance(param, INPUT_RASTER_LAYER_TYPES): datatypes = [QgsProcessing.TypeRaster] elif isinstance(param, QgsProcessingParameterMultipleLayers): datatypes = [param.layerType()] elif isinstance(param, QgsProcessingParameterMeshLayer): datatypes = [QgsProcessing.TypeMesh] else: datatypes = [QgsProcessing.TypeMapLayer] kwargs['metadata'].append( Metadata('processing:dataTypes', ','.join(SourceTypes[dtyp] for dtyp in datatypes))) return datatypes