Skip to content

Commit

Permalink
Merge pull request #3701 from alexbruy/processing-ogr-tests
Browse files Browse the repository at this point in the history
[processsing] tests for GDAL algorithms
  • Loading branch information
alexbruy authored Nov 14, 2016
2 parents 798bc09 + d81533e commit 6e9631e
Show file tree
Hide file tree
Showing 110 changed files with 903 additions and 1,975 deletions.
1 change: 0 additions & 1 deletion python/plugins/processing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from processing.tools.vector import * # NOQA
from processing.tools.raster import * # NOQA
from processing.tools.system import * # NOQA
from processing.tests.TestData import loadTestData # NOQA


def classFactory(iface):
Expand Down
1 change: 0 additions & 1 deletion python/plugins/processing/algs/gdal/GdalAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def processAlgorithm(self, progress):

def shortHelp(self):
return self._formatHelp('''This algorithm is based on the GDAL %s module.
For more info, see the <a href = 'http://www.gdal.org/%s.html'> module help</a>
''' % (self.commandName(), self.commandName()))

Expand Down
19 changes: 8 additions & 11 deletions python/plugins/processing/algs/gdal/offsetcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterSelection
Expand All @@ -46,8 +47,6 @@ class OffsetCurve(GdalAlgorithm):
INPUT_LAYER = 'INPUT_LAYER'
GEOMETRY = 'GEOMETRY'
RADIUS = 'RADIUS'
LEFTRIGHT = 'LEFTRIGHT'
LEFTRIGHTLIST = ['Right', 'Left']
DISSOLVEALL = 'DISSOLVEALL'
FIELD = 'FIELD'
MULTI = 'MULTI'
Expand All @@ -62,10 +61,10 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.GEOMETRY,
self.tr('Geometry column name ("geometry" for Shapefiles, may be different for other formats)'),
'geometry', optional=False))
self.addParameter(ParameterString(self.RADIUS,
self.tr('Offset distance'), '1000', optional=False))
self.addParameter(ParameterSelection(self.LEFTRIGHT,
self.tr('Offset side'), self.LEFTRIGHTLIST, 0))
self.addParameter(ParameterNumber(self.RADIUS,
self.tr('Offset distance (positive value for left-sided and negative - for right-sided)'),
-99999999.999999, 99999999.999999, 1000.0,
optional=False))
self.addParameter(ParameterBoolean(self.DISSOLVEALL,
self.tr('Dissolve all results'), False))
self.addParameter(ParameterTableField(self.FIELD,
Expand All @@ -82,7 +81,6 @@ def getConsoleCommands(self):
inLayer = self.getParameterValue(self.INPUT_LAYER)
geometry = self.getParameterValue(self.GEOMETRY)
distance = self.getParameterValue(self.RADIUS)
leftright = self.getParameterValue(self.LEFTRIGHT)
dissolveall = self.getParameterValue(self.DISSOLVEALL)
field = self.getParameterValue(self.FIELD)
multi = self.getParameterValue(self.MULTI)
Expand All @@ -100,15 +98,14 @@ def getConsoleCommands(self):
arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(layername)
arguments.append('-dialect')
arguments.append('sqlite')
arguments.append('-sql')

if dissolveall or field is not None:
sql = "SELECT ST_Union(ST_OffsetCurve({}, {}, {})) * FROM '{}'".format(geometry, distance, leftright, layername)
sql = "SELECT ST_Union(ST_OffsetCurve({}, {})) * FROM '{}'".format(geometry, distance, layername)
else:
sql = "SELECT ST_OffsetCurve({}, {}, {}), * FROM '{}'".format(geometry, distance, leftright, layername)
sql = "SELECT ST_OffsetCurve({}, {}), * FROM '{}'".format(geometry, distance, layername)

if field is not None:
sql = '"{} GROUP BY {}"'.format(sql, field)
Expand All @@ -118,7 +115,7 @@ def getConsoleCommands(self):
if field is not None and multi:
arguments.append('-explodecollections')

if len(options) > 0:
if options is not None and len(options.strip()) > 0:
arguments.append(options)

commands = []
Expand Down
12 changes: 7 additions & 5 deletions python/plugins/processing/algs/gdal/ogr2ogrbuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputVector
Expand Down Expand Up @@ -59,8 +60,10 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.GEOMETRY,
self.tr('Geometry column name ("geometry" for Shapefiles, may be different for other formats)'),
'geometry', optional=False))
self.addParameter(ParameterString(self.DISTANCE,
self.tr('Buffer distance'), '1000', optional=False))
self.addParameter(ParameterNumber(self.DISTANCE,
self.tr('Buffer distance'),
0.0, 99999999.999999, 1000.0,
optional=False))
self.addParameter(ParameterBoolean(self.DISSOLVEALL,
self.tr('Dissolve all results'), False))
self.addParameter(ParameterTableField(self.FIELD,
Expand Down Expand Up @@ -93,7 +96,6 @@ def getConsoleCommands(self):
arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(ogrLayerName(inLayer))
arguments.append('-dialect')
arguments.append('sqlite')
arguments.append('-sql')
Expand All @@ -111,7 +113,7 @@ def getConsoleCommands(self):
if field is not None and multi:
arguments.append('-explodecollections')

if len(options) > 0:
if options is not None and len(options) > 0:
arguments.append(options)

commands = []
Expand All @@ -124,4 +126,4 @@ def getConsoleCommands(self):
return commands

def commandName(self):
return "ogr2ogr"
return 'ogr2ogr'
68 changes: 36 additions & 32 deletions python/plugins/processing/algs/gdal/ogr2ogrdissolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,52 +85,56 @@ def defineCharacteristics(self):

def getConsoleCommands(self):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = ogrConnectionString(inLayer)[1:-1]
layername = "'" + ogrLayerName(inLayer) + "'"
geometry = str(self.getParameterValue(self.GEOMETRY))
field = str(self.getParameterValue(self.FIELD))
statsatt = str(self.getParameterValue(self.STATSATT))
stats = self.getParameterValue(self.STATS)
area = self.getParameterValue(self.AREA)
geometry = self.getParameterValue(self.GEOMETRY)
field = self.getParameterValue(self.FIELD)
multi = self.getParameterValue(self.MULTI)
count = self.getParameterValue(self.COUNT)
fields = self.getParameterValue(self.FIELDS)
querystart = '-dialect sqlite -sql "SELECT ST_Union(' + geometry + ')'
queryend = ' FROM ' + layername + ' GROUP BY ' + field + '"'
if fields:
queryfields = ",*"
else:
queryfields = "," + field
if count:
querycount = ", COUNT(" + geometry + ") AS count"
else:
querycount = ""
if stats:
querystats = ", SUM(" + statsatt + ") AS sum_diss, MIN(" + statsatt + ") AS min_diss, MAX(" + statsatt + ") AS max_diss, AVG(" + statsatt + ") AS avg_diss"
else:
querystats = ""
if area:
queryarea = ", SUM(ST_area(" + geometry + ")) AS area_diss, ST_perimeter(ST_union(" + geometry + ")) AS peri_diss"
else:
queryarea = ""
count = self.getParameterValue(self.COUNT)
area = self.getParameterValue(self.AREA)
stats = self.getParameterValue(self.STATS)
statsatt = self.getParameterValue(self.STATSATT)
options = self.getParameterValue(self.OPTIONS)

ogrLayer = ogrConnectionString(inLayer)[1:-1]
layername = ogrLayerName(inLayer)

query = querystart + queryfields + querycount + querystats + queryarea + queryend
output = self.getOutputFromName(self.OUTPUT_LAYER)
outFile = output.value

output = ogrConnectionString(outFile)
options = str(self.getParameterValue(self.OPTIONS))

arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(ogrLayerName(inLayer))
arguments.append(query)
arguments.append('-dialect')
arguments.append('sqlite')
arguments.append('-sql')

sql = "SELECT ST_Union('{}')".format(geometry)

sqlOpts = ''
if fields:
sqlOpts += ',*'
else:
sqlOpts += ',{}'.format(field)

if count:
sqlOpts += ", COUNT('{}') AS count".format(geometry)

if stats:
sqlOpts += ", SUM('{0}') AS sum_diss, MIN('{0}') AS min_diss, MAX('{0}') AS max_diss, AVG('{0}') AS avg_diss".format(statsatt)

if area:
sqlOpts += ", SUM(ST_Area('{0}')) AS area_diss, ST_Perimeter(ST_Union('{0}')) AS peri_diss".format(geometry)

sql = '{}{} FROM {} GROUP BY {}'.format(sql, sqlOpts, layername, field)

arguments.append(sql)

if not multi:
arguments.append('-explodecollections')

if len(options) > 0:
if options is not None and len(options) > 0:
arguments.append(options)

commands = []
Expand All @@ -143,4 +147,4 @@ def getConsoleCommands(self):
return commands

def commandName(self):
return "ogr2ogr"
return 'ogr2ogr'
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def getConsoleCommands(self):
arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(ogrLayerName(inLayer))

arguments.append('-dialect sqlite -sql "SELECT ST_Line_Interpolate_Point(')
arguments.append(geometry)
Expand Down
10 changes: 6 additions & 4 deletions python/plugins/processing/algs/gdal/onesidebuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterSelection
Expand Down Expand Up @@ -62,8 +63,10 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.GEOMETRY,
self.tr('Geometry column name ("geometry" for Shapefiles, may be different for other formats)'),
'geometry', optional=False))
self.addParameter(ParameterString(self.RADIUS,
self.tr('Buffer distance'), '1000', optional=False))
self.addParameter(ParameterNumber(self.RADIUS,
self.tr('Buffer distance'),
0.0, 99999999.999999, 1000.0,
optional=False))
self.addParameter(ParameterSelection(self.LEFTRIGHT,
self.tr('Buffer side'), self.LEFTRIGHTLIST, 0))
self.addParameter(ParameterBoolean(self.DISSOLVEALL,
Expand Down Expand Up @@ -100,7 +103,6 @@ def getConsoleCommands(self):
arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(layername)
arguments.append('-dialect')
arguments.append('sqlite')
arguments.append('-sql')
Expand All @@ -118,7 +120,7 @@ def getConsoleCommands(self):
if field is not None and multi:
arguments.append('-explodecollections')

if len(options) > 0:
if options is not None and len(options.strip()) > 0:
arguments.append(options)

commands = []
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass/GrassUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def checkGrassIsInstalled(ignorePreviousState=False):
points(),
False,
False,
'270778.60198,270855.745301,4458921.97814,4458983.8488',
'None',
-1,
0.0001,
0,
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/Grass7Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def checkGrass7IsInstalled(ignorePreviousState=False):
points(),
False,
False,
'270778.60198,270855.745301,4458921.97814,4458983.8488',
'None',
-1,
0.0001,
0,
Expand Down
1 change: 0 additions & 1 deletion python/plugins/processing/tests/AlgorithmsTestBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def test_algorithms(self):
This is the main test function. All others will be executed based on the definitions in testdata/algorithm_tests.yaml
"""
ver = processing.version()
print("Processing {}.{}.{}".format(ver / 10000, ver / 100 % 100, ver % 100))
with open(os.path.join(processingTestDataPath(), self.test_definition_file()), 'r') as stream:
algorithm_tests = yaml.load(stream)

Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FILE(GLOB PY_FILES *.py)
FILE(GLOB TEST_DATA_FILES data/*)
FILE(GLOB TEST_DATA_FILES testdata/points.* testdata/table.dbf)

PLUGIN_INSTALL(processing tests ${PY_FILES})
PLUGIN_INSTALL(processing tests/data ${TEST_DATA_FILES})
PLUGIN_INSTALL(processing tests/testdata ${TEST_DATA_FILES})

IF(ENABLE_TESTS)
INCLUDE(UsePythonTest)
Expand Down
28 changes: 14 additions & 14 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,32 @@ def test_getConnectionString(self):
cs = obj.getConnectionString()
# NOTE: defaults are debatable, see
# https://github.com/qgis/QGIS/pull/3607#issuecomment-253971020
self.assertEquals(obj.getConnectionString(),
"host=localhost port=5432 active_schema=public")
self.assertEqual(obj.getConnectionString(),
"host=localhost port=5432 active_schema=public")

obj.setParameterValue('HOST', 'remote')
self.assertEquals(obj.getConnectionString(),
"host=remote port=5432 active_schema=public")
self.assertEqual(obj.getConnectionString(),
"host=remote port=5432 active_schema=public")

obj.setParameterValue('HOST', '')
self.assertEquals(obj.getConnectionString(),
"port=5432 active_schema=public")
self.assertEqual(obj.getConnectionString(),
"port=5432 active_schema=public")

obj.setParameterValue('PORT', '5555')
self.assertEquals(obj.getConnectionString(),
"port=5555 active_schema=public")
self.assertEqual(obj.getConnectionString(),
"port=5555 active_schema=public")

obj.setParameterValue('PORT', '')
self.assertEquals(obj.getConnectionString(),
"active_schema=public")
self.assertEqual(obj.getConnectionString(),
"active_schema=public")

obj.setParameterValue('USER', 'usr')
self.assertEquals(obj.getConnectionString(),
"active_schema=public user=usr")
self.assertEqual(obj.getConnectionString(),
"active_schema=public user=usr")

obj.setParameterValue('PASSWORD', 'pwd')
self.assertEquals(obj.getConnectionString(),
"password=pwd active_schema=public user=usr")
self.assertEqual(obj.getConnectionString(),
"password=pwd active_schema=public user=usr")


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/tests/ParametersTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
ParameterExpression,
getParameterFromString)
from processing.tools import dataobjects
from processing.tests.TestData import points2
from processing.tests.TestData import points

from qgis.core import (QgsRasterLayer,
QgsVectorLayer)
Expand Down Expand Up @@ -608,7 +608,7 @@ class ParameterTableFieldTest(unittest.TestCase):

def testOptional(self):
parent_name = 'test_parent_layer'
test_data = points2()
test_data = points()
test_layer = QgsVectorLayer(test_data, parent_name, 'ogr')
parent = ParameterVector(parent_name, parent_name)
parent.setValue(test_layer)
Expand Down
Loading

0 comments on commit 6e9631e

Please sign in to comment.