Example #1
0
    def createAlgorithm(self):
        alg = Algorithm(self._alg.commandLineName())
        alg.setName(self.model)
        alg.description = self.descriptionBox.text()
        params = self._alg.parameters
        outputs = self._alg.outputs
        for param in params:
            if param.hidden:
                continue
            if not self.setParamValue(alg, param, self.valueItems[param.name]):
                return None
        for output in outputs:
            if not output.hidden:
                name = unicode(self.valueItems[output.name].text())
                if name.strip() != '' and name != ModelerParametersDialog.ENTER_NAME:
                    alg.outputs[output.name] = ModelerOutput(name)

        selectedOptions = self.dependenciesPanel.selectedoptions
        availableDependencies = self.getAvailableDependencies()
        self.dependencies = []
        for selected in selectedOptions:
            s = availableDependencies[selected]
            alg.dependencies.append(s)

        return alg
Example #2
0
    def createAlgorithm(self):
        alg = Algorithm(self._alg.commandLineName())
        alg.setName(self.model)
        alg.description = self.descriptionBox.text()
        params = self._alg.parameters
        outputs = self._alg.outputs
        for param in params:
            if param.hidden:
                continue
            if not self.setParamValue(alg, param, self.wrappers[param.name]):
                self.bar.pushMessage("Error", "Wrong or missing value for parameter '%s'" % param.description,
                                     level=QgsMessageBar.WARNING)
                return None
        for output in outputs:
            if not output.hidden:
                name = str(self.valueItems[output.name].text())
                if name.strip() != '' and name != ModelerParametersDialog.ENTER_NAME:
                    alg.outputs[output.name] = ModelerOutput(name)

        selectedOptions = self.dependenciesPanel.selectedoptions
        availableDependencies = self.getAvailableDependencies()
        for selected in selectedOptions:
            alg.dependencies.append(availableDependencies[selected].name)

        self._alg.processBeforeAddingToModeler(alg, self.model)
        return alg
    def createAlgorithm(self):
        alg = Algorithm(self._alg.id())
        alg.setName(self.model)
        alg.description = self.descriptionBox.text()
        params = self._alg.parameters
        outputs = self._alg.outputs
        for param in params:
            if param.hidden:
                continue
            if not self.setParamValue(alg, param, self.wrappers[param.name]):
                self.bar.pushMessage(
                    "Error",
                    "Wrong or missing value for parameter '%s'" %
                    param.description,
                    level=QgsMessageBar.WARNING)
                return None
        for output in outputs:
            if not output.hidden:
                name = str(self.valueItems[output.name].text())
                if name.strip(
                ) != '' and name != ModelerParametersDialog.ENTER_NAME:
                    alg.outputs[output.name] = ModelerOutput(name)

        selectedOptions = self.dependenciesPanel.selectedoptions
        availableDependencies = self.getAvailableDependencies()  # spellok
        for selected in selectedOptions:
            alg.dependencies.append(
                availableDependencies[selected].name)  # spellok

        self._alg.processBeforeAddingToModeler(alg, self.model)
        return alg
Example #4
0
    def createAlgorithm(self):
        alg = Algorithm(self._alg.commandLineName())
        alg.setName(self.model)
        alg.description = self.descriptionBox.text()
        params = self._alg.parameters
        outputs = self._alg.outputs
        for param in params:
            if param.hidden:
                continue
            if not self.setParamValue(alg, param, self.valueItems[param.name]):
                return None
        for output in outputs:
            if not output.hidden:
                name = unicode(self.valueItems[output.name].text())
                if name.strip() != '' and name != ModelerParametersDialog.ENTER_NAME:
                    alg.outputs[output.name] = Output(name)

        selectedOptions = self.dependenciesPanel.selectedoptions
        availableDependencies = self.getAvailableDependencies()
        self.dependencies = []
        for selected in selectedOptions:
            s = availableDependencies[selected]
            alg.dependencies.append(s)

        return alg
Example #5
0
    def createAlgorithm(self):
        alg = Algorithm(self._alg.id())
        alg.setName(self.model)
        alg.description = self.descriptionBox.text()
        params = self._alg.parameterDefinitions()
        outputs = self._alg.outputs
        for param in params:
            if param.flags() & QgsProcessingParameterDefinition.FlagHidden:
                continue
            if not param.checkValueIsAcceptable(self.wrappers[param.name()].value):
                self.bar.pushMessage("Error", "Wrong or missing value for parameter '%s'" % param.description(),
                                     level=QgsMessageBar.WARNING)
                return None
        for output in outputs:
            if not output.flags() & QgsProcessingParameterDefinition.FlagHidden:
                name = str(self.valueItems[output.name()].text())
                if name.strip() != '' and name != ModelerParametersDialog.ENTER_NAME:
                    alg.outputs[output.name()] = ModelerOutput(name)

        selectedOptions = self.dependenciesPanel.selectedoptions
        availableDependencies = self.getAvailableDependencies()  # spellok
        for selected in selectedOptions:
            alg.dependencies.append(availableDependencies[selected].name)  # spellok

        self._alg.processBeforeAddingToModeler(alg, self.model)
        return alg
Example #6
0
    def createAlgorithm(self):
        alg = Algorithm('modelertools:calculator')
        alg.setName(self.model)
        alg.description = self.tr('Calculator',
                                  'CalculatorModelerParametersDialog')

        formula = self.formulaText.text()
        alg.params[FORMULA] = formula

        for i in xrange(AVAILABLE_VARIABLES):
            paramname = NUMBER + unicode(i)
            alg.params[paramname] = None

        numbers = self.getAvailableValuesOfType(ParameterNumber, OutputNumber)
        used = []
        for i in range(len(numbers)):
            if unicode(chr(i + 97)) in formula:
                used.append(numbers[i])

        for i, variable in enumerate(used):
            paramname = NUMBER + unicode(i)
            alg.params[paramname] = variable

        # TODO check formula is correct
        return alg
Example #7
0
 def getCopy(self):
     newone = GPFModelerAlgorithm(self.provider)
     newone.algs = {}
     for algname, alg in self.algs.iteritems():
         newone.algs[algname] = Algorithm()
         newone.algs[algname].__dict__.update(copy.deepcopy(alg.todict()))
     newone.inputs = copy.deepcopy(self.inputs)
     newone.defineCharacteristics()
     newone.name = self.name
     newone.group = self.group
     newone.descriptionFile = self.descriptionFile
     newone.helpContent = copy.deepcopy(self.helpContent)
     return newone
Example #8
0
    def testModelerAlgorithmHasDependencies(self):
        # test hasDependencies from ModelerAlgorithm

        m = ModelerAlgorithm()

        a = Algorithm("qgis:clip")
        m.addAlgorithm(a)
        a2 = Algorithm("qgis:clip")
        m.addAlgorithm(a2)

        # test parent algorithm dependency
        self.assertEqual(m.hasDependencies('QGISCLIP_1'), False)
        a2.dependencies = ['QGISCLIP_1']
        self.assertEqual(m.hasDependencies('QGISCLIP_1'), True)

        # test output algorithm dependency
        a2.dependencies = []
        a.outputs['OUTPUT'] = ModelerOutput('out')
        a2.params['INPUT'] = ValueFromOutput('QGISCLIP_1', 'OUTPUT')
        self.assertEqual(m.hasDependencies('QGISCLIP_1'), True)
Example #9
0
    def testModelerAlgorithmHasDependencies(self):
        # test hasDependencies from ModelerAlgorithm

        m = ModelerAlgorithm()

        a = Algorithm("qgis:clip")
        m.addAlgorithm(a)
        a2 = Algorithm("qgis:clip")
        m.addAlgorithm(a2)

        # test parent algorithm dependency
        self.assertEqual(m.hasDependencies('QGISCLIP_1'), False)
        a2.dependencies = ['QGISCLIP_1']
        self.assertEqual(m.hasDependencies('QGISCLIP_1'), True)

        # test output algorithm dependency
        a2.dependencies = []
        a.outputs['OUTPUT'] = ModelerOutput('out')
        a2.params['INPUT'] = ValueFromOutput('QGISCLIP_1', 'OUTPUT')
        self.assertEqual(m.hasDependencies('QGISCLIP_1'), True)
Example #10
0
    def testModelerAlgorithmHasDependencies(self):
        # test hasDependencies from ModelerAlgorithm

        m = ModelerAlgorithm()

        a = Algorithm("qgis:clip")
        m.addAlgorithm(a)
        a2 = Algorithm("qgis:clip")
        m.addAlgorithm(a2)

        # test parent algorithm dependency
        self.assertEqual(m.hasDependencies("QGISCLIP_1"), False)
        a2.dependencies = ["QGISCLIP_1"]
        self.assertEqual(m.hasDependencies("QGISCLIP_1"), True)

        # test output algorithm dependency
        a2.dependencies = []
        a.outputs["OUTPUT"] = ModelerOutput("out")
        a2.params["INPUT"] = ValueFromOutput("QGISCLIP_1", "OUTPUT")
        self.assertEqual(m.hasDependencies("QGISCLIP_1"), True)
    def createAlgorithm(self):
        alg = Algorithm('modelertools:calculator')
        alg.setName(self.model)
        alg.description = self.tr('Calculator', 'CalculatorModelerParametersDialog')

        formula = self.formulaText.text()
        alg.params[FORMULA] = formula

        for i in xrange(AVAILABLE_VARIABLES):
            paramname = NUMBER + unicode(i)
            alg.params[paramname] = None

        numbers = self.getAvailableValuesOfType(ParameterNumber, OutputNumber)
        used = []
        for i in range(len(numbers)):
            if unicode(chr(i + 97)) in formula:
                used.append(numbers[i])

        for i, variable in enumerate(used):
            paramname = NUMBER + unicode(i)
            alg.params[paramname] = variable

        # TODO check formula is correct
        return alg
Example #12
0
    def fromFile(filename, gpfAlgorithmProvider):
        try:
            tree = ET.parse(filename)
            root = tree.getroot()
            if root.tag == "graph" and "id" in root.attrib and root.attrib[
                    "id"] == "Graph":
                model = GPFModelerAlgorithm(gpfAlgorithmProvider)
                model.descriptionFile = filename
                modelConnections = {}
                inConnections = {}
                outConnections = {}
                # Process all graph nodes (algorithms)
                for node in root.findall("node"):
                    alg = gpfAlgorithmProvider.getAlgorithmFromOperator(
                        node.find("operator").text)
                    if alg is not None:
                        modelAlg = Algorithm(alg.commandLineName())
                        modelAlg.description = node.attrib["id"]
                        for param in alg.parameters:
                            modelAlg.params[param.name] = None
                            # Set algorithm parameter values
                            paramNode = node.find("parameters/" + param.name)
                            if paramNode is not None:
                                modelAlg.params[
                                    param.
                                    name] = GPFModelerAlgorithm.parseParameterValue(
                                        param, paramNode.text)
                                # Process model inputs which are saved as XML attributes
                                # of a model parameters
                                if "qgisModelInputPos" in paramNode.attrib and "qgisModelInputVars" in paramNode.attrib:
                                    modelInput = ModelerParameter()
                                    modelInput.param = copy.deepcopy(param)
                                    modelInput.param.__dict__ = ast.literal_eval(
                                        paramNode.attrib["qgisModelInputVars"])
                                    pos = paramNode.attrib[
                                        "qgisModelInputPos"].split(',')
                                    modelInput.pos = QPointF(
                                        float(pos[0]), float(pos[1]))
                                    model.addParameter(modelInput)
                                    modelAlg.params[
                                        param.name] = ValueFromInput(
                                            modelInput.param.name)

                            # Save the connections between nodes in the model
                            # Once all the nodes have been processed they will be processed
                            if node.find("sources/" + param.name) is not None:
                                refid = node.find("sources/" +
                                                  param.name).attrib["refid"]
                                modelConnections[refid] = (modelAlg,
                                                           param.name)

                            # Special treatment for Read operator since it provides
                            # the main raster input to the graph
                            if alg.operator == "Read":
                                param = getParameterFromString(
                                    "ParameterRaster|file|Source product")
                                modelParameter = ModelerParameter(
                                    param, QPointF(0, 0))
                                model.addParameter(modelParameter)
                                modelAlg.params["file"] = ValueFromInput(
                                    "file")
                                inConnections[modelAlg] = modelParameter

                            # Special treatment for Write operator since it provides
                            # the main raster output from the graph
                            if alg.operator == "Write":
                                modelOutput = ModelerOutput("Output file")
                                modelOutput.pos = QPointF(0, 0)
                                modelAlg.outputs["file"] = modelOutput
                                outConnections[modelAlg] = modelOutput

                        model.addAlgorithm(modelAlg)
                    else:
                        raise Exception("Unknown operator " +
                                        node.find("operator").text)

                # Set up connections between nodes of the graph
                for connection in modelConnections:
                    for alg in model.algs.values():
                        if alg.description == connection:
                            modelAlg = modelConnections[connection][0]
                            paramName = modelConnections[connection][1]
                            modelAlg.params[paramName] = ValueFromOutput(
                                alg.name, "-out")
                            break

                presentation = root.find('applicationData[@id="Presentation"]')
                # Set the model name and group
                model.name = presentation.attrib[
                    "name"] if "name" in presentation.attrib.keys(
                    ) else os.path.splitext(os.path.basename(filename))[0]
                model.group = presentation.attrib[
                    "group"] if "group" in presentation.attrib.keys(
                    ) else "Uncategorized"
                # Place the nodes on the graph canvas
                for alg in model.algs.values():
                    position = presentation.find('node[@id="' +
                                                 alg.description +
                                                 '"]/displayPosition')
                    if position is not None:
                        alg.pos = QPointF(float(position.attrib["x"]),
                                          float(position.attrib["y"]))
                        # For algorithms that have input or output model parameters set those parameters
                        # in position relative to the algorithm
                        if alg in inConnections:
                            inConnections[alg].pos = QPointF(
                                max(alg.pos.x() - 50, 0),
                                max(alg.pos.y() - 50, 0))
                        if alg in outConnections:
                            outConnections[alg].pos = QPointF(
                                alg.pos.x() + 50,
                                alg.pos.y() + 50)
                return model
        except Exception, e:
            raise WrongModelException("Error reading GPF XML file: " + str(e))