def otbLibPath(): folder = ProcessingConfig.getSetting(OTBUtils.OTB_LIB_FOLDER) if folder == None: folder = "" #try to configure the path automatically if ProcessingUtils.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 ProcessingUtils.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 checkRIsInstalled(ignoreRegistrySettings=False): if ProcessingUtils.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 ProcessingUtils.isWindows(): if ProcessingConfig.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/processing/3rdParty.html">Click here</a>' 'to know more about how to install and configure R to be used with QGIS</p>') return html
def otbPath(): folder = ProcessingConfig.getSetting(OTBUtils.OTB_FOLDER) if folder == None: folder = "" #try to configure the path automatically if ProcessingUtils.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 ProcessingUtils.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 processAlgorithm(self, progress): #TODO:check correct num of bands input = self.getParameterValue(SplitRGBBands.INPUT) temp = ProcessingUtils.getTempFilename(None).replace('.',''); basename = os.path.basename(temp) validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" safeBasename = ''.join(c for c in basename if c in validChars) temp = os.path.join(os.path.dirname(temp), safeBasename) r = self.getOutputValue(SplitRGBBands.R) g = self.getOutputValue(SplitRGBBands.G) b = self.getOutputValue(SplitRGBBands.B) commands = [] if ProcessingUtils.isWindows(): commands.append("io_gdal 0 -GRIDS \"" + temp + "\" -FILES \"" + input+"\"") commands.append("io_gdal 1 -GRIDS \"" + temp + "_0001.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + r + "\""); commands.append("io_gdal 1 -GRIDS \"" + temp + "_0002.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + g + "\""); commands.append("io_gdal 1 -GRIDS \"" + temp + "_0003.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + b + "\""); else: commands.append("libio_gdal 0 -GRIDS \"" + temp + "\" -FILES \"" + input + "\"") commands.append("libio_gdal 1 -GRIDS \"" + temp + "_0001.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + r + "\""); commands.append("libio_gdal 1 -GRIDS \"" + temp + "_0002.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + g + "\""); commands.append("libio_gdal 1 -GRIDS \"" + temp + "_0003.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + b + "\""); SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) SagaUtils.executeSaga(progress);
def unload(self): AlgorithmProvider.unload(self) if ProcessingUtils.isWindows() or ProcessingUtils.isMac(): ProcessingConfig.removeSetting(GrassUtils.GRASS_FOLDER) ProcessingConfig.removeSetting(GrassUtils.GRASS_WIN_SHELL) ProcessingConfig.removeSetting(GrassUtils.GRASS_LOG_COMMANDS) ProcessingConfig.removeSetting(GrassUtils.GRASS_LOG_CONSOLE)
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if ProcessingUtils.isWindows() or ProcessingUtils.isMac(): ProcessingConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath())) ProcessingConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell())) ProcessingConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False)) ProcessingConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False))
def exportRasterLayer(self, layer): destFilename = ProcessingUtils.getTempFilenameInTempFolder(os.path.basename(layer)[0:5] + ".sgrd") self.exportedLayers[layer]= destFilename saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if ProcessingUtils.isWindows() or ProcessingUtils.isMac() or not saga208: return "io_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + layer+"\"" else: return "libio_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + layer + "\""
def sagaBatchJobFilename(): if ProcessingUtils.isWindows(): filename = "saga_batch_job.bat" else: filename = "saga_batch_job.sh" batchfile = ProcessingUtils.userFolder() + os.sep + filename return batchfile
def exportRasterLayer(self, layer): destFilename = ProcessingUtils.getTempFilenameInTempFolder( os.path.basename(layer)[0:5] + ".sgrd") self.exportedLayers[layer] = destFilename saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if ProcessingUtils.isWindows() or ProcessingUtils.isMac( ) or not saga208: return "io_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + layer + "\"" else: return "libio_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + layer + "\""
def sagaBatchJobFilename(): if ProcessingUtils.isWindows(): filename = "saga_batch_job.bat"; else: filename = "saga_batch_job.sh"; batchfile = ProcessingUtils.userFolder() + os.sep + filename return batchfile
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if ProcessingUtils.isWindows() or ProcessingUtils.isMac(): ProcessingConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath())) ProcessingConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell())) ProcessingConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False)) ProcessingConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False))
def checkGrassIsInstalled(ignoreRegistrySettings=False): if ProcessingUtils.isWindows(): path = GrassUtils.grassPath() if path == "": return "GRASS folder is not configured.\nPlease configure it before running SAGA algorithms." cmdpath = os.path.join(path, "bin","r.out.gdal.exe") if not os.path.exists(cmdpath): return ("The specified GRASS folder does not contain a valid set of GRASS modules.\n" + "Please, go to the processing settings dialog, and check that the GRASS\n" + "folder is correctly configured") settings = QSettings() GRASS_INSTALLED = "/ProcessingQGIS/GrassInstalled" if not ignoreRegistrySettings: if settings.contains(GRASS_INSTALLED): return try: from processing import runalg result = runalg("grass:v.voronoi", points(),False,False,"270778.60198,270855.745301,4458921.97814,4458983.8488",-1,0.0001, 0, None) if not os.path.exists(result['output']): return "It seems that GRASS is not correctly installed and configured in your system.\nPlease install it before running GRASS algorithms." except: s = traceback.format_exc() return "Error while checking GRASS installation. GRASS might not be correctly configured.\n" + s; settings.setValue(GRASS_INSTALLED, True)
def executeSaga(progress): if ProcessingUtils.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 ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_CONSOLE): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
def exportTable( table): '''Takes a QgsVectorLayer and returns the filename to refer to its attributes table, 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 isa remote one or db-based (non-file based) one Currently, the output is restricted to dbf. It also export to a new file if the original one contains non-ascii characters''' settings = QSettings() systemEncoding = settings.value( "/UI/encoding", "System" ) output = ProcessingUtils.getTempFilename("dbf") provider = table.dataProvider() isASCII=True try: unicode(table.source()).decode("ascii") except UnicodeEncodeError: isASCII=False isDbf = unicode(table.source()).endswith("dbf") or unicode(table.source()).endswith("shp") if (not isDbf or not isASCII): writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), QGis.WKBNoGeometry, layer.crs() ) for feat in table.getFeatures(): writer.addFeature(feat) del writer return output else: filename = unicode(table.source()) if unicode(table.source()).endswith("shp"): return filename[:-3] + "dbf" else: return filename
def checkSagaIsInstalled(ignoreRegistrySettings=False): if ProcessingUtils.isWindows(): path = SagaUtils.sagaPath() if path == "": return "SAGA folder is not configured.\nPlease configure it before running SAGA algorithms." cmdpath = os.path.join(path, "saga_cmd.exe") if not os.path.exists(cmdpath): return ("The specified SAGA folder does not contain a valid SAGA executable.\n" + "Please, go to the processing settings dialog, and check that the SAGA\n" + "folder is correctly configured") settings = QSettings() SAGA_INSTALLED = "/ProcessingQGIS/SagaInstalled" if not ignoreRegistrySettings: if settings.contains(SAGA_INSTALLED): return try: from processing import runalg result = runalg("saga:thiessenpolygons", points(), None) if not os.path.exists(result['POLYGONS']): return "It seems that SAGA is not correctly installed in your system.\nPlease install it before running SAGA algorithms." except: s = traceback.format_exc() return "Error while checking SAGA installation. SAGA might not be correctly configured.\n" + s; settings.setValue(SAGA_INSTALLED, True)
def modelsFolder(): folder = ProcessingConfig.getSetting(ModelerUtils.MODELS_FOLDER) if folder == None: folder = unicode(os.path.join(ProcessingUtils.userFolder(), "models")) mkdir(folder) return os.path.abspath(folder)
def checkGrassIsInstalled(ignoreRegistrySettings=False): if ProcessingUtils.isWindows(): path = GrassUtils.grassPath() if path == "": return "GRASS folder is not configured.\nPlease configure it before running SAGA algorithms." cmdpath = os.path.join(path, "bin", "r.out.gdal.exe") if not os.path.exists(cmdpath): return ( "The specified GRASS folder does not contain a valid set of GRASS modules.\n" + "Please, go to the processing settings dialog, and check that the GRASS\n" + "folder is correctly configured") settings = QSettings() GRASS_INSTALLED = "/ProcessingQGIS/GrassInstalled" if not ignoreRegistrySettings: if settings.contains(GRASS_INSTALLED): return try: from processing import runalg result = runalg( "grass:v.voronoi", points(), False, False, "270778.60198,270855.745301,4458921.97814,4458983.8488", -1, 0.0001, 0, None) if not os.path.exists(result['output']): return "It seems that GRASS is not correctly installed and configured in your system.\nPlease install it before running GRASS algorithms." except: s = traceback.format_exc() return "Error while checking GRASS installation. GRASS might not be correctly configured.\n" + s settings.setValue(GRASS_INSTALLED, True)
def WpsDescriptionFolder(): folder = ProcessingConfig.getSetting( WpsAlgorithmProvider.WPS_DESCRIPTIONS) if folder == None: folder = str(os.path.join(ProcessingUtils.userFolder(), "wps")) mkdir(folder) return os.path.abspath(folder)
def checkSagaIsInstalled(ignoreRegistrySettings=False): if ProcessingUtils.isWindows(): path = SagaUtils.sagaPath() if path == "": return "SAGA folder is not configured.\nPlease configure it before running SAGA algorithms." cmdpath = os.path.join(path, "saga_cmd.exe") if not os.path.exists(cmdpath): return ( "The specified SAGA folder does not contain a valid SAGA executable.\n" + "Please, go to the processing settings dialog, and check that the SAGA\n" + "folder is correctly configured" ) settings = QSettings() SAGA_INSTALLED = "/ProcessingQGIS/SagaInstalled" if not ignoreRegistrySettings: if settings.contains(SAGA_INSTALLED): return try: from processing import runalg result = runalg("saga:thiessenpolygons", points(), None) if not os.path.exists(result["POLYGONS"]): return "It seems that SAGA is not correctly installed in your system.\nPlease install it before running SAGA algorithms." except: s = traceback.format_exc() return "Error while checking SAGA installation. SAGA might not be correctly configured.\n" + s settings.setValue(SAGA_INSTALLED, True)
def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self): '''this tests both the exporting to shp and then the format change in the output layer''' layer = processing.getobject(polygonsGeoJson()); feature = layer.getFeatures().next() selected = [feature.id()] layer.setSelectedFeatures(selected) outputs=processing.runalg("saga:polygoncentroids",polygonsGeoJson(),True, ProcessingUtils.getTempFilename("geojson")) layer.setSelectedFeatures([]) output=outputs['CENTROIDS'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['ID','POLY_NUM_A','POLY_ST_A'] expectedtypes=['Real','Real','String'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(1, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["0","1.1","string a"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POINT(270787.49991451 4458955.46775295)' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_SagaRasterAlgorithmWithUnsupportedOutputFormat(self): outputs=processing.runalg("saga:convergenceindex",raster(),0,0,ProcessingUtils.getTempFilename("img")) output=outputs['RESULT'] self.assertTrue(os.path.isfile(output)) dataset=gdal.Open(output, GA_ReadOnly) strhash=hash(str(dataset.ReadAsArray(0).tolist())) self.assertEqual(strhash, 485390137)
def executeRAlgorithm(alg, progress): RUtils.verboseCommands = alg.getVerboseCommands() RUtils.createRScriptFromRCommands(alg.getFullSetOfRCommands()) if ProcessingUtils.isWindows(): if ProcessingConfig.getSetting(RUtils.R_USE64): execDir = "x64" else: execDir = "i386" command = [ RUtils.RFolder() + os.sep + "bin" + os.sep + execDir + os.sep + "R.exe", "CMD", "BATCH", "--vanilla", RUtils.getRScriptFilename(), RUtils.getConsoleOutputFilename() ] else: os.chmod(RUtils.getRScriptFilename(), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE) command = "R CMD BATCH --vanilla " + RUtils.getRScriptFilename( ) + " " + RUtils.getConsoleOutputFilename() proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) proc.wait() RUtils.createConsoleOutput() loglines = [] loglines.append("R execution console output") loglines += RUtils.allConsoleResults for line in loglines: progress.setConsoleInfo(line) ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
def createGrassScript(commands): folder = GrassUtils.grassPath() shell = GrassUtils.grassWinShell() script = GrassUtils.grassScriptFilename() gisrc = ProcessingUtils.userFolder() + os.sep + "processing.gisrc" #temporary gisrc file output = open(gisrc, "w") location = "temp_location" gisdbase = GrassUtils.grassDataFolder() #gisdbase = os.path.join(os.path.expanduser("~"), "processing", "tempdata", "grassdata") output.write("GISDBASE: " + gisdbase + "\n") output.write("LOCATION_NAME: " + location + "\n") output.write("MAPSET: PERMANENT \n") output.write("GRASS_GUI: text\n") output.close() output = open(script, "w") output.write("set HOME=" + os.path.expanduser("~") + "\n") output.write("set GISRC=" + gisrc + "\n") output.write("set GRASS_SH=" + shell + "\\bin\\sh.exe\n") output.write("set PATH=" + shell + os.sep + "bin;" + shell + os.sep + "lib;" + "%PATH%\n") output.write("set WINGISBASE=" + folder + "\n") output.write("set GISBASE=" + folder + "\n") output.write("set GRASS_PROJSHARE=" + folder + os.sep + "share" + os.sep + "proj" + "\n") output.write("set GRASS_MESSAGE_FORMAT=gui\n") #Replacement code for etc/Init.bat output.write( "if \"%GRASS_ADDON_PATH%\"==\"\" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%PATH%\n" ) output.write( "if not \"%GRASS_ADDON_PATH%\"==\"\" set PATH=%WINGISBASE%\\bin;%WINGISBASE%\\lib;%GRASS_ADDON_PATH%;%PATH%\n" ) output.write("\n") output.write("set GRASS_VERSION=" + GrassUtils.getGrassVersion() + "\n") output.write("if not \"%LANG%\"==\"\" goto langset\n") output.write( "FOR /F \"usebackq delims==\" %%i IN (`\"%WINGISBASE%\\etc\\winlocale\"`) DO @set LANG=%%i\n" ) output.write(":langset\n") output.write("\n") output.write("set PATHEXT=%PATHEXT%;.PY\n") output.write( "set PYTHONPATH=%PYTHONPATH%;%WINGISBASE%\\etc\\python;%WINGISBASE%\\etc\\wxpython\\n" ) output.write("\n") output.write("g.gisenv.exe set=\"MAPSET=PERMANENT\"\n") output.write("g.gisenv.exe set=\"LOCATION=" + location + "\"\n") output.write("g.gisenv.exe set=\"LOCATION_NAME=" + location + "\"\n") output.write("g.gisenv.exe set=\"GISDBASE=" + gisdbase + "\"\n") output.write("g.gisenv.exe set=\"GRASS_GUI=text\"\n") for command in commands: output.write(command + "\n") output.write("\n") output.write("exit\n") output.close()
def executeSaga(progress): if ProcessingUtils.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 ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_CONSOLE): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
def RScriptsFolder(): folder = ProcessingConfig.getSetting(RUtils.RSCRIPTS_FOLDER) if folder == None: folder = unicode(os.path.join(ProcessingUtils.userFolder(), "rscripts")) mkdir(folder) return os.path.abspath(folder)
def test_gdalogrsieveWithUnsupportedOutputFormat(self): outputs=processing.runalg("gdalogr:sieve",raster(),2,0, ProcessingUtils.getTempFilename("img")) output=outputs['dst_filename'] self.assertTrue(os.path.isfile(output)) dataset=gdal.Open(output, GA_ReadOnly) strhash=hash(str(dataset.ReadAsArray(0).tolist())) self.assertEqual(strhash,-1353696889)
def exportRasterLayer(self, source): layer = QGisLayers.getObjectFromUri(source, False) if layer: filename = str(layer.name()) else: filename = source.rstrip(".sgrd") validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:" filename = ''.join(c for c in filename if c in validChars) if len(filename) == 0: filename = "layer" destFilename = ProcessingUtils.getTempFilenameInTempFolder(filename + ".sgrd") self.exportedLayers[source]= destFilename saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if ProcessingUtils.isWindows() or ProcessingUtils.isMac() or not saga208: return "io_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + source+"\"" else: return "libio_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + source + "\""
def getValueAsCommandLineParameter(self): if self.value == None: return str(None) else: if not ProcessingUtils.isWindows(): return "\"" + str(self.value) + "\"" else: return "\"" + str(self.value).replace("\\", "\\\\") + "\""
def modelsFolder(): folder = ProcessingConfig.getSetting(ModelerUtils.MODELS_FOLDER) if folder == None: folder = unicode( os.path.join(ProcessingUtils.userFolder(), "models")) mkdir(folder) return os.path.abspath(folder)
def prepareGrassExecution(commands): if ProcessingUtils.isWindows(): GrassUtils.createGrassScript(commands) command = ["cmd.exe", "/C ", GrassUtils.grassScriptFilename()] else: gisrc = ProcessingUtils.userFolder() + os.sep + "processing.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 ProcessingUtils.isMac(): command = GrassUtils.grassPath() + os.sep + "grass.sh " + GrassUtils.grassMapsetFolder() + "/PERMANENT" else: command = "grass64 " + GrassUtils.grassMapsetFolder() + "/PERMANENT" return command
def getValueAsCommandLineParameter(self): if self.value == None: return str(None) else: if not ProcessingUtils.isWindows(): return "\"" + unicode(self.value) + "\"" else: return "\"" + unicode(self.value).replace("\\", "\\\\") + "\""
def RScriptsFolder(): folder = ProcessingConfig.getSetting(RUtils.RSCRIPTS_FOLDER) if folder == None: folder = unicode( os.path.join(ProcessingUtils.userFolder(), "rscripts")) mkdir(folder) return os.path.abspath(folder)
def createSagaBatchJobFileFromSagaCommands(commands): fout = open(SagaUtils.sagaBatchJobFilename(), "w") if ProcessingUtils.isWindows(): fout.write("set SAGA=" + SagaUtils.sagaPath() + "\n") fout.write("set SAGA_MLB=" + SagaUtils.sagaPath() + os.sep + "modules" + "\n") fout.write("PATH=PATH;%SAGA%;%SAGA_MLB%\n") elif ProcessingUtils.isMac(): fout.write("export SAGA_MLB=" + SagaUtils.sagaPath() + "/../lib/saga\n") fout.write("export PATH=" + SagaUtils.sagaPath() + ":$PATH\n") else: pass for command in commands: fout.write("saga_cmd " + command.encode("utf8") + "\n") fout.write("exit") fout.close()
def createSagaBatchJobFileFromSagaCommands(commands): fout = open(SagaUtils.sagaBatchJobFilename(), "w") if ProcessingUtils.isWindows(): fout.write("set SAGA=" + SagaUtils.sagaPath() + "\n"); fout.write("set SAGA_MLB=" + SagaUtils.sagaPath() + os.sep + "modules" + "\n"); fout.write("PATH=PATH;%SAGA%;%SAGA_MLB%\n"); elif ProcessingUtils.isMac(): fout.write("export SAGA_MLB=" + SagaUtils.sagaPath() + "/../lib/saga\n"); fout.write("export PATH=" + SagaUtils.sagaPath() + ":$PATH\n"); else: pass for command in commands: fout.write("saga_cmd " + command.encode("utf8") + "\n") fout.write("exit") fout.close()
def resampleRasterLayer(self,layer): '''this is supposed to be run after having exported all raster layers''' if layer in self.exportedLayers.keys(): inputFilename = self.exportedLayers[layer] else: inputFilename = layer destFilename = ProcessingUtils.getTempFilename("sgrd") self.exportedLayers[layer]= destFilename saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if ProcessingUtils.isWindows() or ProcessingUtils.isMac() or not saga208: s = "grid_tools \"Resampling\" -INPUT \"" + inputFilename + "\" -TARGET 0 -SCALE_UP_METHOD 4 -SCALE_DOWN_METHOD 4 -USER_XMIN " +\ str(self.xmin) + " -USER_XMAX " + str(self.xmax) + " -USER_YMIN " + str(self.ymin) + " -USER_YMAX " + str(self.ymax) +\ " -USER_SIZE " + str(self.cellsize) + " -USER_GRID \"" + destFilename + "\"" else: s = "libgrid_tools \"Resampling\" -INPUT \"" + inputFilename + "\" -TARGET 0 -SCALE_UP_METHOD 4 -SCALE_DOWN_METHOD 4 -USER_XMIN " +\ str(self.xmin) + " -USER_XMAX " + str(self.xmax) + " -USER_YMIN " + str(self.ymin) + " -USER_YMAX " + str(self.ymax) +\ " -USER_SIZE " + str(self.cellsize) + " -USER_GRID \"" + destFilename + "\"" return s
def unload(self): self.toolbox.setVisible(False) self.menu.deleteLater() #delete temporary output files folder = ProcessingUtils.tempFolder() if QDir(folder).exists(): shutil.rmtree(folder, True) self.iface.unregisterMainWindowAction(self.commanderAction)
def sagaPath(): folder = ProcessingConfig.getSetting(SagaUtils.SAGA_FOLDER) if folder == None: folder ="" #try to auto-configure the folder if ProcessingUtils.isMac(): testfolder = os.path.join(str(QgsApplication.prefixPath()), "bin") if os.path.exists(os.path.join(testfolder, "saga_cmd")): folder = testfolder else: testfolder = "/usr/local/bin" if os.path.exists(os.path.join(testfolder, "saga_cmd")): folder = testfolder elif ProcessingUtils.isWindows(): testfolder = os.path.dirname(str(QgsApplication.prefixPath())) testfolder = os.path.join(testfolder, "saga") if os.path.exists(os.path.join(testfolder, "saga_cmd.exe")): folder = testfolder return folder
def resampleRasterLayer(self, layer): '''this is supposed to be run after having exported all raster layers''' if layer in self.exportedLayers.keys(): inputFilename = self.exportedLayers[layer] else: inputFilename = layer destFilename = ProcessingUtils.getTempFilename("sgrd") self.exportedLayers[layer] = destFilename saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if ProcessingUtils.isWindows() or ProcessingUtils.isMac( ) or not saga208: s = "grid_tools \"Resampling\" -INPUT \"" + inputFilename + "\" -TARGET 0 -SCALE_UP_METHOD 4 -SCALE_DOWN_METHOD 4 -USER_XMIN " +\ str(self.xmin) + " -USER_XMAX " + str(self.xmax) + " -USER_YMIN " + str(self.ymin) + " -USER_YMAX " + str(self.ymax) +\ " -USER_SIZE " + str(self.cellsize) + " -USER_GRID \"" + destFilename + "\"" else: s = "libgrid_tools \"Resampling\" -INPUT \"" + inputFilename + "\" -TARGET 0 -SCALE_UP_METHOD 4 -SCALE_DOWN_METHOD 4 -USER_XMIN " +\ str(self.xmin) + " -USER_XMAX " + str(self.xmax) + " -USER_YMIN " + str(self.ymin) + " -USER_YMAX " + str(self.ymax) +\ " -USER_SIZE " + str(self.cellsize) + " -USER_GRID \"" + destFilename + "\"" return s
def sagaPath(): folder = ProcessingConfig.getSetting(SagaUtils.SAGA_FOLDER) if folder == None: folder = "" # try to auto-configure the folder if ProcessingUtils.isMac(): testfolder = os.path.join(str(QgsApplication.prefixPath()), "bin") if os.path.exists(os.path.join(testfolder, "saga_cmd")): folder = testfolder else: testfolder = "/usr/local/bin" if os.path.exists(os.path.join(testfolder, "saga_cmd")): folder = testfolder elif ProcessingUtils.isWindows(): testfolder = os.path.dirname(str(QgsApplication.prefixPath())) testfolder = os.path.join(testfolder, "saga") if os.path.exists(os.path.join(testfolder, "saga_cmd.exe")): folder = testfolder return folder
def exportRasterLayer(self, source): layer = QGisLayers.getObjectFromUri(source, False) if layer: filename = str(layer.name()) else: filename = source.rstrip(".sgrd") validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:" filename = ''.join(c for c in filename if c in validChars) if len(filename) == 0: filename = "layer" destFilename = ProcessingUtils.getTempFilenameInTempFolder(filename + ".sgrd") self.exportedLayers[source] = destFilename saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if ProcessingUtils.isWindows() or ProcessingUtils.isMac( ) or not saga208: return "io_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + source + "\"" else: return "libio_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + source + "\""
def __init__(self): AlgorithmProvider.__init__(self) self.activate = False self.algsList = [] if ProcessingUtils.isWindows(): lastools = [ las2shp(), lasboundary(), las2dem(), las2iso(), lasgrid(), lasground(), lasinfo(), lasheight(), lasprecision(), lassplit(), lasclassify(), lasclip() ] else: lastools = [lasinfo(), lasprecision()] for alg in lastools: alg.group = "LASTools" self.algsList.extend(lastools) if ProcessingUtils.isWindows(): self.actions.append(OpenViewerAction()) fusiontools = [ CloudMetrics(), CanopyMaxima(), CanopyModel(), ClipData(), Cover(), FilterData(), GridMetrics(), GroundFilter(), GridSurfaceCreate(), MergeData() ] for alg in fusiontools: alg.group = "Fusion" self.algsList.extend(fusiontools)
def getCompatibleFileName(self, alg): '''Returns a filename that is compatible with the algorithm that is going to generate this output. If the algorithm supports the file format of the current output value, it returns that value. If not, it returns a temporary file with a supported file format, to be used to generate the output result.''' ext = self.value[self.value.rfind(".") + 1:] if ext in alg.provider.getSupportedOutputVectorLayerExtensions(): return self.value else: if self.compatible is None: self.compatible = ProcessingUtils.getTempFilenameInTempFolder(self.name + "." + self.getDefaultFileExtension(alg)) return self.compatible;
def __init__(self): AlgorithmProvider.__init__(self) self.activate = False self.algsList = [] if ProcessingUtils.isWindows(): lastools = [las2shp(), lasboundary(), las2dem(), las2iso(), lasgrid(), lasground(), lasinfo(), lasheight(), lasprecision(), lassplit(), lasclassify(), lasclip()] else: lastools = [lasinfo(), lasprecision()] for alg in lastools: alg.group = "LASTools" self.algsList.extend(lastools) if ProcessingUtils.isWindows(): self.actions.append(OpenViewerAction()) fusiontools = [CloudMetrics(), CanopyMaxima(), CanopyModel(), ClipData(), Cover(), FilterData(), GridMetrics(), GroundFilter(), GridSurfaceCreate(), MergeData()] for alg in fusiontools: alg.group = "Fusion" self.algsList.extend(fusiontools)
def getCompatibleFileName(self, alg): '''Returns a filename that is compatible with the algorithm that is going to generate this output. If the algorithm supports the file format of the current output value, it returns that value. If not, it returns a temporary file with a supported file format, to be used to generate the output result.''' ext = self.value[self.value.rfind(".") + 1:] if ext in alg.provider.getSupportedOutputTableExtensions(): return self.value else: if self.compatible is None: self.compatible = ProcessingUtils.getTempFilenameInTempFolder(self.name + "." + self.getDefaultFileExtension(alg)) return self.compatible;
def grassPath(): if not ProcessingUtils.isWindows() and not ProcessingUtils.isMac(): return "" folder = ProcessingConfig.getSetting(GrassUtils.GRASS_FOLDER) if folder == None: if ProcessingUtils.isWindows(): testfolder = os.path.dirname(str(QgsApplication.prefixPath())) testfolder = os.path.join(testfolder, "grass") if os.path.isdir(testfolder): for subfolder in os.listdir(testfolder): if subfolder.startswith("grass"): folder = os.path.join(testfolder, subfolder) break else: folder = os.path.join(str(QgsApplication.prefixPath()), "grass") if not os.path.isdir(folder): folder = "/Applications/GRASS-6.4.app/Contents/MacOS" return folder
def unload(self): AlgorithmProvider.unload(self) if ProcessingUtils.isWindows(): ProcessingConfig.removeSetting(SagaUtils.SAGA_FOLDER) ProcessingConfig.removeSetting(SagaUtils.SAGA_AUTO_RESAMPLING) ProcessingConfig.removeSetting(SagaUtils.SAGA_RESAMPLING_REGION_XMIN) ProcessingConfig.removeSetting(SagaUtils.SAGA_RESAMPLING_REGION_YMIN) ProcessingConfig.removeSetting(SagaUtils.SAGA_RESAMPLING_REGION_XMAX) ProcessingConfig.removeSetting(SagaUtils.SAGA_RESAMPLING_REGION_YMAX) ProcessingConfig.removeSetting(SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE) ProcessingConfig.removeSetting(SagaUtils.SAGA_LOG_CONSOLE) ProcessingConfig.removeSetting(SagaUtils.SAGA_LOG_COMMANDS)
def prepareGrassExecution(commands): if ProcessingUtils.isWindows(): GrassUtils.createGrassScript(commands) command = ["cmd.exe", "/C ", GrassUtils.grassScriptFilename()] else: gisrc = ProcessingUtils.userFolder() + os.sep + "processing.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 ProcessingUtils.isMac(): command = GrassUtils.grassPath( ) + os.sep + "grass.sh " + GrassUtils.grassMapsetFolder( ) + "/PERMANENT" else: command = "grass64 " + GrassUtils.grassMapsetFolder( ) + "/PERMANENT" return command
def checkRIsInstalled(ignoreRegistrySettings=False): if ProcessingUtils.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 ProcessingUtils.isWindows(): if ProcessingConfig.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/processing/3rdParty.html">Click here</a>' 'to know more about how to install and configure R to be used with QGIS</p>' ) return html
def processAlgorithm(self, progress): #TODO:check correct num of bands input = self.getParameterValue(SplitRGBBands.INPUT) temp = ProcessingUtils.getTempFilename(None).replace('.', '') basename = os.path.basename(temp) validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" safeBasename = ''.join(c for c in basename if c in validChars) temp = os.path.join(os.path.dirname(temp), safeBasename) r = self.getOutputValue(SplitRGBBands.R) g = self.getOutputValue(SplitRGBBands.G) b = self.getOutputValue(SplitRGBBands.B) commands = [] if ProcessingUtils.isWindows(): commands.append("io_gdal 0 -GRIDS \"" + temp + "\" -FILES \"" + input + "\"") commands.append("io_gdal 1 -GRIDS \"" + temp + "_0001.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + r + "\"") commands.append("io_gdal 1 -GRIDS \"" + temp + "_0002.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + g + "\"") commands.append("io_gdal 1 -GRIDS \"" + temp + "_0003.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + b + "\"") else: commands.append("libio_gdal 0 -GRIDS \"" + temp + "\" -FILES \"" + input + "\"") commands.append("libio_gdal 1 -GRIDS \"" + temp + "_0001.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + r + "\"") commands.append("libio_gdal 1 -GRIDS \"" + temp + "_0002.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + g + "\"") commands.append("libio_gdal 1 -GRIDS \"" + temp + "_0003.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + b + "\"") SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) SagaUtils.executeSaga(progress)
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if ProcessingUtils.isWindows(): ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_FOLDER, "SAGA folder", SagaUtils.sagaPath())) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_208, "Enable SAGA 2.0.8 compatibility", False)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_AUTO_RESAMPLING, "Use min covering grid system for resampling", True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_LOG_COMMANDS, "Log execution commands", True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_LOG_CONSOLE, "Log console output", True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMIN, "Resampling region min x", 0)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMIN, "Resampling region min y", 0)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMAX, "Resampling region max x", 1000)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMAX, "Resampling region max y", 1000)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE, "Resampling region cellsize", 1))
def taudemPath(): folder = ProcessingConfig.getSetting(TauDEMUtils.TAUDEM_FOLDER) if folder == None: folder = "" if ProcessingUtils.isMac(): testfolder = os.path.join(QgsApplication.prefixPath(), "bin") if os.path.exists(os.path.join(testfolder, "slopearea")): folder = testfolder else: testfolder = "/usr/local/bin" if os.path.exists(os.path.join(testfolder, "slopearea")): folder = testfolder return folder
def mpiexecPath(): folder = ProcessingConfig.getSetting(TauDEMUtils.MPIEXEC_FOLDER) if folder == None: folder = "" if ProcessingUtils.isMac(): testfolder = os.path.join(QgsApplication.prefixPath(), "bin") if os.path.exists(os.path.join(testfolder, "mpiexec")): folder = testfolder else: testfolder = "/usr/local/bin" if os.path.exists(os.path.join(testfolder, "mpiexec")): folder = testfolder return folder