Skip to content

Commit

Permalink
made SEXTANTE command line usage more pythonic
Browse files Browse the repository at this point in the history
Fixed problem with temporary grass mapset created by SEXTANTE (wrong path)
  • Loading branch information
volaya committed Nov 19, 2012
1 parent 3398341 commit 6173fa3
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 73 deletions.
2 changes: 2 additions & 0 deletions python/plugins/sextante/__init__.py
Expand Up @@ -17,6 +17,8 @@
***************************************************************************
"""

from sextante.core.Sextante import runalg, alghelp, alglist, algoptions, load, loadFromAlg, extent, getObjectFromName, getObjectFromUri

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/core/GeoAlgorithm.py
Expand Up @@ -264,7 +264,7 @@ def getOutputValue(self, name):
def getAsCommand(self):
'''Returns the command that would run this same algorithm from the console.
Should return null if the algorithm cannot be run from the console.'''
s="Sextante.runalg(\"" + self.commandLineName() + "\","
s="sextante.runalg(\"" + self.commandLineName() + "\","
for param in self.parameters:
s+=param.getValueAsCommandLineParameter() + ","
for out in self.outputs:
Expand Down
139 changes: 80 additions & 59 deletions python/plugins/sextante/core/Sextante.py
Expand Up @@ -24,7 +24,7 @@

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from sextante.core.QGisLayers import QGisLayers
from sextante.core.SextanteConfig import SextanteConfig
from sextante.core.GeoAlgorithm import GeoAlgorithm
Expand Down Expand Up @@ -238,45 +238,14 @@ def getAlgorithm(name):
return provider[name]
return None


##This methods are here to be used from the python console,
##making it easy to use SEXTANTE from there
##==========================================================

@staticmethod
def alglist(text=None):
s=""
for provider in Sextante.algs.values():
sortedlist = sorted(provider.values(), key= lambda alg: alg.name)
for alg in sortedlist:
if text == None or text.lower() in alg.name.lower():
s+=(alg.name.ljust(50, "-") + "--->" + alg.commandLineName() + "\n")
print s


@staticmethod
def algoptions(name):
alg = Sextante.getAlgorithm(name)
if alg != None:
s =""
for param in alg.parameters:
if isinstance(param, ParameterSelection):
s+=param.name + "(" + param.description + ")\n"
i=0
for option in param.options:
s+= "\t" + str(i) + " - " + str(option) + "\n"
i+=1
print(s)
else:
print "Algorithm not found"
def getObject(uri):
'''Returns the QGIS object identified by the given URI'''
return QGisLayers.getObjectFromUri(uri)

@staticmethod
def alghelp(name):
alg = Sextante.getAlgorithm(name)
if alg != None:
print(str(alg))
else:
print "Algorithm not found"
def runandload(name, *args):
Sextante.runAlgorithm(name, SextantePostprocessing.handleAlgorithmResults, *args)

@staticmethod
def runAlgorithm(algOrName, onFinish, *args):
Expand Down Expand Up @@ -359,28 +328,80 @@ def cancel():
QApplication.restoreOverrideCursor()
return alg

@staticmethod
def runalg(algOrName, *args):
alg = Sextante.runAlgorithm(algOrName, None, *args)
return alg.getOutputValuesAsDictionary()


@staticmethod
def load(layer):
'''Loads a layer into QGIS'''
QGisLayers.load(layer)
##==========================================================
##This methods are here to be used from the python console,
##making it easy to use SEXTANTE from there
##==========================================================

@staticmethod
def loadFromAlg(layersdict):
'''Load all layer resulting from a given algorithm.
Layers are passed as a dictionary, obtained from alg.getOutputValuesAsDictionary()'''
QGisLayers.loadFromDict(layersdict)

@staticmethod
def getObject(uri):
'''Returns the QGIS object identified by the given URI'''
return QGisLayers.getObjectFromUri(uri)

@staticmethod
def runandload(name, *args):
Sextante.runAlgorithm(name, SextantePostprocessing.handleAlgorithmResults, *args)
def alglist(text=None):
s=""
for provider in Sextante.algs.values():
sortedlist = sorted(provider.values(), key= lambda alg: alg.name)
for alg in sortedlist:
if text == None or text.lower() in alg.name.lower():
s+=(alg.name.ljust(50, "-") + "--->" + alg.commandLineName() + "\n")
print s

def algoptions(name):
alg = Sextante.getAlgorithm(name)
if alg != None:
s =""
for param in alg.parameters:
if isinstance(param, ParameterSelection):
s+=param.name + "(" + param.description + ")\n"
i=0
for option in param.options:
s+= "\t" + str(i) + " - " + str(option) + "\n"
i+=1
print(s)
else:
print "Algorithm not found"

def alghelp(name):
alg = Sextante.getAlgorithm(name)
if alg != None:
print(str(alg))
else:
print "Algorithm not found"

def runalg(algOrName, *args):
alg = Sextante.runAlgorithm(algOrName, None, *args)
return alg.getOutputValuesAsDictionary()

def extent(layers):
first = True
for layer in layers:
if not isinstance(layer, (QgsRasterLayer, QgsVectorLayer)):
layer = QGisLayers.getObjectFromUri(layer)
if first:
xmin = layer.extent().xMinimum()
xmax = layer.extent().xMaximum()
ymin = layer.extent().yMinimum()
ymax = layer.extent().yMaximum()
else:
xmin = min(xmin, layer.extent().xMinimum())
xmax = max(xmax, layer.extent().xMaximum())
ymin = min(ymin, layer.extent().yMinimum())
ymax = max(ymax, layer.extent().yMaximum())
first = False
return str(xmin) + "," + str(xmax) + "," + str(ymin) + "," + str(ymax)

def getObjectFromName(name):
layers = QGisLayers.getAllLayers()
for layer in layers:
if layer.name() == name:
return layer

def getObjectFromUri(uri):
return QGisLayers.getObjectFromUri(uri, False)

def load(layer):
'''Loads a layer into QGIS'''
QGisLayers.load(layer)

def loadFromAlg(layersdict):
'''Load all layer resulting from a given algorithm.
Layers are passed as a dictionary, obtained from alg.getOutputValuesAsDictionary()'''
QGisLayers.loadFromDict(layersdict)
18 changes: 13 additions & 5 deletions python/plugins/sextante/grass/GrassUtils.py
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from PyQt4 import QtGui

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand Down Expand Up @@ -129,7 +130,8 @@ def createGrassScript(commands):
output = open(gisrc, "w")
location = "temp_location"
mapset = "user"
gisdbase = os.path.join(os.path.expanduser("~"), "sextante", "tempdata", "grassdata")
gisdbase = GrassUtils.grassDataFolder()
#gisdbase = os.path.join(os.path.expanduser("~"), "sextante", "tempdata", "grassdata")
output.write("GISDBASE: " + gisdbase + "\n");
output.write("LOCATION_NAME: " + location + "\n");
output.write("MAPSET: " + mapset + "\n");
Expand Down Expand Up @@ -178,7 +180,13 @@ def createGrassBatchJobFileFromGrassCommands(commands):

@staticmethod
def grassMapsetFolder():
tempfolder = os.path.join(SextanteUtils.tempFolder(), "grassdata", "temp_location")
folder = os.path.join(GrassUtils.grassDataFolder(), "temp_location")
mkdir(folder)
return folder

@staticmethod
def grassDataFolder():
tempfolder = os.path.join(SextanteUtils.tempFolder(), "grassdata")
mkdir(tempfolder)
return tempfolder

Expand All @@ -190,7 +198,7 @@ def createTempMapset():
structure and content will vary slightly depending on whether the user wants to process lat/lon or x/y data.'''

latlon = SextanteConfig.getSetting(GrassUtils.GRASS_LATLON)
folder = GrassUtils.grassMapsetFolder()
folder = GrassUtils.grassMapsetFolder()
mkdir(os.path.join(folder, "PERMANENT"))
mkdir(os.path.join(folder, "user"))
mkdir(os.path.join(folder, "PERMANENT", ".tmp"))
Expand Down Expand Up @@ -309,15 +317,15 @@ def getGrassVersion():
# of the previous ones.
# Starting a session just involves creating the temp mapset structure
@staticmethod
def startGrassSession():
def startGrassSession():
if not GrassUtils.sessionRunning:
GrassUtils.createTempMapset()
GrassUtils.sessionRunning = True

# End session by removing the temporary GRASS mapset and all the layers.
@staticmethod
def endGrassSession():
shutil.rmtree(GrassUtils.grassMapsetFolder(), True)
#shutil.rmtree(GrassUtils.grassMapsetFolder(), True)
GrassUtils.sessionRunning = False
GrassUtils.sessionLayers = {}

Expand Down
1 change: 0 additions & 1 deletion python/plugins/sextante/gui/AlgorithmExecutor.py
Expand Up @@ -93,7 +93,6 @@ def raiseInternalError(self, error):
def runalg(self):
try:
self.algorithm.execute(self.progress)
self.finished.emit()
except GeoAlgorithmExecutionException, e :
self.error.emit(e.msg)
except BaseException, e:
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/outputs/Output.py
Expand Up @@ -58,7 +58,7 @@ def serialize(self):

def setValue(self, value):
try:
if value != None:
if value != None and isinstance(value, basestring):
value = value.strip()
self.value = value
return True
Expand Down
5 changes: 4 additions & 1 deletion python/plugins/sextante/saga/SagaUtils.py
Expand Up @@ -95,7 +95,10 @@ def executeSaga(progress):
for line in iter(proc.readline, ""):
if "%" in line:
s = "".join([x for x in line if x.isdigit()])
progress.setPercentage(int(s))
try:
progress.setPercentage(int(s))
except:
pass
else:
line = line.strip()
if line!="/" and line!="-" and line !="\\" and line!="|":
Expand Down
19 changes: 15 additions & 4 deletions python/plugins/sextante/script/ScriptAlgorithm.py
Expand Up @@ -161,6 +161,10 @@ def processParameterLine(self,line):
out = OutputHTML()
elif tokens[1].lower().strip().startswith("output file"):
out = OutputFile()
elif tokens[1].lower().strip().startswith("output number"):
out = OutputNumber()
elif tokens[1].lower().strip().startswith("output string"):
out = OutputString()

if param != None:
self.addParameter(param)
Expand All @@ -173,18 +177,25 @@ def processParameterLine(self,line):

def processAlgorithm(self, progress):

script = "from sextante.core.Sextante import Sextante\n"
script = "import sextante\n"

ns = {}

for param in self.parameters:
script += param.name + "=" + param.getValueAsCommandLineParameter() + "\n"
#script += param.name + "=" + param.getValueAsCommandLineParameter() + "\n"
ns[param.name] = param.value

for out in self.outputs:
script += out.name + "=" + out.getValueAsCommandLineParameter() + "\n"
ns[out.name] = out.value
#script += out.name + "=" + out.getValueAsCommandLineParameter() + "\n"

script+=self.script
redirection = Redirection(progress)
sys.stdout = redirection
exec(script)
exec(script) in ns
sys.stdout = sys.__stdout__
for out in self.outputs:
out.setValue(ns[out.name])

def helpFile(self):
helpfile = self.descriptionFile + ".help"
Expand Down
Expand Up @@ -9,4 +9,4 @@
centery = (input.extent().yMinimum() + input.extent().yMaximum()) / 2
width = (input.extent().xMaximum() - input.extent().xMinimum())
height = (input.extent().yMaximum() - input.extent().yMinimum())
Sextante.runalg("mmqgisx:creategrid", cellsize, cellsize, width, height, centerx, centery, 3, grid)
sextante.runalg("mmqgisx:creategrid", cellsize, cellsize, width, height, centerx, centery, 3, grid)

0 comments on commit 6173fa3

Please sign in to comment.