def testOptional(self): optionalParameter = ParameterExpression('myName', 'myDesc', default='test', optional=True) self.assertEqual(optionalParameter.value, 'test') optionalParameter.setValue('check') self.assertEqual(optionalParameter.value, 'check') self.assertTrue(optionalParameter.setValue(None)) self.assertEqual(optionalParameter.value, None) requiredParameter = ParameterExpression('myName', 'myDesc', default='test', optional=False) self.assertEqual(requiredParameter.value, 'test') requiredParameter.setValue('check') self.assertEqual(requiredParameter.value, 'check') self.assertFalse(requiredParameter.setValue(None)) self.assertEqual(requiredParameter.value, 'check')
def defineCharacteristics(self): self.name, self.i18n_name = self.trAlgorithm('Geometry by expression') self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools') self.addParameter( ParameterVector(self.INPUT_LAYER, self.tr('Input layer'))) self.geometry_types = [self.tr('Polygon'), 'Line', 'Point'] self.addParameter( ParameterSelection(self.OUTPUT_GEOMETRY, self.tr('Output geometry type'), self.geometry_types, default=0)) self.addParameter( ParameterBoolean(self.WITH_Z, self.tr('Output geometry has z dimension'), False)) self.addParameter( ParameterBoolean(self.WITH_M, self.tr('Output geometry has m values'), False)) self.addParameter( ParameterExpression(self.EXPRESSION, self.tr("Geometry expression"), '$geometry', parent_layer=self.INPUT_LAYER)) self.addOutput( OutputVector(self.OUTPUT_LAYER, self.tr('Modified geometry')))
def __init__(self): super().__init__() self.addParameter( ParameterVector(self.INPUT_LAYER, self.tr('Input layer'))) self.geometry_types = [self.tr('Polygon'), 'Line', 'Point'] self.addParameter( ParameterSelection(self.OUTPUT_GEOMETRY, self.tr('Output geometry type'), self.geometry_types, default=0)) self.addParameter( ParameterBoolean(self.WITH_Z, self.tr('Output geometry has z dimension'), False)) self.addParameter( ParameterBoolean(self.WITH_M, self.tr('Output geometry has m values'), False)) self.addParameter( ParameterExpression(self.EXPRESSION, self.tr("Geometry expression"), '$geometry', parent_layer=self.INPUT_LAYER)) self.addOutput( OutputVector(self.OUTPUT_LAYER, self.tr('Modified geometry')))
def __init__(self): super().__init__() self.addParameter(ParameterVector(self.INPUT, self.tr('Input Layer'))) self.addParameter(ParameterExpression(self.EXPRESSION, self.tr("Expression"), parent_layer=self.INPUT)) self.addOutput(OutputVector(self.OUTPUT, self.tr('Extracted (expression)')))
def defineCharacteristics(self): self.addParameter(ParameterVector(self.INPUT, self.tr('Input Layer'))) self.addParameter( ParameterExpression(self.EXPRESSION, self.tr("Expression"), parent_layer=self.INPUT)) self.addOutput( OutputVector(self.OUTPUT, self.tr('Extracted (expression)')))
def defineCharacteristics(self): self.name, self.i18n_name = self.trAlgorithm('Extract by expression') self.group, self.i18n_group = self.trAlgorithm('Vector selection tools') self.tags = self.tr('extract,filter,expression,field') self.addParameter(ParameterVector(self.INPUT, self.tr('Input Layer'))) self.addParameter(ParameterExpression(self.EXPRESSION, self.tr("Expression"), parent_layer=self.INPUT)) self.addOutput(OutputVector(self.OUTPUT, self.tr('Extracted (expression)')))
def testScriptCode(self): parameter = ParameterExpression('myName', 'myDescription', default='test') code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterExpression) parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterExpression) self.assertTrue(result.optional) self.assertEquals(result.default, parameter.default)
def defineCharacteristics(self): self.methods = [self.tr('creating new selection'), self.tr('adding to current selection'), self.tr('removing from current selection'), self.tr('selecting within current selection')] self.addParameter(ParameterVector(self.LAYERNAME, self.tr('Input Layer'))) self.addParameter(ParameterExpression(self.EXPRESSION, self.tr("Expression"), parent_layer=self.LAYERNAME)) self.addParameter(ParameterSelection(self.METHOD, self.tr('Modify current selection by'), self.methods, 0)) self.addOutput(OutputVector(self.RESULT, self.tr('Selected (expression)'), True))
def okPressed(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 name in self.alg.inputs: name = safeName.lower() + str(i) else: name = self.param.name if (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_BOOLEAN or isinstance(self.param, ParameterBoolean)): self.param = ParameterBoolean(name, description, self.state.isChecked()) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_TABLE_FIELD or isinstance(self.param, ParameterTableField)): 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() self.param = ParameterTableField( name, description, parent, datatype, multiple=self.multipleCheck.isChecked()) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_RASTER or isinstance(self.param, ParameterRaster)): self.param = ParameterRaster(name, description) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_TABLE or isinstance(self.param, ParameterTable)): self.param = ParameterTable(name, description) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_VECTOR or isinstance(self.param, ParameterVector)): self.param = ParameterVector( name, description, [self.shapetypeCombo.currentIndex() - 1]) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_MULTIPLE or isinstance(self.param, ParameterMultipleInput)): self.param = ParameterMultipleInput( name, description, self.datatypeCombo.currentIndex() - 1) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_NUMBER or isinstance(self.param, ParameterNumber)): try: vmin = self.minTextBox.text().strip() if vmin == '': vmin = None else: vmin = float(vmin) vmax = self.maxTextBox.text().strip() if vmax == '': vmax = None else: vmax = float(vmax) self.param = ParameterNumber(name, description, vmin, vmax, str(self.defaultTextBox.text())) 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, ParameterExpression)): parent = self.parentCombo.currentData() self.param = ParameterExpression( name, description, default=str(self.defaultEdit.expression()), parent_layer=parent) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_STRING or isinstance(self.param, ParameterString)): self.param = ParameterString(name, description, str(self.defaultTextBox.text())) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_EXTENT or isinstance(self.param, ParameterExtent)): self.param = ParameterExtent(name, description) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_FILE or isinstance(self.param, ParameterFile)): isFolder = self.fileFolderCombo.currentIndex() == 1 self.param = ParameterFile(name, description, isFolder=isFolder) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_POINT or isinstance(self.param, ParameterPoint)): self.param = ParameterPoint(name, description, str(self.defaultTextBox.text())) elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_CRS or isinstance(self.param, ParameterCrs)): self.param = ParameterCrs(name, description, default=self.selector.crs().authid()) self.param.optional = not self.requiredCheck.isChecked() self.close()
def testSetValue(self): parameter = ParameterExpression('myName', 'myDescription') self.assertTrue(parameter.setValue('\'a\' || "field"')) self.assertEqual(parameter.value, '\'a\' || "field"')