Skip to content

Commit

Permalink
[processing] cleanup OGR dissolve algorithm code
Browse files Browse the repository at this point in the history
Avoid double extensions when creating temp files
  • Loading branch information
alexbruy committed Nov 12, 2016
1 parent 85d1fd7 commit b6d5f35
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
1 change: 0 additions & 1 deletion python/plugins/processing/algs/gdal/GdalAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def processAlgorithm(self, progress):

def shortHelp(self):
return self._formatHelp('''This algorithm is based on the GDAL %s module.
For more info, see the <a href = 'http://www.gdal.org/%s.html'> module help</a>
''' % (self.commandName(), self.commandName()))

Expand Down
68 changes: 36 additions & 32 deletions python/plugins/processing/algs/gdal/ogr2ogrdissolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,52 +85,56 @@ def defineCharacteristics(self):

def getConsoleCommands(self):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = ogrConnectionString(inLayer)[1:-1]
layername = "'" + ogrLayerName(inLayer) + "'"
geometry = str(self.getParameterValue(self.GEOMETRY))
field = str(self.getParameterValue(self.FIELD))
statsatt = str(self.getParameterValue(self.STATSATT))
stats = self.getParameterValue(self.STATS)
area = self.getParameterValue(self.AREA)
geometry = self.getParameterValue(self.GEOMETRY)
field = self.getParameterValue(self.FIELD)
multi = self.getParameterValue(self.MULTI)
count = self.getParameterValue(self.COUNT)
fields = self.getParameterValue(self.FIELDS)
querystart = '-dialect sqlite -sql "SELECT ST_Union(' + geometry + ')'
queryend = ' FROM ' + layername + ' GROUP BY ' + field + '"'
if fields:
queryfields = ",*"
else:
queryfields = "," + field
if count:
querycount = ", COUNT(" + geometry + ") AS count"
else:
querycount = ""
if stats:
querystats = ", SUM(" + statsatt + ") AS sum_diss, MIN(" + statsatt + ") AS min_diss, MAX(" + statsatt + ") AS max_diss, AVG(" + statsatt + ") AS avg_diss"
else:
querystats = ""
if area:
queryarea = ", SUM(ST_area(" + geometry + ")) AS area_diss, ST_perimeter(ST_union(" + geometry + ")) AS peri_diss"
else:
queryarea = ""
count = self.getParameterValue(self.COUNT)
area = self.getParameterValue(self.AREA)
stats = self.getParameterValue(self.STATS)
statsatt = self.getParameterValue(self.STATSATT)
options = self.getParameterValue(self.OPTIONS)

ogrLayer = ogrConnectionString(inLayer)[1:-1]
layername = ogrLayerName(inLayer)

query = querystart + queryfields + querycount + querystats + queryarea + queryend
output = self.getOutputFromName(self.OUTPUT_LAYER)
outFile = output.value

output = ogrConnectionString(outFile)
options = str(self.getParameterValue(self.OPTIONS))

arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(ogrLayerName(inLayer))
arguments.append(query)
arguments.append('-dialect')
arguments.append('sqlite')
arguments.append('-sql')

sql = "SELECT ST_Union('{}')".format(geometry)

sqlOpts = ''
if fields:
sqlOpts += ',*'
else:
sqlOpts += ',{}'.format(field)

if count:
sqlOpts += ", COUNT('{}') AS count".format(geometry)

if stats:
sqlOpts += ", SUM('{0}') AS sum_diss, MIN('{0}') AS min_diss, MAX('{0}') AS max_diss, AVG('{0}') AS avg_diss".format(statsatt)

if area:
sqlOpts += ", SUM(ST_Area('{0}')) AS area_diss, ST_Perimeter(ST_Union('{0}')) AS peri_diss".format(geometry)

sql = '{}{} FROM {} GROUP BY {}'.format(sql, sqlOpts, layername, field)

arguments.append(sql)

if not multi:
arguments.append('-explodecollections')

if len(options) > 0:
if options is not None and len(options) > 0:
arguments.append(options)

commands = []
Expand All @@ -143,4 +147,4 @@ def getConsoleCommands(self):
return commands

def commandName(self):
return "ogr2ogr"
return 'ogr2ogr'
2 changes: 1 addition & 1 deletion python/plugins/processing/tools/dataobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def exportVectorLayer(layer, supported=None):
basename = removeInvalidChars(os.path.basename(layer.source()))
if basename:
if not basename.endswith("shp"):
basename = basename + ".shp"
basename = os.path.splitext(basename)[0] + ".shp"
output = getTempFilenameInTempFolder(basename)
else:
output = getTempFilename("shp")
Expand Down

0 comments on commit b6d5f35

Please sign in to comment.