Skip to content
Permalink
Browse files

[sextante] more tests. Added extra model and script

  • Loading branch information
volaya committed Apr 1, 2013
1 parent bce0a4c commit 40095a97b1b9f14d60e52db4e20f6214519e0e2c
@@ -0,0 +1,39 @@
NAME:Model with algorithms not in running order
GROUP:[Test algorithms]
PARAMETER:ParameterRaster|RASTERLAYER_RASTER|raster|False
120.0,60.0
VALUE:HARDCODEDPARAMVALUE_MINSLOPE_1===0.01
VALUE:HARDCODEDPARAMVALUE_Method_0===0
VALUE:HARDCODEDPARAMVALUE_STEP_0===1
VALUE:HARDCODEDPARAMVALUE_DOLINEAR _0===True
VALUE:HARDCODEDPARAMVALUE_LINEARTHRS_0===500.0
VALUE:HARDCODEDPARAMVALUE_CONVERGENCE_0===1.0
ALGORITHM:saga:catchmentareaparallel
154.0,415.0
None
1|RESULT
None
None
None
None
-1|HARDCODEDPARAMVALUE_STEP_0
-1|HARDCODEDPARAMVALUE_Method_0
-1|HARDCODEDPARAMVALUE_DOLINEAR _0
-1|HARDCODEDPARAMVALUE_LINEARTHRS_0
None
None
-1|HARDCODEDPARAMVALUE_CONVERGENCE_0
None
None
None
None
None
None
None
None
ALGORITHM:saga:fillsinksplanchondarboux2001
340.0,260.0
None
-1|RASTERLAYER_RASTER
-1|HARDCODEDPARAMVALUE_MINSLOPE_1
None
@@ -32,7 +32,6 @@
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sextante.core.QGisLayers import QGisLayers
from sextante.core.SextanteVectorWriter import SextanteVectorWriter

#Here we define the input and outputs
@@ -46,15 +45,15 @@

#input layers values are always a string with its location.
#That string can be converted into a QGIS object (a QgsVectorLayer in this case))
#using the Sextante.getObjectFromUri() method
vectorLayer = QGisLayers.getObjectFromUri(input)
#using the sextante.getobject() method
vectorLayer = sextante.getobject(input)

#And now we can process

#First we create the output layer.
#To do so, we create a SextanteVectorWriter, that we can later use to add features.
provider = vectorLayer.dataProvider()
writer = SextanteVectorWriter(output, None, provider.fields(), provider.geometryType(), vectorLayer.crs() )
writer = SextanteVectorWriter(output, None, provider.fields(), provider.geometryType(), vectorLayer.crs())

#Now we take the selected features and add them to the output layer
selection = vectorLayer.selectedFeatures()
@@ -0,0 +1,35 @@
##input=vector
##output=output vector
from sextante.core.SextanteVectorWriter import SextanteVectorWriter
from qgis.core import *
from PyQt4.QtCore import *

inputLayer = sextante.getobject(input)
features = sextante.getfeatures(inputLayer)
fields = inputLayer.pendingFields().toList()
outputLayer = SextanteVectorWriter(output, None, fields, QGis.WKBPoint, inputLayer.crs())
count = 0
mean = [0 for field in fields]
x = 0
y = 0
for ft in features:
c = ft.geometry().centroid().asPoint()
x += c.x()
y += c.y()
attrs = ft.attributes()
for f in range(len(fields)):
try:
mean[f] += float(attrs[f].toDouble()[0])
except:
pass
count += 1
if count != 0:
mean = [value / count for value in mean]
x /= count
y /= count
outFeat = QgsFeature()
meanPoint = QgsPoint(x, y)
outFeat.setGeometry(QgsGeometry.fromPoint(meanPoint))
outFeat.setAttributes([QVariant(v) for v in mean])
outputLayer.addFeature(outFeat)

@@ -108,6 +108,13 @@ def test_modelerfieldautoextent(self):
strhash=hash(str(dataset.ReadAsArray(0).tolist()))
self.assertEqual(strhash,2026100494)

def test_modelernotinorder(self):
outputs=sextante.runalg("modeler:notinorder",raster(),None)
output=outputs['CAREA_ALG0']
self.assertTrue(os.path.isfile(output))
dataset=gdal.Open(output, GA_ReadOnly)
strhash=hash(str(dataset.ReadAsArray(0).tolist()))
self.assertEqual(strhash,-1557050506)


def suite():
@@ -2,18 +2,37 @@
import unittest
from sextante.tests.TestData import points, points2, polygons, polygons2, lines, union,\
table, polygonsGeoJson
table, polygonsGeoJson, raster
from sextante.core.QGisLayers import QGisLayers
from sextante.core.SextanteUtils import SextanteUtils
from osgeo.gdalconst import GA_ReadOnly
import os
from osgeo import gdal

class SagaTest(unittest.TestCase):
'''tests for saga algorithms'''

def test_sagametricconversions(self):
outputs=sextante.runalg("saga:metricconversions",raster(),0,None)
output=outputs['CONV']
self.assertTrue(os.path.isfile(output))
dataset=gdal.Open(output, GA_ReadOnly)
strhash=hash(str(dataset.ReadAsArray(0).tolist()))
self.assertEqual(strhash,-2137931723)

def test_sagasortgrid(self):
outputs=sextante.runalg("saga:sortgrid",raster(),True,None)
output=outputs['OUTPUT']
self.assertTrue(os.path.isfile(output))
dataset=gdal.Open(output, GA_ReadOnly)
strhash=hash(str(dataset.ReadAsArray(0).tolist()))
self.assertEqual(strhash,1320073153)

'''the following tests are not meant to test the algorithms themselves,
but the algorithm provider, testing things such as the file conversion,
'''the following tests are not meant to test the algorithms themselves,
but the algorithm provider, testing things such as the file conversion,
the selection awareness of SAGA process, etc'''

def test_SagaVvectorAlgorithmWithSelection(self):
def test_SagaVectorAlgorithmWithSelection(self):
layer = sextante.getobject(polygons2());
feature = layer.getFeatures().next()
selected = [feature.id()]
@@ -63,7 +82,13 @@ def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self):
self.assertEqual(expectedvalues, values)
wkt='POINT(270787.49991451 4458955.46775295)'
self.assertEqual(wkt, str(feature.geometry().exportToWkt()))

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



0 comments on commit 40095a9

Please sign in to comment.
You can’t perform that action at this time.