def initAlgorithm(self, config=None): self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer'))) self.addParameter(QgsProcessingParameterFeatureSource(self.MASK, self.tr('Mask layer'), [QgsProcessing.TypeVectorPolygon])) self.addParameter(QgsProcessingParameterNumber(self.NODATA, self.tr('Assign a specified nodata value to output bands'), type=QgsProcessingParameterNumber.Double, defaultValue=None, optional=True)) self.addParameter(QgsProcessingParameterBoolean(self.ALPHA_BAND, self.tr('Create an output alpha band'), defaultValue=False)) self.addParameter(QgsProcessingParameterBoolean(self.CROP_TO_CUTLINE, self.tr('Match the extent of the clipped raster to the extent of the mask layer'), defaultValue=True)) self.addParameter(QgsProcessingParameterBoolean(self.KEEP_RESOLUTION, self.tr('Keep resolution of output raster'), defaultValue=False)) target_extent_param = QgsProcessingParameterExtent(self.TARGET_EXTENT, self.tr('Georeferenced extents of output file to be created'), optional=True) target_extent_param.setFlags(target_extent_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(target_extent_param) target_extent_crs_param = QgsProcessingParameterCrs(self.TARGET_EXTENT_CRS, self.tr('CRS of the target raster extent'), optional=True) target_extent_crs_param.setFlags(target_extent_crs_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(target_extent_crs_param) multithreading_param = QgsProcessingParameterBoolean(self.MULTITHREADING, self.tr('Use multithreaded warping implementation'), defaultValue=False) multithreading_param.setFlags(multithreading_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(multithreading_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) dataType_param = QgsProcessingParameterEnum(self.DATA_TYPE, self.tr('Output data type'), self.TYPES, allowMultiple=False, defaultValue=0) dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(dataType_param) self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Clipped (mask)')))
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, self.tr('Input layers'), QgsProcessing.TypeRaster)) self.addParameter(QgsProcessingParameterEnum(self.RESOLUTION, self.tr('Resolution'), options=self.RESOLUTION_OPTIONS, defaultValue=0)) self.addParameter(QgsProcessingParameterBoolean(self.SEPARATE, self.tr('Place each input file into a separate band'), defaultValue=True)) 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=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) self.addParameter(ParameterVrtDestination(self.OUTPUT, QCoreApplication.translate("ParameterVrtDestination", 'Virtual')))
def initAlgorithm(self, config=None): self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Input layer'))) extent_parameter = QgsProcessingParameterExtent(self.TARGET_AREA, self.tr('Target area for layer')) self.addParameter(extent_parameter) # deprecated crs_param = QgsProcessingParameterCrs(self.TARGET_AREA_CRS, 'Target area CRS', optional=True) crs_param.setFlags(crs_param.flags() | QgsProcessingParameterDefinition.FlagHidden) self.addParameter(crs_param) self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('CRS candidates')))
def initAlgorithm(self, config=None): self.formats = ((self.tr('Auto'), 'AUTO'), (self.tr('Well-known text (WKT)'), 'WKT'), (self.tr('EPSG'), 'EPSG'), (self.tr('Proj.4'), 'PROJ')) self.addParameter(QgsProcessingParameterMultipleLayers(self.LAYERS, self.tr('Input files'), QgsProcessing.TypeRaster)) self.addParameter(QgsProcessingParameterString(self.PATH_FIELD_NAME, self.tr('Field name to hold the file path to the indexed rasters'), defaultValue='location')) self.addParameter(QgsProcessingParameterBoolean(self.ABSOLUTE_PATH, self.tr('Store absolute path to the indexed rasters'), defaultValue=False)) self.addParameter(QgsProcessingParameterBoolean(self.PROJ_DIFFERENCE, self.tr('Skip files with different projection reference'), defaultValue=False)) target_crs_param = QgsProcessingParameterCrs(self.TARGET_CRS, self.tr('Transform geometries to the given CRS'), optional=True) target_crs_param.setFlags(target_crs_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(target_crs_param) crs_field_param = QgsProcessingParameterString(self.CRS_FIELD_NAME, self.tr('The name of the field to store the SRS of each tile'), optional=True) crs_field_param.setFlags(crs_field_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(crs_field_param) crs_format_param = QgsProcessingParameterEnum(self.CRS_FORMAT, self.tr('The format in which the CRS of each tile must be written'), options=[i[0] for i in self.formats], allowMultiple=False, defaultValue=0) crs_format_param.setFlags(crs_format_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(crs_format_param) self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT, self.tr('Tile index'), QgsProcessing.TypeVectorPolygon))
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, self.tr('Input layers'), QgsProcessing.TypeRaster)) self.addParameter( QgsProcessingParameterEnum(self.RESOLUTION, self.tr('Resolution'), options=self.RESOLUTION_OPTIONS, defaultValue=0)) self.addParameter( QgsProcessingParameterBoolean( self.SEPARATE, self.tr('Place each input file into a separate band'), defaultValue=True)) 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=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) self.addParameter( ParameterVrtDestination( self.OUTPUT, QCoreApplication.translate("ParameterVrtDestination", 'Virtual')))
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.tr('Average'), 'average'), (self.tr('Mode'), 'mode'), (self.tr('Maximum'), 'max'), (self.tr('Minimum'), 'min'), (self.tr('Median'), 'med'), (self.tr('First quartile'), 'q1'), (self.tr('Third quartile'), 'q3')) self.addParameter( QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer'))) self.addParameter( QgsProcessingParameterCrs(self.SOURCE_CRS, self.tr('Source CRS'), optional=True)) self.addParameter( QgsProcessingParameterCrs(self.TARGET_CRS, self.tr('Target CRS'), 'EPSG:4326')) self.addParameter( QgsProcessingParameterEnum(self.RESAMPLING, self.tr('Resampling method to use'), options=[i[0] for i in self.methods], defaultValue=0)) self.addParameter( QgsProcessingParameterNumber( self.NODATA, self.tr('Nodata value for output bands'), type=QgsProcessingParameterNumber.Double, defaultValue=None, optional=True)) self.addParameter( QgsProcessingParameterNumber( self.TARGET_RESOLUTION, self.tr( 'Output file resolution in target georeferenced units'), type=QgsProcessingParameterNumber.Double, minValue=0.0, defaultValue=None, optional=True)) 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) dataType_param = QgsProcessingParameterEnum( self.DATA_TYPE, self.tr('Output data type'), self.TYPES, allowMultiple=False, defaultValue=0) dataType_param.setFlags( dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(dataType_param) target_extent_param = QgsProcessingParameterExtent( self.TARGET_EXTENT, self.tr('Georeferenced extents of output file to be created'), optional=True) target_extent_param.setFlags( target_extent_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(target_extent_param) target_extent_crs_param = QgsProcessingParameterCrs( self.TARGET_EXTENT_CRS, self.tr('CRS of the target raster extent'), optional=True) target_extent_crs_param.setFlags( target_extent_crs_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(target_extent_crs_param) multithreading_param = QgsProcessingParameterBoolean( self.MULTITHREADING, self.tr('Use multithreaded warping implementation'), defaultValue=False) multithreading_param.setFlags( multithreading_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(multithreading_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( QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Reprojected')))
class ModelerParameterDefinitionDialog(QDialog): PARAMETER_NUMBER = 'Number' PARAMETER_RASTER = 'Raster Layer' PARAMETER_TABLE = 'Vector Layer' PARAMETER_VECTOR = 'Feature Source' PARAMETER_STRING = 'String' PARAMETER_EXPRESSION = 'Expression' PARAMETER_BOOLEAN = 'Boolean' PARAMETER_TABLE_FIELD = 'Layer Field' PARAMETER_EXTENT = 'Extent' PARAMETER_FILE = 'File' PARAMETER_POINT = 'Point' PARAMETER_CRS = 'CRS' PARAMETER_MULTIPLE = 'Multiple Input' PARAMETER_BAND = 'Raster Band' PARAMETER_MAP_LAYER = 'Map Layer' paramTypes = [ PARAMETER_BOOLEAN, PARAMETER_EXTENT, PARAMETER_FILE, PARAMETER_NUMBER, PARAMETER_RASTER, PARAMETER_STRING, PARAMETER_EXPRESSION, PARAMETER_MAP_LAYER, PARAMETER_TABLE, PARAMETER_TABLE_FIELD, PARAMETER_VECTOR, PARAMETER_POINT, PARAMETER_CRS, PARAMETER_MULTIPLE, PARAMETER_BAND ] def __init__(self, alg, paramType=None, param=None): self.alg = alg self.paramType = paramType self.param = param QDialog.__init__(self) self.setModal(True) self.setupUi() settings = QgsSettings() self.restoreGeometry( settings.value( "/Processing/modelParametersDefinitionDialogGeometry", QByteArray())) def closeEvent(self, event): settings = QgsSettings() settings.setValue( "/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry()) super(ModelerParameterDefinitionDialog, self).closeEvent(event) def setupUi(self): self.setWindowTitle(self.tr('Parameter Definition')) self.setMinimumWidth(300) self.verticalLayout = QVBoxLayout(self) self.verticalLayout.setMargin(20) self.label = QLabel(self.tr('Parameter name')) self.verticalLayout.addWidget(self.label) self.nameTextBox = QLineEdit() self.verticalLayout.addWidget(self.nameTextBox) if isinstance(self.param, QgsProcessingParameterDefinition): self.nameTextBox.setText(self.param.description()) if self.paramType == ModelerParameterDefinitionDialog.PARAMETER_BOOLEAN or \ isinstance(self.param, QgsProcessingParameterBoolean): self.state = QCheckBox() self.state.setText(self.tr('Checked')) self.state.setChecked(False) if self.param is not None: self.state.setChecked(bool(self.param.defaultValue())) self.verticalLayout.addWidget(self.state) elif self.paramType == ModelerParameterDefinitionDialog.PARAMETER_TABLE_FIELD or \ isinstance(self.param, QgsProcessingParameterField): self.verticalLayout.addWidget(QLabel(self.tr('Parent layer'))) self.parentCombo = QComboBox() idx = 0 for param in list(self.alg.parameterComponents().values()): definition = self.alg.parameterDefinition( param.parameterName()) if isinstance(definition, (QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer)): self.parentCombo.addItem(definition.description(), definition.name()) if self.param is not None: if self.param.parentLayerParameterName( ) == definition.name(): self.parentCombo.setCurrentIndex(idx) idx += 1 self.verticalLayout.addWidget(self.parentCombo) # add the datatype selector self.verticalLayout.addWidget(QLabel(self.tr('Allowed data type'))) self.datatypeCombo = QComboBox() self.datatypeCombo.addItem(self.tr('Any'), -1) self.datatypeCombo.addItem(self.tr('Number'), 0) self.datatypeCombo.addItem(self.tr('String'), 1) self.datatypeCombo.addItem(self.tr('Date/time'), 2) self.verticalLayout.addWidget(self.datatypeCombo) if self.param is not None and self.param.dataType() is not None: # QComboBoxes indexes start at 0, # self.param.datatype start with -1 that is why I need to do +1 datatypeIndex = self.param.dataType() + 1 self.datatypeCombo.setCurrentIndex(datatypeIndex) self.multipleCheck = QCheckBox() self.multipleCheck.setText(self.tr('Accept multiple fields')) self.multipleCheck.setChecked(False) if self.param is not None: self.multipleCheck.setChecked(self.param.allowMultiple()) self.verticalLayout.addWidget(self.multipleCheck) self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.defaultTextBox = QLineEdit() self.defaultTextBox.setToolTip( self. tr('Default field name, or ; separated list of field names for multiple field parameters' )) if self.param is not None: default = self.param.defaultValue() if default is not None: self.defaultTextBox.setText(str(default)) self.verticalLayout.addWidget(self.defaultTextBox) elif self.paramType == ModelerParameterDefinitionDialog.PARAMETER_BAND or \ isinstance(self.param, QgsProcessingParameterBand): self.verticalLayout.addWidget(QLabel(self.tr('Parent layer'))) self.parentCombo = QComboBox() idx = 0 for param in list(self.alg.parameterComponents().values()): definition = self.alg.parameterDefinition( param.parameterName()) if isinstance(definition, (QgsProcessingParameterRasterLayer)): self.parentCombo.addItem(definition.description(), definition.name()) if self.param is not None: if self.param.parentLayerParameterName( ) == definition.name(): self.parentCombo.setCurrentIndex(idx) idx += 1 self.verticalLayout.addWidget(self.parentCombo) elif (self.paramType in (ModelerParameterDefinitionDialog.PARAMETER_VECTOR, ModelerParameterDefinitionDialog.PARAMETER_TABLE) or isinstance(self.param, (QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer))): self.verticalLayout.addWidget(QLabel(self.tr('Geometry type'))) self.shapetypeCombo = QComboBox() self.shapetypeCombo.addItem(self.tr('Geometry Not Required'), QgsProcessing.TypeVector) self.shapetypeCombo.addItem(self.tr('Point'), QgsProcessing.TypeVectorPoint) self.shapetypeCombo.addItem(self.tr('Line'), QgsProcessing.TypeVectorLine) self.shapetypeCombo.addItem(self.tr('Polygon'), QgsProcessing.TypeVectorPolygon) self.shapetypeCombo.addItem(self.tr('Any Geometry Type'), QgsProcessing.TypeVectorAnyGeometry) if self.param is not None: self.shapetypeCombo.setCurrentIndex( self.shapetypeCombo.findData(self.param.dataTypes()[0])) self.verticalLayout.addWidget(self.shapetypeCombo) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_MULTIPLE or isinstance(self.param, QgsProcessingParameterMultipleLayers)): self.verticalLayout.addWidget(QLabel(self.tr('Data type'))) self.datatypeCombo = QComboBox() self.datatypeCombo.addItem(self.tr('Any Map Layer'), QgsProcessing.TypeMapLayer) self.datatypeCombo.addItem( self.tr('Vector (No Geometry Required)'), QgsProcessing.TypeVector) self.datatypeCombo.addItem(self.tr('Vector (Point)'), QgsProcessing.TypeVectorPoint) self.datatypeCombo.addItem(self.tr('Vector (Line)'), QgsProcessing.TypeVectorLine) self.datatypeCombo.addItem(self.tr('Vector (Polygon)'), QgsProcessing.TypeVectorPolygon) self.datatypeCombo.addItem(self.tr('Vector (Any Geometry Type)'), QgsProcessing.TypeVectorAnyGeometry) self.datatypeCombo.addItem(self.tr('Raster'), QgsProcessing.TypeRaster) self.datatypeCombo.addItem(self.tr('File'), QgsProcessing.TypeFile) if self.param is not None: self.datatypeCombo.setCurrentIndex( self.datatypeCombo.findData(self.param.layerType())) self.verticalLayout.addWidget(self.datatypeCombo) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_NUMBER or isinstance(self.param, QgsProcessingParameterNumber)): self.verticalLayout.addWidget(QLabel(self.tr('Min value'))) self.minTextBox = QLineEdit() self.verticalLayout.addWidget(self.minTextBox) self.verticalLayout.addWidget(QLabel(self.tr('Max value'))) self.maxTextBox = QLineEdit() self.verticalLayout.addWidget(self.maxTextBox) if self.param is not None: self.minTextBox.setText(str(self.param.minimum())) self.maxTextBox.setText(str(self.param.maximum())) self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.defaultTextBox = QLineEdit() self.defaultTextBox.setText(self.tr('0')) if self.param is not None: default = self.param.defaultValue() if self.param.dataType( ) == QgsProcessingParameterNumber.Integer: default = int(math.floor(default)) if default: self.defaultTextBox.setText(str(default)) self.verticalLayout.addWidget(self.defaultTextBox) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_EXPRESSION or isinstance(self.param, QgsProcessingParameterExpression)): self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.defaultEdit = QgsExpressionLineEdit() if self.param is not None: self.defaultEdit.setExpression(self.param.defaultValue()) self.verticalLayout.addWidget(self.defaultEdit) self.verticalLayout.addWidget(QLabel(self.tr('Parent layer'))) self.parentCombo = QComboBox() self.parentCombo.addItem(self.tr("None"), None) idx = 1 for param in list(self.alg.parameterComponents().values()): definition = self.alg.parameterDefinition( param.parameterName()) if isinstance(definition, (QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer)): self.parentCombo.addItem(definition.description(), definition.name()) if self.param is not None: if self.param.parentLayerParameterName( ) == definition.name(): self.parentCombo.setCurrentIndex(idx) idx += 1 self.verticalLayout.addWidget(self.parentCombo) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_STRING or isinstance(self.param, QgsProcessingParameterString)): self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.defaultTextBox = QLineEdit() if self.param is not None: self.defaultTextBox.setText(self.param.defaultValue()) self.verticalLayout.addWidget(self.defaultTextBox) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_FILE or isinstance(self.param, QgsProcessingParameterFile)): self.verticalLayout.addWidget(QLabel(self.tr('Type'))) self.fileFolderCombo = QComboBox() self.fileFolderCombo.addItem(self.tr('File')) self.fileFolderCombo.addItem(self.tr('Folder')) if self.param is not None: self.fileFolderCombo.setCurrentIndex(1 if self.param.behavior( ) == QgsProcessingParameterFile.Folder else 0) self.verticalLayout.addWidget(self.fileFolderCombo) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_POINT or isinstance(self.param, QgsProcessingParameterPoint)): self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.defaultTextBox = QLineEdit() if self.param is not None: self.defaultTextBox.setText(self.param.defaultValue()) self.verticalLayout.addWidget(self.defaultTextBox) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_CRS or isinstance(self.param, QgsProcessingParameterCrs)): self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.selector = QgsProjectionSelectionWidget() if self.param is not None: self.selector.setCrs( QgsCoordinateReferenceSystem(self.param.defaultValue())) else: self.selector.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) self.verticalLayout.addWidget(self.selector) self.verticalLayout.addSpacing(20) self.requiredCheck = QCheckBox() self.requiredCheck.setText(self.tr('Mandatory')) self.requiredCheck.setChecked(True) if self.param is not None: self.requiredCheck.setChecked( not self.param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.verticalLayout.addWidget(self.requiredCheck) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setOrientation(Qt.Horizontal) self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok) self.buttonBox.setObjectName('buttonBox') self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.verticalLayout.addStretch() self.verticalLayout.addWidget(self.buttonBox) self.setLayout(self.verticalLayout) 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 reject(self): self.param = None settings = QgsSettings() settings.setValue( "/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry()) QDialog.reject(self)
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.tr('Average'), 'average'), (self.tr('Mode'), 'mode'), (self.tr('Maximum'), 'max'), (self.tr('Minimum'), 'min'), (self.tr('Median'), 'med'), (self.tr('First quartile'), 'q1'), (self.tr('Third quartile'), 'q3')) self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer'))) self.addParameter(QgsProcessingParameterCrs(self.SOURCE_CRS, self.tr('Source CRS'), optional=True)) self.addParameter(QgsProcessingParameterCrs(self.TARGET_CRS, self.tr('Target CRS'), 'EPSG:4326')) self.addParameter(QgsProcessingParameterNumber(self.NODATA, self.tr('Nodata value for output bands'), type=QgsProcessingParameterNumber.Double, defaultValue=0.0)) self.addParameter(QgsProcessingParameterNumber(self.TARGET_RESOLUTION, self.tr('Output file resolution in target georeferenced units'), type=QgsProcessingParameterNumber.Double, minValue=0.0, defaultValue=None)) options_param = QgsProcessingParameterString(self.OPTIONS, self.tr('Additional creation parameters'), 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.RESAMPLING, self.tr('Resampling method to use'), options=[i[0] for i in self.methods], defaultValue=0)) dataType_param = QgsProcessingParameterEnum(self.DATA_TYPE, self.tr('Output data type'), self.TYPES, allowMultiple=False, defaultValue=5) dataType_param.setFlags(dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(dataType_param) target_extent_param = QgsProcessingParameterExtent(self.TARGET_EXTENT, self.tr('Georeferenced extents of output file to be created'), optional=True) target_extent_param.setFlags(target_extent_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(target_extent_param) target_extent_crs_param = QgsProcessingParameterCrs(self.TARGET_EXTENT_CRS, self.tr('CRS of the target raster extent'), optional=True) target_extent_crs_param.setFlags(target_extent_crs_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(target_extent_crs_param) multithreading_param = QgsProcessingParameterBoolean(self.MULTITHREADING, self.tr('Use multithreaded warping implementation'), defaultValue=False) multithreading_param.setFlags(multithreading_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(multithreading_param) self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Reprojected')))
def initAlgorithm(self, config=None): self.addParameter( QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer'))) self.addParameter( QgsProcessingParameterFeatureSource( self.MASK, self.tr('Mask layer'), [QgsProcessing.TypeVectorPolygon])) self.addParameter( QgsProcessingParameterNumber( self.NODATA, self.tr('Assign a specified nodata value to output bands'), type=QgsProcessingParameterNumber.Double, defaultValue=None, optional=True)) self.addParameter( QgsProcessingParameterBoolean( self.ALPHA_BAND, self.tr('Create an output alpha band'), defaultValue=False)) self.addParameter( QgsProcessingParameterBoolean( self.CROP_TO_CUTLINE, self. tr('Match the extent of the clipped raster to the extent of the mask layer' ), defaultValue=True)) self.addParameter( QgsProcessingParameterBoolean( self.KEEP_RESOLUTION, self.tr('Keep resolution of output raster'), defaultValue=False)) target_extent_param = QgsProcessingParameterExtent( self.TARGET_EXTENT, self.tr('Georeferenced extents of output file to be created'), optional=True) target_extent_param.setFlags( target_extent_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(target_extent_param) target_extent_crs_param = QgsProcessingParameterCrs( self.TARGET_EXTENT_CRS, self.tr('CRS of the target raster extent'), optional=True) target_extent_crs_param.setFlags( target_extent_crs_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(target_extent_crs_param) multithreading_param = QgsProcessingParameterBoolean( self.MULTITHREADING, self.tr('Use multithreaded warping implementation'), defaultValue=False) multithreading_param.setFlags( multithreading_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(multithreading_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) dataType_param = QgsProcessingParameterEnum( self.DATA_TYPE, self.tr('Output data type'), self.TYPES, allowMultiple=False, defaultValue=0) dataType_param.setFlags( dataType_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(dataType_param) self.addParameter( QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Clipped (mask)')))
class ModelerParameterDefinitionDialog(QDialog): PARAMETER_NUMBER = 'Number' PARAMETER_RASTER = 'Raster Layer' PARAMETER_TABLE = 'Vector Layer' PARAMETER_VECTOR = 'Feature Source' PARAMETER_STRING = 'String' PARAMETER_EXPRESSION = 'Expression' PARAMETER_BOOLEAN = 'Boolean' PARAMETER_TABLE_FIELD = 'Layer Field' PARAMETER_EXTENT = 'Extent' PARAMETER_FILE = 'File' PARAMETER_POINT = 'Point' PARAMETER_CRS = 'CRS' PARAMETER_MULTIPLE = 'Multiple Input' PARAMETER_BAND = 'Raster Band' PARAMETER_MAP_LAYER = 'Map Layer' paramTypes = [ PARAMETER_BOOLEAN, PARAMETER_EXTENT, PARAMETER_FILE, PARAMETER_NUMBER, PARAMETER_RASTER, PARAMETER_STRING, PARAMETER_EXPRESSION, PARAMETER_MAP_LAYER, PARAMETER_TABLE, PARAMETER_TABLE_FIELD, PARAMETER_VECTOR, PARAMETER_POINT, PARAMETER_CRS, PARAMETER_MULTIPLE, PARAMETER_BAND ] def __init__(self, alg, paramType=None, param=None): self.alg = alg self.paramType = paramType self.param = param QDialog.__init__(self) self.setModal(True) self.setupUi() settings = QgsSettings() self.restoreGeometry(settings.value("/Processing/modelParametersDefinitionDialogGeometry", QByteArray())) def closeEvent(self, event): settings = QgsSettings() settings.setValue("/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry()) super(ModelerParameterDefinitionDialog, self).closeEvent(event) def setupUi(self): self.setWindowTitle(self.tr('Parameter definition')) self.setMinimumWidth(300) self.verticalLayout = QVBoxLayout(self) self.verticalLayout.setMargin(20) self.label = QLabel(self.tr('Parameter name')) self.verticalLayout.addWidget(self.label) self.nameTextBox = QLineEdit() self.verticalLayout.addWidget(self.nameTextBox) if isinstance(self.param, QgsProcessingParameterDefinition): self.nameTextBox.setText(self.param.description()) if self.paramType == ModelerParameterDefinitionDialog.PARAMETER_BOOLEAN or \ isinstance(self.param, QgsProcessingParameterBoolean): self.state = QCheckBox() self.state.setText(self.tr('Checked')) self.state.setChecked(False) if self.param is not None: self.state.setChecked(bool(self.param.defaultValue())) self.verticalLayout.addWidget(self.state) elif self.paramType == ModelerParameterDefinitionDialog.PARAMETER_TABLE_FIELD or \ isinstance(self.param, QgsProcessingParameterField): self.verticalLayout.addWidget(QLabel(self.tr('Parent layer'))) self.parentCombo = QComboBox() idx = 0 for param in list(self.alg.parameterComponents().values()): definition = self.alg.parameterDefinition(param.parameterName()) if isinstance(definition, (QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer)): self.parentCombo.addItem(definition.description(), definition.name()) if self.param is not None: if self.param.parentLayerParameterName() == definition.name(): self.parentCombo.setCurrentIndex(idx) idx += 1 self.verticalLayout.addWidget(self.parentCombo) # add the datatype selector self.verticalLayout.addWidget(QLabel(self.tr('Allowed data type'))) self.datatypeCombo = QComboBox() self.datatypeCombo.addItem(self.tr('Any'), -1) self.datatypeCombo.addItem(self.tr('Number'), 0) self.datatypeCombo.addItem(self.tr('String'), 1) self.datatypeCombo.addItem(self.tr('Date/time'), 2) self.verticalLayout.addWidget(self.datatypeCombo) if self.param is not None and self.param.dataType() is not None: # QComboBoxes indexes start at 0, # self.param.datatype start with -1 that is why I need to do +1 datatypeIndex = self.param.dataType() + 1 self.datatypeCombo.setCurrentIndex(datatypeIndex) self.multipleCheck = QCheckBox() self.multipleCheck.setText(self.tr('Accept multiple fields')) self.multipleCheck.setChecked(False) if self.param is not None: self.multipleCheck.setChecked(self.param.allowMultiple()) self.verticalLayout.addWidget(self.multipleCheck) self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.defaultTextBox = QLineEdit() self.defaultTextBox.setToolTip( self.tr('Default field name, or ; separated list of field names for multiple field parameters')) if self.param is not None: default = self.param.defaultValue() if default is not None: self.defaultTextBox.setText(str(default)) self.verticalLayout.addWidget(self.defaultTextBox) elif self.paramType == ModelerParameterDefinitionDialog.PARAMETER_BAND or \ isinstance(self.param, QgsProcessingParameterBand): self.verticalLayout.addWidget(QLabel(self.tr('Parent layer'))) self.parentCombo = QComboBox() idx = 0 for param in list(self.alg.parameterComponents().values()): definition = self.alg.parameterDefinition(param.parameterName()) if isinstance(definition, (QgsProcessingParameterRasterLayer)): self.parentCombo.addItem(definition.description(), definition.name()) if self.param is not None: if self.param.parentLayerParameterName() == definition.name(): self.parentCombo.setCurrentIndex(idx) idx += 1 self.verticalLayout.addWidget(self.parentCombo) elif (self.paramType in ( ModelerParameterDefinitionDialog.PARAMETER_VECTOR, ModelerParameterDefinitionDialog.PARAMETER_TABLE) or isinstance(self.param, (QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer))): self.verticalLayout.addWidget(QLabel(self.tr('Geometry type'))) self.shapetypeCombo = QComboBox() self.shapetypeCombo.addItem(self.tr('Geometry Not Required'), QgsProcessing.TypeVector) self.shapetypeCombo.addItem(self.tr('Point'), QgsProcessing.TypeVectorPoint) self.shapetypeCombo.addItem(self.tr('Line'), QgsProcessing.TypeVectorLine) self.shapetypeCombo.addItem(self.tr('Polygon'), QgsProcessing.TypeVectorPolygon) self.shapetypeCombo.addItem(self.tr('Any Geometry Type'), QgsProcessing.TypeVectorAnyGeometry) if self.param is not None: self.shapetypeCombo.setCurrentIndex(self.shapetypeCombo.findData(self.param.dataTypes()[0])) self.verticalLayout.addWidget(self.shapetypeCombo) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_MULTIPLE or isinstance(self.param, QgsProcessingParameterMultipleLayers)): self.verticalLayout.addWidget(QLabel(self.tr('Data type'))) self.datatypeCombo = QComboBox() self.datatypeCombo.addItem(self.tr('Any Map Layer'), QgsProcessing.TypeMapLayer) self.datatypeCombo.addItem(self.tr('Vector (No Geometry Required)'), QgsProcessing.TypeVector) self.datatypeCombo.addItem(self.tr('Vector (Point)'), QgsProcessing.TypeVectorPoint) self.datatypeCombo.addItem(self.tr('Vector (Line)'), QgsProcessing.TypeVectorLine) self.datatypeCombo.addItem(self.tr('Vector (Polygon)'), QgsProcessing.TypeVectorPolygon) self.datatypeCombo.addItem(self.tr('Vector (Any Geometry Type)'), QgsProcessing.TypeVectorAnyGeometry) self.datatypeCombo.addItem(self.tr('Raster'), QgsProcessing.TypeRaster) self.datatypeCombo.addItem(self.tr('File'), QgsProcessing.TypeFile) if self.param is not None: self.datatypeCombo.setCurrentIndex(self.datatypeCombo.findData(self.param.layerType())) self.verticalLayout.addWidget(self.datatypeCombo) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_NUMBER or isinstance(self.param, QgsProcessingParameterNumber)): self.verticalLayout.addWidget(QLabel(self.tr('Min value'))) self.minTextBox = QLineEdit() self.verticalLayout.addWidget(self.minTextBox) self.verticalLayout.addWidget(QLabel(self.tr('Max value'))) self.maxTextBox = QLineEdit() self.verticalLayout.addWidget(self.maxTextBox) if self.param is not None: self.minTextBox.setText(str(self.param.minimum())) self.maxTextBox.setText(str(self.param.maximum())) self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.defaultTextBox = QLineEdit() self.defaultTextBox.setText(self.tr('0')) if self.param is not None: default = self.param.defaultValue() if self.param.dataType() == QgsProcessingParameterNumber.Integer: default = int(math.floor(default)) if default: self.defaultTextBox.setText(str(default)) self.verticalLayout.addWidget(self.defaultTextBox) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_EXPRESSION or isinstance(self.param, QgsProcessingParameterExpression)): self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.defaultEdit = QgsExpressionLineEdit() if self.param is not None: self.defaultEdit.setExpression(self.param.defaultValue()) self.verticalLayout.addWidget(self.defaultEdit) self.verticalLayout.addWidget(QLabel(self.tr('Parent layer'))) self.parentCombo = QComboBox() self.parentCombo.addItem(self.tr("None"), None) idx = 1 for param in list(self.alg.parameterComponents().values()): definition = self.alg.parameterDefinition(param.parameterName()) if isinstance(definition, (QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer)): self.parentCombo.addItem(definition.description(), definition.name()) if self.param is not None: if self.param.parentLayerParameterName() == definition.name(): self.parentCombo.setCurrentIndex(idx) idx += 1 self.verticalLayout.addWidget(self.parentCombo) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_STRING or isinstance(self.param, QgsProcessingParameterString)): self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.defaultTextBox = QLineEdit() if self.param is not None: self.defaultTextBox.setText(self.param.defaultValue()) self.verticalLayout.addWidget(self.defaultTextBox) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_FILE or isinstance(self.param, QgsProcessingParameterFile)): self.verticalLayout.addWidget(QLabel(self.tr('Type'))) self.fileFolderCombo = QComboBox() self.fileFolderCombo.addItem(self.tr('File')) self.fileFolderCombo.addItem(self.tr('Folder')) if self.param is not None: self.fileFolderCombo.setCurrentIndex( 1 if self.param.behavior() == QgsProcessingParameterFile.Folder else 0) self.verticalLayout.addWidget(self.fileFolderCombo) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_POINT or isinstance(self.param, QgsProcessingParameterPoint)): self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.defaultTextBox = QLineEdit() if self.param is not None: self.defaultTextBox.setText(self.param.defaultValue()) self.verticalLayout.addWidget(self.defaultTextBox) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_CRS or isinstance(self.param, QgsProcessingParameterCrs)): self.verticalLayout.addWidget(QLabel(self.tr('Default value'))) self.selector = QgsProjectionSelectionWidget() if self.param is not None: self.selector.setCrs(QgsCoordinateReferenceSystem(self.param.defaultValue())) else: self.selector.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) self.verticalLayout.addWidget(self.selector) self.verticalLayout.addSpacing(20) self.requiredCheck = QCheckBox() self.requiredCheck.setText(self.tr('Mandatory')) self.requiredCheck.setChecked(True) if self.param is not None: self.requiredCheck.setChecked(not self.param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.verticalLayout.addWidget(self.requiredCheck) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setOrientation(Qt.Horizontal) self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok) self.buttonBox.setObjectName('buttonBox') self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.verticalLayout.addStretch() self.verticalLayout.addWidget(self.buttonBox) self.setLayout(self.verticalLayout) 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 reject(self): self.param = None settings = QgsSettings() settings.setValue("/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry()) QDialog.reject(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' def parameterAsOutputLayer(self, definition, value, context): return super(QgsProcessingParameterRasterDestination, self).parameterAsOutputLayer(definition, value, context) 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)) self.addParameter(QgsProcessingParameterBoolean(self.SEPARATE, self.tr('Place each input file into a separate band'), defaultValue=True)) 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): 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')))