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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 sagaBatchJobFilename(): if ProcessingUtils.isWindows(): filename = "saga_batch_job.bat" else: filename = "saga_batch_job.sh" batchfile = ProcessingUtils.userFolder() + os.sep + filename return batchfile
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 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 __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 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 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 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 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 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 processAlgorithm(self, progress): arguments = [] arguments.append("-n") arguments.append(str(self.getParameterValue(rgb2pct.NCOLORS))) arguments.append("-of") out = self.getOutputValue(rgb2pct.OUTPUT) arguments.append(GdalUtils.getFormatShortNameFromFilename(out)) arguments.append(self.getParameterValue(rgb2pct.INPUT)) arguments.append(out) if ProcessingUtils.isWindows(): commands = ["cmd.exe", "/C ", "rgb2pct.bat", GdalUtils.escapeAndJoin(arguments)] else: commands = ["rgb2pct.py", GdalUtils.escapeAndJoin(arguments)] GdalUtils.runGdal(commands, progress)
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 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 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 processAlgorithm(self, progress): arguments = [] arguments.append(self.getParameterValue(polygonize.INPUT)) arguments.append('-f') arguments.append('"ESRI Shapefile"') output = self.getOutputValue(polygonize.OUTPUT) arguments.append(output) arguments.append(QtCore.QFileInfo(output).baseName()) arguments.append(self.getParameterValue(polygonize.FIELD)) commands = [] if ProcessingUtils.isWindows(): commands = ["cmd.exe", "/C ", "gdal_polygonize.bat", GdalUtils.escapeAndJoin(arguments)] else: commands = ["gdal_polygonize.py", GdalUtils.escapeAndJoin(arguments)] GdalUtils.runGdal(commands, progress)
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 processAlgorithm(self, progress): arguments = [] arguments.append("-n") arguments.append(str(self.getParameterValue(rgb2pct.NCOLORS))) arguments.append("-of") out = self.getOutputValue(rgb2pct.OUTPUT) arguments.append(GdalUtils.getFormatShortNameFromFilename(out)) arguments.append(self.getParameterValue(rgb2pct.INPUT)) arguments.append(out) if ProcessingUtils.isWindows(): commands = [ "cmd.exe", "/C ", "rgb2pct.bat", GdalUtils.escapeAndJoin(arguments) ] else: commands = ["rgb2pct.py", GdalUtils.escapeAndJoin(arguments)] GdalUtils.runGdal(commands, progress)
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 processAlgorithm(self, progress): arguments = [] if self.getParameterValue(merge.SEPARATE): arguments.append("-separate") if self.getParameterValue(merge.PCT): arguments.append("-pct") arguments.append("-o") out = self.getOutputValue(merge.OUTPUT) arguments.append(out) arguments.append("-of") arguments.append(GdalUtils.getFormatShortNameFromFilename(out)) arguments.extend(self.getParameterValue(merge.INPUT).split(";")) commands = [] if ProcessingUtils.isWindows(): commands = ["cmd.exe", "/C ", "gdal_merge.bat", GdalUtils.escapeAndJoin(arguments)] else: commands = ["gdal_merge.py", GdalUtils.escapeAndJoin(arguments)] GdalUtils.runGdal(commands, progress)
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 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 processAlgorithm(self, progress): if ProcessingUtils.isWindows(): path = RUtils.RFolder() if path == "": raise GeoAlgorithmExecutionException("R folder is not configured.\nPlease configure it before running R scripts.") loglines = [] loglines.append("R execution commands") loglines += self.getFullSetOfRCommands() for line in loglines: progress.setCommand(line) ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) RUtils.executeRAlgorithm(self, progress) if self.showPlots: htmlfilename = self.getOutputValue(RAlgorithm.RPLOTS) f = open(htmlfilename, "w") f.write("<img src=\"" + self.plotsFilename + "\"/>") f.close() if self.showConsoleOutput: htmlfilename = self.getOutputValue(RAlgorithm.R_CONSOLE_OUTPUT) f = open(htmlfilename, "w") f.write(RUtils.getConsoleOutput()) f.close()
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 processAlgorithm(self, progress): arguments = [] arguments.append(self.getParameterValue(polygonize.INPUT)) arguments.append('-f') arguments.append('"ESRI Shapefile"') output = self.getOutputValue(polygonize.OUTPUT) arguments.append(output) arguments.append(QtCore.QFileInfo(output).baseName()) arguments.append(self.getParameterValue(polygonize.FIELD)) commands = [] if ProcessingUtils.isWindows(): commands = [ "cmd.exe", "/C ", "gdal_polygonize.bat", GdalUtils.escapeAndJoin(arguments) ] else: commands = [ "gdal_polygonize.py", GdalUtils.escapeAndJoin(arguments) ] GdalUtils.runGdal(commands, progress)
def processAlgorithm(self, progress): if ProcessingUtils.isWindows(): path = RUtils.RFolder() if path == "": raise GeoAlgorithmExecutionException( "R folder is not configured.\nPlease configure it before running R scripts." ) loglines = [] loglines.append("R execution commands") loglines += self.getFullSetOfRCommands() for line in loglines: progress.setCommand(line) ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) RUtils.executeRAlgorithm(self, progress) if self.showPlots: htmlfilename = self.getOutputValue(RAlgorithm.RPLOTS) f = open(htmlfilename, "w") f.write("<img src=\"" + self.plotsFilename + "\"/>") f.close() if self.showConsoleOutput: htmlfilename = self.getOutputValue(RAlgorithm.R_CONSOLE_OUTPUT) f = open(htmlfilename, "w") f.write(RUtils.getConsoleOutput()) f.close()
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 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 getValue(self): s = unicode(self.text.text()) if ProcessingUtils.isWindows(): s = s.replace("\\", "/") return s
def processAlgorithm(self, progress): if ProcessingUtils.isWindows(): path = SagaUtils.sagaPath() if path == "": raise GeoAlgorithmExecutionException( "SAGA folder is not configured.\nPlease configure it before running SAGA algorithms." ) commands = list() self.exportedLayers = {} self.preProcessInputs() #1: Export rasters to sgrd and vectors to shp # Tables must be in dbf format. We check that. if self.resample: self.calculateResamplingExtent() for param in self.parameters: if isinstance(param, ParameterRaster): if param.value == None: continue value = param.value if not value.endswith("sgrd"): commands.append(self.exportRasterLayer(value)) if self.resample: commands.append(self.resampleRasterLayer(value)) if isinstance(param, ParameterVector): if param.value == None: continue layer = QGisLayers.getObjectFromUri(param.value, False) if layer: filename = LayerExporter.exportVectorLayer(layer) self.exportedLayers[param.value] = filename elif not param.value.endswith("shp"): raise GeoAlgorithmExecutionException( "Unsupported file format") if isinstance(param, ParameterTable): if param.value == None: continue table = QGisLayers.getObjectFromUri(param.value, False) if table: filename = LayerExporter.exportTable(table) self.exportedLayers[param.value] = filename elif not param.value.endswith("shp"): raise GeoAlgorithmExecutionException( "Unsupported file format") if isinstance(param, ParameterMultipleInput): if param.value == None: continue layers = param.value.split(";") if layers == None or len(layers) == 0: continue if param.datatype == ParameterMultipleInput.TYPE_RASTER: for layerfile in layers: if not layerfile.endswith("sgrd"): commands.append(self.exportRasterLayer(layerfile)) if self.resample: commands.append( self.resampleRasterLayer(layerfile)) elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layerfile in layers: layer = QGisLayers.getObjectFromUri(layerfile, False) if layer: filename = LayerExporter.exportVectorLayer(layer) self.exportedLayers[layerfile] = filename elif (not layerfile.endswith("shp")): raise GeoAlgorithmExecutionException( "Unsupported file format") #2: set parameters and outputs saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if ProcessingUtils.isWindows() or ProcessingUtils.isMac( ) or not saga208: command = self.undecoratedGroup + " \"" + self.cmdname + "\"" else: command = "lib" + self.undecoratedGroup + " \"" + self.cmdname + "\"" if self.hardcodedStrings: for s in self.hardcodedStrings: command += " " + s for param in self.parameters: if param.value is None: continue if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable)): value = param.value if value in self.exportedLayers.keys(): command += (" -" + param.name + " \"" + self.exportedLayers[value] + "\"") else: command += (" -" + param.name + " \"" + value + "\"") elif isinstance(param, ParameterMultipleInput): s = param.value for layer in self.exportedLayers.keys(): s = s.replace(layer, self.exportedLayers[layer]) command += (" -" + param.name + " \"" + s + "\"") elif isinstance(param, ParameterBoolean): if param.value: command += (" -" + param.name) elif isinstance(param, ParameterFixedTable): tempTableFile = ProcessingUtils.getTempFilename("txt") f = open(tempTableFile, "w") f.write('\t'.join([col for col in param.cols]) + "\n") values = param.value.split(",") for i in range(0, len(values), 3): s = values[i] + "\t" + values[i + 1] + "\t" + values[i + 2] + "\n" f.write(s) f.close() command += (" -" + param.name + " \"" + tempTableFile + "\"") elif isinstance(param, ParameterExtent): #'we have to substract/add half cell size, since saga is center based, not corner based halfcell = self.getOutputCellsize() / 2 offset = [halfcell, -halfcell, halfcell, -halfcell] values = param.value.split(",") for i in range(4): command += (" -" + self.extentParamNames[i] + " " + str(float(values[i]) + offset[i])) elif isinstance(param, (ParameterNumber, ParameterSelection)): command += (" -" + param.name + " " + str(param.value)) else: command += (" -" + param.name + " \"" + str(param.value) + "\"") for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename = ProcessingUtils.tempFolder( ) + os.sep + os.path.basename(filename) + ".sgrd" command += (" -" + out.name + " \"" + filename + "\"") if isinstance(out, OutputVector): filename = out.getCompatibleFileName(self) command += (" -" + out.name + " \"" + filename + "\"") if isinstance(out, OutputTable): filename = out.getCompatibleFileName(self) command += (" -" + out.name + " \"" + filename + "\"") commands.append(command) #3:Export resulting raster layers for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename2 = ProcessingUtils.tempFolder( ) + os.sep + os.path.basename(filename) + ".sgrd" formatIndex = 1 if saga208 else 4 if ProcessingUtils.isWindows() or ProcessingUtils.isMac( ) or not saga208: commands.append("io_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT " + str(formatIndex) + " -TYPE 0 -FILE \"" + filename + "\"") else: commands.append("libio_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\"") #4 Run SAGA commands = self.editCommands(commands) SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) loglines = [] loglines.append("SAGA execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) SagaUtils.executeSaga(progress)
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) ProcessingConfig.addSetting(Setting(self.getDescription(), RUtils.RSCRIPTS_FOLDER, "R Scripts folder", RUtils.RScriptsFolder())) if ProcessingUtils.isWindows(): ProcessingConfig.addSetting(Setting(self.getDescription(), RUtils.R_FOLDER, "R folder", RUtils.RFolder())) ProcessingConfig.addSetting(Setting(self.getDescription(), RUtils.R_USE64, "Use 64 bit version", False))
def unload(self): AlgorithmProvider.unload(self) ProcessingConfig.removeSetting(RUtils.RSCRIPTS_FOLDER) if ProcessingUtils.isWindows(): ProcessingConfig.removeSetting(RUtils.R_FOLDER) ProcessingConfig.removeSetting(RUtils.R_USE64)
def processAlgorithm(self, progress): if ProcessingUtils.isWindows(): path = GrassUtils.grassPath() if path == "": raise GeoAlgorithmExecutionException( "GRASS folder is not configured.\nPlease configure it before running GRASS algorithms." ) commands = [] self.exportedLayers = {} outputCommands = [] # if GRASS session has been created outside of this algorithm then get the list of layers loaded in GRASS # otherwise start a new session existingSession = GrassUtils.sessionRunning if existingSession: self.exportedLayers = GrassUtils.getSessionLayers() else: GrassUtils.startGrassSession() # 1: Export layer to grass mapset for param in self.parameters: if isinstance(param, ParameterRaster): if param.value == None: continue value = param.value # check if the layer hasn't already been exported in, for example, previous GRASS calls in this session if value in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(value, commands) commands.append(self.exportRasterLayer(value)) if isinstance(param, ParameterVector): if param.value == None: continue value = param.value if value in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(value, commands) commands.append(self.exportVectorLayer(value)) if isinstance(param, ParameterTable): pass if isinstance(param, ParameterMultipleInput): if param.value == None: continue layers = param.value.split(";") if layers == None or len(layers) == 0: continue if param.datatype == ParameterMultipleInput.TYPE_RASTER: for layer in layers: if layer in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(layer, commands) commands.append(self.exportRasterLayer(layer)) elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layer in layers: if layer in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(layer, commands) commands.append(self.exportVectorLayer(layer)) self.setSessionProjectionFromProject(commands) region = str(self.getParameterValue(self.GRASS_REGION_EXTENT_PARAMETER)) regionCoords = region.split(",") command = "g.region" command += " n=" + str(regionCoords[3]) command += " s=" + str(regionCoords[2]) command += " e=" + str(regionCoords[1]) command += " w=" + str(regionCoords[0]) cellsize = self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER) if cellsize: command += " res=" + str(cellsize) else: command += " res=" + str(self.getDefaultCellsize()) alignToResolution = self.getParameterValue(self.GRASS_REGION_ALIGN_TO_RESOLUTION) if alignToResolution: command += " -a" commands.append(command) # 2: set parameters and outputs command = self.grassName for param in self.parameters: if param.value == None or param.value == "": continue if ( param.name == self.GRASS_REGION_CELLSIZE_PARAMETER or param.name == self.GRASS_REGION_EXTENT_PARAMETER or param.name == self.GRASS_MIN_AREA_PARAMETER or param.name == self.GRASS_SNAP_TOLERANCE_PARAMETER or param.name == self.GRASS_OUTPUT_TYPE_PARAMETER or param.name == self.GRASS_REGION_ALIGN_TO_RESOLUTION ): continue if isinstance(param, (ParameterRaster, ParameterVector)): value = param.value if value in self.exportedLayers.keys(): command += " " + param.name + "=" + self.exportedLayers[value] else: command += " " + param.name + "=" + value elif isinstance(param, ParameterMultipleInput): s = param.value for layer in self.exportedLayers.keys(): s = s.replace(layer, self.exportedLayers[layer]) s = s.replace(";", ",") command += " " + param.name + "=" + s elif isinstance(param, ParameterBoolean): if param.value: command += " " + param.name elif isinstance(param, ParameterSelection): idx = int(param.value) command += " " + param.name + "=" + str(param.options[idx]) elif isinstance(param, ParameterString): command += " " + param.name + '="' + str(param.value) + '"' else: command += " " + param.name + "=" + str(param.value) uniqueSufix = str(uuid.uuid4()).replace("-", "") for out in self.outputs: if isinstance(out, OutputFile): if out.name == "outputtext": # the 'outputtext' file is generated by piping output from GRASS, is not an actual grass command command += " > " + out.value else: command += " " + out.name + '="' + out.value + '"' elif not isinstance(out, OutputHTML): # html files are not generated by GRASS, only by us to decorate GRASS output, so we skip them # An output name to make sure it is unique if the session uses this algorithm several times uniqueOutputName = out.name + uniqueSufix command += " " + out.name + "=" + uniqueOutputName # add output file to exported layers, to indicate that they are present in GRASS self.exportedLayers[out.value] = uniqueOutputName command += " --overwrite" commands.append(command) # 3:Export resulting layers to a format that qgis can read for out in self.outputs: if isinstance(out, OutputRaster): filename = out.value # Raster layer output: adjust region to layer before exporting commands.append("g.region rast=" + out.name + uniqueSufix) outputCommands.append("g.region rast=" + out.name + uniqueSufix) command = 'r.out.gdal -c createopt="TFW=YES,COMPRESS=LZW"' command += " input=" command += out.name + uniqueSufix command += ' output="' + filename + '"' commands.append(command) outputCommands.append(command) if isinstance(out, OutputVector): filename = out.value command = "v.out.ogr -e input=" + out.name + uniqueSufix command += ' dsn="' + os.path.dirname(out.value) + '"' command += " format=ESRI_Shapefile" command += " olayer=" + os.path.basename(out.value)[:-4] typeidx = self.getParameterValue(self.GRASS_OUTPUT_TYPE_PARAMETER) outtype = "auto" if typeidx is None else self.OUTPUT_TYPES[typeidx] command += " type=" + outtype commands.append(command) outputCommands.append(command) # 4 Run GRASS loglines = [] loglines.append("GRASS execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if ProcessingConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) self.consoleOutput = GrassUtils.executeGrass(commands, progress, outputCommands) self.postProcessResults() # if the session has been created outside of this algorithm, add the new GRASS layers to it # otherwise finish the session if existingSession: GrassUtils.addSessionLayers(self.exportedLayers) else: GrassUtils.endGrassSession()
def processAlgorithm(self, progress): if ProcessingUtils.isWindows(): path = GrassUtils.grassPath() if path == "": raise GeoAlgorithmExecutionException("GRASS folder is not configured.\nPlease configure it before running GRASS algorithms.") commands = [] self.exportedLayers = {} outputCommands = [] # if GRASS session has been created outside of this algorithm then get the list of layers loaded in GRASS # otherwise start a new session existingSession = GrassUtils.sessionRunning if existingSession: self.exportedLayers = GrassUtils.getSessionLayers() else: GrassUtils.startGrassSession() #1: Export layer to grass mapset for param in self.parameters: if isinstance(param, ParameterRaster): if param.value == None: continue value = param.value # check if the layer hasn't already been exported in, for example, previous GRASS calls in this session if value in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(value, commands) commands.append(self.exportRasterLayer(value)) if isinstance(param, ParameterVector): if param.value == None: continue value = param.value if value in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(value, commands) commands.append(self.exportVectorLayer(value)) if isinstance(param, ParameterTable): pass if isinstance(param, ParameterMultipleInput): if param.value == None: continue layers = param.value.split(";") if layers == None or len(layers) == 0: continue if param.datatype == ParameterMultipleInput.TYPE_RASTER: for layer in layers: if layer in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(layer, commands) commands.append(self.exportRasterLayer(layer)) elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layer in layers: if layer in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(layer, commands) commands.append(self.exportVectorLayer(layer)) self.setSessionProjectionFromProject(commands) region = str(self.getParameterValue(self.GRASS_REGION_EXTENT_PARAMETER)) regionCoords = region.split(",") command = "g.region" command += " n=" + str(regionCoords[3]) command +=" s=" + str(regionCoords[2]) command +=" e=" + str(regionCoords[1]) command +=" w=" + str(regionCoords[0]) cellsize = self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER) if cellsize: command +=" res=" + str(cellsize); else: command +=" res=" + str(self.getDefaultCellsize()) alignToResolution = self.getParameterValue(self.GRASS_REGION_ALIGN_TO_RESOLUTION) if alignToResolution: command +=" -a" commands.append(command) #2: set parameters and outputs command = self.grassName for param in self.parameters: if param.value == None or param.value == "": continue if (param.name == self.GRASS_REGION_CELLSIZE_PARAMETER or param.name == self.GRASS_REGION_EXTENT_PARAMETER or param.name == self.GRASS_MIN_AREA_PARAMETER or param.name == self.GRASS_SNAP_TOLERANCE_PARAMETER or param.name == self.GRASS_OUTPUT_TYPE_PARAMETER or param.name == self.GRASS_REGION_ALIGN_TO_RESOLUTION): continue if isinstance(param, (ParameterRaster, ParameterVector)): value = param.value if value in self.exportedLayers.keys(): command+=(" " + param.name + "=" + self.exportedLayers[value]) else: command+=(" " + param.name + "=" + value) elif isinstance(param, ParameterMultipleInput): s = param.value for layer in self.exportedLayers.keys(): s = s.replace(layer, self.exportedLayers[layer]) s = s.replace(";",",") command+=(" " + param.name + "=" + s); elif isinstance(param, ParameterBoolean): if param.value: command += (" " + param.name) elif isinstance(param, ParameterSelection): idx = int(param.value) command+=(" " + param.name + "=" + str(param.options[idx])); elif isinstance(param, ParameterString): command+=(" " + param.name + "=\"" + str(param.value) + "\""); else: command+=(" " + param.name + "=" + str(param.value)); uniqueSufix = str(uuid.uuid4()).replace("-",""); for out in self.outputs: if isinstance(out, OutputFile): if out.name == 'outputtext': #the 'outputtext' file is generated by piping output from GRASS, is not an actual grass command command+= (" > " + out.value) else: command+=(" " + out.name + "=\"" + out.value + "\""); elif not isinstance(out, OutputHTML): #html files are not generated by GRASS, only by us to decorate GRASS output, so we skip them #An output name to make sure it is unique if the session uses this algorithm several times uniqueOutputName = out.name + uniqueSufix command += (" " + out.name + "=" + uniqueOutputName) # add output file to exported layers, to indicate that they are present in GRASS self.exportedLayers[out.value]= uniqueOutputName command += " --overwrite" commands.append(command) #3:Export resulting layers to a format that qgis can read for out in self.outputs: if isinstance(out, OutputRaster): filename = out.value #Raster layer output: adjust region to layer before exporting commands.append("g.region rast=" + out.name + uniqueSufix) outputCommands.append("g.region rast=" + out.name + uniqueSufix) command = "r.out.gdal -c createopt=\"TFW=YES,COMPRESS=LZW\"" command += " input=" command += out.name + uniqueSufix command += " output=\"" + filename + "\"" commands.append(command) outputCommands.append(command) if isinstance(out, OutputVector): filename = out.value command = "v.out.ogr -e input=" + out.name + uniqueSufix command += " dsn=\"" + os.path.dirname(out.value) + "\"" command += " format=ESRI_Shapefile" command += " olayer=" + os.path.basename(out.value)[:-4] typeidx = self.getParameterValue(self.GRASS_OUTPUT_TYPE_PARAMETER); outtype = "auto" if typeidx is None else self.OUTPUT_TYPES[typeidx] command += " type=" + outtype commands.append(command) outputCommands.append(command) #4 Run GRASS loglines = [] loglines.append("GRASS execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if ProcessingConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) self.consoleOutput = GrassUtils.executeGrass(commands, progress, outputCommands); self.postProcessResults(); # if the session has been created outside of this algorithm, add the new GRASS layers to it # otherwise finish the session if existingSession: GrassUtils.addSessionLayers(self.exportedLayers) else: GrassUtils.endGrassSession()