def initializeSettings(self): '''this is the place where you should add config parameters to SEXTANTE using the SextanteConfig class. this method is called when a provider is added to SEXTANTE. By default it just adds a setting to activate or deactivate algorithms from the provider''' SextanteConfig.settingIcons[self.getDescription()] = self.getIcon() name = "ACTIVATE_" + self.getName().upper().replace(" ", "_") SextanteConfig.addSetting(Setting(self.getDescription(), name, "Activate", self.activate))
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) SextanteConfig.addSetting( Setting(self.getDescription(), WpsAlgorithmProvider.WPS_DESCRIPTIONS, "WPS description cache folder", WpsAlgorithmProvider.WpsDescriptionFolder()))
def fillTree(self): useCategories = SextanteConfig.getSetting(SextanteConfig.USE_CATEGORIES) if useCategories: self.fillTreeUsingCategories() else: self.fillTreeUsingProviders() self.algorithmTree.sortItems(0, Qt.AscendingOrder) showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: recent = SextanteLog.getRecentAlgorithms() if len(recent) != 0: found = False recentItem = QTreeWidgetItem() recentItem.setText(0, self.tr("Recently used algorithms")) for algname in recent: alg = Sextante.getAlgorithm(algname) if alg is not None: algItem = TreeAlgorithmItem(alg) recentItem.addChild(algItem) found = True if found: self.algorithmTree.insertTopLevelItem(0, recentItem) recentItem.setExpanded(True) self.algorithmTree.setWordWrap(True)
def execute(self): layers = QGisLayers.getAllLayers() layersMap = dict([(layer.name(), layer) for layer in layers]) layerNames = [layer.name() for layer in layers] item, ok = QtGui.QInputDialog.getItem(None, "Select a layer", "Layer selection", layerNames, editable=False) if ok: layer = layersMap[item] SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_XMIN, layer.extent().xMinimum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_YMIN, layer.extent().yMinimum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_XMAX, layer.extent().xMaximum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_YMAX, layer.extent().yMaximum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_AUTO_REGION, False) s = str(layer.extent().xMinimum()) + "," + str( layer.extent().xMaximum()) + "," + str( layer.extent().yMinimum()) + "," + str( layer.extent().yMaximum()) QtGui.QMessageBox.information(None, "GRASS Region", "GRASS region set to:\n" + s + \ "\nTo set the cellsize or set back the autoregion option,\ngo to the SEXTANTE configuration.")
def addToLog(msgtype, msg): try: #it seems that this fails sometimes depending on the msg added: #To avoid it stopping the normal functioning of the algorithm, #we catch all errors, assuming that is better to miss some log info #that breaking the algorithm. if isinstance(msg, list): a = "|".join(m.strip("\n") for m in msg) text = a else: text = msg.replace("\n", "|") line = msgtype + "|" + datetime.datetime.now().strftime( SextanteLog.DATE_FORMAT).decode("utf-8") + "|" + text + "\n" logfile = codecs.open(SextanteLog.logFilename(), "a", encoding="utf-8") #logfile = codecs.open(SextanteLog.logFilename(), "a", encoding='utf-8') logfile.write(line) logfile.close() if msgtype == SextanteLog.LOG_ALGORITHM: algname = text[len("Sextante.runalg(\""):] algname = algname[:algname.index("\"")] if algname not in SextanteLog.recentAlgs: SextanteLog.recentAlgs.append(algname) recentAlgsString = ';'.join(SextanteLog.recentAlgs[-6:]) SextanteConfig.setSettingValue( SextanteConfig.RECENT_ALGORITHMS, recentAlgsString) except: pass
def unload(self): AlgorithmProvider.unload(self) if SextanteUtils.isWindows() or SextanteUtils.isMac(): SextanteConfig.removeSetting(GrassUtils.GRASS_FOLDER) SextanteConfig.removeSetting(GrassUtils.GRASS_WIN_SHELL) SextanteConfig.removeSetting(GrassUtils.GRASS_HELP_FOLDER) SextanteConfig.removeSetting(GrassUtils.GRASS_LOG_COMMANDS) SextanteConfig.removeSetting(GrassUtils.GRASS_LOG_CONSOLE)
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if SextanteUtils.isWindows() or SextanteUtils.isMac(): SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath())) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell())) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False)) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False)) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))
def setUp(self): SextanteConfig.setSettingValue(SextanteConfig.USE_THREADS, self.threaded) print print bcolors.INFO, self.msg, bcolors.ENDC, print "Parameters: ", self.alg.parameters, print "Outputs: ", [out for out in self.alg.outputs if not out.hidden], self.args = list(self.gen_test_parameters(self.alg, True)) print ' => ', self.args, bcolors.WARNING,
def initializeSettings(self): '''In this method we add settings needed to configure our provider. Do not forget to call the parent method, since it takes care or automatically adding a setting for activating or deactivating the algorithms in the provider''' AlgorithmProvider.initializeSettings(self) SextanteConfig.addSetting(Setting("Example algorithms", ExampleAlgorithmProvider.MY_DUMMY_SETTING, "Example setting", "Default value")) '''To get the parameter of a setting parameter, use SextanteConfig.getSetting(name_of_parameter)'''
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) SextanteConfig.addSetting( Setting(self.getDescription(), LasToolsUtils.LASTOOLS_FOLDER, "LASTools folder", LasToolsUtils.LasToolsPath())) SextanteConfig.addSetting( Setting(self.getDescription(), FusionUtils.FUSION_FOLDER, "Fusion folder", FusionUtils.FusionPath()))
def initializeSettings(self): '''add settings needed to configure our provider.''' # call the parent method which takes care of adding a setting for # activating or deactivating the algorithms in the provider AlgorithmProvider.initializeSettings(self) # add settings SextanteConfig.addSetting(Setting("LWGEOM algorithms", LwgeomAlgorithmProvider.LWGEOM_PATH_SETTING, "Path to liblwgeom", ""))
def addProvider(provider): '''use this method to add algorithms from external providers''' '''Adding a new provider automatically initializes it, so there is no need to do it in advance''' #Note: this might slow down the initialization process if there are many new providers added. #Should think of a different solution provider.initializeSettings() Sextante.providers.append(provider) SextanteConfig.loadSettings() Sextante.updateAlgsList()
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) SextanteConfig.addSetting( Setting(self.getDescription(), RUtils.RSCRIPTS_FOLDER, "R Scripts folder", RUtils.RScriptsFolder())) if SextanteUtils.isWindows(): SextanteConfig.addSetting( Setting(self.getDescription(), RUtils.R_FOLDER, "R folder", RUtils.RFolder()))
def initializeSettings(self): '''In this method we add settings needed to configure our provider. Do not forget to call the parent method, since it takes care or automatically adding a setting for activating or deactivating the algorithms in the provider ''' AlgorithmProvider.initializeSettings(self) SextanteConfig.addSetting(Setting("Example algorithms", ExampleAlgorithmProvider.MY_DUMMY_SETTING, "Example setting", "Default value")) '''To get the parameter of a setting parameter, use
def removeProvider(provider): '''Use this method to remove a provider. This method should be called when unloading a plugin that contributes a provider to SEXTANTE''' try: provider.unload() Sextante.providers.remove(provider) SextanteConfig.loadSettings() Sextante.updateAlgsList() except: pass #This try catch block is here to avoid problems if the plugin with a provider is unloaded
def fillCoords(self): r = self.tool.rectangle() SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_XMIN, r.xMinimum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_YMIN, r.yMinimum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_XMAX, r.xMaximum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_YMAX, r.yMaximum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_AUTO_REGION, False) s = str(r.xMinimum()) + "," + str(r.xMaximum()) + "," + str(r.yMinimum()) + "," + str(r.yMaximum()) self.tool.reset() canvas = QGisLayers.iface.mapCanvas() canvas.setMapTool(self.prevMapTool) QtGui.QMessageBox.information(None, "GRASS Region", "GRASS region set to:\n" + s + \ "\nTo set the cellsize or set back the autoregion option,\ngo to the SEXTANTE configuration.")
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if SextanteUtils.isWindows() or SextanteUtils.isMac(): SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath())) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell())) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False)) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False))
def load(fileName, name=None, crs=None, style=None): if fileName == None: return prjSetting = None settings = QSettings() if crs != None: prjSetting = settings.value("/Projections/defaultBehaviour") settings.setValue("/Projections/defaultBehaviour", QVariant("")) if name == None: name = path.split(fileName)[1] qgslayer = QgsVectorLayer(fileName, name, 'ogr') if qgslayer.isValid(): if crs is not None and qgslayer.crs() is None: qgslayer.setCrs(crs, False) if style == None: if qgslayer.geometryType == 0: style = SextanteConfig.getSetting( SextanteConfig.VECTOR_POINT_STYLE) elif qgslayer.geometryType == 1: style = SextanteConfig.getSetting( SextanteConfig.VECTOR_LINE_STYLE) else: style = SextanteConfig.getSetting( SextanteConfig.VECTOR_POLYGON_STYLE) qgslayer.loadNamedStyle(style) QgsMapLayerRegistry.instance().addMapLayers([qgslayer]) else: qgslayer = QgsRasterLayer(fileName, name) if qgslayer.isValid(): if crs != None: qgslayer.setCrs(crs, False) if style == None: style = SextanteConfig.getSetting( SextanteConfig.RASTER_STYLE) qgslayer.loadNamedStyle(style) QgsMapLayerRegistry.instance().addMapLayers([qgslayer]) QGisLayers.iface.legendInterface().refreshLayerSymbology( qgslayer) else: if prjSetting: settings.setValue("/Projections/defaultBehaviour", prjSetting) raise RuntimeError( "Could not load layer: " + unicode(fileName) + "\nCheck the SEXTANTE log to look for errors") if prjSetting: settings.setValue("/Projections/defaultBehaviour", prjSetting) return qgslayer
def execute(self): layers = QGisLayers.getAllLayers(); layersMap = dict([(layer.name(), layer) for layer in layers]) layerNames = [layer.name() for layer in layers] item, ok = QtGui.QInputDialog.getItem(None, "Select a layer", "Layer selection", layerNames, editable=False) if ok: layer = layersMap[item] SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_XMIN, layer.extent().xMinimum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_YMIN, layer.extent().yMinimum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_XMAX, layer.extent().xMaximum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_YMAX, layer.extent().yMaximum()) SextanteConfig.setSettingValue(GrassUtils.GRASS_AUTO_REGION, False) s = str(layer.extent().xMinimum()) + "," + str(layer.extent().xMaximum()) + "," + str(layer.extent().yMinimum()) + "," + str(layer.extent().yMaximum()) QtGui.QMessageBox.information(None, "GRASS Region", "GRASS region set to:\n" + s + \ "\nTo set the cellsize or set back the autoregion option,\ngo to the SEXTANTE configuration.")
def executeAlgorithm(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = Sextante.getAlgorithm(item.alg.commandLineName()) message = alg.checkBeforeOpeningParametersDialog() if message: QtGui.QMessageBox.warning(self, "Warning", message) return alg = alg.getCopy() #copy.deepcopy(alg) dlg = alg.getCustomParametersDialog() if not dlg: dlg = ParametersDialog(alg) canvas = QGisLayers.iface.mapCanvas() prevMapTool = canvas.mapTool() dlg.show() dlg.exec_() if canvas.mapTool() != prevMapTool: try: canvas.mapTool().reset() except: pass canvas.setMapTool(prevMapTool) if dlg.executed: showRecent = SextanteConfig.getSetting( SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: self.fillTree() if isinstance(item, TreeActionItem): action = item.action action.setData(self) action.execute()
def scriptsFolder(): folder = SextanteConfig.getSetting(ScriptUtils.SCRIPTS_FOLDER) if folder == None: folder = SextanteUtils.userFolder() + os.sep + "scripts" mkdir(folder) return folder
def exportVectorLayer(self, orgFilename): #only export to an intermediate shp if the layer is not file-based. #We assume that almost all file formats will be supported by ogr #We also export if there is a selection if not os.path.exists(orgFilename): layer = QGisLayers.getObjectFromUri(orgFilename, False) if layer: filename = LayerExporter.exportVectorLayer(layer) else: layer = QGisLayers.getObjectFromUri(orgFilename, False) if layer: useSelection = SextanteConfig.getSetting( SextanteConfig.USE_SELECTED) if useSelection and layer.selectedFeatureCount() != 0: filename = LayerExporter.exportVectorLayer(layer) else: filename = orgFilename else: filename = orgFilename destFilename = self.getTempFilename() self.exportedLayers[orgFilename] = destFilename command = "v.in.ogr" command += " min_area=-1" command += " dsn=\"" + os.path.dirname(filename) + "\"" command += " layer=" + os.path.basename(filename)[:-4] command += " output=" + destFilename command += " --overwrite -o" return command
def otbLibPath(): folder = SextanteConfig.getSetting(OTBUtils.OTB_LIB_FOLDER) if folder == None: folder = "" #try to configure the path automatically if SextanteUtils.isMac(): testfolder = os.path.join(str(QgsApplication.prefixPath()), "lib/otb/applications") if os.path.exists(testfolder): folder = testfolder else: testfolder = "/usr/local/lib/otb/applications" if os.path.exists(testfolder): folder = testfolder elif SextanteUtils.isWindows(): testfolder = os.path.dirname(str(QgsApplication.prefixPath())) testfolder = os.path.join(testfolder, "orfeotoolbox") testfolder = os.path.join(testfolder, "applications") if os.path.exists(testfolder): folder = testfolder else: testfolder = "/usr/lib/otb/applications" if os.path.exists(testfolder): folder = testfolder return folder
def addRecentAlgorithms(self, updating): showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: recent = SextanteLog.getRecentAlgorithms() if len(recent) != 0: found = False if updating: recentItem = self.algorithmTree.topLevelItem(0) treeWidget = recentItem.treeWidget() treeWidget.takeTopLevelItem(treeWidget.indexOfTopLevelItem(recentItem)) #self.algorithmTree.removeItemWidget(first, 0) recentItem = QTreeWidgetItem() recentItem.setText(0, self.tr("Recently used algorithms")) for algname in recent: alg = Sextante.getAlgorithm(algname) if alg is not None: algItem = TreeAlgorithmItem(alg) recentItem.addChild(algItem) found = True if found: self.algorithmTree.insertTopLevelItem(0, recentItem) recentItem.setExpanded(True) self.algorithmTree.setWordWrap(True)
def executeGrass(commands, progress, outputCommands = None): loglines = [] loglines.append("GRASS execution console output") grassOutDone = False command = GrassUtils.prepareGrassExecution(commands) proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True).stdout for line in iter(proc.readline, ""): if "GRASS_INFO_PERCENT" in line: try: progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:])) except: pass else: if "r.out" in line or "v.out" in line: grassOutDone = True loglines.append(line) progress.setConsoleInfo(line) # Some GRASS scripts, like r.mapcalculator or r.fillnulls, call other GRASS scripts during execution. This may override any commands that are # still to be executed by the subprocess, which are usually the output ones. If that is the case runs the output commands again. if not grassOutDone and outputCommands: command = GrassUtils.prepareGrassExecution(outputCommands) proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True).stdout for line in iter(proc.readline, ""): if "GRASS_INFO_PERCENT" in line: try: progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:])) except: pass else: loglines.append(line) progress.setConsoleInfo(line) if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE): SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) return loglines;
def checkRIsInstalled(ignoreRegistrySettings=False): if SextanteUtils.isWindows(): path = RUtils.RFolder() if path == "": return "R folder is not configured.\nPlease configure it before running R scripts." R_INSTALLED = "R_INSTALLED" settings = QSettings() if not ignoreRegistrySettings: if settings.contains(R_INSTALLED): return if SextanteUtils.isWindows(): if SextanteConfig.getSetting(RUtils.R_USE64): execDir = "x64" else: execDir = "i386" command = [RUtils.RFolder() + os.sep + "bin" + os.sep + execDir + os.sep + "R.exe", "--version"] else: command = ["R --version"] proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True).stdout for line in iter(proc.readline, ""): if "R version" in line: settings.setValue(R_INSTALLED, True) return html = ("<p>This algorithm requires R to be run." "Unfortunately, it seems that R is not installed in your system, or it is not correctly configured to be used from QGIS</p>" '<p><a href= "http://docs.qgis.org/2.0/html/en/docs/user_manual/sextante/3rdParty.html">Click here</a>' 'to know more about how to install and configure R to be used with SEXTANTE</p>') return html
def executeSaga(progress): if SextanteUtils.isWindows(): command = ["cmd.exe", "/C ", SagaUtils.sagaBatchJobFilename()] else: os.chmod(SagaUtils.sagaBatchJobFilename(), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE) command = [SagaUtils.sagaBatchJobFilename()] loglines = [] loglines.append("SAGA execution console output") proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True).stdout for line in iter(proc.readline, ""): if "%" in line: s = "".join([x for x in line if x.isdigit()]) try: progress.setPercentage(int(s)) except: pass else: line = line.strip() if line != "/" and line != "-" and line != "\\" and line != "|": loglines.append(line) progress.setConsoleInfo(line) if SextanteConfig.getSetting(SagaUtils.SAGA_LOG_CONSOLE): SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
def runIli2c(args, progress): # ili2c USAGE # ili2c [Options] file1.ili file2.ili ... # # OPTIONS # #--no-auto don't look automatically after required models. #-o0 Generate no output (default). #-o1 Generate INTERLIS-1 output. #-o2 Generate INTERLIS-2 output. #-oXSD Generate an XML-Schema. #-oFMT Generate an INTERLIS-1 Format. #-oIMD Generate Model as IlisMeta INTERLIS-Transfer (XTF). #-oIOM (deprecated) Generate Model as INTERLIS-Transfer (XTF). #--out file/dir file or folder for output. #--ilidirs %ILI_DIR;http://models.interlis.ch/;%JAR_DIR list of directories with ili-files. #--proxy host proxy server to access model repositories. #--proxyPort port proxy port to access model repositories. #--with-predefined Include the predefined MODEL INTERLIS in # the output. Usually, this is omitted. #--without-warnings Report only errors, no warnings. Usually, # warnings are generated as well. #--trace Display detailed trace messages. #--quiet Suppress info messages. #-h|--help Display this help text. #-u|--usage Display short information about usage. #-v|--version Display the version of ili2c. IliUtils.runJava(SextanteConfig.getSetting(IliUtils.ILI2C_JAR), args, progress)
def WpsDescriptionFolder(): folder = SextanteConfig.getSetting( WpsAlgorithmProvider.WPS_DESCRIPTIONS) if folder == None: folder = unicode(os.path.join(SextanteUtils.userFolder(), "wps")) mkdir(folder) return os.path.abspath(folder)
def addRecentAlgorithms(self, updating): showRecent = SextanteConfig.getSetting( SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: recent = SextanteLog.getRecentAlgorithms() if len(recent) != 0: found = False if updating: recentItem = self.algorithmTree.topLevelItem(0) treeWidget = recentItem.treeWidget() treeWidget.takeTopLevelItem( treeWidget.indexOfTopLevelItem(recentItem)) #self.algorithmTree.removeItemWidget(first, 0) recentItem = QTreeWidgetItem() recentItem.setText(0, self.tr("Recently used algorithms")) for algname in recent: alg = Sextante.getAlgorithm(algname) if alg is not None: algItem = TreeAlgorithmItem(alg) recentItem.addChild(algItem) found = True if found: self.algorithmTree.insertTopLevelItem(0, recentItem) recentItem.setExpanded(True) self.algorithmTree.setWordWrap(True)
def executeSaga(progress): if SextanteUtils.isWindows(): command = ["cmd.exe", "/C ", SagaUtils.sagaBatchJobFilename()] else: os.chmod(SagaUtils.sagaBatchJobFilename(), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE) command = [SagaUtils.sagaBatchJobFilename()] loglines = [] loglines.append("SAGA execution console output") proc = subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, ).stdout for line in iter(proc.readline, ""): if "%" in line: s = "".join([x for x in line if x.isdigit()]) try: progress.setPercentage(int(s)) except: pass else: line = line.strip() if line != "/" and line != "-" and line != "\\" and line != "|": loglines.append(line) progress.setConsoleInfo(line) if SextanteConfig.getSetting(SagaUtils.SAGA_LOG_CONSOLE): SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
def handleAlgorithmResults(alg, showResults=True): htmlResults = False for out in alg.outputs: if out.hidden or not out.open: continue if isinstance(out, (OutputRaster, OutputVector, OutputTable)): try: if out.value.startswith("memory:"): layer = out.memoryLayer QgsMapLayerRegistry.instance().addMapLayer(layer) else: if SextanteConfig.getSetting( SextanteConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description QGisLayers.load( out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(), out.name)) except Exception, e: QMessageBox.critical(None, "Error", str(e)) elif isinstance(out, OutputHTML): SextanteResults.addResult(out.description, out.value) htmlResults = True
def processAlgorithm(self, progress): commands.append(os.path.join(TauDEMUtils.mpiexecPath(), "mpiexec")) processNum = SextanteConfig.getSetting(TauDEMUtils.MPI_PROCESSES) if processNum <= 0: raise GeoAlgorithmExecutionException("Wrong number of MPI processes used.\nPlease set correct number before running TauDEM algorithms.") commands.append("-n") commands.append(str(processNum)) commands.append(os.path.join(TauDEMUtils.taudemPath(), self.cmdName)) commands.append("-ad8") commands.append(self.getParameterValue(self.D8_CONTRIB_AREA_GRID)) commands.append("-p") commands.append(self.getParameterValue(self.D8_FLOW_DIR_GRID)) commands.append("-fel") commands.append(self.getParameterValue(self.PIT_FILLED_GRID)) commands.append("-ssa") commands.append(self.getParameterValue(self.ACCUM_STREAM_SOURCE_GRID)) commands.append("-o") commands.append(self.getParameterValue(self.OUTLETS_SHAPE)) commands.append("-par") commands.append(str(self.getParameterValue(self.MIN_TRESHOLD))) commands.append(str(self.getParameterValue(self.MAX_THRESHOLD))) commands.append(str(self.getParameterValue(self.TRESHOLD_NUM))) commands.append(str(self.getParameterValue(self.STEPS))) commands.append("-drp") commands.append(self.getOutputValue(self.DROP_ANALYSIS_FILE)) loglines = [] loglines.append("TauDEM execution command") for line in commands: loglines.append(line) SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) TauDEMUtils.executeTauDEM(commands, progress)
def exportVectorLayer(self, orgFilename): #only export to an intermediate shp if the layer is not file-based. #We assume that almost all file formats will be supported by ogr #We also export if there is a selection if not os.path.exists(orgFilename): layer = QGisLayers.getObjectFromUri(orgFilename, False) if layer: filename = LayerExporter.exportVectorLayer(layer) else: layer = QGisLayers.getObjectFromUri(orgFilename, False) if layer: useSelection = SextanteConfig.getSetting(SextanteConfig.USE_SELECTED) if useSelection and layer.selectedFeatureCount() != 0: filename = LayerExporter.exportVectorLayer(layer) else: filename = orgFilename else: filename = orgFilename destFilename = self.getTempFilename() self.exportedLayers[orgFilename]= destFilename command = "v.in.ogr" command += " min_area=-1" command +=" dsn=\"" + os.path.dirname(filename) + "\"" command +=" layer=" + os.path.basename(filename)[:-4] command +=" output=" + destFilename; command +=" --overwrite -o" return command
def checkBeforeOpeningParametersDialog(self): if SextanteUtils.isWindows(): path = RUtils.RFolder() if path == "": return "R folder is not configured.\nPlease configure it before running R scripts." R_INSTALLED = "R_INSTALLED" settings = QSettings() if settings.contains(R_INSTALLED): return if SextanteUtils.isWindows(): if SextanteConfig.getSetting(RUtils.R_USE64): execDir = "x64" else: execDir = "i386" command = [RUtils.RFolder() + os.sep + "bin" + os.sep + execDir + os.sep + "R.exe --version"] else: command = ["R --version"] proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True).stdout for line in iter(proc.readline, ""): if "R version" in line: settings.setValue(R_INSTALLED, True) return return "It seems that R is not correctly installed in your system.\nPlease install it before running R Scripts."
def executeAlgorithm(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = Sextante.getAlgorithm(item.alg.commandLineName()) message = alg.checkBeforeOpeningParametersDialog() if message: dlg = MissingDependencyDialog(message) dlg.exec_() #QMessageBox.warning(self, self.tr("Warning"), message) return alg = alg.getCopy() dlg = alg.getCustomParametersDialog() if not dlg: dlg = ParametersDialog(alg) canvas = QGisLayers.iface.mapCanvas() prevMapTool = canvas.mapTool() dlg.show() dlg.exec_() if canvas.mapTool()!=prevMapTool: try: canvas.mapTool().reset() except: pass canvas.setMapTool(prevMapTool) if dlg.executed: showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: self.addRecentAlgorithms(True) if isinstance(item, TreeActionItem): action = item.action action.setData(self) action.execute()
def executeGrass(commands, progress): if SextanteUtils.isWindows(): GrassUtils.createGrassScript(commands) command = ["cmd.exe", "/C ", GrassUtils.grassScriptFilename()] else: gisrc = SextanteUtils.userFolder() + os.sep + "sextante.gisrc" os.putenv("GISRC", gisrc) os.putenv("GRASS_MESSAGE_FORMAT", "gui") os.putenv("GRASS_BATCH_JOB", GrassUtils.grassBatchJobFilename()) GrassUtils.createGrassBatchJobFileFromGrassCommands(commands) os.chmod(GrassUtils.grassBatchJobFilename(), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE) if SextanteUtils.isMac(): command = GrassUtils.grassPath() + os.sep + "grass.sh " + GrassUtils.grassMapsetFolder() + "/user" else: command = "grass64 " + GrassUtils.grassMapsetFolder() + "/user" loglines = [] loglines.append("GRASS execution console output") proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True).stdout for line in iter(proc.readline, ""): if "GRASS_INFO_PERCENT" in line: try: progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:])) except: pass else: loglines.append(line) progress.setConsoleInfo(line) if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE): SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) shutil.rmtree(GrassUtils.grassMapsetFolder(), True)
def saveToFile(self): filefilter = self.output.getFileFilter(self.alg) settings = QSettings() if settings.contains("/SextanteQGIS/LastOutputPath"): path = str( settings.value("/SextanteQGIS/LastOutputPath", QVariant("")).toString()) else: path = SextanteConfig.getSetting(SextanteConfig.OUTPUT_FOLDER) lastEncoding = settings.value("/SextanteQGIS/encoding", "System").toString() fileDialog = QgsEncodingFileDialog(self, "Save file", QString(path), filefilter, lastEncoding) fileDialog.setFileMode(QFileDialog.AnyFile) fileDialog.setAcceptMode(QFileDialog.AcceptSave) fileDialog.setConfirmOverwrite(True) if fileDialog.exec_() == QDialog.Accepted: files = fileDialog.selectedFiles() encoding = unicode(fileDialog.encoding()) self.output.encoding = encoding filename = unicode(files.first()) self.text.setText(filename) settings.setValue("/SextanteQGIS/LastOutputPath", os.path.dirname(str(filename))) settings.setValue("/SextanteQGIS/encoding", encoding)
def processAlgorithm(self, progress): commands = [] commands.append(os.path.join(TauDEMUtils.mpiexecPath(), "mpiexec")) processNum = SextanteConfig.getSetting(TauDEMUtils.MPI_PROCESSES) if processNum <= 0: raise GeoAlgorithmExecutionException("Wrong number of MPI processes used.\nPlease set correct number before running TauDEM algorithms.") commands.append("-n") commands.append(str(processNum)) commands.append(os.path.join(TauDEMUtils.taudemPath(), self.cmdName)) commands.append("-ang") commands.append(self.getParameterValue(self.DINF_FLOW_DIR_GRID)) commands.append("-fel") commands.append(self.getParameterValue(self.PIT_FILLED_GRID)) commands.append("-m") commands.append(str(self.STAT_DICT[self.getParameterValue(self.STAT_METHOD)])) commands.append(str(self.DIST_DICT[self.getParameterValue(self.DIST_METHOD)])) commands.append("-thresh") commands.append(str(self.getParameterValue(self.THRESHOLD))) if str(self.getParameterValue(self.EDGE_CONTAM)).lower() == "false": commands.append("-nc") commands.append("-du") commands.append(self.getOutputValue(self.DIST_UP_GRID)) loglines = [] loglines.append("TauDEM execution command") for line in commands: loglines.append(line) SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) TauDEMUtils.executeTauDEM(commands, progress)
def exportVectorLayer(layer): '''Takes a QgsVectorLayer and returns the filename to refer to it, which allows external apps which support only file-based layers to use it. It performs the necessary export in case the input layer is not in a standard format suitable for most applications, it is a remote one or db-based (non-file based) one, or if there is a selection and it should be used, exporting just the selected features. Currently, the output is restricted to shapefiles, so anything that is not in a shapefile will get exported''' settings = QSettings() systemEncoding = settings.value( "/UI/encoding", "System" ).toString() output = SextanteUtils.getTempFilename("shp") provider = layer.dataProvider() allAttrs = provider.attributeIndexes() provider.select( allAttrs ) useSelection = SextanteConfig.getSetting(SextanteConfig.USE_SELECTED) if useSelection and layer.selectedFeatureCount() != 0: writer = QgsVectorFileWriter( output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() ) selection = layer.selectedFeatures() for feat in selection: writer.addFeature(feat) del writer return output else: if (not unicode(layer.source()).endswith("shp")): writer = QgsVectorFileWriter( output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() ) feat = QgsFeature() while provider.nextFeature(feat): writer.addFeature(feat) del writer return output else: return unicode(layer.source())
def getRecentAlgorithms(): recentAlgsSetting = SextanteConfig.getSetting(SextanteConfig.RECENT_ALGORITHMS) try: SextanteLog.recentAlgs = recentAlgsSetting.split(';') except: pass return SextanteLog.recentAlgs
def otbPath(): folder = SextanteConfig.getSetting(OTBUtils.OTB_FOLDER) if folder == None: folder = "" #try to configure the path automatically if SextanteUtils.isMac(): testfolder = os.path.join(str(QgsApplication.prefixPath()), "bin") if os.path.exists(os.path.join(testfolder, "otbcli")): folder = testfolder else: testfolder = "/usr/local/bin" if os.path.exists(os.path.join(testfolder, "otbcli")): folder = testfolder elif SextanteUtils.isWindows(): testfolder = os.path.dirname(str(QgsApplication.prefixPath())) testfolder = os.path.dirname(testfolder) testfolder = os.path.join(testfolder, "bin") path = os.path.join(testfolder, "otbcli.bat") if os.path.exists(path): folder = testfolder else: testfolder = "/usr/bin" if os.path.exists(os.path.join(testfolder, "otbcli")): folder = testfolder return folder
def handleAlgorithmResults(alg, progress, showResults = True): wrongLayers = [] htmlResults = False; progress.setText("Loading resulting layers") i = 0 for out in alg.outputs: progress.setPercentage(100 * i / float(len(alg.outputs))) if out.hidden or not out.open: continue if isinstance(out, (OutputRaster, OutputVector, OutputTable)): try: if out.value.startswith("memory:"): layer = out.memoryLayer QgsMapLayerRegistry.instance().addMapLayers([layer]) else: if SextanteConfig.getSetting(SextanteConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description QGisLayers.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(),out.name)) except Exception, e: wrongLayers.append(out) #QMessageBox.critical(None, "Error", str(e)) elif isinstance(out, OutputHTML): SextanteResults.addResult(out.description, out.value) htmlResults = True
def RScriptsFolder(): folder = SextanteConfig.getSetting(RUtils.RSCRIPTS_FOLDER) if folder == None: folder = os.path.join(os.path.dirname(__file__), "scripts") mkdir(folder) return folder
def RScriptsFolder(): folder = SextanteConfig.getSetting(RUtils.RSCRIPTS_FOLDER) if folder == None: folder = unicode(os.path.join(SextanteUtils.userFolder(), "rscripts")) mkdir(folder) return os.path.abspath(folder)
def processAlgorithm(self, progress): commands = [] commands.append(os.path.join(TauDEMUtils.mpiexecPath(), "mpiexec")) processNum = SextanteConfig.getSetting(TauDEMUtils.MPI_PROCESSES) if processNum <= 0: raise GeoAlgorithmExecutionException("Wrong number of MPI processes used.\nPlease set correct number before running TauDEM algorithms.") commands.append("-n") commands.append(str(processNum)) commands.append(os.path.join(TauDEMUtils.taudemPath(), self.cmdName)) commands.append("-plen") commands.append(self.getParameterValue(self.LENGTH_GRID)) commands.append("-ad8") commands.append(self.getParameterValue(self.CONTRIB_AREA_GRID)) commands.append("-par") commands.append(str(self.getParameterValue(self.THRESHOLD))) commands.append(str(self.getParameterValue(self.EXPONENT))) commands.append("-ss") commands.append(self.getOutputValue(self.STREAM_SOURCE_GRID)) loglines = [] loglines.append("TauDEM execution command") for line in commands: loglines.append(line) SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) TauDEMUtils.executeTauDEM(commands, progress)
def saveToFile(self): filefilter = self.output.getFileFilter(self.alg) settings = QSettings() if settings.contains("/SextanteQGIS/LastOutputPath"): path = settings.value( "/SextanteQGIS/LastOutputPath") else: path = SextanteConfig.getSetting(SextanteConfig.OUTPUT_FOLDER) lastEncoding = settings.value("/SextanteQGIS/encoding", "System") fileDialog = QgsEncodingFileDialog(self, "Save file", path, filefilter, lastEncoding) fileDialog.setFileMode(QFileDialog.AnyFile) fileDialog.setAcceptMode(QFileDialog.AcceptSave) fileDialog.setConfirmOverwrite(True) if fileDialog.exec_() == QDialog.Accepted: files = fileDialog.selectedFiles() encoding = unicode(fileDialog.encoding()) self.output.encoding = encoding filename = unicode(files[0]) selectedFilefilter = unicode(fileDialog.selectedNameFilter()) if not filename.lower().endswith(tuple(re.findall("\*(\.[a-z]{1,5})", filefilter))): ext = re.search("\*(\.[a-z]{1,5})", selectedFilefilter) if ext: filename = filename + ext.group(1) self.text.setText(filename) settings.setValue("/SextanteQGIS/LastOutputPath", os.path.dirname(filename)) settings.setValue("/SextanteQGIS/encoding", encoding)
def handleAlgorithmResults(alg, progress, showResults=True): wrongLayers = [] htmlResults = False progress.setText("Loading resulting layers") i = 0 for out in alg.outputs: progress.setPercentage(100 * i / float(len(alg.outputs))) if out.hidden or not out.open: continue if isinstance(out, (OutputRaster, OutputVector, OutputTable)): try: if out.value.startswith("memory:"): layer = out.memoryLayer QgsMapLayerRegistry.instance().addMapLayers([layer]) else: if SextanteConfig.getSetting( SextanteConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description QGisLayers.load( out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(), out.name)) except Exception, e: wrongLayers.append(out) #QMessageBox.critical(None, "Error", str(e)) elif isinstance(out, OutputHTML): SextanteResults.addResult(out.description, out.value) htmlResults = True
def modelsFolder(): folder = SextanteConfig.getSetting(ModelerUtils.MODELS_FOLDER) if folder == None: folder = os.path.join(os.path.dirname(__file__), "models") mkdir(folder) return folder
def getExtendedLayerName(self, layer): authid = layer.crs().authid() if SextanteConfig.getSetting( SextanteConfig.SHOW_CRS_DEF) and authid is not None: return layer.name() + " [" + str(authid) + "]" else: return layer.name()