def fromJson(s): def fromdict(d): try: fullClassName = d["class"] if isinstance(fullClassName, str): tokens = fullClassName.split(".") else: tokens = fullClassName.__class__.__name__.split(".") className = tokens[-1] moduleName = ".".join(tokens[:-1]) values = d["values"] if className == "point": return QPointF(values["x"], values["y"]) def _import(name): __import__(name) return sys.modules[name] if moduleName.startswith("processing.parameters"): moduleName = "processing.core.parameters" module = _import(moduleName) clazz = getattr(module, className) instance = clazz() for k, v in list(values.items()): instance.__dict__[k] = v return instance except KeyError: return d except Exception as e: raise e try: model = json.loads(s, object_hook=fromdict) except Exception as e: raise WrongModelException(e.args[0]) return model
def fromJson(s): def fromdict(d): try: fullClassName = d["class"] if isinstance(fullClassName, str): tokens = fullClassName.split(".") else: tokens = fullClassName.__class__.__name__.split(".") className = tokens[-1] moduleName = ".".join(tokens[:-1]) values = d["values"] if className == "point": return QPointF(values["x"], values["y"]) def _import(name): __import__(name) return sys.modules[name] if moduleName.startswith("processing.parameters"): moduleName = "processing.core.parameters" module = _import(moduleName) clazz = getattr(module, className) instance = clazz() for k, v in list(values.items()): # upgrade old model files if k == 'group': k = '_group' elif k == 'name': instance.__dict__['_name'] = v k = 'modeler_name' if not issubclass(clazz, GeoAlgorithm): instance.__dict__['name'] = v instance.__dict__[k] = v return instance except KeyError: return d except Exception as e: raise e try: model = json.loads(s, object_hook=fromdict) except Exception as e: raise WrongModelException(e.args[0]) if hasattr(model, "modeler_name"): model._name = model.modeler_name return model
def fromOldFormatFile(filename): def _tr(s): return QCoreApplication.translate('ModelerAlgorithm', s) hardcodedValues = {} modelParameters = [] modelAlgs = [] model = ModelerAlgorithm() model.descriptionFile = filename lines = codecs.open(filename, 'r', encoding='utf-8') line = lines.readline().strip('\n').strip('\r') try: while line != '': if line.startswith('PARAMETER:'): paramLine = line[len('PARAMETER:'):] param = getParameterFromString(paramLine) if param: pass else: raise WrongModelException( _tr('Error in parameter line: %s', 'ModelerAlgorithm') % line) line = lines.readline().strip('\n') tokens = line.split(',') model.addParameter(ModelerParameter(param, QPointF(float(tokens[0]), float(tokens[1])))) modelParameters.append(param.name) elif line.startswith('VALUE:'): valueLine = line[len('VALUE:'):] tokens = valueLine.split('===') name = tokens[0] value = tokens[1].replace(ModelerAlgorithm.LINE_BREAK_STRING, '\n') hardcodedValues[name] = value elif line.startswith('NAME:'): model.name = line[len('NAME:'):] elif line.startswith('GROUP:'): model.group = line[len('GROUP:'):] elif line.startswith('ALGORITHM:'): algLine = line[len('ALGORITHM:'):] alg = algList.getAlgorithm(algLine) if alg is not None: modelAlg = Algorithm(alg.commandLineName()) modelAlg.description = alg.name posline = lines.readline().strip('\n').strip('\r') tokens = posline.split(',') modelAlg.pos = QPointF(float(tokens[0]), float(tokens[1])) # dependenceline = lines.readline().strip('\n').strip('\r') for param in alg.parameters: if not param.hidden: line = lines.readline().strip('\n').strip('\r') if line == unicode(None): modelAlg.params[param.name] = None else: tokens = line.split('|') try: algIdx = int(tokens[0]) except: raise WrongModelException( _tr('Number of parameters in the ' '{} algorithm does not match ' 'current Processing ' 'implementation'.format(alg.name))) if algIdx == -1: if tokens[1] in modelParameters: modelAlg.params[param.name] = ValueFromInput(tokens[1]) else: modelAlg.params[param.name] = hardcodedValues[tokens[1]] else: modelAlg.params[param.name] = ValueFromOutput(algIdx, tokens[1]) for out in alg.outputs: if not out.hidden: line = lines.readline().strip('\n').strip('\r') if unicode(None) != line: if '|' in line: tokens = line.split('|') name = tokens[0] tokens = tokens[1].split(',') pos = QPointF(float(tokens[0]), float(tokens[1])) else: name = line pos = None modelerOutput = ModelerOutput(name) modelerOutput.pos = pos modelAlg.outputs[out.name] = modelerOutput model.addAlgorithm(modelAlg) modelAlgs.append(modelAlg.name) else: raise WrongModelException( _tr('Error in algorithm name: %s',) % algLine) line = lines.readline().strip('\n').strip('\r') for modelAlg in model.algs.values(): for name, value in modelAlg.params.iteritems(): if isinstance(value, ValueFromOutput): value.alg = modelAlgs[value.alg] return model except Exception as e: if isinstance(e, WrongModelException): raise e else: raise WrongModelException(_tr('Error in model definition line: ') + '%s\n%s' % (line.strip(), traceback.format_exc()))
def _import(name): __import__(name) return sys.modules[name] if moduleName.startswith("processing.parameters"): moduleName = "processing.core.parameters" module = _import(moduleName) clazz = getattr(module, className) instance = clazz() for k, v in values.iteritems(): instance.__dict__[k] = v return instance except KeyError: return d except Exception, e: raise e try: model = json.loads(s, object_hook=fromdict) except Exception, e: raise WrongModelException(e.args[0]) return model @staticmethod def fromJsonFile(filename): with open(filename) as f: s = f.read() alg = ModelerAlgorithm.fromJson(s) alg.descriptionFile = filename return alg
def fromOldFormatFile(filename): hardcodedValues = {} modelParameters = [] modelAlgs = [] model = ModelerAlgorithm() model.descriptionFile = filename lines = codecs.open(filename, 'r', encoding='utf-8') line = lines.readline().strip('\n').strip('\r') try: while line != '': if line.startswith('PARAMETER:'): paramLine = line[len('PARAMETER:'):] param = getParameterFromString(paramLine) if param: pass else: raise WrongModelException('Error in parameter line: ' + line) line = lines.readline().strip('\n') tokens = line.split(',') model.addParameter( ModelerParameter( param, QtCore.QPointF(float(tokens[0]), float(tokens[1])))) modelParameters.append(param.name) elif line.startswith('VALUE:'): valueLine = line[len('VALUE:'):] tokens = valueLine.split('===') name = tokens[0] value = tokens[1].replace( ModelerAlgorithm.LINE_BREAK_STRING, '\n') hardcodedValues[name] = value elif line.startswith('NAME:'): model.name = line[len('NAME:'):] elif line.startswith('GROUP:'): model.group = line[len('GROUP:'):] elif line.startswith('ALGORITHM:'): algLine = line[len('ALGORITHM:'):] alg = ModelerUtils.getAlgorithm(algLine) if alg is not None: modelAlg = Algorithm(alg.commandLineName()) modelAlg.description = alg.name posline = lines.readline().strip('\n').strip('\r') tokens = posline.split(',') modelAlg.pos = QtCore.QPointF(float(tokens[0]), float(tokens[1])) dependenceline = lines.readline().strip('\n').strip( '\r') #unused for param in alg.parameters: if not param.hidden: line = lines.readline().strip('\n').strip('\r') if line == str(None): modelAlg.params[param.name] = None else: tokens = line.split('|') algIdx = int(tokens[0]) if algIdx == -1: if tokens[1] in modelParameters: modelAlg.params[ param.name] = ValueFromInput( tokens[1]) else: modelAlg.params[ param.name] = hardcodedValues[ tokens[1]] else: modelAlg.params[ param.name] = ValueFromOutput( algIdx, tokens[1]) for out in alg.outputs: if not out.hidden: line = lines.readline().strip('\n').strip('\r') if str(None) != line: if '|' in line: tokens = line.split('|') name = tokens[0] tokens = tokens[1].split(',') pos = QtCore.QPointF( float(tokens[0]), float(tokens[1])) else: name = line pos = None modelerOutput = ModelerOutput(name) modelerOutput.pos = pos modelAlg.outputs[out.name] = modelerOutput model.addAlgorithm(modelAlg) modelAlgs.append(modelAlg.name) else: raise WrongModelException('Error in algorithm name: ' + algLine) line = lines.readline().strip('\n').strip('\r') for modelAlg in model.algs.values(): for name, value in modelAlg.params.iteritems(): if isinstance(value, ValueFromOutput): value.alg = modelAlgs[value.alg] return model except Exception, e: if isinstance(e, WrongModelException): raise e else: raise WrongModelException('Error in model definition line:' + line.strip() + ' : ' + traceback.format_exc())
def openModel(self, filename): self.algPos = [] self.paramPos = [] self.outputOutputs = [] self.algs = [] self.algParameters = [] self.algOutputs = [] self.paramValues = {} self.dependencies = [] self.descriptionFile = filename lines = codecs.open(filename, 'r', encoding='utf-8') line = lines.readline().strip('\n').strip('\r') iAlg = 0 try: while line != '': if line.startswith('PARAMETER:'): paramLine = line[len('PARAMETER:'):] param = ParameterFactory.getFromString(paramLine) if param: self.parameters.append(param) else: raise WrongModelException('Error in parameter line: ' + line) line = lines.readline().strip('\n') tokens = line.split(',') self.paramPos.append( QtCore.QPointF(float(tokens[0]), float(tokens[1]))) elif line.startswith('VALUE:'): valueLine = line[len('VALUE:'):] tokens = valueLine.split('===') self.paramValues[tokens[0]] = \ tokens[1].replace(ModelerAlgorithm.LINE_BREAK_STRING, '\n') elif line.startswith('NAME:'): self.name = line[len('NAME:'):] elif line.startswith('GROUP:'): self.group = line[len('GROUP:'):] if self.group == '[Test models]': self.showInModeler = False self.showInToolbox = False elif line.startswith('ALGORITHM:'): algParams = {} algOutputs = {} algLine = line[len('ALGORITHM:'):] alg = ModelerUtils.getAlgorithm(algLine) if alg is not None: posline = lines.readline().strip('\n').strip('\r') tokens = posline.split(',') self.algPos.append( QtCore.QPointF(float(tokens[0]), float(tokens[1]))) self.algs.append(alg) dependenceline = lines.readline().strip('\n').strip( '\r') dependencies = [] if dependenceline != str(None): for index in dependenceline.split(','): try: dependencies.append(int(index)) except: # A quick fix while I figure out # how to solve problems when # parsing this pass for param in alg.parameters: line = lines.readline().strip('\n').strip('\r') if line == str(None): algParams[param.name] = None else: tokens = line.split('|') algParams[param.name] = \ AlgorithmAndParameter(int(tokens[0]), tokens[1]) outputPos = {} for out in alg.outputs: line = lines.readline().strip('\n').strip('\r') if str(None) != line: if '|' in line: tokens = line.split('|') name = tokens[0] tokens = tokens[1].split(',') outputPos[out.name] = QtCore.QPointF( float(tokens[0]), float(tokens[1])) else: name = line outputPos[out.name] = None algOutputs[out.name] = name # We add the output to the algorithm, # with a name indicating where it comes # from that guarantees that the name is # unique output = copy.deepcopy(out) output.description = name output.name = self.getSafeNameForOutput( iAlg, output) self.addOutput(output) else: algOutputs[out.name] = None self.outputPos.append(outputPos) self.algOutputs.append(algOutputs) self.algParameters.append(algParams) self.dependencies.append(dependencies) iAlg += 1 else: raise WrongModelException('Error in algorithm name: ' + algLine) line = lines.readline().strip('\n').strip('\r') except Exception, e: if isinstance(e, WrongModelException): raise e else: raise WrongModelException('Error in model definition line:' + line.strip() + ' : ' + traceback.format_exc())
def openModel(self, filename): self.algPos = [] self.paramPos = [] self.outputOutputs = [] self.algs = [] self.algParameters = [] self.algOutputs = [] self.paramValues = {} self.dependencies = [] self.descriptionFile = filename lines = codecs.open(filename, "r", encoding='utf-8') line = lines.readline().strip("\n").strip("\r") iAlg = 0 try: while line != "": if line.startswith("PARAMETER:"): paramLine = line[len("PARAMETER:"):] param = ParameterFactory.getFromString(paramLine) if param: self.parameters.append(param) else: raise WrongModelException("Error in parameter line: " + line) line = lines.readline().strip("\n") tokens = line.split(",") self.paramPos.append( QtCore.QPointF(float(tokens[0]), float(tokens[1]))) elif line.startswith("VALUE:"): valueLine = line[len("VALUE:"):] tokens = valueLine.split("===") self.paramValues[tokens[0]] = tokens[1].replace( ModelerAlgorithm.LINE_BREAK_STRING, '\n') elif line.startswith("NAME:"): self.name = line[len("NAME:"):] elif line.startswith("GROUP:"): self.group = line[len("GROUP:"):] elif line.startswith("ALGORITHM:"): algParams = {} algOutputs = {} algLine = line[len("ALGORITHM:"):] alg = ModelerUtils.getAlgorithm(algLine) if alg is not None: posline = lines.readline().strip("\n").strip("\r") tokens = posline.split(",") self.algPos.append( QtCore.QPointF(float(tokens[0]), float(tokens[1]))) self.algs.append(alg) dependenceline = lines.readline().strip("\n").strip( "\r") dependencies = [] if dependenceline != str(None): for index in dependenceline.split(","): try: dependencies.append(int(index)) except: pass #a quick fix while I figure out how to solve problems when parsing this for param in alg.parameters: line = lines.readline().strip("\n").strip("\r") if line == str(None): algParams[param.name] = None else: tokens = line.split("|") algParams[param.name] = AlgorithmAndParameter( int(tokens[0]), tokens[1]) outputPos = {} for out in alg.outputs: line = lines.readline().strip("\n").strip("\r") if str(None) != line: if "|" in line: tokens = line.split("|") name = tokens[0] tokens = tokens[1].split(",") outputPos[out.name] = QtCore.QPointF( float(tokens[0]), float(tokens[1])) else: name = line outputPos[out.name] = None algOutputs[out.name] = name #we add the output to the algorithm, with a name indicating where it comes from #that guarantees that the name is unique output = copy.deepcopy(out) output.description = name output.name = self.getSafeNameForOutput( iAlg, output) self.addOutput(output) else: algOutputs[out.name] = None self.outputPos.append(outputPos) self.algOutputs.append(algOutputs) self.algParameters.append(algParams) self.dependencies.append(dependencies) iAlg += 1 else: raise WrongModelException("Error in algorithm name: " + algLine) line = lines.readline().strip("\n").strip("\r") except Exception, e: if isinstance(e, WrongModelException): raise e else: raise WrongModelException("Error in model definition line:" + line.strip() + " : " + traceback.format_exc())
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))