Пример #1
0
    def processAlgorithm(self, key, progress):
        # Create a GFP for execution with SNAP's GPT
        graph = ET.Element("graph", {'id':self.operator+'_gpf'})
        version = ET.SubElement(graph, "version")
        version.text = "1.0"

        # Add node with this algorithm's operator
        graph = self.addGPFNode(graph)

        # Add outputs as write nodes (except for Write operator)
        if self.operator != "Write" and len(self.outputs) >= 1:
            for output in self.outputs:
                graph = self.addWriteNode(graph, output, key)

        # Log the GPF
        loglines = []
        loglines.append("GPF Graph")
        GPFUtils.indentXML(graph)
        for line in ET.tostring(graph).splitlines():
            loglines.append(line)
        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)

        # Execute the GPF
        GPFUtils.executeGpf(key, ET.tostring(graph), progress)
Пример #2
0
    def toXml(self, forExecution=False):
        graph = ET.Element("graph", {'id': "Graph"})
        version = ET.SubElement(graph, "version")
        version.text = "1.0"

        # If the XML is made to be saved then set parameters and outputs.
        # If it is made for execution then parameters and outputs are already set.
        if not forExecution:
            self.defineCharacteristics()

        # Set the connections between nodes
        for alg in self.algs.values():
            for param in alg.params.values():
                if isinstance(param, ValueFromOutput):
                    alg.algorithm.getParameterFromName(
                        "sourceProduct").setValue(
                            self.algs[param.alg].algorithm.nodeID)

        # Save model algorithms
        for alg in self.algs.values():
            self.prepareAlgorithm(alg)

            # Only Write operators can save raster outputs
            for out in alg.algorithm.outputs:
                if alg.algorithm.operator != "Write" and isinstance(
                        out, OutputRaster):
                    if out.name in alg.outputs:
                        QMessageBox.warning(
                            None, self.tr('Unable to save model'),
                            self.
                            tr('Output rasters can only be saved by Write operator. Remove the value of raster output in %s algorithm or add a Write operator'
                               % (alg.algorithm.operator, )))
                        return

            graph = alg.algorithm.addGPFNode(graph)
            # Save also the position and settings of model inputs.
            # They are saved as attributes of relevant parameter XML nodes.
            # This way they do not interfere with the model when it's opened
            # in SNAP.
            if alg.algorithm.operator != "Read":
                for param in alg.params.keys():
                    paramValue = str(alg.params[param])
                    if paramValue in self.inputs.keys():
                        # Only Read operators can read raster inputs
                        if param == "sourceProduct":
                            QMessageBox.warning(
                                None, self.tr('Unable to save model'),
                                self.
                                tr('Input rasters can only be loaded by Read operator. Change the value of raster input in %s algorithm to an output of another algorithm'
                                   % (alg.algorithm.operator, )))
                            return
                        paramTag = graph.find('node[@id="' +
                                              alg.algorithm.nodeID +
                                              '"]/parameters/' + param)
                        if paramTag is not None:
                            pos = self.inputs[paramValue].pos
                            paramTag.attrib["qgisModelInputPos"] = str(
                                pos.x()) + "," + str(pos.y())
                            paramTag.attrib["qgisModelInputVars"] = str(
                                self.inputs[paramValue].param.todict())

        # Save model layout
        presentation = ET.SubElement(graph, "applicationData", {
            "id": "Presentation",
            "name": self.name,
            "group": self.group
        })
        ET.SubElement(presentation, "Description")
        for alg in self.algs.values():
            node = ET.SubElement(presentation, "node",
                                 {"id": alg.algorithm.nodeID})
            ET.SubElement(node, "displayPosition", {
                "x": str(alg.pos.x()),
                "y": str(alg.pos.y())
            })

        # Make it look nice in text file
        GPFUtils.indentXML(graph)

        return ET.tostring(graph)