Exemplo n.º 1
0
    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_LAYOUTITEM
              or isinstance(self.param, QgsProcessingParameterLayoutItem)):
            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 = QgsProcessingParameterLayoutItem(
                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_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
        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)
Exemplo n.º 2
0
 def initAlgorithm(self, config=None):
     self.addParameter(QgsProcessingParameterMapLayer(self.INPUT, self.tr('Input layer')))
     self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Extent'), type=QgsProcessing.TypeVectorPolygon))
    def initAlgorithm(self, config=None):
        """
        Here we define the inputs and output of the algorithm, along
        with some other properties.
        """

        # We add the input vector features source. It can have any kind of
        # geometry.
        self.addParameter(
            QgsProcessingParameterMapLayer(
                self.INPUT, self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry]))

        self.addParameter(
            QgsProcessingParameterEnum(
                self.OBJECTIVE,
                'Select objective',
                options=['Meeting Rooms', 'Toilet Facilities'],
                defaultValue=0))

        self.addParameter(
            QgsProcessingParameterString(self.SEARCH_KEY,
                                         'Enter building code column name',
                                         defaultValue="BUILD_NO"))

        self.addParameter(
            QgsProcessingParameterString(self.BUILDING_NAME_KEY,
                                         'Enter building name column name',
                                         defaultValue="NAME"))

        self.addParameter(
            QgsProcessingParameterString(
                self.CURRENT_BUILDING,
                'Enter building code from where you want predictions',
                defaultValue="104"))

        self.addParameter(
            QgsProcessingParameterString(self.RADIUS,
                                         'Enter preferred radius in meters',
                                         defaultValue="200"))

        self.addParameter(
            QgsProcessingParameterString(
                self.DELTA,
                'How much are you willing to relax your budget?',
                defaultValue="0"))

        self.addParameter(
            QgsProcessingParameterString(self.K,
                                         'How many buildings to select?',
                                         defaultValue="5"))

        self.addParameter(
            QgsProcessingParameterBoolean(
                self.HIGH_CAPACITY,
                'Prioritize high capacity',
            ))

        self.addParameter(
            QgsProcessingParameterBoolean(
                self.EASY_AVAILABILITY,
                'Prioritize easy availability',
            ))

        self.addParameter(
            QgsProcessingParameterBoolean(
                self.WITH_EQUIPMENTS,
                'Prioritize meeting rooms with equipments',
            ))

        self.addParameter(
            QgsProcessingParameterEnum(self.COVID_LOCKDOWN,
                                       'COVID Lockdown Situation',
                                       options=[
                                           'NA', 'Strict (0% - demand)',
                                           'Medium (25% - demand)',
                                           'Low (50% - demand)'
                                       ],
                                       defaultValue=0))

        self.addParameter(
            QgsProcessingParameterEnum(
                self.ROOM_CONDITION,
                'Specific Room Condition',
                options=['NA', 'Excellent', 'Very Good', 'Good'],
                defaultValue=0))

        self.addParameter(
            QgsProcessingParameterString(
                self.REQUIRED_CAPACITY,
                'Do you have any specific capacity requirement?',
                defaultValue="NA"))

        # We add a feature sink in which to store our processed features (this
        # usually takes the form of a newly created vector layer when the
        # algorithm is run in QGIS).
        self.addParameter(
            QgsProcessingParameterFeatureSink(self.OUTPUT,
                                              self.tr('Output layer')))
Exemplo n.º 4
0
    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 initAlgorithm(self, config=None):
        """
        Here we define the inputs and output of the algorithm, along
        with some other properties.
        """

        # We add the input vector features source. It can have any kind of
        # geometry.
        self.addParameter(
            QgsProcessingParameterMapLayer(
                self.INPUT, self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry]))

        self.addParameter(
            QgsProcessingParameterEnum(
                self.OBJECTIVE,
                'Select objective',
                options=['Meeting Rooms', 'Toilet Facilities'],
                defaultValue=0))

        self.addParameter(
            QgsProcessingParameterString(self.SEARCH_KEY,
                                         'Enter building code column name',
                                         defaultValue="BUILD_NO"))

        self.addParameter(
            QgsProcessingParameterString(
                self.CURRENT_BUILDING,
                'Enter building code from where you want predictions',
                defaultValue="104"))

        self.addParameter(
            QgsProcessingParameterString(self.PLOT_PATH,
                                         'Enter directory for storing plot',
                                         defaultValue="/"))

        self.addParameter(
            QgsProcessingParameterBoolean(
                self.HIGH_CAPACITY,
                'Prioritize high capacity',
            ))

        self.addParameter(
            QgsProcessingParameterBoolean(
                self.EASY_AVAILABILITY,
                'Prioritize easy availability',
            ))

        self.addParameter(
            QgsProcessingParameterBoolean(
                self.WITH_EQUIPMENTS,
                'Prioritize meeting rooms with equipments',
            ))

        self.addParameter(
            QgsProcessingParameterBoolean(
                self.SCATTER_PLOT,
                'Enable Plot ?',
            ))

        self.addParameter(
            QgsProcessingParameterEnum(self.COVID_LOCKDOWN,
                                       'COVID Lockdown Situation',
                                       options=[
                                           'NA', 'Strict (0% - demand)',
                                           'Medium (25% - demand)',
                                           'Low (50% - demand)'
                                       ],
                                       defaultValue=0))

        self.addParameter(
            QgsProcessingParameterEnum(
                self.ROOM_CONDITION,
                'Specific Room Condition',
                options=['NA', 'Excellent', 'Very Good', 'Good'],
                defaultValue=0))

        self.addParameter(
            QgsProcessingParameterString(
                self.REQUIRED_CAPACITY,
                'Do you have any specific capacity requirement?',
                defaultValue="NA"))