Skip to content
Permalink
Browse files

[processing][gdal] Dissolve field name should be optional

  • Loading branch information
nyalldawson committed Sep 25, 2018
1 parent a3af1f9 commit ea2c16dd64316327e9f5e12d6c930eab07f4872f
@@ -25,8 +25,7 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsProcessing,
QgsProcessingException,
from qgis.core import (QgsProcessingException,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
@@ -61,7 +60,7 @@ def initAlgorithm(self, config=None):
self.tr('Dissolve field'),
None,
self.INPUT,
QgsProcessingParameterField.Any))
QgsProcessingParameterField.Any, optional=True))
self.addParameter(QgsProcessingParameterString(self.GEOMETRY,
self.tr('Geometry column name'),
defaultValue='geometry'))
@@ -85,7 +84,7 @@ def initAlgorithm(self, config=None):
self.tr('Numeric attribute to calculate statistics on'),
None,
self.INPUT,
QgsProcessingParameterField.Any,
QgsProcessingParameterField.Numeric,
optional=True))
params.append(QgsProcessingParameterString(self.OPTIONS,
self.tr('Additional creation options'),
@@ -160,15 +159,17 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

params = ','.join(tokens)
if params:
if self.parameterAsBool(parameters, self.KEEP_ATTRIBUTES, context):
sql = "SELECT ST_Union({}) AS {}{}, {} FROM '{}' GROUP BY {}".format(geometry, geometry, other_fields, params, layerName, fieldName)
else:
sql = "SELECT ST_Union({}) AS {}, {}, {} FROM '{}' GROUP BY {}".format(geometry, geometry, fieldName, params, layerName, fieldName)
params = ', ' + params

group_by = ''
if fieldName:
group_by = ' GROUP BY {}'.format(fieldName)

if self.parameterAsBool(parameters, self.KEEP_ATTRIBUTES, context):
sql = "SELECT ST_Union({}) AS {}{}{} FROM '{}'{}".format(geometry, geometry, other_fields, params, layerName, group_by)
else:
if self.parameterAsBool(parameters, self.KEEP_ATTRIBUTES, context):
sql = "SELECT ST_Union({}) AS {}{} FROM '{}' GROUP BY {}".format(geometry, geometry, other_fields, layerName, fieldName)
else:
sql = "SELECT ST_Union({}) AS {}, {} FROM '{}' GROUP BY {}".format(geometry, geometry, fieldName, layerName, fieldName)
sql = "SELECT ST_Union({}) AS {}{}{} FROM '{}'{}".format(geometry, geometry, ', ' + fieldName if fieldName else '',
params, layerName, group_by)

arguments.append(sql)

@@ -621,6 +621,15 @@ def testDissolve(self):
alg.initAlgorithm()

with tempfile.TemporaryDirectory() as outdir:
self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'OUTPUT': outdir + '/check.shp'}, context, feedback),
['ogr2ogr',
outdir + '/check.shp ' +
source + ' ' +
'-dialect sqlite -sql "SELECT ST_Union(geometry) AS geometry FROM \'polys2\'" ' +
'-f "ESRI Shapefile"'])

self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'FIELD': 'my_field',
@@ -652,6 +661,27 @@ def testDissolve(self):
'-dialect sqlite -sql "SELECT ST_Union(the_geom) AS the_geom, my_field FROM \'polys2\' ' +
'GROUP BY my_field" -f "ESRI Shapefile"'])

self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'FIELD': 'my_field',
'KEEP_ATTRIBUTES': False,
'OUTPUT': outdir + '/check.shp'}, context, feedback),
['ogr2ogr',
outdir + '/check.shp ' +
source + ' ' +
'-dialect sqlite -sql "SELECT ST_Union(geometry) AS geometry, my_field FROM \'polys2\' ' +
'GROUP BY my_field" -f "ESRI Shapefile"'])

self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'KEEP_ATTRIBUTES': False,
'OUTPUT': outdir + '/check.shp'}, context, feedback),
['ogr2ogr',
outdir + '/check.shp ' +
source + ' ' +
'-dialect sqlite -sql "SELECT ST_Union(geometry) AS geometry FROM \'polys2\'" ' +
'-f "ESRI Shapefile"'])

self.assertEqual(
alg.getConsoleCommands({'INPUT': source,
'FIELD': 'my_field',

0 comments on commit ea2c16d

Please sign in to comment.
You can’t perform that action at this time.