Skip to content

Commit

Permalink
Did some R and GRASS testing and debugging
Browse files Browse the repository at this point in the history
Added the OTB folder with a skeleton of OTB alg provider 

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@70 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf@gmail.com committed Apr 11, 2012
1 parent b86bbe5 commit 3eba62c
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 26 deletions.
23 changes: 23 additions & 0 deletions src/otb/OTBAlgorithmProvider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sextante.core.AlgorithmProvider import AlgorithmProvider

class GdalAlgorithmProvider(AlgorithmProvider):

def __init__(self):
AlgorithmProvider.__init__(self)
self.createAlgsList()

def getName(self):
return "OTB"

def getIcon(self):
return QIcon(os.path.dirname(__file__) + "/icons/otb.png")

def _loadAlgorithms(self):
self.algs = self.preloadedAlgs

def createAlgsList(self):
self.preloadedAlgs = []

Empty file added src/otb/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions src/sextante/core/SextanteConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class SextanteConfig():
VECTOR_POLYGON_STYLE = "VECTOR_POLYGON_STYLE"
SHOW_RECENT_ALGORITHMS = "SHOW_RECENT_ALGORITHMS"
USE_SELECTED = "USE_SELECTED"
USE_FILENAME_AS_LAYER_NAME = "USE_FILENAME_AS_LAYER_NAME"

settings = {}

@staticmethod
def initialize():
SextanteConfig.addSetting(Setting("General", SextanteConfig.USE_SELECTED, "Use only selected features in external application", True))
SextanteConfig.addSetting(Setting("General", SextanteConfig.USE_FILENAME_AS_LAYER_NAME, "Use filename as layer name", True))
SextanteConfig.addSetting(Setting("General", SextanteConfig.SHOW_RECENT_ALGORITHMS, "Show recently executed algorithms", True))
SextanteConfig.addSetting(Setting("General", SextanteConfig.OUTPUT_FOLDER,
"Output folder", SextanteUtils.tempFolder()))
Expand Down
1 change: 1 addition & 0 deletions src/sextante/gdal/GdalUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def getSupportedRasters():
gdal.AllRegister()

GdalUtils.supportedRasters = {}
GdalUtils.supportedRasters["GTiff"] = ["tif"]
for i in range(gdal.GetDriverCount()):
driver = gdal.GetDriver(i)
if driver == None:
Expand Down
19 changes: 10 additions & 9 deletions src/sextante/grass/GrassAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,14 @@ def calculateRegion(self):
layer = param.value
else:
layer = QGisLayers.getObjectFromUri(param.value)
self.addToResamplingExtent(layer, first)
self.addToRegion(layer, first)
first = False
elif isinstance(param, ParameterMultipleInput):
if param.datatype == ParameterMultipleInput.TYPE_RASTER:
layers = param.value.split(";")
for layername in layers:
layer = QGisLayers.getObjectFromUri(layername, first)
self.addToResamplingExtent(layer, first)
first = False
layers = param.value.split(";")
for layername in layers:
layer = QGisLayers.getObjectFromUri(layername, first)
self.addToRegion(layer, first)
first = False
else:
self.xmin = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_XMIN)
self.xmax = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_XMAX)
Expand All @@ -93,11 +92,13 @@ def calculateRegion(self):

def addToRegion(self, layer, first):
if first:
self.cellsize = 0
self.xmin = layer.extent().xMinimum()
self.xmax = layer.extent().xMaximum()
self.ymin = layer.extent().yMinimum()
self.ymax = layer.extent().yMaximum()
self.cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum())/layer.width()
if isinstance(layer, QgsRasterLayer):
self.cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum())/layer.width()
else:
self.xmin = min(self.xmin, layer.extent().xMinimum())
self.xmax = max(self.xmax, layer.extent().xMaximum())
Expand Down Expand Up @@ -246,7 +247,7 @@ def exportRasterLayer(self, layer):
self.exportedLayers[layer]= destFilename
command = "r.in.gdal"
command +=" input=\"" + layer + "\""
command +=" band=0"
command +=" band=1"
command +=" out=" + destFilename;
command +=" --overwrite -o"
return command
Expand Down
22 changes: 11 additions & 11 deletions src/sextante/grass/GrassUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def createGrassScript(commands):

#temporary gisrc file
output = open(gisrc, "w")
mapset = "temp_mapset"
location = "temp_location"
mapset = "user"
gisdbase = os.path.join(os.path.expanduser("~"), "sextante", "tempdata", "grassdata")
output.write("GISDBASE: " + gisdbase + "\n");
output.write("LOCATION_NAME: " + location + "\n");
Expand All @@ -83,11 +83,10 @@ def createGrassScript(commands):
output.close();

output=open(script, "w")
shToolsPath = os.path.basename(shell)
output.write("set HOME=" + os.path.expanduser("~") + "\n");
output.write("set GISRC=" + gisrc + "\n")
output.write("set GRASS_SH=" + shell + "\n")
output.write("set PATH=" + shToolsPath + os.sep + "bin;" + shToolsPath + os.sep + "lib;" + "%PATH%\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")
Expand All @@ -104,9 +103,9 @@ def createGrassScript(commands):
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=" + mapset + "\"\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=\"MAPSET= " + mapset + "\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:
Expand All @@ -125,7 +124,7 @@ def createGrassBatchJobFileFromGrassCommands(commands):

@staticmethod
def grassMapsetFolder():
tempfolder = os.path.join(os.path.expanduser("~"), "sextante", "tempdata", "grassdata", "temp_location", "temp_mapset")
tempfolder = os.path.join(os.path.expanduser("~"), "sextante", "tempdata", "grassdata", "temp_location")
mkdir(tempfolder)
return tempfolder

Expand All @@ -141,7 +140,7 @@ def createTempMapset():
mkdir(os.path.join(folder, "PERMANENT"))
mkdir(os.path.join(folder, "user"))
mkdir(os.path.join(folder, "PERMANENT", ".tmp"))
mkdir(os.path.join(folder, "PERMANENT", "DEFAULT_WIND"))
GrassUtils.writeGrassWindow(os.path.join(folder, "PERMANENT", "DEFAULT_WIND"));
outfile = open(os.path.join(folder, "PERMANENT", "MYNAME"), "w")
if not latlon:
outfile.write("SEXTANTE GRASS interface: temporary x/y data processing location.\n");
Expand Down Expand Up @@ -229,10 +228,11 @@ def executeGrass(commands, progress):
for line in iter(proc.readline, ""):
if "GRASS_INFO_PERCENT" in line:
try:
progress.setPercentage(line[line.rfind(":") + 2:])
progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:]))
except:
pass
loglines.append(line)
else:
loglines.append(line)
SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
shutil.rmtree(GrassUtils.grassMapsetFolder(), True)

Expand Down
8 changes: 7 additions & 1 deletion src/sextante/gui/SextantePostprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from sextante.gui.RenderingStyles import RenderingStyles
from sextante.outputs.OutputHTML import OutputHTML
from PyQt4.QtGui import *
from sextante.core.SextanteConfig import SextanteConfig
import os
class SextantePostprocessing:

@staticmethod
Expand All @@ -17,7 +19,11 @@ def handleAlgorithmResults(alg):
continue
if isinstance(out, (OutputRaster, OutputVector, OutputTable)):
try:
QGisLayers.load(out.value, out.description, alg.crs, RenderingStyles.getStyle(alg.commandLineName(),out.name))
if SextanteConfig.getSetting(SextanteConfig.USE_FILENAME_AS_LAYER_NAME):
name = os.path.basename(out.value)
else:
name = out.description
QGisLayers.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(),out.name))
except Exception, e:
QMessageBox.critical(None, "Error", str(e))
elif isinstance(out, OutputHTML):
Expand Down
4 changes: 2 additions & 2 deletions src/sextante/r/RAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, descriptionfile):
self.defineCharacteristicsFromFile()

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/script.png")
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/r.png")

def defineCharacteristicsFromFile(self):
self.script = ""
Expand Down Expand Up @@ -212,7 +212,7 @@ def getImportCommands(self):
filename = os.path.basename(value)
filename = filename[:-4]
commands.append(param.name + " = readOGR(\"" + value + "\",layer=\"" + filename + "\")")
if isinstance(param, ParameterVector):
if isinstance(param, ParameterTable):
value = param.value
if not value.lower().endswith("csv"):
raise GeoAlgorithmExecutionException("Unsupported input file format.\n" + value)
Expand Down
6 changes: 3 additions & 3 deletions src/sextante/r/RUtils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from PyQt4.QtGui import *
from sextante.core.SextanteConfig import SextanteConfig
import os
from sextante.core.SextanteUtils import mkdir, SextanteUtils
Expand Down Expand Up @@ -69,8 +70,8 @@ def createConsoleOutput():
RUtils.allConsoleResults = []
add = False
lines = open(RUtils.getConsoleOutputFilename())
line = lines.readline().strip("\n").strip(" ")
while line != "":
for line in lines:
line = line.strip("\n").strip(" ")
if line.startswith(">"):
line = line[1:].strip(" ")
if line in RUtils.verboseCommands:
Expand All @@ -80,7 +81,6 @@ def createConsoleOutput():
elif add:
RUtils.consoleResults.append("<p>" + line + "</p>\n");
RUtils.allConsoleResults.append(line);
line = lines.readline().strip("\n").strip(" ")


@staticmethod
Expand Down

0 comments on commit 3eba62c

Please sign in to comment.