def testScriptCode(self): parameter = ParameterString("myName", "myDescription", default="test") code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterString) self.assertEqual(result.default, parameter.default) parameter.default = None parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterString) self.assertTrue(result.optional) self.assertEqual(result.default, parameter.default) parameter.default = "None" code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterString) self.assertTrue(result.optional) self.assertEqual(result.default, parameter.default) parameter.default = "It's Mario" code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterString) self.assertTrue(result.optional) self.assertEqual(result.default, parameter.default)
def testScriptCode(self): parameter = ParameterString('myName', 'myDescription', default='test') code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterString) self.assertEqual(result.default, parameter.default) parameter.default = None parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterString) self.assertTrue(result.optional) self.assertEqual(result.default, parameter.default) parameter.default = 'None' code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterString) self.assertTrue(result.optional) self.assertEqual(result.default, parameter.default) parameter.default = 'It\'s Mario' code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterString) self.assertTrue(result.optional) self.assertEqual(result.default, parameter.default)
def testScriptCode(self): parameter = ParameterTable("myName", "myDesc") code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterTable) parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterTable) self.assertTrue(result.optional)
def testScriptCode(self): parameter = ParameterMultipleInput('myName', 'myDescription') code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertTrue(isinstance(result, ParameterMultipleInput)) parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertTrue(isinstance(result, ParameterMultipleInput)) self.assertTrue(result.optional)
def testScriptCode(self): parameter = ParameterExpression('myName', 'myDescription', default='test') code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertTrue(isinstance(result, ParameterExpression)) parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertTrue(isinstance(result, ParameterExpression)) self.assertTrue(result.optional)
def testScriptCode(self): parameter = ParameterNumber('myName', 'myDescription') code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterNumber) parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterNumber) self.assertTrue(result.optional)
def testScriptCode(self): parameter = ParameterExpression("myName", "myDescription", default="test") code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterExpression) parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterExpression) self.assertTrue(result.optional) self.assertEquals(result.default, parameter.default)
def testScriptCode(self): parameter = ParameterBoolean('myName', 'myDescription') code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertTrue(isinstance(result, ParameterBoolean)) self.assertFalse(result.optional) parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertTrue(isinstance(result, ParameterBoolean)) self.assertTrue(result.optional)
def testScriptCode(self): parent_name = "test_parent_layer" test_data = points() test_layer = QgsVectorLayer(test_data, parent_name, "ogr") parameter = ParameterTableField("myName", "myDesc", parent_name) code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterTableField) parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertIsInstance(result, ParameterTableField) self.assertTrue(result.optional)
def regroupRasters(alg, parameters, field, groupField, subgroupField=None, extFile=None): """ Group multiple input rasters into a group * If there is a subgroupField, a subgroup will automatically be created. * When an external file is provided, the file is copied into the respective directory of the subgroup. * extFile is a dict of the form 'parameterName':'directory name'. :param parameters: """ # List of rasters names new_parameters = deepcopy(parameters) rasters = alg.getParameterFromName(field) rastersList = rasters.value.split(';') del new_parameters[field] # Insert a i.group command group = getParameterFromString("ParameterString|{}|group of rasters|None|False|False".format(groupField)) new_parameters[group.name()] = alg.getTempFilename() if subgroupField: subgroup = getParameterFromString("ParameterString|{}|subgroup of rasters|None|False|False".format(subgroupField)) new_parameters[subgroup.name()] = alg.getTempFilename() command = 'i.group group={}{} input={}'.format( new_parameters[group.name()], ' subgroup={}'.format(new_parameters[subgroup.name()]) if subgroupField else '', ','.join([alg.exportedLayers[f] for f in rastersList]) ) alg.commands.append(command) # Handle external files if subgroupField and extFile: for ext in list(extFile.keys()): extFileName = new_parameters[ext] if extFileName: shortFileName = path.basename(extFileName) destPath = path.join(Grass7Utils.grassMapsetFolder(), 'PERMANENT', 'group', new_parameters[group.name()], 'subgroup', new_parameters[subgroup.name()], extFile[ext], shortFileName) copyFile(alg, extFileName, destPath) new_parameters[ext] = shortFileName # modify parameters values alg.processCommand(new_parameters) return group.value
def processDescriptionParameterLine(self, line): try: if line.startswith('Parameter'): self.addParameter(getParameterFromString(line)) elif line.startswith('*Parameter'): param = getParameterFromString(line[1:]) param.isAdvanced = True self.addParameter(param) else: self.addOutput(getOutputFromString(line)) except Exception: raise WrongScriptException( self.tr('Could not load script: %s.\n' 'Problem with line %d', 'ScriptAlgorithm') % (self.descriptionFile or '', line))
def defineCharacteristicsFromFile(self): with open(self.descriptionFile) as content: dom_model = ET.fromstring(content.read()) self.appkey = dom_model.find("key").text self.cliName = dom_model.find("exec").text self.name = dom_model.find("longname").text self.i18n_name = QCoreApplication.translate("OTBAlgorithm", self.name) self.group = dom_model.find("group").text self.i18n_group = QCoreApplication.translate("OTBAlgorithm", self.group) rebu = None the_result = None try: rebu = self.get_list_from_node(dom_model) the_result = list(map(self.adapt_list_to_string, rebu)) except Exception as e: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, self.tr("Could not open OTB algorithm: %s\n%s" % (self.descriptionFile, traceback.format_exc())), ) raise e for line in the_result: try: if line.startswith("Parameter") or line.startswith("*Parameter"): if line.startswith("*Parameter"): param = getParameterFromString(line[1:]) param.isAdvanced = True else: param = getParameterFromString(line) # Hack for initializing the elevation parameters from Processing configuration if param.name == "-elev.dem.path" or param.name == "-elev.dem" or "elev.dem" in param.name: param.default = OTBUtils.otbSRTMPath() elif param.name == "-elev.dem.geoid" or param.name == "-elev.geoid" or "elev.geoid" in param.name: param.default = OTBUtils.otbGeoidPath() self.addParameter(param) elif line.startswith("Extent"): self.addParameter(ParameterExtent(self.REGION_OF_INTEREST, "Region of interest", "0,1,0,1")) self.hasROI = True else: self.addOutput(getOutputFromString(line)) except Exception as e: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, self.tr("Could not open OTB algorithm: %s\n%s" % (self.descriptionFile, line)), ) raise e
def testScriptCode(self): parent_name = 'test_parent_layer' test_data = points() test_layer = QgsVectorLayer(test_data, parent_name, 'ogr') parameter = ParameterTableField( 'myName', 'myDesc', parent_name) code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertTrue(isinstance(result, ParameterTableField)) parameter.optional = True code = parameter.getAsScriptCode() result = getParameterFromString(code) self.assertTrue(isinstance(result, ParameterTableField)) self.assertTrue(result.optional)
def processDescriptionParameterLine(self, line): try: if line.startswith('Parameter'): self.addParameter(getParameterFromString(line)) elif line.startswith('*Parameter'): param = getParameterFromString(line[1:]) param.isAdvanced = True self.addParameter(param) else: self.addOutput(getOutputFromString(line)) except Exception: raise WrongScriptException('Could not load script:' + self.descriptionFile or '' + '.\n Problem with line "' + line + '"' )
def defineCharacteristicsFromFile(self): lines = open(self.descriptionFile) line = lines.readline().strip('\n').strip() self.grassName = line line = lines.readline().strip('\n').strip() self.name = line self.i18n_name = QCoreApplication.translate("GrassAlgorithm", line) if " - " not in self.name: self.name = self.grassName + " - " + self.name self.i18n_name = self.grassName + " - " + self.i18n_name line = lines.readline().strip('\n').strip() self.group = line self.i18n_group = QCoreApplication.translate("GrassAlgorithm", line) hasRasterOutput = False hasVectorInput = False vectorOutputs = 0 line = lines.readline().strip('\n').strip() while line != '': try: line = line.strip('\n').strip() if line.startswith('Parameter'): parameter = getParameterFromString(line) self.addParameter(parameter) if isinstance(parameter, ParameterVector): hasVectorInput = True if isinstance(parameter, ParameterMultipleInput) \ and parameter.datatype < 3: hasVectorInput = True elif line.startswith('*Parameter'): param = getParameterFromString(line[1:]) param.isAdvanced = True self.addParameter(param) else: output = getOutputFromString(line) self.addOutput(output) if isinstance(output, OutputRaster): hasRasterOutput = True elif isinstance(output, OutputVector): vectorOutputs += 1 if isinstance(output, OutputHTML): self.addOutput(OutputFile("rawoutput", output.description + " (raw output)", "txt")) line = lines.readline().strip('\n').strip() except Exception, e: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, self.tr('Could not open GRASS algorithm: %s.\n%s' % (self.descriptionFile, line))) raise e
def defineCharacteristicsFromFile(self): lines = open(self.descriptionFile) line = lines.readline().strip("\n").strip() self.name = line line = lines.readline().strip("\n").strip() self.cmdName = line line = lines.readline().strip("\n").strip() self.group = line line = lines.readline().strip("\n").strip() while line != "": try: line = line.strip("\n").strip() if line.startswith("Parameter"): param = getParameterFromString(line) self.addParameter(param) else: self.addOutput(getOutputFromString(line)) line = lines.readline().strip("\n").strip() except Exception as e: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, self.tr("Could not load TauDEM algorithm: %s\n%s" % (self.descriptionFile, line)), ) raise e lines.close()
def defineCharacteristicsFromFile(self): lines = open(self.descriptionFile) line = lines.readline().strip('\n').strip() self.name = line self.i18n_name = QCoreApplication.translate("TAUDEMAlgorithm", line) line = lines.readline().strip('\n').strip() self.cmdName = line line = lines.readline().strip('\n').strip() self.group = line self.i18n_group = QCoreApplication.translate("TAUDEMAlgorithm", line) line = lines.readline().strip('\n').strip() while line != '': try: line = line.strip('\n').strip() if line.startswith('Parameter'): param = getParameterFromString(line) self.addParameter(param) else: self.addOutput(getOutputFromString(line)) line = lines.readline().strip('\n').strip() except Exception as e: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, self.tr('Could not load TauDEM algorithm: %s\n%s' % (self.descriptionFile, line))) raise e lines.close()
def defineCharacteristicsFromFile(self): lines = open(self.descriptionFile) line = lines.readline().strip("\n").strip() self.name = line if "|" in self.name: tokens = self.name.split("|") self.name = tokens[0] self.cmdname = tokens[1] else: self.cmdname = self.name self.name = self.name[0].upper() + self.name[1:].lower() line = lines.readline().strip("\n").strip() self.undecoratedGroup = line self.group = SagaGroupNameDecorator.getDecoratedName(self.undecoratedGroup) line = lines.readline().strip("\n").strip() while line != "": if line.startswith("Hardcoded"): self.hardcodedStrings.append(line[len("Harcoded|") + 1 :]) elif line.startswith("Parameter"): self.addParameter(getParameterFromString(line)) elif line.startswith("AllowUnmatching"): self.allowUnmatchingGridExtents = True elif line.startswith("Extent"): # An extent parameter that wraps 4 SAGA numerical parameters self.extentParamNames = line[6:].strip().split(" ") self.addParameter(ParameterExtent(self.OUTPUT_EXTENT, "Output extent", "0,1,0,1")) else: self.addOutput(getOutputFromString(line)) line = lines.readline().strip("\n").strip() lines.close()
def defineCharacteristicsFromFile(self): with codecs.open(self.descriptionFile, encoding='utf-8') as f: line = f.readline().strip('\n').strip() self.name = line self.i18n_name = self.tr(line) line = f.readline().strip('\n').strip() self.cmdName = line line = f.readline().strip('\n').strip() self.group = line self.i18n_group = self.tr(line) line = f.readline().strip('\n').strip() while line != '': try: line = line.strip('\n').strip() if line.startswith('Parameter'): param = getParameterFromString(line) self.addParameter(param) else: self.addOutput(getOutputFromString(line)) line = f.readline().strip('\n').strip() except Exception as e: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, self.tr('Could not load TauDEM algorithm: {}\n{}'.format(self.descriptionFile, line))) raise e
def processCommand(alg): # handle inline add data input_txt = alg.getParameterFromName('inline_points') inputParameter = alg.getParameterFromName('points') if input_txt.value: # Creates a temporary txt file ruleFile = alg.getTempFilename() # Inject rules into temporary txt file with open(ruleFile, "w") as tempRules: tempRules.write(input_txt.value) inputParameter.value = ruleFile alg.parameters.remove(input_txt) # exclude output for from_output output = alg.getOutputFromName('rmsfile') alg.removeOutputFromName('rmsfile') # Create a false input parameter for rmsfile param = getParameterFromString(u"ParameterString|rmsfile|the file|None|False|False") param.value = output.value alg.addParameter(param) alg.processCommand() alg.parameters.remove(param) alg.addOutput(output) if input_txt.value: inputParameter.value = None alg.addParameter(input_txt)
def processCommand(alg): # Creates a new location with the CRS crsParam = alg.getParameterFromName('crs') crsId = int(crsParam.value[5:]) #QgsMessageLog.logMessage('crs = {}'.format(crs), 'DEBUG', QgsMessageLog.INFO) crs = QgsCoordinateReferenceSystem() crs.createFromId(crsId, QgsCoordinateReferenceSystem.EpsgCrsId) command = "g.proj proj4=\"{}\" location=TARGET".format(crs.toProj4()) alg.commands.append(command) alg.parameters.remove(crsParam) # Regroup rasters rasters = alg.getParameterFromName('rasters') rastersList = rasters.value.split(';') alg.parameters.remove(rasters) # Insert a i.group command group = getParameterFromString("ParameterString|group|group of rasters|None|False|False") group.value = alg.getTempFilename() alg.addParameter(group) command = 'i.group group={} input={}'.format( group.value, ','.join([alg.exportedLayers[f] for f in rastersList]) ) alg.commands.append(command) # Handle POINT File gcp = alg.getParameterFromName('gcp') extFileName = gcp.value destPath = path.join(Grass7Utils.grassMapsetFolder(), 'PERMANENT', 'group', group.value, 'POINTS') copyFile(alg, extFileName, destPath) alg.parameters.remove(gcp) # Add a target destination for our group command = "i.target group={} location=TARGET mapset=PERMANENT".format(group.value) alg.commands.append(command) # remove output output = alg.getOutputFromName('output') alg.removeOutputFromName('output') # Add an extension #extension = getParameterFromString("ParameterString|extension|Output raster map(s) suffix|None|False|False") #extension.value = "rectified" #alg.addParameter(extension) # modify parameters values alg.processCommand() # Re-add input rasters alg.addParameter(rasters) alg.addParameter(gcp) alg.addParameter(crs) # Re-add output alg.addOutput(output)
def processParameterLine(self, line): param = None line = line.replace('#', '') if line == "nomodeler": self.showInModeler = False return if line == "nocrswarning": self.noCRSWarning = True return tokens = line.split('=', 1) desc = self.createDescriptiveName(tokens[0]) if tokens[1].lower().strip() == 'group': self.group = self.i18n_group = tokens[0] return if tokens[1].lower().strip() == 'name': self.name = self.i18n_name = tokens[0] return out = getOutputFromString(line) if out is None: param = getParameterFromString(line) if param is not None: self.addParameter(param) elif out is not None: out.name = tokens[0] out.description = desc self.addOutput(out) else: raise WrongScriptException( self.tr('Could not load script: %s.\n' 'Problem with line "%s"', 'ScriptAlgorithm') % (self.descriptionFile or '', line))
def processParameterLine(self, line): param = None line = line.replace('#', '') if line == "nomodeler": self._flags = self._flags | QgsProcessingAlgorithm.FlagHideFromModeler return if line == "nocrswarning": self.noCRSWarning = True return tokens = line.split('=', 1) desc = self.createDescriptiveName(tokens[0]) if tokens[1].lower().strip() == 'group': self._group = tokens[0] return if tokens[1].lower().strip() == 'name': self._name = self._display_name = tokens[0] return out = getOutputFromString(line) if out is None: param = getParameterFromString(line) if param is not None: self.addParameter(param) elif out is not None: out.setName(tokens[0]) out.setDescription(desc) self.addOutput(out) else: raise WrongScriptException( self.tr('Could not load script: {0}.\n' 'Problem with line "{1}"', 'ScriptAlgorithm').format(self.descriptionFile or '', line))
def defineCharacteristicsFromFile(self): lines = open(self.descriptionFile) line = lines.readline().strip('\n').strip() self.name = line if '|' in self.name: tokens = self.name.split('|') self.name = tokens[0] self.i18n_name = QCoreApplication.translate("SAGAAlgorithm", unicode(self.name)) self.cmdname = tokens[1] else: self.cmdname = self.name self.i18n_name = QCoreApplication.translate("SAGAAlgorithm", unicode(self.name)) self.name = self.name[0].upper() + self.name[1:].lower() line = lines.readline().strip('\n').strip() self.undecoratedGroup = line self.group = SagaGroupNameDecorator.getDecoratedName(self.undecoratedGroup) self.i18n_group = QCoreApplication.translate("SAGAAlgorithm", self.group) line = lines.readline().strip('\n').strip() while line != '': if line.startswith('Hardcoded'): self.hardcodedStrings.append(line[len('Harcoded|') + 1:]) elif line.startswith('Parameter'): self.addParameter(getParameterFromString(line)) elif line.startswith('AllowUnmatching'): self.allowUnmatchingGridExtents = True elif line.startswith('Extent'): # An extent parameter that wraps 4 SAGA numerical parameters self.extentParamNames = line[6:].strip().split(' ') self.addParameter(ParameterExtent(self.OUTPUT_EXTENT, 'Output extent', '0,1,0,1')) else: self.addOutput(getOutputFromString(line)) line = lines.readline().strip('\n').strip() lines.close()
def file2Output(alg, output): """Transform an OutputFile to a parameter""" # Get the outputFile outputFile = alg.getOutputFromName(output) alg.removeOutputFromName(output) # Create output parameter param = getParameterFromString("ParameterString|{}|output file|None|False|False".format(output)) param.value = outputFile.value alg.addParameter(param) return outputFile
def defineCharacteristicsFromFile(self): with open(self.descriptionFile) as lines: line = lines.readline().strip('\n').strip() self.name = line if '|' in self.name: tokens = self.name.split('|') self.name = tokens[0] #cmdname is the name of the algorithm in SAGA, that is, the name to use to call it in the console self.cmdname = tokens[1] else: self.cmdname = self.name self.i18n_name = QCoreApplication.translate("SAGAAlgorithm", str(self.name)) #_commandLineName is the name used in processing to call the algorithm #Most of the time will be equal to the cmdname, but in same cases, several processing algorithms #call the same SAGA one self._commandLineName = self.createCommandLineName(self.name) self.name = decoratedAlgorithmName(self.name) self.i18n_name = QCoreApplication.translate("SAGAAlgorithm", str(self.name)) line = lines.readline().strip('\n').strip() self.undecoratedGroup = line self.group = decoratedGroupName(self.undecoratedGroup) self.i18n_group = QCoreApplication.translate("SAGAAlgorithm", self.group) line = lines.readline().strip('\n').strip() while line != '': if line.startswith('Hardcoded'): self.hardcodedStrings.append(line[len('Hardcoded|'):]) elif line.startswith('Parameter'): self.addParameter(getParameterFromString(line)) elif line.startswith('AllowUnmatching'): self.allowUnmatchingGridExtents = True elif line.startswith('NoResamplingChoice'): self.noResamplingChoice = True elif line.startswith('Extent'): # An extent parameter that wraps 4 SAGA numerical parameters self.extentParamNames = line[6:].strip().split(' ') self.addParameter(ParameterExtent(self.OUTPUT_EXTENT, 'Output extent')) else: self.addOutput(getOutputFromString(line)) line = lines.readline().strip('\n').strip() hasRaster = False for param in self.parameters: if (isinstance(param, ParameterRaster) or (isinstance(param, ParameterMultipleInput) and param.datatype == ParameterMultipleInput.TYPE_RASTER)): hasRaster = True break if (not self.noResamplingChoice and hasRaster): param = ParameterSelection(self.RESAMPLING, "Resampling method", ["Nearest Neighbour", "Bilinear Interpolation", "Bicubic Spline Interpolation", "B-Spline Interpolation"], 3) param.isAdvanced = True self.addParameter(param)
def defineCharacteristicsFromFile(self): lines = open(self.descriptionFile) line = lines.readline().strip('\n').strip() self.grassName = line line = lines.readline().strip('\n').strip() self.name = line line = lines.readline().strip('\n').strip() self.group = line hasRasterOutput = False hasVectorInput = False vectorOutputs = 0 line = lines.readline().strip('\n').strip() while line != '': try: line = line.strip('\n').strip() if line.startswith('Parameter'): parameter = getParameterFromString(line) self.addParameter(parameter) if isinstance(parameter, ParameterVector): hasVectorInput = True if isinstance(parameter, ParameterMultipleInput) \ and parameter.datatype < 3: hasVectorInput = True elif line.startswith('*Parameter'): param = getParameterFromString(line[1:]) param.isAdvanced = True self.addParameter(param) else: output = getOutputFromString(line) self.addOutput(output) if isinstance(output, OutputRaster): hasRasterOutput = True elif isinstance(output, OutputVector): vectorOutputs += 1 line = lines.readline().strip('\n').strip() except Exception, e: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, self.tr('Could not open GRASS GIS 7 algorithm: %s\n%s' % (self.descriptionFile, line))) raise e
def configFile(alg, outputTxt=False): """ Handle inline configuration """ # Where is the GRASS7 user directory ? userGrass7Path = rliPath() if not os.path.isdir(userGrass7Path): mkdir(userGrass7Path) if not os.path.isdir(os.path.join(userGrass7Path, 'output')): mkdir(os.path.join(userGrass7Path, 'output')) origConfigFile = alg.getParameterValue('config') # Handle inline configuration configTxt = alg.getParameterFromName('config_txt') if configTxt.value: # Creates a temporary txt file in user r.li directory tempConfig = alg.getTempFilename() configFilePath = os.path.join(userGrass7Path, tempConfig) # Inject rules into temporary txt file with open(configFilePath, "w") as f: f.write(configTxt.value) # Use temporary file as rules file alg.setParameterValue('config', os.path.basename(configFilePath)) alg.parameters.remove(configTxt) # If we have a configuration file, we need to copy it into user dir if origConfigFile: configFilePath = os.path.join(userGrass7Path, os.path.basename(origConfigFile)) # Copy the file shutil.copy(origConfigFile, configFilePath) # Change the parameter value alg.setParameterValue('config', os.path.basename(configFilePath)) origOutput = alg.getOutputFromName('output') if outputTxt: param = getParameterFromString("ParameterString|output|txt output|None|False|True") param.value = os.path.basename(origOutput.value) alg.addParameter(param) alg.removeOutputFromName('output') alg.processCommand() # Remove Config file: removeConfigFile(alg) # re-add configTxt alg.addParameter(configTxt) alg.setParameterValue('config', origConfigFile) if outputTxt: for param in [f for f in alg.parameters if f.name == 'output']: alg.parameters.remove(param) alg.addOutput(origOutput)
def processCommand(alg): # Remove output output = alg.getOutputFromName('output') alg.removeOutputFromName('output') # Create output parameter param = getParameterFromString("ParameterString|output|output basename|None|False|False") param.value = alg.getTempFilename() alg.addParameter(param) alg.processCommand() # re-add output alg.addOutput(output)
def configFile(alg, parameters, outputTxt=False): """ Handle inline configuration :param parameters: """ # Where is the GRASS7 user directory ? new_parameters = deepcopy(parameters) userGrass7Path = rliPath() if not os.path.isdir(userGrass7Path): mkdir(userGrass7Path) if not os.path.isdir(os.path.join(userGrass7Path, 'output')): mkdir(os.path.join(userGrass7Path, 'output')) origConfigFile = new_parameters['config'] # Handle inline configuration if new_parameters['config_txt']: # Creates a temporary txt file in user r.li directory tempConfig = alg.getTempFilename() configFilePath = os.path.join(userGrass7Path, tempConfig) # Inject rules into temporary txt file with open(configFilePath, "w") as f: f.write(new_parameters['config_txt']) # Use temporary file as rules file new_parameters['config'] = os.path.basename(configFilePath) del new_parameters['config_txt'] # If we have a configuration file, we need to copy it into user dir if origConfigFile: configFilePath = os.path.join(userGrass7Path, os.path.basename(origConfigFile)) # Copy the file shutil.copy(origConfigFile, configFilePath) # Change the parameter value new_parameters['config'] = os.path.basename(configFilePath) origOutput = alg.getOutputFromName('output') if new_parameters['output']: param = getParameterFromString("ParameterString|output|txt output|None|False|True") new_parameters[param.name()] = origOutput.value alg.removeOutputFromName('output') alg.processCommand(new_parameters) # Remove Config file: removeConfigFile(alg) # re-add configTxt if outputTxt: alg.addOutput(origOutput)
def processParameterLine(self, line): param = None line = line.replace('#', '') if line == "nomodeler": self._flags = self._flags | QgsProcessingAlgorithm.FlagHideFromModeler return if line == "nocrswarning": self.noCRSWarning = True return tokens = line.split('=', 1) desc = self.createDescriptiveName(tokens[0]) if tokens[1].lower().strip() == 'group': self._group = tokens[0] return if tokens[1].lower().strip() == 'name': self._name = self._display_name = tokens[0] return out = getOutputFromString(line) if out is None: param = getParameterFromString(line) if param is not None: self.addParameter(param) elif out is not None: out.setName(tokens[0]) out.setDescription(desc) self.addOutput(out) else: raise WrongScriptException( self.tr( 'Could not load script: {0}.\n' 'Problem with line "{1}"', 'ScriptAlgorithm').format(self.descriptionFile or '', line))
def defineCharacteristicsFromFile(self): lines = open(self.descriptionFile) line = lines.readline().strip('\n').strip() self.name = line line = lines.readline().strip('\n').strip() self.cmdName = line line = lines.readline().strip('\n').strip() self.group = line line = lines.readline().strip('\n').strip() while line != '': try: line = line.strip('\n').strip() if line.startswith('Parameter'): param = getParameterFromString(line) self.addParameter(param) else: self.addOutput(getOutputFromString(line)) line = lines.readline().strip('\n').strip() except Exception as e: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, self.tr('Could not load TauDEM algorithm: %s\n%s' % (self.descriptionFile, line))) raise e lines.close()
def process_parameter_line(self, line): """ Processes a single script line representing a parameter """ value, _ = self.split_tokens(line) description = JsUtils.create_descriptive_name(value) output = create_output_from_string(line) if output is not None: output.setName(value) output.setDescription(description) if issubclass(output.__class__, QgsProcessingOutputDefinition): self.addOutput(output) else: # destination type parameter self.addParameter(output) else: line = JsUtils.upgrade_parameter_line(line) param = getParameterFromString(line) if param is not None: self.addParameter(param) else: self.error = self.tr('This script has a syntax error.\n' 'Problem with line: {0}').format(line)
def testParameterNumberDesc(self): desc = 'QgsProcessingParameterNumber|in_number|Input Number' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'number') self.assertEqual(param.name(), 'in_number') self.assertEqual(param.description(), 'Input Number') self.assertEqual(param.dataType(), QgsProcessingParameterNumber.Integer) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterNumber|in_number|Input Number|QgsProcessingParameterNumber.Double' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'number') self.assertEqual(param.name(), 'in_number') self.assertEqual(param.description(), 'Input Number') self.assertEqual(param.dataType(), QgsProcessingParameterNumber.Double) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterNumber|in_number|Input Number|QgsProcessingParameterNumber.Integer|10' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'number') self.assertEqual(param.name(), 'in_number') self.assertEqual(param.description(), 'Input Number') self.assertEqual(param.dataType(), QgsProcessingParameterNumber.Integer) self.assertEqual(param.defaultValue(), 10) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterNumber|in_number|Input Number|QgsProcessingParameterNumber.Integer|None|True' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'number') self.assertEqual(param.name(), 'in_number') self.assertEqual(param.description(), 'Input Number') self.assertEqual(param.dataType(), QgsProcessingParameterNumber.Integer) self.assertIsNone(param.defaultValue()) self.assertTrue(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterNumber|in_number|Input Number|QgsProcessingParameterNumber.Integer|10|False|0' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'number') self.assertEqual(param.name(), 'in_number') self.assertEqual(param.description(), 'Input Number') self.assertEqual(param.dataType(), QgsProcessingParameterNumber.Integer) self.assertEqual(param.defaultValue(), 10) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertEqual(param.minimum(), 0) desc = 'QgsProcessingParameterNumber|in_number|Input Number|QgsProcessingParameterNumber.Integer|10|False|0|20' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'number') self.assertEqual(param.name(), 'in_number') self.assertEqual(param.description(), 'Input Number') self.assertEqual(param.dataType(), QgsProcessingParameterNumber.Integer) self.assertEqual(param.defaultValue(), 10) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertEqual(param.minimum(), 0) self.assertEqual(param.maximum(), 20)
def testParameterVectorLayerDesc(self): desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), []) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|0' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVectorPoint]) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|QgsProcessing.TypeVectorPoint' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVectorPoint]) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|1' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVectorLine]) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|QgsProcessing.TypeVectorLine' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVectorLine]) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|2' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVectorPolygon]) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|QgsProcessing.TypeVectorPolygon' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVectorPolygon]) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|5' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVector]) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|QgsProcessing.TypeVector' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVector]) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|1;2' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVectorLine, QgsProcessing.TypeVectorPolygon]) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|QgsProcessing.TypeVectorLine;QgsProcessing.TypeVectorPolygon' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVectorLine, QgsProcessing.TypeVectorPolygon]) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) desc = 'QgsProcessingParameterVectorLayer|in_vector|Input Vector|-1|None|True' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vector') self.assertEqual(param.name(), 'in_vector') self.assertEqual(param.description(), 'Input Vector') self.assertListEqual(param.dataTypes(), [QgsProcessing.TypeVectorAnyGeometry]) self.assertIsNone(param.defaultValue()) self.assertTrue(param.flags() & QgsProcessingParameterDefinition.FlagOptional)
def defineCharacteristicsFromFile(self): line = None try: with open(self._descriptionfile) as lines: line = lines.readline().strip('\n').strip() self._name = line.split('|')[0] self.appkey = self._name line = lines.readline().strip('\n').strip() self.doc = line self.i18n_doc = QCoreApplication.translate("OtbAlgorithm", self.doc) #self._name = self._name #+ " - " + self.doc self._display_name = self.tr(self._name) self.i18n_name = QCoreApplication.translate("OtbAlgorithm", self._name) line = lines.readline().strip('\n').strip() self._group = line self.i18n_group = QCoreApplication.translate("OtbAlgorithm", self._group) line = lines.readline().strip('\n').strip() while line != '': line = line.strip('\n').strip() if line.startswith('#'): line = lines.readline().strip('\n').strip() continue param = None if 'OTBParameterChoice' in line: tokens = line.split("|") params = [t if str(t) != str(None) else None for t in tokens[1:]] options = params[2].split(';') param = OtbParameterChoice(params[0], params[1], options, params[3], params[4]) else: param = getParameterFromString(line, 'OtbAlgorithm') #if parameter is None, then move to next line and continue if param is None: line = lines.readline().strip('\n').strip() continue name = param.name() if '.' in name and len(name.split('.')) > 2: p = name.split('.')[:-2] group_key = '.'.join(p) group_value = name.split('.')[-2] metadata = param.metadata() metadata['group_key'] = group_key metadata['group_value'] = group_value param.setMetadata(metadata) #'elev.dem.path', 'elev.dem', 'elev.dem.geoid', 'elev.geoid' are special! #Even though it is not typical for OTB to fix on parameter keys, #we current use below !hack! to set SRTM path and GEOID files #Future releases of OTB must follow this rule keep #compatibility or update this checklist. if name in ["elev.dem.path", "elev.dem"]: param.setDefaultValue(OtbUtils.srtmFolder()) if name in ["elev.dem.geoid", "elev.geoid"]: param.setDefaultValue(OtbUtils.geoidFile()) # outputpixeltype is a special parameter associated with raster output # reset list of options to 'self.pixelTypes'. if name == 'outputpixeltype': param.setOptions(self.pixelTypes) param.setDefaultValue(self.pixelTypes.index('float')) self.addParameter(param) #parameter is added now and we must move to next line line = lines.readline().strip('\n').strip() except BaseException as e: import traceback errmsg = "Could not open OTB algorithm from file: \n" + self._descriptionfile + "\nline=" + line + "\nError:\n" + traceback.format_exc() QgsMessageLog.logMessage(self.tr(errmsg), self.tr('Processing'), Qgis.Critical) raise e
def defineCharacteristicsFromFile(self): """ Create algorithm parameters and outputs from a text file. """ with open(self.descriptionFile) as lines: # First line of the file is the Grass algorithm name line = lines.readline().strip('\n').strip() self.grass7Name = line # Second line if the algorithm name in Processing line = lines.readline().strip('\n').strip() self._short_description = line if " - " not in line: self._name = self.grass7Name else: self._name = line[:line.find(' ')].lower() self._display_name = self._name # Read the grass group line = lines.readline().strip('\n').strip() self._group = QCoreApplication.translate("GrassAlgorithm", line) self._groupId = self.groupIdRegex.search(line).group(0).lower() hasRasterOutput = False hasRasterInput = False hasVectorInput = False vectorOutputs = False # Then you have parameters/output definition line = lines.readline().strip('\n').strip() while line != '': try: line = line.strip('\n').strip() if line.startswith('Hardcoded'): self.hardcodedStrings.append(line[len('Hardcoded|'):]) parameter = getParameterFromString(line) if parameter is not None: self.params.append(parameter) if isinstance(parameter, (QgsProcessingParameterVectorLayer, QgsProcessingParameterFeatureSource)): hasVectorInput = True elif isinstance(parameter, QgsProcessingParameterRasterLayer): hasRasterInput = True elif isinstance(parameter, QgsProcessingParameterMultipleLayers): if parameter.layerType( ) < 3 or parameter.layerType() == 5: hasVectorInput = True elif parameter.layerType() == 3: hasRasterInput = True elif isinstance( parameter, QgsProcessingParameterVectorDestination): vectorOutputs = True elif isinstance( parameter, QgsProcessingParameterRasterDestination): hasRasterOutput = True line = lines.readline().strip('\n').strip() except Exception as e: QgsMessageLog.logMessage( self.tr( 'Could not open GRASS GIS 7 algorithm: {0}\n{1}'). format(self.descriptionFile, line), self.tr('Processing'), Qgis.Critical) raise e param = QgsProcessingParameterExtent( self.GRASS_REGION_EXTENT_PARAMETER, self.tr('GRASS GIS 7 region extent'), optional=True) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) if hasRasterOutput or hasRasterInput: # Add a cellsize parameter param = QgsProcessingParameterNumber( self.GRASS_REGION_CELLSIZE_PARAMETER, self.tr('GRASS GIS 7 region cellsize (leave 0 for default)'), type=QgsProcessingParameterNumber.Double, minValue=0.0, maxValue=sys.float_info.max + 1, defaultValue=0.0) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) if hasRasterOutput: # Add a createopt parameter for format export param = QgsProcessingParameterString( self.GRASS_RASTER_FORMAT_OPT, self.tr('Output Rasters format options (createopt)'), multiLine=True, optional=True) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) # Add a metadata parameter for format export param = QgsProcessingParameterString( self.GRASS_RASTER_FORMAT_META, self.tr('Output Rasters format metadata options (metaopt)'), multiLine=True, optional=True) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) if hasVectorInput: param = QgsProcessingParameterNumber( self.GRASS_SNAP_TOLERANCE_PARAMETER, self.tr('v.in.ogr snap tolerance (-1 = no snap)'), type=QgsProcessingParameterNumber.Double, minValue=-1.0, maxValue=sys.float_info.max + 1, defaultValue=-1.0) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) param = QgsProcessingParameterNumber( self.GRASS_MIN_AREA_PARAMETER, self.tr('v.in.ogr min area'), type=QgsProcessingParameterNumber.Double, minValue=0.0, maxValue=sys.float_info.max + 1, defaultValue=0.0001) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) if vectorOutputs: # Add an optional output type param = QgsProcessingParameterEnum( self.GRASS_OUTPUT_TYPE_PARAMETER, self.tr('v.out.ogr output type'), self.OUTPUT_TYPES) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) # Add a DSCO parameter for format export param = QgsProcessingParameterString( self.GRASS_VECTOR_DSCO, self.tr('v.out.ogr output data source options (dsco)'), multiLine=True, optional=True) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) # Add a LCO parameter for format export param = QgsProcessingParameterString( self.GRASS_VECTOR_LCO, self.tr('v.out.ogr output layer options (lco)'), multiLine=True, optional=True) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) # Add a -c flag for export param = QgsProcessingParameterBoolean( self.GRASS_VECTOR_EXPORT_NOCAT, self. tr('Also export features without category (not labeled). Otherwise only features with category are exported' ), False) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param)
def processCommand(alg): """ Handle data preparation for v.net.distance: * Integrate point layers into network vector map. * Make v.net.distance use those layers. * Delete the threshold parameter. * If where statement, connect to the db """ paramsToDelete = [] # Grab the threshold value for our v.net connect command threshold = alg.getParameterValue(u'threshold') if threshold: paramsToDelete.append(alg.getParameterFromName(u'threshold')) # Grab the network layer and tell to v.net.alloc to use the temp layer instead line_layer = alg.getParameterValue(u'input') if line_layer: line_layer = alg.exportedLayers[line_layer] # import the two point layers into the network for i, layer in enumerate([u'from', u'to']): # Get a temp layer name intLayer = alg.getTempFilename() # Grab the from point layer and delete this parameter (not used by v.net.distance) point_layer = alg.getParameterValue(layer + u'_points') if point_layer: point_layer = alg.exportedLayers[point_layer] paramsToDelete.append(alg.getParameterFromName(layer + u'_points')) # Create the v.net connect command for point layer integration command = u"v.net -s input={} points={} out={} op=connect threshold={} arc_layer=1 node_layer={}".format( line_layer, point_layer, intLayer, threshold, i + 2) alg.commands.append(command) line_layer = intLayer # Add the parameter to the algorithm parameter = alg.getParameterFromName(u'{}_layer'.format(layer)) if not parameter: parameter = getParameterFromString( u'ParameterNumber|{0}_layer|{0} layer number|1|3|2|False'. format(layer)) alg.addParameter(parameter) parameter.setValue(i + 2) # Make the connection with attribute table command = u"v.db.connect -o map={} table={} layer={}".format( line_layer, point_layer, i + 2) alg.commands.append(command) alg.setParameterValue(u'input', line_layer) # Delete some unnecessary parameters for param in paramsToDelete: alg.parameters.remove(param) alg.processCommand() # Bring back the parameters: for param in paramsToDelete: alg.parameters.append(param) # Delete from_layer and to_layer for word in [u'from', u'to']: alg.parameters.remove( alg.getParameterFromName(u'{}_layer'.format(word)))
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 testParameterVectorDestDesc(self): desc = 'QgsProcessingParameterVectorDestination|param_vector_dest|Vector Destination' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vectorDestination') self.assertEqual(param.name(), 'param_vector_dest') self.assertEqual(param.description(), 'Vector Destination') self.assertEqual(param.dataType(), QgsProcessing.TypeVectorAnyGeometry) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertTrue(param.createByDefault()) desc = 'QgsProcessingParameterVectorDestination|param_vector_dest|Vector Destination Point|0' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vectorDestination') self.assertEqual(param.name(), 'param_vector_dest') self.assertEqual(param.description(), 'Vector Destination Point') self.assertEqual(param.dataType(), QgsProcessing.TypeVectorPoint) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertTrue(param.createByDefault()) desc = 'QgsProcessingParameterVectorDestination|param_vector_dest|Vector Destination Point|QgsProcessing.TypeVectorPoint' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vectorDestination') self.assertEqual(param.name(), 'param_vector_dest') self.assertEqual(param.description(), 'Vector Destination Point') self.assertEqual(param.dataType(), QgsProcessing.TypeVectorPoint) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertTrue(param.createByDefault()) desc = 'QgsProcessingParameterVectorDestination|param_vector_dest|Vector Destination Line|1' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vectorDestination') self.assertEqual(param.name(), 'param_vector_dest') self.assertEqual(param.description(), 'Vector Destination Line') self.assertEqual(param.dataType(), QgsProcessing.TypeVectorLine) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertTrue(param.createByDefault()) desc = 'QgsProcessingParameterVectorDestination|param_vector_dest|Vector Destination Line|QgsProcessing.TypeVectorLine' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vectorDestination') self.assertEqual(param.name(), 'param_vector_dest') self.assertEqual(param.description(), 'Vector Destination Line') self.assertEqual(param.dataType(), QgsProcessing.TypeVectorLine) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertTrue(param.createByDefault()) desc = 'QgsProcessingParameterVectorDestination|param_vector_dest|Vector Destination Polygon|2' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vectorDestination') self.assertEqual(param.name(), 'param_vector_dest') self.assertEqual(param.description(), 'Vector Destination Polygon') self.assertEqual(param.dataType(), QgsProcessing.TypeVectorPolygon) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertTrue(param.createByDefault()) desc = 'QgsProcessingParameterVectorDestination|param_vector_dest|Vector Destination Polygon|QgsProcessing.TypeVectorPolygon' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vectorDestination') self.assertEqual(param.name(), 'param_vector_dest') self.assertEqual(param.description(), 'Vector Destination Polygon') self.assertEqual(param.dataType(), QgsProcessing.TypeVectorPolygon) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertTrue(param.createByDefault()) desc = 'QgsProcessingParameterVectorDestination|param_vector_dest|Vector Destination Table|5' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vectorDestination') self.assertEqual(param.name(), 'param_vector_dest') self.assertEqual(param.description(), 'Vector Destination Table') self.assertEqual(param.dataType(), QgsProcessing.TypeVector) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertTrue(param.createByDefault()) desc = 'QgsProcessingParameterVectorDestination|param_vector_dest|Vector Destination Table|QgsProcessing.TypeVector' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vectorDestination') self.assertEqual(param.name(), 'param_vector_dest') self.assertEqual(param.description(), 'Vector Destination Table') self.assertEqual(param.dataType(), QgsProcessing.TypeVector) self.assertIsNone(param.defaultValue()) self.assertFalse(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertTrue(param.createByDefault()) desc = 'QgsProcessingParameterVectorDestination|param_vector_dest|Vector Destination|-1|None|True|False' param = getParameterFromString(desc) self.assertIsNotNone(param) self.assertEqual(param.type(), 'vectorDestination') self.assertEqual(param.name(), 'param_vector_dest') self.assertEqual(param.description(), 'Vector Destination') self.assertEqual(param.dataType(), QgsProcessing.TypeVectorAnyGeometry) self.assertIsNone(param.defaultValue()) self.assertTrue(param.flags() & QgsProcessingParameterDefinition.FlagOptional) self.assertFalse(param.createByDefault())
class OTBAlgorithm(GeoAlgorithm): REGION_OF_INTEREST = "ROI" def __init__(self, descriptionfile): GeoAlgorithm.__init__(self) self.roiFile = None self.descriptionFile = descriptionfile self.defineCharacteristicsFromFile() self.numExportedLayers = 0 self.hasROI = None def __str__(self): return ("Algo : " + self.name + " from app : " + self.cliName + " in : " + self.group) def getCopy(self): newone = OTBAlgorithm(self.descriptionFile) newone.provider = self.provider return newone def getIcon(self): return PyQt4.QtGui.QIcon( os.path.dirname(__file__) + "/../../images/otb.png") def help(self): folder = os.path.join(OTBUtils.otbDescriptionPath(), 'doc') helpfile = os.path.join(str(folder), self.appkey + ".html") if os.path.exists(helpfile): return False, helpfile else: raise False, None def adapt_list_to_string(self, c_list): a_list = c_list[1:] if a_list[0] in ["ParameterVector", "ParameterMultipleInput"]: if c_list[0] == "ParameterType_InputImageList": a_list[3] = 3 elif c_list[0] == "ParameterType_InputFilenameList": a_list[3] = 4 else: a_list[3] = -1 a_list[1] = "-%s" % a_list[1] def mystr(par): if type(par) == type([]): return ";".join(par) return str(par) b_list = map(mystr, a_list) res = "|".join(b_list) return res def get_list_from_node(self, myet): all_params = [] for parameter in myet.iter('parameter'): rebuild = [] par_type = parameter.find('parameter_type').text key = parameter.find('key').text name = parameter.find('name').text source_par_type = parameter.find( 'parameter_type').attrib['source_parameter_type'] rebuild.append(source_par_type) rebuild.append(par_type) rebuild.append(key) rebuild.append(name) for each in parameter[4:]: if not each.tag in ["hidden"]: if len(list(each)) == 0: rebuild.append(each.text) else: rebuild.append( [item.text for item in each.iter('choice')]) all_params.append(rebuild) return all_params def defineCharacteristicsFromFile(self): content = open(self.descriptionFile).read() dom_model = ET.fromstring(content) self.appkey = dom_model.find('key').text self.cliName = dom_model.find('exec').text self.name = dom_model.find('longname').text self.group = dom_model.find('group').text #ProcessingLog.addToLog(ProcessingLog.LOG_INFO, "Reading parameters for %s" % self.appkey) rebu = None the_result = None try: rebu = self.get_list_from_node(dom_model) the_result = map(self.adapt_list_to_string, rebu) except Exception, e: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, "Could not open OTB algorithm: " + self.descriptionFile + "\n" + traceback.format_exc()) raise e for line in the_result: try: if line.startswith("Parameter") or line.startswith( "*Parameter"): if line.startswith("*Parameter"): param = getParameterFromString(line[1:]) param.isAdvanced = True else: param = getParameterFromString(line) # Hack for initializing the elevation parameters from Processing configuration if param.name == "-elev.dem.path" or param.name == "-elev.dem" or "elev.dem" in param.name: param.default = OTBUtils.otbSRTMPath() elif param.name == "-elev.dem.geoid" or param.name == "-elev.geoid" or "elev.geoid" in param.name: param.default = OTBUtils.otbGeoidPath() self.addParameter(param) elif line.startswith("Extent"): self.addParameter( ParameterExtent(self.REGION_OF_INTEREST, "Region of interest", "0,1,0,1")) self.hasROI = True else: self.addOutput(getOutputFromString(line)) except Exception, e: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, "Could not open OTB algorithm: " + self.descriptionFile + "\n" + line) raise e
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 defineCharacteristicsFromFile(self): with open(self.descriptionFile) as lines: line = lines.readline().strip('\n').strip() self.grass7Name = line line = lines.readline().strip('\n').strip() self._name = line self._display_name = QCoreApplication.translate( "GrassAlgorithm", line) if " - " not in self._name: self._name = self.grass7Name + " - " + self._name self._display_name = self.grass7Name + " - " + self._display_name self._name = self._name[:self._name.find(' ')].lower() line = lines.readline().strip('\n').strip() self._group = QCoreApplication.translate("GrassAlgorithm", line) hasRasterOutput = False hasVectorInput = False vectorOutputs = 0 line = lines.readline().strip('\n').strip() while line != '': try: line = line.strip('\n').strip() if line.startswith('Hardcoded'): self.hardcodedStrings.append(line[len('Hardcoded|'):]) parameter = getParameterFromString(line) if parameter is not None: self.addParameter(parameter) if isinstance(parameter, ParameterVector): hasVectorInput = True if isinstance(parameter, ParameterMultipleInput) \ and parameter.datatype < 3: hasVectorInput = True else: output = getOutputFromString(line) self.addOutput(output) if isinstance(output, OutputRaster): hasRasterOutput = True elif isinstance(output, OutputVector): vectorOutputs += 1 if isinstance(output, OutputHTML): self.addOutput( OutputFile( "rawoutput", self.tr("{0} (raw output)").format( output.description), "txt")) line = lines.readline().strip('\n').strip() except Exception as e: QgsMessageLog.logMessage( self.tr( 'Could not open GRASS GIS 7 algorithm: {0}\n{1}'). format(self.descriptionFile, line), self.tr('Processing'), QgsMessageLog.CRITICAL) raise e self.addParameter( ParameterExtent(self.GRASS_REGION_EXTENT_PARAMETER, self.tr('GRASS GIS 7 region extent'))) if hasRasterOutput: self.addParameter( ParameterNumber( self.GRASS_REGION_CELLSIZE_PARAMETER, self.tr( 'GRASS GIS 7 region cellsize (leave 0 for default)'), 0, None, 0.0)) if hasVectorInput: param = ParameterNumber(self.GRASS_SNAP_TOLERANCE_PARAMETER, 'v.in.ogr snap tolerance (-1 = no snap)', -1, None, -1.0) param.isAdvanced = True self.addParameter(param) param = ParameterNumber(self.GRASS_MIN_AREA_PARAMETER, 'v.in.ogr min area', 0, None, 0.0001) param.isAdvanced = True self.addParameter(param) if vectorOutputs == 1: param = ParameterSelection(self.GRASS_OUTPUT_TYPE_PARAMETER, 'v.out.ogr output type', self.OUTPUT_TYPES) param.isAdvanced = True self.addParameter(param)
def defineCharacteristicsFromFile(self): lines = open(self.descriptionFile) line = lines.readline().strip('\n').strip() self.grass7Name = line line = lines.readline().strip('\n').strip() self.name = line self.i18n_name = QCoreApplication.translate("GrassAlgorithm", line) if " - " not in self.name: self.name = self.grass7Name + " - " + self.name self.i18n_name = self.grass7Name + " - " + self.i18n_name line = lines.readline().strip('\n').strip() self.group = line self.i18n_group = QCoreApplication.translate("GrassAlgorithm", line) hasRasterOutput = False hasVectorInput = False vectorOutputs = 0 line = lines.readline().strip('\n').strip() while line != '': try: line = line.strip('\n').strip() if line.startswith('Hardcoded'): self.hardcodedStrings.append(line[len('Hardcoded|'):]) elif line.startswith('Parameter'): parameter = getParameterFromString(line) self.addParameter(parameter) if isinstance(parameter, ParameterVector): hasVectorInput = True if isinstance(parameter, ParameterMultipleInput) \ and parameter.datatype < 3: hasVectorInput = True elif line.startswith('*Parameter'): param = getParameterFromString(line[1:]) param.isAdvanced = True self.addParameter(param) else: output = getOutputFromString(line) self.addOutput(output) if isinstance(output, OutputRaster): hasRasterOutput = True elif isinstance(output, OutputVector): vectorOutputs += 1 if isinstance(output, OutputHTML): self.addOutput( OutputFile("rawoutput", output.description + " (raw output)", "txt")) line = lines.readline().strip('\n').strip() except Exception as e: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, self.tr('Could not open GRASS GIS 7 algorithm: %s\n%s' % (self.descriptionFile, line))) raise e lines.close() self.addParameter( ParameterExtent(self.GRASS_REGION_EXTENT_PARAMETER, self.tr('GRASS GIS 7 region extent'))) if hasRasterOutput: self.addParameter( ParameterNumber( self.GRASS_REGION_CELLSIZE_PARAMETER, self.tr( 'GRASS GIS 7 region cellsize (leave 0 for default)'), 0, None, 0.0)) if hasVectorInput: param = ParameterNumber(self.GRASS_SNAP_TOLERANCE_PARAMETER, 'v.in.ogr snap tolerance (-1 = no snap)', -1, None, -1.0) param.isAdvanced = True self.addParameter(param) param = ParameterNumber(self.GRASS_MIN_AREA_PARAMETER, 'v.in.ogr min area', 0, None, 0.0001) param.isAdvanced = True self.addParameter(param) if vectorOutputs == 1: param = ParameterSelection(self.GRASS_OUTPUT_TYPE_PARAMETER, 'v.out.ogr output type', self.OUTPUT_TYPES) param.isAdvanced = True self.addParameter(param)
def regroupRasters(alg, field, groupField, subgroupField=None, extFile=None): """ Group multiple input rasters into a group * If there is a subgroupField, a subgroup will automatically created. * When an external file is provided, the file is copied into the respective directory of the subgroup. * extFile is a dict of the form 'parameterName':'directory name'. """ # List of rasters names rasters = alg.getParameterFromName(field) rastersList = rasters.value.split(';') alg.parameters.remove(rasters) # Insert a i.group command group = getParameterFromString( "ParameterString|{}|group of rasters|None|False|False".format( groupField)) group.value = alg.getTempFilename() alg.addParameter(group) if subgroupField: subgroup = getParameterFromString( "ParameterString|{}|subgroup of rasters|None|False|False".format( subgroupField)) subgroup.value = alg.getTempFilename() alg.addParameter(subgroup) command = 'i.group group={}{} input={}'.format( group.value, ' subgroup={}'.format(subgroup.value) if subgroupField else '', ','.join([alg.exportedLayers[f] for f in rastersList])) alg.commands.append(command) # Handle external files origExtParams = {} if subgroupField and extFile: for ext in list(extFile.keys()): extFileName = alg.getParameterValue(ext) if extFileName: shortFileName = path.basename(extFileName) destPath = path.join(Grass7Utils.grassMapsetFolder(), 'PERMANENT', 'group', group.value, 'subgroup', subgroup.value, extFile[ext], shortFileName) copyFile(alg, extFileName, destPath) origExtParams[ext] = extFileName alg.setParameterValue(ext, shortFileName) # modify parameters values alg.processCommand() # Re-add input rasters alg.addParameter(rasters) # replace external files value with original value for param in list(origExtParams.keys()): alg.setParameterValue(param, origExtParams[param]) # Delete group: alg.parameters.remove(group) if subgroupField: alg.parameters.remove(subgroup) return group.value, subgroup.value return group.value
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))