Skip to content

Commit

Permalink
Several changes. Now saga algorithms can be executed and also scripts…
Browse files Browse the repository at this point in the history
…. Still have to test both, but it is more or less working fine

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@21 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf@gmail.com committed Feb 3, 2012
1 parent 955454e commit 2513431
Show file tree
Hide file tree
Showing 47 changed files with 887 additions and 275 deletions.
Binary file added images/config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/documenter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/grass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/history.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/r.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/saga.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/sextante.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/sextante_toolbox.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/sextante_toolbox2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions src/sextante/core/SextanteTest.py → src/Test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'''
Created on 13/01/2012
@author: volaya
'''
from sextante.core.Sextante import Sextante


def main():
Sextante.initialize()
print(Sextante.asStr())

Sextante.asStr()


if __name__ == '__main__':
main()
main()
1 change: 0 additions & 1 deletion src/sextante/SextantePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def initGui(self):

def unload(self):
self.toolbox.setVisible(False)
menuBar = self.iface.mainWindow().menuBar()
self.menu.deleteLater()

def openToolbox(self):
Expand Down
43 changes: 37 additions & 6 deletions src/sextante/core/GeoAlgorithm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from sextante.outputs.Output import Output
from sextante.parameters.Parameter import Parameter
from sextante.core.QGisLayers import QGisLayers
from sextante.parameters.ParameterRaster import ParameterRaster
from sextante.parameters.ParameterVector import ParameterVector


class GeoAlgorithm:

Expand All @@ -10,15 +14,25 @@ def __init__(self):
self.group = ""
self.defineCharacteristics()
self.providerName = ""
self.crs = None

def execute(self):
if self.checkParameters():
self.proccessAlgorithm()
def execute(self, progress):
self.setOutputCRSFromInputLayers()
self.processAlgorithm(progress)

def setOutputCRSFromInputLayers(self):
for param in self.parameters:
if isinstance(param, ParameterRaster):
layers = QGisLayers.getRasterLayers()
elif isinstance(param, ParameterVector):
layers = QGisLayers.getVectorLayers()
else:
continue
for layer in layers:
if layer.dataProvider().dataSourceUri() == param.value:
self.crs = layer.crs()
return

def checkParameters(self):
#TODO!!!!!!
return True

def defineCharacteristics(self):
pass
Expand All @@ -27,10 +41,12 @@ def processAlgorithm(self):
pass

def putOutput(self, output):
#TODO: check that name does not exist
if isinstance(output, Output):
self.outputs.append(output)

def putParameter(self, param):
#TODO: check that name does not exist
if isinstance(param, Parameter):
self.parameters.append(param)

Expand All @@ -57,7 +73,22 @@ def __str__(self):
s+=("\n")
return s


def commandLineName(self):
return self.providerName + self.name.lower().replace(" ", "")


def getOuputsChannelsAsMap(self):
retmap = {}
for out in self.outputs:
retmap[out.name] = out.channel
return retmap

def getAsCommand(self):
s="Sextante.runalg(\"" + self.commandLineName() + "\","
for param in self.parameters:
s+=param.getValueAsCommandLineParameter() + ","
for out in self.outputs:
s+=out.getChannelAsCommandLineParameter() + ","
s= s[:-1] + ")"
return s
5 changes: 5 additions & 0 deletions src/sextante/core/GeoAlgorithmExecutionException.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class GeoAlgorithmExecutionException(Exception):

def __init__(self, msg):
Exception.__init__(self)
self.msg = msg
75 changes: 66 additions & 9 deletions src/sextante/core/QGisLayers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
from os import path

class QGisLayers:

Expand All @@ -23,26 +27,79 @@ def getVectorLayers():
layerNames.append(layer)
return layerNames

@staticmethod
def getTables():
layers = QGisLayers.iface.legendInterface().layers()
tables = list()
for layer in layers:
if layer.type() == layer.VectorLayer :
uri = str(layer.dataProvider().dataSourceUri())
if (uri.endswith("shp")):
tables.append(layer)
return tables

@staticmethod
def setInterface(iface):
QGisLayers.iface = iface


@staticmethod
def getLayersCount():
count = LayersCount()
return count
def loadList(layers):
for layer in layers:
QGisLayers.load(layer)

@staticmethod
def load(layer, name = None, crs = None):
prjSetting = None
settings = QSettings()
try:
if crs != None:
prjSetting = settings.value("/Projections/defaultBehaviour")
settings.setValue("/Projections/defaultBehaviour", QVariant(""))
if name == None:
name = path.split(layer)[1]
if layer.endswith("shp"):
qgslayer = QgsVectorLayer(layer, name, 'ogr')
if crs != None:
qgslayer.setCrs(crs, False)
QgsMapLayerRegistry.instance().addMapLayer(qgslayer)
else:
qgslayer = QgsRasterLayer(layer, name)
if crs != None:
qgslayer.setCrs(crs,False)
QgsMapLayerRegistry.instance().addMapLayer(qgslayer)
finally:
if prjSetting:
settings.setValue("/Projections/defaultBehaviour", prjSetting)


@staticmethod
def loadMap(layersmap, crs):
for name in layersmap.keys():
QGisLayers.load(layersmap[name], name, crs)


@staticmethod
def loadFromAlg(alg):
QGisLayers.loadMap(alg.getOuputsChannelsAsMap(), alg.crs)


@staticmethod
def getObjectFromUri(uri):
layers = QGisLayers.getRasterLayers()
for layer in layers:
if layer.dataProvider().dataSourceUri() == uri:
return layer
layers = QGisLayers.getVectorLayers()
for layer in layers:
if layer.dataProvider().dataSourceUri() == uri:
return layer





class LayersCount:

def __init__(self):
self.raster = 0
self.vector_point=0
self.vector_line=0
self.vector_polygon=0



59 changes: 55 additions & 4 deletions src/sextante/core/Sextante.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
from sextante.saga.SagaAlgorithmProvider import SagaAlgorithmProvider
from sextante.script.ScriptAlgorithmProvider import ScriptAlgorithmProvider
import copy
from sextante.core.QGisLayers import QGisLayers
from sextante.gui.AlgorithmExecutor import AlgorithmExecutor, SilentProgress
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

class Sextante:

Expand Down Expand Up @@ -52,23 +59,67 @@ 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():
def alglist(text=None):
s=""
for provider in Sextante.algs.values():
for alg in provider.values():
s+=(alg.name + " --->" + alg.commandLineName() + "\n")
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 algHelp(name):
def alghelp(name):
alg = Sextante.getAlgorithm(name)
if alg != None:
print(str(alg))
else:
print "Algorithm not found"

@staticmethod
def runalg(name, *args):
alg = Sextante.getAlgorithm(name)
if alg == None:
print("Error: Algorithm not found\n")
return
if len(args) != len(alg.parameters) + len(alg.outputs):
print ("Error: Wrong number of parameters")
Sextante.alghelp(name)
return

alg = copy.deepcopy(alg)
i = 0
for param in alg.parameters:
if not param.setValue(args[i]):
print ("Error: Wrong parameter value: " + args[i])
return
i = i +1

for output in alg.outputs:
if not output.setChannel(args[i]):
print ("Error: Wrong output channel: " + args[i])
return
i = i +1

try:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
AlgorithmExecutor.runalg(alg, SilentProgress())
QApplication.restoreOverrideCursor()
return alg.getOuputsChannelsAsMap()
except GeoAlgorithmExecutionException, e:
print "*****Error executing algoritm*****"
print e.msg

@staticmethod
def load(layer):
QGisLayers.load(layer)

@staticmethod
def getObject(string):
QGisLayers.getObjectFromUri(string)


65 changes: 61 additions & 4 deletions src/sextante/core/SextanteUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
from sextante.outputs.OutputTable import OutputTable
from sextante.outputs.OutputVector import OutputVector
from sextante.outputs.OutputRaster import OutputRaster
import datetime
class SextanteUtils:

NUM_EXPORTED = 1
LOG_ERROR = "ERROR"
LOG_INFO = "INFO"
LOG_WARNING = "WARNING"
LOG_ALGORITHM = "ALGORITHM"

@staticmethod
def userFolder():
userfolder = os.getenv('HOME') + os.sep + "sextante"
Expand All @@ -19,7 +26,7 @@ def softwareFolder():

@staticmethod
def isWindows():
return os.path =="nt"
return os.name =="nt"

@staticmethod
def tempFolder():
Expand All @@ -40,15 +47,65 @@ def setTempOutput(out):
else:
ext =""

filename = SextanteUtils.tempFolder() + os.sep + seconds + os.sep + ext
filename = SextanteUtils.tempFolder() + os.sep + seconds + str(SextanteUtils.NUM_EXPORTED) + ext
out.channel = filename
SextanteUtils.NUM_EXPORTED += 1


@staticmethod
def addToLog(msg):
pass
def logFilename():
batchfile = SextanteUtils.userFolder() + os.sep + "sextante_qgis.log"
return batchfile


@staticmethod
def addToLog(msgtype, msg):

if isinstance(msg, list):
text=""
for i in range(0, len(msg)):
text+=msg[i] + "|"
text = text[:-1]
else:
text = str(msg)
line = msgtype + "|" +str(datetime.datetime.now()) + "|" + text + "\n"
logfile = open(SextanteUtils.logFilename(), "a")
logfile.write(line)
logfile.close()

@staticmethod
def getLogEntries():
entries={}
errors=[]
algorithms=[]
warnings=[]
info=[]
lines = open(SextanteUtils.logFilename())
line = lines.readline()
while line != "":
line = line.strip("\n").strip()
tokens = line.split("|")
text=""
for i in range(2, len(tokens)):
text+=tokens[i] + "\n"
if line.startswith(SextanteUtils.LOG_ERROR):
errors.append(LogElement(tokens[1], text))
elif line.startswith(SextanteUtils.LOG_ALGORITHM):
algorithms.append(LogElement(tokens[1], text))
elif line.startswith(SextanteUtils.LOG_WARNING):
warnings.append(LogElement(tokens[1], text))
elif line.startswith(SextanteUtils.LOG_INFO):
info.append(LogElement(tokens[1], text))
lines.close()
entries[SextanteUtils.LOG_ERROR] = errors
entries[SextanteUtils.LOG_ALGORITHM] = algorithms
return entries

class LogElement():

def __init__(self, date, text):
self.date = date
self.text = text



Expand Down
Loading

0 comments on commit 2513431

Please sign in to comment.