def getAvailableValuesOfType(self, paramType, outType=None, dataType=None): # upgrade paramType to list if type(paramType) is not list: paramType = [paramType] values = [] inputs = self.model.inputs for i in list(inputs.values()): param = i.param for t in paramType: if isinstance(param, t): if dataType is not None: if param.datatype in dataType: values.append(ValueFromInput(param.name)) else: values.append(ValueFromInput(param.name)) break if outType is None: return values if self._algName is None: dependent = [] else: dependent = self.model.getDependentAlgorithms(self._algName) for alg in list(self.model.algs.values()): if alg.modeler_name not in dependent: for out in alg.algorithm.outputs: if isinstance(out, outType): if dataType is not None and out.datatype in dataType: values.append( ValueFromOutput(alg.modeler_name, out.name)) else: values.append( ValueFromOutput(alg.modeler_name, out.name)) return values
def getAvailableValuesOfType(self, paramType, outType=None, dataType=None): values = [] inputs = self.model.inputs for i in inputs.values(): param = i.param if isinstance(param, paramType): if dataType is not None and param.datatype in dataType: values.append(ValueFromInput(param.name)) else: values.append(ValueFromInput(param.name)) if outType is None: return values if self._algName is None: dependent = [] else: dependent = self.model.getDependentAlgorithms(self._algName) for alg in self.model.algs.values(): if alg.name not in dependent: for out in alg.algorithm.outputs: if isinstance(out, outType): if dataType is not None and out.datatype in dataType: values.append(ValueFromOutput(alg.name, out.name)) else: values.append(ValueFromOutput(alg.name, out.name)) return values
def getValue(self): value = self.leText.text() values = [] for param in self.modelParametersDialog.model.parameters: if isinstance(param, ParameterNumber): if "@" + param.name in value: values.append(ValueFromInput(param.name)) for alg in list(self.modelParametersDialog.model.algs.values()): for out in alg.algorithm.outputs: if isinstance(out, OutputNumber) and "@%s_%s" % (alg.name(), out.name) in value: values.append(ValueFromOutput(alg.name(), out.name)) if values: return CompoundValue(values, value) else: return value
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 processBeforeAddingToModeler(self, algorithm, model): values = [] expression = algorithm.params[self.EXPRESSION] for i in list(model.inputs.values()): param = i.param if isinstance(param, ParameterRaster) and "{}@".format(param.name) in expression: values.append(ValueFromInput(param.name)) if algorithm.name: dependent = model.getDependentAlgorithms(algorithm.name) else: dependent = [] for alg in list(model.algs.values()): if alg.name not in dependent: for out in alg.algorithm.outputs: if (isinstance(out, OutputRaster) and "{}:{}@".format(alg.name, out.name) in expression): values.append(ValueFromOutput(alg.name, out.name)) algorithm.params[self.LAYERS] = values
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))