2013-03-28 21:23:01 +01:00
|
|
|
import sextante
|
|
|
|
import unittest
|
|
|
|
from sextante.tests.TestData import points, points2, polygons, polygons2, lines, union,\
|
2013-04-01 23:34:11 +02:00
|
|
|
table, polygonsGeoJson, raster
|
2013-03-28 21:23:01 +01:00
|
|
|
from sextante.core.QGisLayers import QGisLayers
|
|
|
|
from sextante.core.SextanteUtils import SextanteUtils
|
2013-04-01 23:34:11 +02:00
|
|
|
from osgeo.gdalconst import GA_ReadOnly
|
|
|
|
import os
|
|
|
|
from osgeo import gdal
|
2013-03-28 21:23:01 +01:00
|
|
|
|
|
|
|
class SagaTest(unittest.TestCase):
|
|
|
|
'''tests for saga algorithms'''
|
2013-03-31 21:18:27 +02:00
|
|
|
|
2013-04-01 23:34:11 +02:00
|
|
|
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)
|
2013-04-02 22:34:21 +02:00
|
|
|
|
2013-04-01 23:34:11 +02:00
|
|
|
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)
|
2013-03-31 21:18:27 +02:00
|
|
|
|
2013-04-02 22:34:21 +02:00
|
|
|
'''the following tests are not meant to test the algorithms themselves,
|
|
|
|
but the algorithm provider, testing things such as the file conversion,
|
2013-03-28 21:23:01 +01:00
|
|
|
the selection awareness of SAGA process, etc'''
|
2013-03-31 21:18:27 +02:00
|
|
|
|
2013-04-01 23:34:11 +02:00
|
|
|
def test_SagaVectorAlgorithmWithSelection(self):
|
2013-03-28 21:23:01 +01:00
|
|
|
layer = sextante.getobject(polygons2());
|
|
|
|
feature = layer.getFeatures().next()
|
2013-03-31 21:18:27 +02:00
|
|
|
selected = [feature.id()]
|
2013-03-28 21:23:01 +01:00
|
|
|
layer.setSelectedFeatures(selected)
|
|
|
|
outputs=sextante.runalg("saga:polygoncentroids",polygons2(),True,None)
|
2013-04-09 23:11:11 +02:00
|
|
|
layer.setSelectedFeatures([])
|
2013-03-28 21:23:01 +01:00
|
|
|
output=outputs['CENTROIDS']
|
|
|
|
layer=QGisLayers.getObjectFromUri(output, True)
|
|
|
|
fields=layer.pendingFields()
|
|
|
|
expectednames=['ID','POLY_NUM_B','POLY_ST_B']
|
|
|
|
expectedtypes=['Real','Real','String']
|
|
|
|
names=[str(f.name()) for f in fields]
|
|
|
|
types=[str(f.typeName()) for f in fields]
|
|
|
|
self.assertEqual(expectednames, names)
|
|
|
|
self.assertEqual(expectedtypes, types)
|
|
|
|
features=sextante.getfeatures(layer)
|
|
|
|
self.assertEqual(1, len(features))
|
|
|
|
feature=features.next()
|
|
|
|
attrs=feature.attributes()
|
|
|
|
expectedvalues=["2","1","string a"]
|
|
|
|
values=[str(attr.toString()) for attr in attrs]
|
|
|
|
self.assertEqual(expectedvalues, values)
|
2013-04-09 23:11:11 +02:00
|
|
|
wkt='POINT(270806.69221918 4458924.97720492)'
|
2013-03-28 21:23:01 +01:00
|
|
|
self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
|
2013-03-31 21:18:27 +02:00
|
|
|
|
2013-03-28 21:23:01 +01:00
|
|
|
def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self):
|
2013-03-31 21:18:27 +02:00
|
|
|
'''this tests both the exporting to shp and then the format change in the output layer'''
|
2013-03-28 21:23:01 +01:00
|
|
|
layer = sextante.getobject(polygonsGeoJson());
|
|
|
|
feature = layer.getFeatures().next()
|
2013-03-31 21:18:27 +02:00
|
|
|
selected = [feature.id()]
|
2013-03-28 21:23:01 +01:00
|
|
|
layer.setSelectedFeatures(selected)
|
|
|
|
outputs=sextante.runalg("saga:polygoncentroids",polygonsGeoJson(),True, SextanteUtils.getTempFilename("geojson"))
|
2013-04-09 23:11:11 +02:00
|
|
|
layer.setSelectedFeatures([])
|
2013-03-28 21:23:01 +01:00
|
|
|
output=outputs['CENTROIDS']
|
|
|
|
layer=QGisLayers.getObjectFromUri(output, True)
|
|
|
|
fields=layer.pendingFields()
|
|
|
|
expectednames=['ID','POLY_NUM_A','POLY_ST_A']
|
|
|
|
expectedtypes=['Real','Real','String']
|
|
|
|
names=[str(f.name()) for f in fields]
|
|
|
|
types=[str(f.typeName()) for f in fields]
|
|
|
|
self.assertEqual(expectednames, names)
|
|
|
|
self.assertEqual(expectedtypes, types)
|
|
|
|
features=sextante.getfeatures(layer)
|
|
|
|
self.assertEqual(1, len(features))
|
|
|
|
feature=features.next()
|
|
|
|
attrs=feature.attributes()
|
|
|
|
expectedvalues=["0","1.1","string a"]
|
|
|
|
values=[str(attr.toString()) for attr in attrs]
|
|
|
|
self.assertEqual(expectedvalues, values)
|
|
|
|
wkt='POINT(270787.49991451 4458955.46775295)'
|
2013-03-31 21:18:27 +02:00
|
|
|
self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
|
2013-04-09 23:11:11 +02:00
|
|
|
|
2013-04-01 23:34:11 +02:00
|
|
|
def test_SagaRasterAlgorithmWithUnsupportedOutputFormat(self):
|
2013-04-10 15:03:17 +02:00
|
|
|
outputs=sextante.runalg("saga:convergenceindex",raster(),0,0,SextanteUtils.getTempFilename("img"))
|
2013-04-01 23:34:11 +02:00
|
|
|
output=outputs['RESULT']
|
|
|
|
self.assertTrue(os.path.isfile(output))
|
|
|
|
dataset=gdal.Open(output, GA_ReadOnly)
|
|
|
|
strhash=hash(str(dataset.ReadAsArray(0).tolist()))
|
2013-04-10 15:03:17 +02:00
|
|
|
self.assertEqual(strhash, 485390137)
|
2013-03-31 21:18:27 +02:00
|
|
|
|
2013-03-28 21:23:01 +01:00
|
|
|
|
|
|
|
def suite():
|
2013-03-31 21:18:27 +02:00
|
|
|
suite = unittest.makeSuite(SagaTest, 'test')
|
2013-03-28 21:23:01 +01:00
|
|
|
return suite
|
|
|
|
|
|
|
|
def runtests():
|
2013-03-31 21:18:27 +02:00
|
|
|
result = unittest.TestResult()
|
2013-03-28 21:23:01 +01:00
|
|
|
testsuite = suite()
|
|
|
|
testsuite.run(result)
|
|
|
|
return result
|