Skip to content

Commit

Permalink
Merge pull request #8776 from alexbruy/gdal_tests
Browse files Browse the repository at this point in the history
[processing] cover more GDAL algorithms with unittests
  • Loading branch information
alexbruy committed Jan 2, 2019
2 parents 196024c + f332233 commit 2f1efd8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 50 deletions.
4 changes: 3 additions & 1 deletion python/plugins/processing/algs/gdal/polygonize.py
Expand Up @@ -111,7 +111,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if outFormat:
arguments.append('-f {}'.format(outFormat))

arguments.append(GdalUtils.ogrLayerName(output))
layerName = GdalUtils.ogrLayerName(output)
if layerName:
arguments.append(layerName)
arguments.append(self.parameterAsString(parameters, self.FIELD, context))

commands = []
Expand Down
84 changes: 84 additions & 0 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Expand Up @@ -61,6 +61,8 @@
from processing.algs.gdal.rearrange_bands import rearrange_bands
from processing.algs.gdal.gdaladdo import gdaladdo
from processing.algs.gdal.sieve import sieve
from processing.algs.gdal.gdal2xyz import gdal2xyz
from processing.algs.gdal.polygonize import polygonize

from processing.tools.system import isWindows

Expand Down Expand Up @@ -2514,6 +2516,88 @@ def testSieve(self):
source + ' ' +
outsource])

def testGdal2Xyz(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'dem.tif')

with tempfile.TemporaryDirectory() as outdir:
outsource = outdir + '/check.csv'
alg = gdal2xyz()
alg.initAlgorithm()

# defaults
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'BAND': 1,
'CSV': False,
'OUTPUT': outsource}, context, feedback),
['gdal2xyz.py',
'-band 1 ' +
source + ' ' +
outsource])

# csv output
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'BAND': 1,
'CSV': True,
'OUTPUT': outsource}, context, feedback),
['gdal2xyz.py',
'-band 1 -csv ' +
source + ' ' +
outsource])

def testGdalPolygonize(self):
context = QgsProcessingContext()
feedback = QgsProcessingFeedback()
source = os.path.join(testDataPath, 'dem.tif')

with tempfile.TemporaryDirectory() as outdir:
outsource = outdir + '/check.shp'
alg = polygonize()
alg.initAlgorithm()

# defaults
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'BAND': 1,
'FIELD': 'DN',
'EIGHT_CONNECTEDNESS': False,
'OUTPUT': outsource}, context, feedback),
['gdal_polygonize.py',
source + ' ' +
outsource + ' ' +
'-b 1 -f "ESRI Shapefile" DN'
])

# 8 connectedness
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'BAND': 1,
'FIELD': 'DN',
'EIGHT_CONNECTEDNESS': True,
'OUTPUT': outsource}, context, feedback),
['gdal_polygonize.py',
source + ' ' +
outsource + ' ' +
'-8 -b 1 -f "ESRI Shapefile" DN'
])

# custom output format
outsource = outdir + '/check.gpkg'
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'BAND': 1,
'FIELD': 'DN',
'EIGHT_CONNECTEDNESS': False,
'OUTPUT': outsource}, context, feedback),
['gdal_polygonize.py',
source + ' ' +
outsource + ' ' +
'-b 1 -f "GPKG" DN'
])


class TestGdalOgrToPostGis(unittest.TestCase):

Expand Down
49 changes: 0 additions & 49 deletions python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml
Expand Up @@ -168,20 +168,6 @@ tests:
- 'Band 1 Block=373x5 Type=Float32, ColorInterp=Gray'
- ' NoData Value=-99999'

# Disabled as gdal2xyz.py is not available on Travis
# - algorithm: gdal:gdal2xyz
# name: gdal2xyz
# params:
# BAND: 1
# CSV: false
# INPUT:
# name: dem.tif
# type: raster
# results:
# OUTPUT:
# name: expected/gdal/xyz.csv
# type: file

- algorithm: gdal:tileindex
name: Tile index (gdaltindex)
params:
Expand Down Expand Up @@ -349,41 +335,6 @@ tests:
hash: fff4a08498e93494f3f2cf1a9074451e6fd68341849aedc9e2c45e6a
type: rasterhash

# Disabled as gdal_poligonize.py is not available on Travis
# - algorithm: gdal:polygonize
# name: Polygonize
# params:
# BAND: 1
# EIGHT_CONNECTEDNESS: false
# FIELD: DN
# INPUT:
# name: dem.tif
# type: raster
# results:
# OUTPUT:
# name: expected/gdal/polygonize.gml
# type: vector

# Disabled as gdal_proximity.py is not available on Travis
# - algorithm: gdal:proximity
# name: Proximity
# params:
# BAND: 1
# DATA_TYPE: 5
# INPUT:
# name: dem.tif
# type: raster
# MAX_DISTANCE: 0.0
# NODATA: 0.0
# OPTIONS: ''
# REPLACE: 0.0
# UNITS: 1
# VALUES: '90'
# results:
# OUTPUT:
# hash: 32802271d1ce083ca14078bfefaef6300ae8809af11f6a4270583d0c
# type: rasterhash

- algorithm: gdal:rasterize
name: Test (gdal:rasterize)
params:
Expand Down

0 comments on commit 2f1efd8

Please sign in to comment.