Skip to content

Commit

Permalink
[processing] add unittest for gdal_polygonize algorithm
Browse files Browse the repository at this point in the history
(cherry picked from commit 6809d34)
  • Loading branch information
alexbruy committed Jan 2, 2019
1 parent 29ee893 commit ce591a7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion python/plugins/processing/algs/gdal/polygonize.py
Original file line number Diff line number Diff line change
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
51 changes: 51 additions & 0 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
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 @@ -2547,6 +2548,56 @@ def testGdal2Xyz(self):
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

0 comments on commit ce591a7

Please sign in to comment.