6 changes: 3 additions & 3 deletions python/plugins/sextante/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
***************************************************************************
"""

from sextante.core.Sextante import runalg, runandload, alghelp, alglist, algoptions, load, \
extent, getObjectFromName, getObjectFromUri, getobject, getfeatures

from sextante.tools.general import runalg, runandload, alghelp, alglist, algoptions, load, extent, getobject
from sextante.tools.vector import getfeatures, spatialindex, values, uniquevalues
from sextante.tools.raster import scanraster
from sextante.tests.TestData import loadTestData

__author__ = 'Victor Olaya'
Expand Down
14 changes: 2 additions & 12 deletions python/plugins/sextante/algs/FieldPyculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def processAlgorithm(self, progress):
bytecode = compile(code, '<string>', 'exec')
except:
raise GeoAlgorithmExecutionException("FieldPyculator code execute error\n"+
"Field code block can't be executed! %s \n %s"
(unicode(sys.exc_info()[0].__name__), unicode(sys.exc_info()[1])))
"Field code block can't be executed! %s \n %s",
unicode(sys.exc_info()[0].__name__), unicode(sys.exc_info()[1]))

#run
features = QGisLayers.features(layer)
Expand Down Expand Up @@ -151,17 +151,7 @@ def processAlgorithm(self, progress):
del new_ns[self.RESULT_VAR_NAME]

#exec
#try:
exec bytecode in new_ns
#except:
# raise e
#===============================================================
# GeoAlgorithmExecutionException("FieldPyculator code execute error\n"+
# "Field code block can't be executed for feature %s\n%s\n%s" %
# (unicode(sys.exc_info()[0].__name__),
# unicode(sys.exc_info()[1]),
# unicode(feat_id)))
#===============================================================

#check result
if not new_ns.has_key(self.RESULT_VAR_NAME):
Expand Down
91 changes: 0 additions & 91 deletions python/plugins/sextante/core/Sextante.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,94 +346,3 @@ def cancel():
##==========================================================


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))
algoptions(name)
else:
print "Algorithm not found"

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

def runandload(name, *args):
return Sextante.runAlgorithm(name, SextantePostprocessing.handleAlgorithmResults, *args)

def extent(layers):
first = True
for layer in layers:
if not isinstance(layer, (QgsRasterLayer, QgsVectorLayer)):
layer = QGisLayers.getObjectFromUri(layer)
if layer is None:
continue
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
if first:
return "0,0,0,0"
else:
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, True)

def getobject(uriorname):
ret = getObjectFromName(uriorname)
if ret is None:
ret = getObjectFromUri(uriorname)
return ret

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

def getfeatures(layer):
return QGisLayers.features(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)
#===============================================================================
11 changes: 8 additions & 3 deletions python/plugins/sextante/grass/GrassAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,14 @@ def processAlgorithm(self, progress):
uniqueSufix = str(uuid.uuid4()).replace("-","");
for out in self.outputs:
if isinstance(out, OutputFile):
command+=(" " + out.name + "=\"" + out.value + "\"");
elif not isinstance(out, OutputHTML): #html files are not generated by grass, only by sextante to decorate grass output
#an output name to make sure it is unique if the session uses this algorithm several times
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 SEXTANTE 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
Expand Down
1 change: 1 addition & 0 deletions python/plugins/sextante/grass/description/r.quantile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ ParameterRaster|input|Input raster layer|False
ParameterNumber|quantiles|Number of quantiles|2|None|4
*ParameterBoolean|-r|Generate recode rules based on quantile-defined intervals|False
OutputHTML|html|Output report
OutputFile|outputtext|Output text file
16 changes: 16 additions & 0 deletions python/plugins/sextante/grass/grass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,19 @@ to tag a parameter as "advanced", just add "*" before its declaration. For insta
*ParameterBoolean|-c|-c|True


ADVANCED OUTPUT PROCESSING
-------------------------

In some cases, it might be interesting to take the console otput from GRASS and extract a part of it for saving or formatting.

Two things can be done about this:

-Creating an HTML file with output. Just add an output of type OutputHTML.
It's value will not be passed to GRASS, but you can use it later to create the HTML file from the console output.
You should create a python file in the grass/ext package, with the same name as the grass module, with dots replaced by low hyphens
(for instance r_quantile.py for the r.quantile command), and add a postProcessResults(alg) method. It will be called when the
execution of the GRASS command is finished.
-Creating a text file. Do as above, but adding an otput of type OutputFile. Since some GRASS commands might use this type of output,
and to make sure that the value of this output is not passed to the GRASS comman when calling it, the output has to be named 'outputtext'


Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
##numpolygons=number 10
##polygons=output vector

input = sextante.getObjectFromUri(input)
input = sextante.getobject(input)
centerx = (input.extent().xMinimum() + input.extent().xMaximum()) / 2
centery = (input.extent().yMinimum() + input.extent().yMaximum()) / 2
width = (input.extent().xMaximum() - input.extent().xMinimum())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
##grid=output vector
from sextante.core.QGisLayers import QGisLayers

input = QGisLayers.getObjectFromUri(input)
input = sextante.getobject(input)
centerx = (input.extent().xMinimum() + input.extent().xMaximum()) / 2
centery = (input.extent().yMinimum() + input.extent().yMaximum()) / 2
width = (input.extent().xMaximum() - input.extent().xMinimum())
Expand Down
37 changes: 37 additions & 0 deletions python/plugins/sextante/tests/GdalTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sextante
import unittest
from sextante.tests.TestData import points, points2, polygons, polygons2, lines, union,\
table, polygonsGeoJson, raster
from sextante.core.QGisLayers import QGisLayers
import os
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly
from sextante.core.SextanteUtils import SextanteUtils

class GdalTest(unittest.TestCase):

def test_gdalogrsieve(self):
outputs=sextante.runalg("gdalogr:sieve",raster(),2,0,None)
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 test_gdalogrsieveWithUnsupportedOutputFormat(self):
outputs=sextante.runalg("gdalogr:sieve",raster(),2,0, SextanteUtils.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 suite():
suite = unittest.makeSuite(GdalTest, 'test')
return suite

def runtests():
result = unittest.TestResult()
testsuite = suite()
testsuite.run(result)
return result
9 changes: 1 addition & 8 deletions python/plugins/sextante/tests/ModelerAlgorithmTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ def testRemoveAlgorithm(self):
model.provider = Providers.providers['model']
self.assertTrue(2, len(model.algs))
self.assertFalse(model.removeAlgorithm(0))
self.assertTrue(model.removeAlgorithm(len(model.algs) - 1));
from threading import settrace

import sys
sys.path.append("D:\eclipse\plugins\org.python.pydev_2.6.0.2012062818\pysrc")
from pydevd import *
settrace()

self.assertTrue(model.removeAlgorithm(len(model.algs) - 1));
model.execute(None)
outputs = model.outputs
self.assertEquals(1, len(outputs))
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/sextante/tests/SagaTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self):
self.assertEqual(wkt, str(feature.geometry().exportToWkt()))

def test_SagaRasterAlgorithmWithUnsupportedOutputFormat(self):
outputs=sextante.runalg("saga:convergenceindex",raster(),0,0,None)
outputs=sextante.runalg("saga:convergenceindex",raster(),0,0,SextanteUtils.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,-807227462)
self.assertEqual(strhash, 485390137)


def suite():
Expand Down
4 changes: 3 additions & 1 deletion python/plugins/sextante/tests/ScriptTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def test_scripthexgridfromlayerbounds(self):
def test_scriptascriptthatreturnsanumber(self):
outputs=sextante.runalg("script:ascriptthatreturnsanumber")
output=outputs['number']
self.assertTrue(10, output.value)
self.assertTrue(10, output)



def suite():
suite = unittest.makeSuite(ScriptTest, 'test')
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/sextante/tests/SextanteTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
from sextante.tests import ScriptTest
from sextante.tests import SagaTest
from sextante.tests import GeoAlgorithmTest
from sextante.tests import GdalTest

def suite():
suite = unittest.TestSuite()
suite.addTests(QgisAlgsTest.suite())
suite.addTests(ModelerAlgorithmTest.suite())
suite.addTests(SagaTest.suite())
suite.addTests(GdalTest.suite())
suite.addTests(ScriptTest.suite())
suite.addTests(SextanteToolsTest.suite())
#suite.addTests(ParametersTest.suite())
Expand Down
9 changes: 5 additions & 4 deletions python/plugins/sextante/tests/SextanteToolsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from sextante.tests.TestData import points, points2, polygons, polygons2, lines, union,\
table, polygonsGeoJson, raster
from sextante.core import Sextante
from sextante.tools.vector import getAttributeValues
from sextante.tools.vector import values
from sextante.tools.general import getfromname

class SextanteToolsTest(unittest.TestCase):
'''tests the method imported when doing an "import sextante", and also in sextante.tools.
Expand All @@ -17,7 +18,7 @@ def test_getobject(self):

def test_runandload(self):
sextante.runandload("qgis:countpointsinpolygon",polygons(),points(),"NUMPOINTS", None)
layer = Sextante.getObjectFromName("Result")
layer = getfromname("Result")
self.assertIsNotNone(layer)

def test_featuresWithoutSelection(self):
Expand All @@ -36,9 +37,9 @@ def test_featuresWithSelection(self):

def test_attributeValues(self):
layer = sextante.getobject(points())
values = getAttributeValues(layer, "ID")
attributeValues = values(layer, "ID")
i = 1
for value in values['ID']:
for value in attributeValues['ID']:
self.assertEqual(int(i), int(value))
i+=1
self.assertEquals(13,i)
Expand Down
2 changes: 0 additions & 2 deletions python/plugins/sextante/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from sextante.tools.vector import getAttributeValues
from sextante.tools.raster import scanraster
Loading