Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing][gdal] Fix polygonize field name is ignored
(cherry picked from commit 06d5b99)
  • Loading branch information
nyalldawson committed Feb 7, 2019
1 parent 331f590 commit ae21d07
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
8 changes: 5 additions & 3 deletions python/plugins/processing/algs/gdal/GdalUtils.py
Expand Up @@ -353,11 +353,13 @@ def ogrConnectionStringAndFormatFromLayer(layer):
return ogrstr, '"' + format + '"'

@staticmethod
def ogrLayerName(uri):
def ogrOutputLayerName(uri):
uri = uri.strip('"')
#if os.path.isfile(uri):
# return os.path.basename(os.path.splitext(uri)[0])
return os.path.basename(os.path.splitext(uri)[0])

@staticmethod
def ogrLayerName(uri):
uri = uri.strip('"')
if ' table=' in uri:
# table="schema"."table"
re_table_schema = re.compile(' table="([^"]*)"\\."([^"]*)"')
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/polygonize.py
Expand Up @@ -111,7 +111,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if outFormat:
arguments.append('-f {}'.format(outFormat))

layerName = GdalUtils.ogrLayerName(output)
layerName = GdalUtils.ogrOutputLayerName(output)
if layerName:
arguments.append(layerName)
arguments.append(self.parameterAsString(parameters, self.FIELD, context))
Expand Down
24 changes: 21 additions & 3 deletions python/plugins/processing/tests/GdalAlgorithmsTest.py
Expand Up @@ -258,6 +258,12 @@ def testGetOgrCompatibleSourceFromFeatureSource(self):

QgsProject.instance().removeMapLayer(layer)

def testOgrOutputLayerName(self):
self.assertEqual(GdalUtils.ogrOutputLayerName('/home/me/out.shp'), 'out')
self.assertEqual(GdalUtils.ogrOutputLayerName('d:/test/test_out.shp'), 'test_out')
self.assertEqual(GdalUtils.ogrOutputLayerName('d:/test/TEST_OUT.shp'), 'TEST_OUT')
self.assertEqual(GdalUtils.ogrOutputLayerName('d:/test/test_out.gpkg'), 'test_out')

def testOgrLayerNameExtraction(self):
with tempfile.TemporaryDirectory() as outdir:
def _copyFile(dst):
Expand Down Expand Up @@ -2660,7 +2666,19 @@ def testGdalPolygonize(self):
['gdal_polygonize.py',
source + ' ' +
outsource + ' ' +
'-b 1 -f "ESRI Shapefile" DN'
'-b 1 -f "ESRI Shapefile" check DN'
])

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

# 8 connectedness
Expand All @@ -2673,7 +2691,7 @@ def testGdalPolygonize(self):
['gdal_polygonize.py',
source + ' ' +
outsource + ' ' +
'-8 -b 1 -f "ESRI Shapefile" DN'
'-8 -b 1 -f "ESRI Shapefile" check DN'
])

# custom output format
Expand All @@ -2687,7 +2705,7 @@ def testGdalPolygonize(self):
['gdal_polygonize.py',
source + ' ' +
outsource + ' ' +
'-b 1 -f "GPKG" DN'
'-b 1 -f "GPKG" check DN'
])


Expand Down

0 comments on commit ae21d07

Please sign in to comment.