def initAlgorithm(self, config=None): self.addParameter( QgsProcessingParameterMultipleLayers(self.INPUT, self.tr('Input layers'), QgsProcessing.TypeRaster)) self.addParameter( QgsProcessingParameterBoolean( self.PCT, self.tr('Grab pseudocolor table from first layer'), defaultValue=False)) self.addParameter( QgsProcessingParameterBoolean( self.SEPARATE, self.tr('Place each input file into a separate band'), defaultValue=False)) nodata_param = QgsProcessingParameterNumber( self.NODATA_INPUT, self.tr('Input pixel value to treat as "nodata"'), type=QgsProcessingParameterNumber.Integer, defaultValue=None, optional=True) nodata_param.setFlags(nodata_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(nodata_param) nodata_out_param = QgsProcessingParameterNumber( self.NODATA_OUTPUT, self.tr('Assign specified "nodata" value to output'), type=QgsProcessingParameterNumber.Integer, defaultValue=None, optional=True) nodata_out_param.setFlags( nodata_out_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(nodata_out_param) options_param = QgsProcessingParameterString( self.OPTIONS, self.tr('Additional creation options'), defaultValue='', optional=True) options_param.setFlags(options_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) options_param.setMetadata({ 'widget_wrapper': { 'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper' } }) self.addParameter(options_param) self.addParameter( QgsProcessingParameterEnum(self.DATA_TYPE, self.tr('Output data type'), self.TYPES, allowMultiple=False, defaultValue=5)) self.addParameter( QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Merged')))
def initAlgorithm(self, config=None): self.methods = ( (self.tr('Nearest neighbour'), 'near'), (self.tr('Bilinear'), 'bilinear'), (self.tr('Cubic'), 'cubic'), (self.tr('Cubic spline'), 'cubicspline'), (self.tr('Lanczos windowed sinc'), 'lanczos'), ) self.addParameter( QgsProcessingParameterMultipleLayers(self.INPUT, self.tr('Input files'), QgsProcessing.TypeRaster)) self.addParameter( QgsProcessingParameterNumber( self.TILE_SIZE_X, self.tr('Tile width'), type=QgsProcessingParameterNumber.Integer, minValue=0, defaultValue=256)) self.addParameter( QgsProcessingParameterNumber( self.TILE_SIZE_Y, self.tr('Tile height'), type=QgsProcessingParameterNumber.Integer, minValue=0, defaultValue=256)) self.addParameter( QgsProcessingParameterNumber( self.OVERLAP, self.tr('Overlap in pixels between consecutive tiles'), type=QgsProcessingParameterNumber.Integer, minValue=0, defaultValue=0)) self.addParameter( QgsProcessingParameterNumber( self.LEVELS, self.tr('Number of pyramids levels to build'), type=QgsProcessingParameterNumber.Integer, minValue=0, defaultValue=1)) params = [] params.append( QgsProcessingParameterCrs( self.SOURCE_CRS, self.tr('Source coordinate reference system'), optional=True)) params.append( QgsProcessingParameterEnum(self.RESAMPLING, self.tr('Resampling method'), options=[i[0] for i in self.methods], allowMultiple=False, defaultValue=0)) params.append( QgsProcessingParameterString( self.DELIMITER, self.tr('Column delimiter used in the CSV file'), defaultValue=';', optional=True)) options_param = QgsProcessingParameterString( self.OPTIONS, self.tr('Additional creation options'), defaultValue='', optional=True) options_param.setMetadata({ 'widget_wrapper': { 'class': 'processing.algs.gdal.ui.RasterOptionsWidget.RasterOptionsWidgetWrapper' } }) params.append(options_param) params.append( QgsProcessingParameterEnum(self.DATA_TYPE, self.tr('Output data type'), self.TYPES, allowMultiple=False, defaultValue=5)) params.append( QgsProcessingParameterBoolean(self.ONLY_PYRAMIDS, self.tr('Build only the pyramids'), defaultValue=False)) params.append( QgsProcessingParameterBoolean( self.DIR_FOR_ROW, self.tr('Use separate directory for each tiles row'), defaultValue=False)) for param in params: param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(param) self.addParameter( QgsProcessingParameterFolderDestination( self.OUTPUT, self.tr('Output directory'))) output_csv_param = QgsProcessingParameterFileDestination( self.OUTPUT_CSV, self.tr( 'CSV file containing the tile(s) georeferencing information'), 'CSV files (*.csv)', optional=True) output_csv_param.setCreateByDefault(False) self.addParameter(output_csv_param)
def initAlgorithm(self, config=None): layer_param = QgsProcessingParameterMultipleLayers( self.LAYERS, self.tr('Input layers'), layerType=QgsProcessing.TypeRaster, optional=True) layer_param.setMetadata({ 'widget_wrapper': 'processing.algs.qgis.ui.RasterCalculatorWidgets.LayersListWidgetWrapper' }) self.addParameter(layer_param) class ParameterRasterCalculatorExpression(QgsProcessingParameterString ): def __init__(self, name='', description='', multiLine=False): super().__init__(name, description, multiLine=multiLine) self.setMetadata({ 'widget_wrapper': 'processing.algs.qgis.ui.RasterCalculatorWidgets.ExpressionWidgetWrapper' }) def type(self): return 'raster_calc_expression' def clone(self): return ParameterRasterCalculatorExpression( self.name(), self.description(), self.multiLine()) def evaluateForModeler(self, value, model): for i in list(model.inputs.values()): param = i.param if isinstance(param, QgsProcessingParameterRasterLayer): new = "{}@".format(os.path.basename(param.value)) old = "{}@".format(param.name()) value = value.replace(old, new) for alg in list(model.algs.values()): for out in alg.algorithm.outputs: if isinstance(out, QgsProcessingOutputRasterLayer): if out.value: new = "{}@".format( os.path.basename(out.value)) old = "{}:{}@".format( alg.modeler_name, out.name) value = value.replace(old, new) return value self.addParameter( ParameterRasterCalculatorExpression(self.EXPRESSION, self.tr('Expression'), multiLine=True)) self.addParameter( QgsProcessingParameterNumber( self.CELLSIZE, self.tr('Cell size (use 0 or empty to set it automatically)'), type=QgsProcessingParameterNumber.Double, minValue=0.0, defaultValue=0.0, optional=True)) self.addParameter( QgsProcessingParameterExtent(self.EXTENT, self.tr('Output extent'), optional=True)) self.addParameter( QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Output')))
def accept(self): description = str(self.nameTextBox.text()) if description.strip() == '': QMessageBox.warning(self, self.tr('Unable to define parameter'), self.tr('Invalid parameter name')) return if self.param is None: validChars = \ 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' safeName = ''.join(c for c in description if c in validChars) name = safeName.lower() i = 2 while self.alg.parameterDefinition(name): name = safeName.lower() + str(i) i += 1 else: name = self.param.name() if (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_BOOLEAN or isinstance(self.param, QgsProcessingParameterBoolean)): self.param = QgsProcessingParameterBoolean(name, description, self.state.isChecked()) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_TABLE_FIELD or isinstance(self.param, QgsProcessingParameterField)): if self.parentCombo.currentIndex() < 0: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return parent = self.parentCombo.currentData() datatype = self.datatypeCombo.currentData() default = self.defaultTextBox.text() if not default: default = None self.param = QgsProcessingParameterField( name, description, defaultValue=default, parentLayerParameterName=parent, type=datatype, allowMultiple=self.multipleCheck.isChecked()) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_BAND or isinstance(self.param, QgsProcessingParameterBand)): if self.parentCombo.currentIndex() < 0: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return parent = self.parentCombo.currentData() self.param = QgsProcessingParameterBand(name, description, None, parent) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_MAP_LAYER or isinstance(self.param, QgsProcessingParameterMapLayer)): self.param = QgsProcessingParameterMapLayer(name, description) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_RASTER or isinstance(self.param, QgsProcessingParameterRasterLayer)): self.param = QgsProcessingParameterRasterLayer(name, description) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_TABLE or isinstance(self.param, QgsProcessingParameterVectorLayer)): self.param = QgsProcessingParameterVectorLayer( name, description, [self.shapetypeCombo.currentData()]) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_VECTOR or isinstance(self.param, QgsProcessingParameterFeatureSource)): self.param = QgsProcessingParameterFeatureSource( name, description, [self.shapetypeCombo.currentData()]) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_MULTIPLE or isinstance(self.param, QgsProcessingParameterMultipleLayers)): self.param = QgsProcessingParameterMultipleLayers( name, description, self.datatypeCombo.currentData()) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_NUMBER or isinstance(self.param, QgsProcessingParameterNumber)): try: self.param = QgsProcessingParameterNumber( name, description, QgsProcessingParameterNumber.Double, self.defaultTextBox.text()) vmin = self.minTextBox.text().strip() if not vmin == '': self.param.setMinimum(float(vmin)) vmax = self.maxTextBox.text().strip() if not vmax == '': self.param.setMaximum(float(vmax)) except: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_EXPRESSION or isinstance(self.param, QgsProcessingParameterExpression)): parent = self.parentCombo.currentData() self.param = QgsProcessingParameterExpression( name, description, str(self.defaultEdit.expression()), parent) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_STRING or isinstance(self.param, QgsProcessingParameterString)): self.param = QgsProcessingParameterString( name, description, str(self.defaultTextBox.text())) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_EXTENT or isinstance(self.param, QgsProcessingParameterExtent)): self.param = QgsProcessingParameterExtent(name, description) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_FILE or isinstance(self.param, QgsProcessingParameterFile)): isFolder = self.fileFolderCombo.currentIndex() == 1 self.param = QgsProcessingParameterFile( name, description, QgsProcessingParameterFile.Folder if isFolder else QgsProcessingParameterFile.File) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_POINT or isinstance(self.param, QgsProcessingParameterPoint)): self.param = QgsProcessingParameterPoint( name, description, str(self.defaultTextBox.text())) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_CRS or isinstance(self.param, QgsProcessingParameterCrs)): self.param = QgsProcessingParameterCrs( name, description, self.selector.crs().authid()) if not self.requiredCheck.isChecked(): self.param.setFlags( self.param.flags() | QgsProcessingParameterDefinition.FlagOptional) settings = QgsSettings() settings.setValue( "/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry()) QDialog.accept(self)
def accept(self): description = self.nameTextBox.text() if description.strip() == '': QMessageBox.warning(self, self.tr('Unable to define parameter'), self.tr('Invalid parameter name')) return if self.param is None: validChars = \ 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' safeName = ''.join(c for c in description if c in validChars) name = safeName.lower() i = 2 while self.alg.parameterDefinition(name): name = safeName.lower() + str(i) i += 1 else: name = self.param.name() if (self.paramType == parameters.PARAMETER_BOOLEAN or isinstance(self.param, QgsProcessingParameterBoolean)): self.param = QgsProcessingParameterBoolean(name, description, self.state.isChecked()) elif (self.paramType == parameters.PARAMETER_TABLE_FIELD or isinstance(self.param, QgsProcessingParameterField)): if self.parentCombo.currentIndex() < 0: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return parent = self.parentCombo.currentData() datatype = self.datatypeCombo.currentData() default = self.defaultTextBox.text() if not default: default = None self.param = QgsProcessingParameterField( name, description, defaultValue=default, parentLayerParameterName=parent, type=datatype, allowMultiple=self.multipleCheck.isChecked()) elif (self.paramType == parameters.PARAMETER_BAND or isinstance(self.param, QgsProcessingParameterBand)): if self.parentCombo.currentIndex() < 0: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return parent = self.parentCombo.currentData() self.param = QgsProcessingParameterBand(name, description, None, parent) elif (self.paramType == parameters.PARAMETER_MAP_LAYER or isinstance(self.param, QgsProcessingParameterMapLayer)): self.param = QgsProcessingParameterMapLayer(name, description) elif (self.paramType == parameters.PARAMETER_RASTER or isinstance(self.param, QgsProcessingParameterRasterLayer)): self.param = QgsProcessingParameterRasterLayer(name, description) elif (self.paramType == parameters.PARAMETER_TABLE or isinstance(self.param, QgsProcessingParameterVectorLayer)): self.param = QgsProcessingParameterVectorLayer( name, description, [self.shapetypeCombo.currentData()]) elif (self.paramType == parameters.PARAMETER_VECTOR or isinstance(self.param, QgsProcessingParameterFeatureSource)): self.param = QgsProcessingParameterFeatureSource( name, description, [self.shapetypeCombo.currentData()]) elif (self.paramType == parameters.PARAMETER_MULTIPLE or isinstance(self.param, QgsProcessingParameterMultipleLayers)): self.param = QgsProcessingParameterMultipleLayers( name, description, self.datatypeCombo.currentData()) elif (self.paramType == parameters.PARAMETER_NUMBER or isinstance( self.param, (QgsProcessingParameterNumber, QgsProcessingParameterDistance))): try: self.param = QgsProcessingParameterNumber( name, description, QgsProcessingParameterNumber.Double, self.defaultTextBox.text()) vmin = self.minTextBox.text().strip() if not vmin == '': self.param.setMinimum(float(vmin)) vmax = self.maxTextBox.text().strip() if not vmax == '': self.param.setMaximum(float(vmax)) except: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return elif (self.paramType == parameters.PARAMETER_EXPRESSION or isinstance(self.param, QgsProcessingParameterExpression)): parent = self.parentCombo.currentData() self.param = QgsProcessingParameterExpression( name, description, str(self.defaultEdit.expression()), parent) elif (self.paramType == parameters.PARAMETER_STRING or isinstance(self.param, QgsProcessingParameterString)): self.param = QgsProcessingParameterString( name, description, str(self.defaultTextBox.text())) elif (self.paramType == parameters.PARAMETER_EXTENT or isinstance(self.param, QgsProcessingParameterExtent)): self.param = QgsProcessingParameterExtent(name, description) elif (self.paramType == parameters.PARAMETER_FILE or isinstance(self.param, QgsProcessingParameterFile)): isFolder = self.fileFolderCombo.currentIndex() == 1 self.param = QgsProcessingParameterFile( name, description, QgsProcessingParameterFile.Folder if isFolder else QgsProcessingParameterFile.File) elif (self.paramType == parameters.PARAMETER_POINT or isinstance(self.param, QgsProcessingParameterPoint)): self.param = QgsProcessingParameterPoint( name, description, str(self.defaultTextBox.text())) elif (self.paramType == parameters.PARAMETER_CRS or isinstance(self.param, QgsProcessingParameterCrs)): self.param = QgsProcessingParameterCrs( name, description, self.selector.crs().authid()) elif (self.paramType == parameters.PARAMETER_ENUM or isinstance(self.param, QgsProcessingParameterEnum)): self.param = QgsProcessingParameterEnum( name, description, self.widget.options(), self.widget.allowMultiple(), self.widget.defaultOptions()) elif (self.paramType == parameters.PARAMETER_MATRIX or isinstance(self.param, QgsProcessingParameterMatrix)): self.param = QgsProcessingParameterMatrix( name, description, hasFixedNumberRows=self.widget.fixedRows(), headers=self.widget.headers(), defaultValue=self.widget.value()) # Destination parameter elif (isinstance(self.param, QgsProcessingParameterFeatureSink)): self.param = QgsProcessingParameterFeatureSink( name=name, description=self.param.description(), type=self.param.dataType(), defaultValue=self.defaultWidget.getValue()) elif (isinstance(self.param, QgsProcessingParameterFileDestination)): self.param = QgsProcessingParameterFileDestination( name=name, description=self.param.description(), fileFilter=self.param.fileFilter(), defaultValue=self.defaultWidget.getValue()) elif (isinstance(self.param, QgsProcessingParameterFolderDestination)): self.param = QgsProcessingParameterFolderDestination( name=name, description=self.param.description(), defaultValue=self.defaultWidget.getValue()) elif (isinstance(self.param, QgsProcessingParameterRasterDestination)): self.param = QgsProcessingParameterRasterDestination( name=name, description=self.param.description(), defaultValue=self.defaultWidget.getValue()) elif (isinstance(self.param, QgsProcessingParameterVectorDestination)): self.param = QgsProcessingParameterVectorDestination( name=name, description=self.param.description(), type=self.param.dataType(), defaultValue=self.defaultWidget.getValue()) else: if self.paramType: typeId = self.paramType else: typeId = self.param.type() paramTypeDef = QgsApplication.instance().processingRegistry( ).parameterType(typeId) if not paramTypeDef: msg = self.tr( 'The parameter `{}` is not registered, are you missing a required plugin?' .format(typeId)) raise UndefinedParameterException(msg) self.param = paramTypeDef.create(name) self.param.setDescription(description) self.param.setMetadata(paramTypeDef.metadata()) if not self.requiredCheck.isChecked(): self.param.setFlags( self.param.flags() | QgsProcessingParameterDefinition.FlagOptional) else: self.param.setFlags( self.param.flags() & ~QgsProcessingParameterDefinition.FlagOptional) settings = QgsSettings() settings.setValue( "/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry()) QDialog.accept(self)
def initAlgorithm(self, config=None): class ParameterVrtDestination(QgsProcessingParameterRasterDestination): def __init__(self, name, description): super().__init__(name, description) def clone(self): copy = ParameterVrtDestination(self.name(), self.description()) return copy def type(self): return 'vrt_destination' def defaultFileExtension(self): return 'vrt' self.addParameter( QgsProcessingParameterMultipleLayers( self.INPUT, QCoreApplication.translate("ParameterVrtDestination", 'Input layers'), QgsProcessing.TypeRaster)) self.addParameter( QgsProcessingParameterEnum(self.RESOLUTION, QCoreApplication.translate( "ParameterVrtDestination", 'Resolution'), options=self.RESOLUTION_OPTIONS, defaultValue=0)) self.addParameter( QgsProcessingParameterBoolean( self.SEPARATE, QCoreApplication.translate( "ParameterVrtDestination", 'Place each input file into a separate band'), defaultValue=True)) self.addParameter( QgsProcessingParameterBoolean(self.PROJ_DIFFERENCE, QCoreApplication.translate( "ParameterVrtDestination", 'Allow projection difference'), defaultValue=False)) add_alpha_param = QgsProcessingParameterBoolean( self.ADD_ALPHA, QCoreApplication.translate( "ParameterVrtDestination", 'Add alpha mask band to VRT when source raster has none'), defaultValue=False) add_alpha_param.setFlags( add_alpha_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(add_alpha_param) assign_crs = QgsProcessingParameterCrs( self.ASSIGN_CRS, QCoreApplication.translate( "ParameterVrtDestination", 'Override projection for the output file'), defaultValue=None, optional=True) assign_crs.setFlags(assign_crs.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(assign_crs) resampling = QgsProcessingParameterEnum( self.RESAMPLING, QCoreApplication.translate("ParameterVrtDestination", 'Resampling algorithm'), options=self.RESAMPLING_OPTIONS, defaultValue=0) resampling.setFlags(resampling.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(resampling) self.addParameter( ParameterVrtDestination( self.OUTPUT, QCoreApplication.translate("ParameterVrtDestination", 'Virtual')))
def initAlgorithm(self, config=None): self.addParameter(QgsProcessingParameterMultipleLayers('LAYERS', 'LAYERS', layerType=QgsProcessing.TypeRaster, defaultValue=None)) self.addParameter(QgsProcessingParameterString('FORMULA', 'FORMULA', defaultValue=None)) self.addParameter(QgsProcessingParameterRasterLayer('SAMPLE', 'SAMPLE', defaultValue=None)) self.addParameter(QgsProcessingParameterRasterDestination('OUTPUT', 'OUTPUT', createByDefault=True, defaultValue=None))
def initAlgorithm(self, config=None): self.addParameter(QgsProcessingParameterMultipleLayers(self.LAYERS, self.tr('Layers to merge'), QgsProcessing.TypeVectorAnyGeometry)) self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Merged')))
def accept(self): description = self.nameTextBox.text() if description.strip() == '': QMessageBox.warning(self, self.tr('Unable to define parameter'), self.tr('Invalid parameter name')) return if self.param is None: validChars = \ 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' safeName = ''.join(c for c in description if c in validChars) name = safeName.lower() i = 2 while self.alg.parameterDefinition(name): name = safeName.lower() + str(i) i += 1 else: name = self.param.name() if (self.paramType == parameters.PARAMETER_TABLE_FIELD or isinstance(self.param, QgsProcessingParameterField)): if self.parentCombo.currentIndex() < 0: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return parent = self.parentCombo.currentData() datatype = self.datatypeCombo.currentData() default = self.defaultTextBox.text() if not default: default = None self.param = QgsProcessingParameterField( name, description, defaultValue=default, parentLayerParameterName=parent, type=datatype, allowMultiple=self.multipleCheck.isChecked()) elif (self.paramType == parameters.PARAMETER_BAND or isinstance(self.param, QgsProcessingParameterBand)): if self.parentCombo.currentIndex() < 0: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return parent = self.parentCombo.currentData() self.param = QgsProcessingParameterBand(name, description, None, parent) elif (self.paramType == parameters.PARAMETER_MAP_LAYER or isinstance(self.param, QgsProcessingParameterMapLayer)): self.param = QgsProcessingParameterMapLayer( name, description, types=[self.datatypeCombo.currentData()]) elif (self.paramType == parameters.PARAMETER_RASTER or isinstance(self.param, QgsProcessingParameterRasterLayer)): self.param = QgsProcessingParameterRasterLayer(name, description) elif (self.paramType == parameters.PARAMETER_TABLE or isinstance(self.param, QgsProcessingParameterVectorLayer)): self.param = QgsProcessingParameterVectorLayer( name, description, [self.shapetypeCombo.currentData()]) elif (self.paramType == parameters.PARAMETER_VECTOR or isinstance(self.param, QgsProcessingParameterFeatureSource)): self.param = QgsProcessingParameterFeatureSource( name, description, [self.shapetypeCombo.currentData()]) elif (self.paramType == parameters.PARAMETER_MULTIPLE or isinstance(self.param, QgsProcessingParameterMultipleLayers)): self.param = QgsProcessingParameterMultipleLayers( name, description, self.datatypeCombo.currentData()) elif (self.paramType == parameters.PARAMETER_DISTANCE or isinstance(self.param, QgsProcessingParameterDistance)): self.param = QgsProcessingParameterDistance( name, description, self.defaultTextBox.text()) try: vmin = self.minTextBox.text().strip() if not vmin == '': self.param.setMinimum(float(vmin)) vmax = self.maxTextBox.text().strip() if not vmax == '': self.param.setMaximum(float(vmax)) except: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return if self.parentCombo.currentIndex() < 0: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return parent = self.parentCombo.currentData() if parent: self.param.setParentParameterName(parent) elif (self.paramType == parameters.PARAMETER_SCALE or isinstance(self.param, QgsProcessingParameterScale)): self.param = QgsProcessingParameterScale( name, description, self.defaultTextBox.text()) elif (self.paramType == parameters.PARAMETER_NUMBER or isinstance(self.param, QgsProcessingParameterNumber)): type = self.type_combo.currentData() self.param = QgsProcessingParameterNumber( name, description, type, self.defaultTextBox.text()) try: vmin = self.minTextBox.text().strip() if not vmin == '': self.param.setMinimum(float(vmin)) vmax = self.maxTextBox.text().strip() if not vmax == '': self.param.setMaximum(float(vmax)) except: QMessageBox.warning( self, self.tr('Unable to define parameter'), self.tr('Wrong or missing parameter values')) return # Destination parameter elif (isinstance(self.param, QgsProcessingParameterFeatureSink)): self.param = QgsProcessingParameterFeatureSink( name=name, description=self.param.description(), type=self.param.dataType(), defaultValue=self.defaultWidget.value()) elif (isinstance(self.param, QgsProcessingParameterFileDestination)): self.param = QgsProcessingParameterFileDestination( name=name, description=self.param.description(), fileFilter=self.param.fileFilter(), defaultValue=self.defaultWidget.value()) elif (isinstance(self.param, QgsProcessingParameterFolderDestination)): self.param = QgsProcessingParameterFolderDestination( name=name, description=self.param.description(), defaultValue=self.defaultWidget.value()) elif (isinstance(self.param, QgsProcessingParameterRasterDestination)): self.param = QgsProcessingParameterRasterDestination( name=name, description=self.param.description(), defaultValue=self.defaultWidget.value()) elif (isinstance(self.param, QgsProcessingParameterVectorDestination)): self.param = QgsProcessingParameterVectorDestination( name=name, description=self.param.description(), type=self.param.dataType(), defaultValue=self.defaultWidget.value()) else: if self.paramType: typeId = self.paramType else: typeId = self.param.type() paramTypeDef = QgsApplication.instance().processingRegistry( ).parameterType(typeId) if not paramTypeDef: msg = self.tr( 'The parameter `{}` is not registered, are you missing a required plugin?' .format(typeId)) raise UndefinedParameterException(msg) self.param = paramTypeDef.create(name) self.param.setDescription(description) self.param.setMetadata(paramTypeDef.metadata()) if not self.requiredCheck.isChecked(): self.param.setFlags( self.param.flags() | QgsProcessingParameterDefinition.FlagOptional) else: self.param.setFlags( self.param.flags() & ~QgsProcessingParameterDefinition.FlagOptional) if self.advancedCheck.isChecked(): self.param.setFlags( self.param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) else: self.param.setFlags( self.param.flags() & ~QgsProcessingParameterDefinition.FlagAdvanced) settings = QgsSettings() settings.setValue( "/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry()) QDialog.accept(self)
def initAlgorithm(self, config): """ Parameter setting. """ self.addParameter( QgsProcessingParameterVectorLayer(self.NETWORK_LAYER, self.tr('Network layer'), [QgsProcessing.TypeVectorLine])) self.addParameter( QgsProcessingParameterField(self.ATTRIBUTE_BLACK_LIST, self.tr('Fields to ignore'), None, 'NETWORK_LAYER', QgsProcessingParameterField.Any, allowMultiple=True, optional=True)) self.addParameter( QgsProcessingParameterBoolean(self.IGNORE_VIRTUAL_FIELDS, self.tr('Ignore virtual fields'), defaultValue=True)) self.addParameter( QgsProcessingParameterBoolean(self.IGNORE_PK_FIELDS, self.tr('Ignore primary key fields'), defaultValue=True)) self.addParameter( QgsProcessingParameterVectorLayer(self.NODE_LAYER, self.tr('Node layer'), [QgsProcessing.TypeVectorPoint])) self.addParameter( QgsProcessingParameterVectorLayer( self.REF_LAYER, self.tr('Reference layer'), [QgsProcessing.TypeVectorPolygon])) self.addParameter( QgsProcessingParameterVectorLayer(self.SINK_LAYER, self.tr('Water sink layer'), [QgsProcessing.TypeVectorPoint], optional=True)) self.addParameter( QgsProcessingParameterVectorLayer(self.SPILLWAY_LAYER, self.tr('Spillway layer'), [QgsProcessing.TypeVectorPoint], optional=True)) self.addParameter( QgsProcessingParameterMultipleLayers( self.WATER_BODY_LAYERS, self.tr('Water body layers'), QgsProcessing.TypeVectorPolygon, optional=True)) self.addParameter( QgsProcessingParameterVectorLayer(self.DITCH_LAYER, self.tr('Ditch layer'), [QgsProcessing.TypeVectorLine], optional=True)) self.addParameter( QgsProcessingParameterNumber( self.MAX_CYCLES, self.tr('Maximum cycles'), minValue=1, defaultValue=2, type=QgsProcessingParameterNumber.Integer)) self.addParameter( QgsProcessingParameterNumber( self.SEARCH_RADIUS, self.tr('Search radius'), minValue=0, defaultValue=1, type=QgsProcessingParameterNumber.Double)) self.addParameter( QgsProcessingParameterBoolean( self.SELECT_ALL_VALID, self.tr('Select all valid lines after the process'))) self.addParameter( QgsProcessingParameterFeatureSink( self.FLAGS, self.tr('{0} network node errors').format(self.displayName()))) self.addParameter( QgsProcessingParameterFeatureSink( self.LINE_FLAGS, self.tr('{0} line errors').format(self.displayName())))
def initAlgorithm(self, config=None): class ParameterVrtDestination(QgsProcessingParameterRasterDestination): def __init__(self, name, description): super().__init__(name, description) def clone(self): copy = ParameterVrtDestination(self.name(), self.description()) return copy def defaultFileExtension(self): return 'vrt' def createFileFilter(self): return '{} (*.vrt *.VRT)'.format(QCoreApplication.translate("GdalAlgorithm", 'VRT files')) def supportedOutputRasterLayerExtensions(self): return ['vrt'] def parameterAsOutputLayer(self, definition, value, context): return super(QgsProcessingParameterRasterDestination, self).parameterAsOutputLayer(definition, value, context) def isSupportedOutputValue(self, value, context): output_path = QgsProcessingParameters.parameterAsOutputLayer(self, value, context) if pathlib.Path(output_path).suffix.lower() != '.vrt': return False, QCoreApplication.translate("GdalAlgorithm", 'Output filename must use a .vrt extension') return True, '' self.RESAMPLING_OPTIONS = ((self.tr('Nearest Neighbour'), 'nearest'), (self.tr('Bilinear'), 'bilinear'), (self.tr('Cubic Convolution'), 'cubic'), (self.tr('B-Spline Convolution'), 'cubicspline'), (self.tr('Lanczos Windowed Sinc'), 'lanczos'), (self.tr('Average'), 'average'), (self.tr('Mode'), 'mode')) self.RESOLUTION_OPTIONS = ((self.tr('Average'), 'average'), (self.tr('Highest'), 'highest'), (self.tr('Lowest'), 'lowest')) self.addParameter(QgsProcessingParameterMultipleLayers(self.INPUT, self.tr('Input layers'), QgsProcessing.TypeRaster)) self.addParameter(QgsProcessingParameterEnum(self.RESOLUTION, self.tr('Resolution'), options=[i[0] for i in self.RESOLUTION_OPTIONS], defaultValue=0)) separate_param = QgsProcessingParameterBoolean(self.SEPARATE, self.tr('Place each input file into a separate band'), defaultValue=True) # default to not using separate bands is a friendlier option, but we can't change the parameter's actual # defaultValue without breaking API! separate_param.setGuiDefaultValueOverride(False) self.addParameter(separate_param) self.addParameter(QgsProcessingParameterBoolean(self.PROJ_DIFFERENCE, self.tr('Allow projection difference'), defaultValue=False)) add_alpha_param = QgsProcessingParameterBoolean(self.ADD_ALPHA, self.tr('Add alpha mask band to VRT when source raster has none'), defaultValue=False) add_alpha_param.setFlags(add_alpha_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(add_alpha_param) assign_crs = QgsProcessingParameterCrs(self.ASSIGN_CRS, self.tr('Override projection for the output file'), defaultValue=None, optional=True) assign_crs.setFlags(assign_crs.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(assign_crs) resampling = QgsProcessingParameterEnum(self.RESAMPLING, self.tr('Resampling algorithm'), options=[i[0] for i in self.RESAMPLING_OPTIONS], defaultValue=0) resampling.setFlags(resampling.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(resampling) src_nodata_param = QgsProcessingParameterString(self.SRC_NODATA, self.tr('Nodata value(s) for input bands (space separated)'), defaultValue=None, optional=True) src_nodata_param.setFlags(src_nodata_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(src_nodata_param) extra_param = QgsProcessingParameterString(self.EXTRA, self.tr('Additional command-line parameters'), defaultValue=None, optional=True) extra_param.setFlags(extra_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(extra_param) self.addParameter(ParameterVrtDestination(self.OUTPUT, QCoreApplication.translate("ParameterVrtDestination", 'Virtual')))
def initAlgorithm(self, config=None): self.addParameter( QgsProcessingParameterMultipleLayers(self.INPUT_LAYERS, self.tr('Input layers'), QgsProcessing.TypeRaster)) self.addParameter( QgsProcessingParameterFeatureSink(self.OUTPUT_FOOTPRINTS, self.tr('Images footprint'), QgsProcessing.TypeVectorPolygon)) self.addParameter( QgsProcessingParameterFeatureSink(self.OUTPUT_NADIRS, self.tr('Images nadir'), QgsProcessing.TypeVectorPoint)) self.addParameter( QgsProcessingParameterEnum( self.CAMERA_MODEL, self.tr('Camera model defining FOV values'), options=list(self.CAMERA_DATA.keys()))) self.addParameter( QgsProcessingParameterCrs(self.SOURCE_CRS, self.tr('Source CRS'), defaultValue='EPSG:4326')) self.addParameter( QgsProcessingParameterCrs(self.DESTINATION_CRS, self.tr('Destination CRS'), optional=True, defaultValue='ProjectCrs')) # horizontal referred to flight direction => means wide angle parameter = QgsProcessingParameterNumber( self.HORIZONTAL_FOV, self.tr('Wide camera angle'), type=QgsProcessingParameterNumber.Double, defaultValue=84.0, minValue=0, maxValue=360) parameter.setFlags(parameter.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(parameter) # vertical referred to flight direction => means tall angle parameter = QgsProcessingParameterNumber( self.VERTICAL_FOV, self.tr('Tall camera angle'), type=QgsProcessingParameterNumber.Double, defaultValue=54.0, minValue=0, maxValue=360) parameter.setFlags(parameter.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(parameter) parameter = QgsProcessingParameterNumber( self.NADIR_TO_BOTTOM_OFFSET, self.tr('Offset to add to bottom distance result'), type=QgsProcessingParameterNumber.Double) parameter.setFlags(parameter.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(parameter) parameter = QgsProcessingParameterNumber( self.NADIR_TO_UPPPER_OFFSET, self.tr('Offset to add to upper distance result'), type=QgsProcessingParameterNumber.Double) parameter.setFlags(parameter.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(parameter)