mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
[processing] restore dissolve algorithm
This commit is contained in:
parent
750e80f7de
commit
fb958df64f
164
python/plugins/processing/algs/gdal/Dissolve.py
Normal file
164
python/plugins/processing/algs/gdal/Dissolve.py
Normal file
@ -0,0 +1,164 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
***************************************************************************
|
||||
Dissolve.py
|
||||
---------------------
|
||||
Date : Janaury 2015
|
||||
Copyright : (C) 2015 by Giovanni Manghi
|
||||
Email : giovanni dot manghi at naturalgis dot pt
|
||||
***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************
|
||||
"""
|
||||
|
||||
__author__ = 'Giovanni Manghi'
|
||||
__date__ = 'January 2015'
|
||||
__copyright__ = '(C) 2015, Giovanni Manghi'
|
||||
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
from qgis.core import (QgsProcessing,
|
||||
QgsProcessingParameterDefinition,
|
||||
QgsProcessingParameterFeatureSource,
|
||||
QgsProcessingParameterField,
|
||||
QgsProcessingParameterString,
|
||||
QgsProcessingParameterBoolean,
|
||||
QgsProcessingParameterVectorDestination)
|
||||
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
||||
from processing.algs.gdal.GdalUtils import GdalUtils
|
||||
|
||||
|
||||
class Dissolve(GdalAlgorithm):
|
||||
|
||||
INPUT = 'INPUT'
|
||||
FIELD = 'FIELD'
|
||||
GEOMETRY = 'GEOMETRY'
|
||||
EXPLODE_COLLECTIONS = 'EXPLODE_COLLECTIONS'
|
||||
KEEP_ATTRIBUTES = 'KEEP_ATTRIBUTES'
|
||||
COUNT_FEATURES = 'COUNT_FEATURES'
|
||||
COMPUTE_AREA = 'COMPUTE_AREA'
|
||||
COMPUTE_STATISTICS = 'COMPUTE_STATISTICS'
|
||||
STATISTICS_ATTRIBUTE = 'STATISTICS_ATTRIBUTE'
|
||||
OPTIONS = 'OPTIONS'
|
||||
OUTPUT = 'OUTPUT'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def initAlgorithm(self, config=None):
|
||||
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
||||
self.tr('Input layer')))
|
||||
self.addParameter(QgsProcessingParameterField(self.FIELD,
|
||||
self.tr('Dissolve field'),
|
||||
None,
|
||||
self.INPUT,
|
||||
QgsProcessingParameterField.Any))
|
||||
self.addParameter(QgsProcessingParameterString(self.GEOMETRY,
|
||||
self.tr('Geometry column name'),
|
||||
defaultValue='geometry'))
|
||||
params = []
|
||||
params.append(QgsProcessingParameterBoolean(self.EXPLODE_COLLECTIONS,
|
||||
self.tr('Produce one feature for each geometry in any kind of geometry collection in the source file'),
|
||||
defaultValue=False))
|
||||
params.append(QgsProcessingParameterBoolean(self.KEEP_ATTRIBUTES,
|
||||
self.tr('Keep input attributes'),
|
||||
defaultValue=False))
|
||||
params.append(QgsProcessingParameterBoolean(self.COUNT_FEATURES,
|
||||
self.tr('Count dissolved features'),
|
||||
defaultValue=False))
|
||||
params.append(QgsProcessingParameterBoolean(self.COMPUTE_AREA,
|
||||
self.tr('Compute area and perimeter of dissolved features'),
|
||||
defaultValue=False))
|
||||
params.append(QgsProcessingParameterBoolean(self.COMPUTE_STATISTICS,
|
||||
self.tr('Compute min/max/sum/mean for attribute'),
|
||||
defaultValue=False))
|
||||
params.append(QgsProcessingParameterField(self.STATISTICS_ATTRIBUTE,
|
||||
self.tr('Numeric attribute to calculate statistics on'),
|
||||
None,
|
||||
self.INPUT,
|
||||
QgsProcessingParameterField.Any,
|
||||
optional=True))
|
||||
params.append(QgsProcessingParameterString(self.OPTIONS,
|
||||
self.tr('Additional creation options'),
|
||||
defaultValue='',
|
||||
optional=True))
|
||||
for param in params:
|
||||
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
||||
self.addParameter(param)
|
||||
|
||||
self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUT,
|
||||
self.tr('Buffer'),
|
||||
QgsProcessing.TypeVectorPolygon))
|
||||
|
||||
def name(self):
|
||||
return 'dissolve'
|
||||
|
||||
def displayName(self):
|
||||
return self.tr('Dissolve')
|
||||
|
||||
def group(self):
|
||||
return self.tr('Vector geoprocessing')
|
||||
|
||||
def commandName(self):
|
||||
return 'ogr2ogr'
|
||||
|
||||
def getConsoleCommands(self, parameters, context, feedback):
|
||||
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback)
|
||||
geometry = self.parameterAsString(parameters, self.GEOMETRY, context)
|
||||
fieldName = self.parameterAsString(parameters, self.FIELD, context)
|
||||
|
||||
options = self.parameterAsString(parameters, self.OPTIONS, context)
|
||||
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
|
||||
|
||||
output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
|
||||
|
||||
arguments = []
|
||||
arguments.append(output)
|
||||
arguments.append(ogrLayer)
|
||||
arguments.append('-dialect')
|
||||
arguments.append('sqlite')
|
||||
arguments.append('-sql')
|
||||
|
||||
tokens = []
|
||||
if self.parameterAsBool(parameters, self.COUNT_FEATURES, context):
|
||||
tokens.append("COUNT({}) AS count".format(geometry))
|
||||
|
||||
if self.parameterAsBool(parameters, self.COMPUTE_AREA, context):
|
||||
tokens.append("SUM(ST_Area({0})) AS area, ST_Perimeter(ST_Union({0})) AS perimeter".format(geometry))
|
||||
|
||||
statsField = self.parameterAsString(parameters, self.FIELD, context)
|
||||
if statsField and self.parameterAsBool(parameters, self.COMPUTE_STATISTICS, context):
|
||||
tokens.append("SUM({0}) AS sum, MIN({0}) AS min, MAX({0}) AS max, AVG({0}) AS avg".format(statsField))
|
||||
|
||||
params = ','.join(tokens)
|
||||
if params:
|
||||
if self.parameterAsBool(parameters, self.KEEP_ATTRIBUTES, context):
|
||||
sql = "SELECT ST_Union({}), *, {} FROM {} GROUP BY {}".format(geometry, params, layerName, fieldName)
|
||||
else:
|
||||
sql = "SELECT ST_Union({}), {}, {} FROM {} GROUP BY {}".format(geometry, fieldName, params, layerName, fieldName)
|
||||
else:
|
||||
if self.parameterAsBool(parameters, self.KEEP_ATTRIBUTES, context):
|
||||
sql = "SELECT ST_Union({}), * FROM {} GROUP BY {}".format(geometry, layerName, fieldName)
|
||||
else:
|
||||
sql = "SELECT ST_Union({}), {} FROM {} GROUP BY {}".format(geometry, fieldName, layerName, fieldName)
|
||||
|
||||
arguments.append(sql)
|
||||
|
||||
if self.parameterAsBool(parameters, self.EXPLODE_COLLECTIONS, context):
|
||||
arguments.append('-explodecollections')
|
||||
|
||||
if options:
|
||||
arguments.append(options)
|
||||
|
||||
if outputFormat:
|
||||
arguments.append('-f {}'.format(outputFormat))
|
||||
|
||||
return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
|
@ -76,6 +76,7 @@ from .warp import warp
|
||||
from .Buffer import Buffer
|
||||
from .ClipVectorByExtent import ClipVectorByExtent
|
||||
from .ClipVectorByMask import ClipVectorByMask
|
||||
from .Dissolve import Dissolve
|
||||
from .OffsetCurve import OffsetCurve
|
||||
from .ogr2ogr import ogr2ogr
|
||||
from .ogrinfo import ogrinfo
|
||||
@ -84,7 +85,6 @@ from .OneSideBuffer import OneSideBuffer
|
||||
from .PointsAlongLines import PointsAlongLines
|
||||
|
||||
# from .ogr2ogrtopostgislist import Ogr2OgrToPostGisList
|
||||
# from .ogr2ogrdissolve import Ogr2OgrDissolve
|
||||
# from .ogr2ogrtabletopostgislist import Ogr2OgrTableToPostGisList
|
||||
# from .ogrsql import OgrSql
|
||||
|
||||
@ -178,6 +178,7 @@ class GdalAlgorithmProvider(QgsProcessingProvider):
|
||||
Buffer(),
|
||||
ClipVectorByExtent(),
|
||||
ClipVectorByMask(),
|
||||
Dissolve(),
|
||||
OffsetCurve(),
|
||||
ogr2ogr(),
|
||||
ogrinfo(),
|
||||
@ -185,7 +186,6 @@ class GdalAlgorithmProvider(QgsProcessingProvider):
|
||||
OneSideBuffer(),
|
||||
PointsAlongLines(),
|
||||
# Ogr2OgrToPostGisList(),
|
||||
# Ogr2OgrDissolve(),
|
||||
# Ogr2OgrTableToPostGisList(),
|
||||
# OgrSql(),
|
||||
]
|
||||
|
@ -1,157 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
***************************************************************************
|
||||
ogr2ogrdissolve.py
|
||||
---------------------
|
||||
Date : Janaury 2015
|
||||
Copyright : (C) 2015 by Giovanni Manghi
|
||||
Email : giovanni dot manghi at naturalgis dot pt
|
||||
***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************
|
||||
"""
|
||||
|
||||
__author__ = 'Giovanni Manghi'
|
||||
__date__ = 'January 2015'
|
||||
__copyright__ = '(C) 2015, Giovanni Manghi'
|
||||
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
from processing.core.parameters import ParameterVector
|
||||
from processing.core.parameters import ParameterString
|
||||
from processing.core.parameters import ParameterBoolean
|
||||
from processing.core.parameters import ParameterTableField
|
||||
from processing.core.outputs import OutputVector
|
||||
|
||||
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
||||
from processing.algs.gdal.GdalUtils import GdalUtils
|
||||
|
||||
from processing.tools import dataobjects
|
||||
from processing.tools.system import isWindows
|
||||
|
||||
|
||||
class Ogr2OgrDissolve(GdalAlgorithm):
|
||||
|
||||
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
||||
INPUT_LAYER = 'INPUT_LAYER'
|
||||
GEOMETRY = 'GEOMETRY'
|
||||
FIELD = 'FIELD'
|
||||
MULTI = 'MULTI'
|
||||
COUNT = 'COUNT'
|
||||
STATS = 'STATS'
|
||||
STATSATT = 'STATSATT'
|
||||
AREA = 'AREA'
|
||||
FIELDS = 'FIELDS'
|
||||
OPTIONS = 'OPTIONS'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def initAlgorithm(self, config=None):
|
||||
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
||||
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POLYGON]))
|
||||
self.addParameter(ParameterString(self.GEOMETRY,
|
||||
self.tr('Geometry column name ("geometry" for Shapefiles, may be different for other formats)'),
|
||||
'geometry', optional=False))
|
||||
self.addParameter(ParameterTableField(self.FIELD,
|
||||
self.tr('Dissolve field'), self.INPUT_LAYER))
|
||||
self.addParameter(ParameterBoolean(self.MULTI,
|
||||
self.tr('Output as multipart geometries'), True))
|
||||
self.addParameter(ParameterBoolean(self.FIELDS,
|
||||
self.tr('Keep input attributes'), False))
|
||||
self.addParameter(ParameterBoolean(self.COUNT,
|
||||
self.tr('Count dissolved features'), False))
|
||||
self.addParameter(ParameterBoolean(self.AREA,
|
||||
self.tr('Compute area and perimeter of dissolved features'), False))
|
||||
self.addParameter(ParameterBoolean(self.STATS,
|
||||
self.tr('Compute min/max/sum/mean for the following numeric attribute'), False))
|
||||
self.addParameter(ParameterTableField(self.STATSATT,
|
||||
self.tr('Numeric attribute to compute dissolved features stats'), self.INPUT_LAYER, optional=True))
|
||||
self.addParameter(ParameterString(self.OPTIONS,
|
||||
self.tr('Additional creation options (see ogr2ogr manual)'),
|
||||
'', optional=True))
|
||||
|
||||
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Dissolved'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
|
||||
|
||||
def name(self):
|
||||
return 'dissolvepolygons'
|
||||
|
||||
def displayName(self):
|
||||
return self.tr('Dissolve polygons')
|
||||
|
||||
def group(self):
|
||||
return self.tr('Vector geoprocessing')
|
||||
|
||||
def getConsoleCommands(self, parameters, context, feedback):
|
||||
inLayer = self.getParameterValue(self.INPUT_LAYER)
|
||||
geometry = self.getParameterValue(self.GEOMETRY)
|
||||
field = self.getParameterValue(self.FIELD)
|
||||
multi = self.getParameterValue(self.MULTI)
|
||||
fields = self.getParameterValue(self.FIELDS)
|
||||
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 = GdalUtils.ogrConnectionString(inLayer, context)[1:-1]
|
||||
layername = GdalUtils.ogrLayerName(inLayer)
|
||||
|
||||
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
||||
outFile = output.value
|
||||
|
||||
output = GdalUtils.ogrConnectionString(outFile, context)
|
||||
|
||||
arguments = []
|
||||
arguments.append(output)
|
||||
arguments.append(ogrLayer)
|
||||
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 options is not None and len(options.strip()) > 0:
|
||||
arguments.append(options)
|
||||
|
||||
commands = []
|
||||
if isWindows():
|
||||
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
|
||||
GdalUtils.escapeAndJoin(arguments)]
|
||||
else:
|
||||
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
|
||||
|
||||
return commands
|
||||
|
||||
def commandName(self):
|
||||
return 'ogr2ogr'
|
Loading…
x
Reference in New Issue
Block a user