2013-04-13 11:04:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ModelerAlgorithmTest.py
|
|
|
|
---------------------
|
|
|
|
Date : March 2013
|
|
|
|
Copyright : (C) 2013 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'March 2013'
|
|
|
|
__copyright__ = '(C) 2013, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-04-13 11:04:22 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-04-13 11:04:22 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2013-03-25 00:00:40 +01:00
|
|
|
import os
|
2013-10-01 20:52:22 +03:00
|
|
|
import unittest
|
2013-03-25 00:00:40 +01:00
|
|
|
from osgeo import gdal
|
|
|
|
from osgeo.gdalconst import GA_ReadOnly
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
|
|
import processing
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.modeler import ModelerAlgorithmProvider
|
|
|
|
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
|
|
|
|
from processing.modeler.Providers import Providers
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.tests.TestData import points, polygons, union, raster
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-03-29 00:44:33 +01:00
|
|
|
|
2013-03-25 00:00:40 +01:00
|
|
|
class ModelerAlgorithmTest(unittest.TestCase):
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2013-03-25 00:00:40 +01:00
|
|
|
def testCreateModel(self):
|
|
|
|
pass
|
2013-04-15 07:16:20 +02:00
|
|
|
|
2013-04-09 23:11:11 +02:00
|
|
|
def testEditModelParameter(self):
|
|
|
|
pass
|
2013-04-15 07:16:20 +02:00
|
|
|
|
2013-04-09 23:11:11 +02:00
|
|
|
def testEditModelAlgorithm(self):
|
|
|
|
pass
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2013-03-25 00:00:40 +01:00
|
|
|
def testRemoveAlgorithm(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
folder = \
|
|
|
|
os.path.join(os.path.dirname(ModelerAlgorithmProvider.__file__),
|
|
|
|
'models')
|
|
|
|
modelfile = os.path.join(folder, 'noinputs.model')
|
2013-03-29 00:44:33 +01:00
|
|
|
model = ModelerAlgorithm()
|
|
|
|
model.openModel(modelfile)
|
2013-04-09 23:11:11 +02:00
|
|
|
model.provider = Providers.providers['model']
|
2013-03-29 00:44:33 +01:00
|
|
|
self.assertTrue(2, len(model.algs))
|
|
|
|
self.assertFalse(model.removeAlgorithm(0))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertTrue(model.removeAlgorithm(len(model.algs) - 1))
|
2013-04-09 23:11:11 +02:00
|
|
|
model.execute(None)
|
|
|
|
outputs = model.outputs
|
|
|
|
self.assertEquals(1, len(outputs))
|
2013-10-01 20:52:22 +03:00
|
|
|
output = outputs[0].value
|
2013-04-15 07:16:20 +02:00
|
|
|
self.assertTrue(os.path.exists(output))
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2013-03-29 00:44:33 +01:00
|
|
|
def testRemoveParameter(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
folder = \
|
|
|
|
os.path.join(os.path.dirname(ModelerAlgorithmProvider.__file__),
|
|
|
|
'models')
|
|
|
|
modelfile = os.path.join(folder, 'watersheds.model')
|
2013-03-30 18:58:53 +01:00
|
|
|
model = ModelerAlgorithm()
|
|
|
|
model.openModel(modelfile)
|
|
|
|
self.assertTrue(2, len(model.parameters))
|
|
|
|
self.assertFalse(model.removeParameter(0))
|
|
|
|
self.assertTrue(2, len(model.parameters))
|
2013-03-31 21:18:27 +02:00
|
|
|
|
2013-03-30 18:58:53 +01:00
|
|
|
def testComputingDependecies(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
folder = \
|
|
|
|
os.path.join(os.path.dirname(ModelerAlgorithmProvider.__file__),
|
|
|
|
'models')
|
|
|
|
modelfile = os.path.join(folder, 'watersheds.model')
|
2013-03-30 18:58:53 +01:00
|
|
|
model = ModelerAlgorithm()
|
|
|
|
model.openModel(modelfile)
|
|
|
|
self.assertTrue(2, len(model.parameters))
|
|
|
|
self.assertTrue(5, len(model.algs))
|
|
|
|
dependent = model.getDependentAlgorithms(0)
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertEquals([0, 1, 2, 3, 4], dependent)
|
2013-03-30 18:58:53 +01:00
|
|
|
dependent = model.getDependentAlgorithms(1)
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertEquals([1, 2, 3, 4], dependent)
|
2013-03-30 18:58:53 +01:00
|
|
|
dependent = model.getDependentAlgorithms(2)
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertEquals([2, 3, 4], dependent)
|
2013-03-30 18:58:53 +01:00
|
|
|
dependent = model.getDependentAlgorithms(3)
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertEquals([3, 4], dependent)
|
2013-03-30 18:58:53 +01:00
|
|
|
dependent = model.getDependentAlgorithms(4)
|
|
|
|
self.assertEquals([4], dependent)
|
2013-03-31 21:18:27 +02:00
|
|
|
|
2013-03-30 18:58:53 +01:00
|
|
|
depends = model.getDependsOnAlgorithms(0)
|
|
|
|
self.assertEquals([], depends)
|
|
|
|
depends = model.getDependsOnAlgorithms(1)
|
|
|
|
self.assertEquals([0], depends)
|
|
|
|
depends = model.getDependsOnAlgorithms(2)
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertEquals([1, 0], depends)
|
2013-03-30 18:58:53 +01:00
|
|
|
depends = model.getDependsOnAlgorithms(3)
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertEquals([2, 1, 0], depends)
|
2013-03-30 18:58:53 +01:00
|
|
|
depends = model.getDependsOnAlgorithms(4)
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertEquals([3, 2, 1, 0], depends)
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2013-03-25 00:00:40 +01:00
|
|
|
def test_modelersagagrass(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
outputs = processing.runalg('modeler:sagagrass', points(), None)
|
|
|
|
output = outputs['CENTROIDS_ALG1']
|
|
|
|
layer = dataobjects.getObjectFromUri(output, True)
|
|
|
|
fields = layer.pendingFields()
|
|
|
|
expectednames = ['CAT']
|
|
|
|
expectedtypes = ['Real']
|
2015-08-16 20:57:24 +02:00
|
|
|
names = [unicode(f.name()) for f in fields]
|
|
|
|
types = [unicode(f.typeName()) for f in fields]
|
2013-03-25 00:00:40 +01:00
|
|
|
self.assertEqual(expectednames, names)
|
|
|
|
self.assertEqual(expectedtypes, types)
|
2013-10-01 20:52:22 +03:00
|
|
|
features = processing.features(layer)
|
2013-03-25 00:00:40 +01:00
|
|
|
self.assertEqual(12, len(features))
|
2013-10-01 20:52:22 +03:00
|
|
|
feature = features.next()
|
|
|
|
attrs = feature.attributes()
|
|
|
|
expectedvalues = ['1']
|
2015-08-16 20:57:24 +02:00
|
|
|
values = [unicode(attr) for attr in attrs]
|
2013-03-25 00:00:40 +01:00
|
|
|
self.assertEqual(expectedvalues, values)
|
2013-10-01 20:52:22 +03:00
|
|
|
wkt = 'POINT(270839.65586926 4458983.16267036)'
|
2015-08-16 20:57:24 +02:00
|
|
|
self.assertEqual(wkt, unicode(feature.geometry().exportToWkt()))
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2013-03-25 00:00:40 +01:00
|
|
|
def test_modelersimplemodel(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
outputs = processing.runalg('modeler:simplemodel', raster(), None)
|
|
|
|
output = outputs['SLOPE_ALG0']
|
2013-03-25 00:00:40 +01:00
|
|
|
self.assertTrue(os.path.isfile(output))
|
2013-10-01 20:52:22 +03:00
|
|
|
dataset = gdal.Open(output, GA_ReadOnly)
|
2015-08-16 20:57:24 +02:00
|
|
|
strhash = hash(unicode(dataset.ReadAsArray(0).tolist()))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertEqual(strhash, 1891122097)
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2013-03-25 00:00:40 +01:00
|
|
|
def test_modelerfieldautoextent(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
outputs = processing.runalg('modeler:fieldautoextent', polygons(),
|
|
|
|
'POLY_NUM_A', None)
|
|
|
|
output = outputs['USER_GRID_ALG0']
|
2013-03-25 00:00:40 +01:00
|
|
|
self.assertTrue(os.path.isfile(output))
|
2013-10-01 20:52:22 +03:00
|
|
|
dataset = gdal.Open(output, GA_ReadOnly)
|
2015-08-16 20:57:24 +02:00
|
|
|
strhash = hash(unicode(dataset.ReadAsArray(0).tolist()))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertEqual(strhash, 2026100494)
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2013-04-01 23:34:11 +02:00
|
|
|
def test_modelernotinorder(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
outputs = processing.runalg('modeler:notinorder', raster(), None)
|
|
|
|
output = outputs['CAREA_ALG0']
|
2013-04-01 23:34:11 +02:00
|
|
|
self.assertTrue(os.path.isfile(output))
|
2013-10-01 20:52:22 +03:00
|
|
|
dataset = gdal.Open(output, GA_ReadOnly)
|
2015-08-16 20:57:24 +02:00
|
|
|
strhash = hash(unicode(dataset.ReadAsArray(0).tolist()))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.assertEqual(strhash, -1557050506)
|
2013-04-15 07:16:20 +02:00
|
|
|
|
2013-04-13 09:40:01 +02:00
|
|
|
def test_modeleroptionalfield(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
outputs = processing.runalg('modeler:optionalfield', points(), None)
|
|
|
|
output = outputs['OUTPUT_ALG0']
|
|
|
|
layer = dataobjects.getObjectFromUri(output, True)
|
|
|
|
fields = layer.pendingFields()
|
|
|
|
expectednames = ['id', 'value', 'area', 'perim']
|
|
|
|
expectedtypes = ['Integer', 'String', 'Real', 'Real']
|
2015-08-16 20:57:24 +02:00
|
|
|
names = [unicode(f.name()) for f in fields]
|
|
|
|
types = [unicode(f.typeName()) for f in fields]
|
2013-04-13 09:40:01 +02:00
|
|
|
self.assertEqual(expectednames, names)
|
|
|
|
self.assertEqual(expectedtypes, types)
|
2013-10-01 20:52:22 +03:00
|
|
|
features = processing.features(layer)
|
2013-04-13 09:40:01 +02:00
|
|
|
self.assertEqual(1, len(features))
|
2013-10-01 20:52:22 +03:00
|
|
|
feature = features.next()
|
|
|
|
attrs = feature.attributes()
|
|
|
|
expectedvalues = ['0', 'all', '3592.818848', '230.989919']
|
2015-08-16 20:57:24 +02:00
|
|
|
values = [unicode(attr) for attr in attrs]
|
2013-04-13 09:40:01 +02:00
|
|
|
self.assertEqual(expectedvalues, values)
|
2013-10-01 20:52:22 +03:00
|
|
|
wkt = 'POLYGON((270839.46818665 4458921.97813894,270778.60197966 4458935.96883677,270786.54279065 4458980.04784113,270803.15756434 4458983.84880322,270839.65586926 4458983.16267036,270855.74530134 4458940.79948673,270839.46818665 4458921.97813894))'
|
2015-08-16 20:57:24 +02:00
|
|
|
self.assertEqual(wkt, unicode(feature.geometry().exportToWkt()))
|
2013-04-13 09:40:01 +02:00
|
|
|
|
|
|
|
def test_modeleremptystring(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
outputs = processing.runalg('modeler:emptystring', union(), None)
|
|
|
|
output = outputs['OUTPUT_LAYER_ALG0']
|
|
|
|
layer = dataobjects.getObjectFromUri(output, True)
|
|
|
|
fields = layer.pendingFields()
|
|
|
|
expectednames = [
|
|
|
|
'ID',
|
|
|
|
'POLY_NUM_A',
|
|
|
|
'POLY_ST_A',
|
|
|
|
'ID_2',
|
|
|
|
'POLY_NUM_B',
|
|
|
|
'POLY_ST_B',
|
|
|
|
'NewField',
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
]
|
2013-10-01 20:52:22 +03:00
|
|
|
expectedtypes = [
|
|
|
|
'Integer',
|
|
|
|
'Real',
|
|
|
|
'String',
|
|
|
|
'Integer',
|
|
|
|
'Real',
|
|
|
|
'String',
|
|
|
|
'Integer',
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
]
|
2015-08-16 20:57:24 +02:00
|
|
|
names = [unicode(f.name()) for f in fields]
|
|
|
|
types = [unicode(f.typeName()) for f in fields]
|
2013-04-13 09:40:01 +02:00
|
|
|
self.assertEqual(expectednames, names)
|
|
|
|
self.assertEqual(expectedtypes, types)
|
2013-10-01 20:52:22 +03:00
|
|
|
features = processing.features(layer)
|
2013-04-13 09:40:01 +02:00
|
|
|
self.assertEqual(8, len(features))
|
2013-10-01 20:52:22 +03:00
|
|
|
feature = features.next()
|
|
|
|
attrs = feature.attributes()
|
|
|
|
expectedvalues = [
|
|
|
|
'1',
|
|
|
|
'1.1',
|
|
|
|
'string a',
|
|
|
|
'2',
|
|
|
|
'1',
|
|
|
|
'string a',
|
|
|
|
'10',
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
]
|
2015-08-16 20:57:24 +02:00
|
|
|
values = [unicode(attr) for attr in attrs]
|
2013-04-13 09:40:01 +02:00
|
|
|
self.assertEqual(expectedvalues, values)
|
2013-10-01 20:52:22 +03:00
|
|
|
wkt = 'POLYGON((270807.08580285 4458940.1594565,270798.42294527 4458914.62661676,270780.81854858 4458914.21983449,270763.52289518 4458920.715993,270760.3449542 4458926.6570575,270763.78234766 4458958.22561242,270794.30290024 4458942.16424502,270807.08580285 4458940.1594565))'
|
2015-08-16 20:57:24 +02:00
|
|
|
self.assertEqual(wkt, unicode(feature.geometry().exportToWkt()))
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2013-03-25 00:00:40 +01:00
|
|
|
|
|
|
|
def suite():
|
2013-03-26 14:15:12 +01:00
|
|
|
suite = unittest.makeSuite(ModelerAlgorithmTest, 'test')
|
2013-03-25 00:00:40 +01:00
|
|
|
return suite
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-03-25 00:00:40 +01:00
|
|
|
def runtests():
|
2013-03-26 14:15:12 +01:00
|
|
|
result = unittest.TestResult()
|
2013-03-25 00:00:40 +01:00
|
|
|
testsuite = suite()
|
|
|
|
testsuite.run(result)
|
|
|
|
return result
|