Skip to content

Commit ce591a7

Browse files
committedJan 2, 2019
[processing] add unittest for gdal_polygonize algorithm
(cherry picked from commit 6809d34)
1 parent 29ee893 commit ce591a7

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed
 

‎python/plugins/processing/algs/gdal/polygonize.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
111111
if outFormat:
112112
arguments.append('-f {}'.format(outFormat))
113113

114-
arguments.append(GdalUtils.ogrLayerName(output))
114+
layerName = GdalUtils.ogrLayerName(output)
115+
if layerName:
116+
arguments.append(layerName)
115117
arguments.append(self.parameterAsString(parameters, self.FIELD, context))
116118

117119
commands = []

‎python/plugins/processing/tests/GdalAlgorithmsTest.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
from processing.algs.gdal.gdaladdo import gdaladdo
6363
from processing.algs.gdal.sieve import sieve
6464
from processing.algs.gdal.gdal2xyz import gdal2xyz
65+
from processing.algs.gdal.polygonize import polygonize
6566

6667
from processing.tools.system import isWindows
6768

@@ -2547,6 +2548,56 @@ def testGdal2Xyz(self):
25472548
source + ' ' +
25482549
outsource])
25492550

2551+
def testGdalPolygonize(self):
2552+
context = QgsProcessingContext()
2553+
feedback = QgsProcessingFeedback()
2554+
source = os.path.join(testDataPath, 'dem.tif')
2555+
2556+
with tempfile.TemporaryDirectory() as outdir:
2557+
outsource = outdir + '/check.shp'
2558+
alg = polygonize()
2559+
alg.initAlgorithm()
2560+
2561+
# defaults
2562+
self.assertEqual(
2563+
alg.getConsoleCommands({'INPUT': source,
2564+
'BAND': 1,
2565+
'FIELD': 'DN',
2566+
'EIGHT_CONNECTEDNESS': False,
2567+
'OUTPUT': outsource}, context, feedback),
2568+
['gdal_polygonize.py',
2569+
source + ' ' +
2570+
outsource + ' ' +
2571+
'-b 1 -f "ESRI Shapefile" DN'
2572+
])
2573+
2574+
# 8 connectedness
2575+
self.assertEqual(
2576+
alg.getConsoleCommands({'INPUT': source,
2577+
'BAND': 1,
2578+
'FIELD': 'DN',
2579+
'EIGHT_CONNECTEDNESS': True,
2580+
'OUTPUT': outsource}, context, feedback),
2581+
['gdal_polygonize.py',
2582+
source + ' ' +
2583+
outsource + ' ' +
2584+
'-8 -b 1 -f "ESRI Shapefile" DN'
2585+
])
2586+
2587+
# custom output format
2588+
outsource = outdir + '/check.gpkg'
2589+
self.assertEqual(
2590+
alg.getConsoleCommands({'INPUT': source,
2591+
'BAND': 1,
2592+
'FIELD': 'DN',
2593+
'EIGHT_CONNECTEDNESS': False,
2594+
'OUTPUT': outsource}, context, feedback),
2595+
['gdal_polygonize.py',
2596+
source + ' ' +
2597+
outsource + ' ' +
2598+
'-b 1 -f "GPKG" DN'
2599+
])
2600+
25502601

25512602
class TestGdalOgrToPostGis(unittest.TestCase):
25522603

0 commit comments

Comments
 (0)
Please sign in to comment.