2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Dissolve.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* 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__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2015-10-20 09:23:12 +02:00
|
|
|
from qgis.core import QgsFeature, QgsGeometry
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2016-03-21 21:30:23 +01:00
|
|
|
from processing.core.ProcessingLog import ProcessingLog
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
2015-10-20 09:23:12 +02:00
|
|
|
from processing.core.parameters import ParameterBoolean
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.outputs import OutputVector
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import vector, dataobjects
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
class Dissolve(GeoAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
FIELD = 'FIELD'
|
2015-10-20 09:23:12 +02:00
|
|
|
DISSOLVE_ALL = 'DISSOLVE_ALL'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
def getIcon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'dissolve.png'))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2016-02-17 09:36:59 +02:00
|
|
|
def defineCharacteristics(self):
|
|
|
|
self.name = 'Dissolve'
|
|
|
|
self.group = 'Vector geometry tools'
|
|
|
|
self.addParameter(ParameterVector(Dissolve.INPUT,
|
|
|
|
self.tr('Input layer'),
|
|
|
|
[ParameterVector.VECTOR_TYPE_POLYGON, ParameterVector.VECTOR_TYPE_LINE]))
|
|
|
|
self.addParameter(ParameterBoolean(Dissolve.DISSOLVE_ALL,
|
|
|
|
self.tr('Dissolve all (do not use field)'), True))
|
|
|
|
self.addParameter(ParameterTableField(Dissolve.FIELD,
|
|
|
|
self.tr('Unique ID field'), Dissolve.INPUT, optional=True))
|
|
|
|
self.addOutput(OutputVector(Dissolve.OUTPUT, self.tr('Dissolved')))
|
|
|
|
|
2013-01-12 23:36:00 +01:00
|
|
|
def processAlgorithm(self, progress):
|
2015-10-20 09:23:12 +02:00
|
|
|
useField = not self.getParameterValue(Dissolve.DISSOLVE_ALL)
|
|
|
|
fieldname = self.getParameterValue(Dissolve.FIELD)
|
2013-10-01 20:52:22 +03:00
|
|
|
vlayerA = dataobjects.getObjectFromUri(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
self.getParameterValue(Dissolve.INPUT))
|
2013-02-07 01:09:39 +01:00
|
|
|
vproviderA = vlayerA.dataProvider()
|
2015-11-11 16:20:59 +01:00
|
|
|
fields = vlayerA.fields()
|
2013-10-01 20:52:22 +03:00
|
|
|
writer = self.getOutputFromName(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
Dissolve.OUTPUT).getVectorWriter(fields,
|
|
|
|
vproviderA.geometryType(),
|
|
|
|
vproviderA.crs())
|
2013-02-07 01:09:39 +01:00
|
|
|
outFeat = QgsFeature()
|
2015-11-11 16:20:59 +01:00
|
|
|
features = vector.features(vlayerA)
|
2016-02-17 09:36:59 +02:00
|
|
|
total = 100.0 / len(features)
|
2015-11-11 16:20:59 +01:00
|
|
|
|
2012-12-28 00:35:36 +01:00
|
|
|
if not useField:
|
2012-09-15 18:25:25 +03:00
|
|
|
first = True
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, inFeat in enumerate(features):
|
|
|
|
progress.setPercentage(int(current * total))
|
2012-12-28 00:35:36 +01:00
|
|
|
if first:
|
2015-10-20 09:23:12 +02:00
|
|
|
attrs = inFeat.attributes()
|
2016-08-01 16:25:46 +10:00
|
|
|
tmpInGeom = inFeat.geometry()
|
|
|
|
if tmpInGeom.isEmpty() or tmpInGeom.isGeosEmpty():
|
2016-03-21 21:30:23 +01:00
|
|
|
continue
|
|
|
|
errors = tmpInGeom.validateGeometry()
|
|
|
|
if len(errors) != 0:
|
|
|
|
for error in errors:
|
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
|
2016-03-22 22:01:21 +01:00
|
|
|
self.tr('ValidateGeometry()'
|
|
|
|
'error: One or more '
|
|
|
|
'input features have '
|
|
|
|
'invalid geometry: ')
|
|
|
|
+ error.what())
|
2016-01-18 14:20:41 +02:00
|
|
|
continue
|
2015-10-20 09:23:12 +02:00
|
|
|
outFeat.setGeometry(tmpInGeom)
|
2012-09-15 18:25:25 +03:00
|
|
|
first = False
|
2012-12-28 00:35:36 +01:00
|
|
|
else:
|
2016-08-01 16:25:46 +10:00
|
|
|
tmpInGeom = inFeat.geometry()
|
|
|
|
if tmpInGeom.isEmpty() or tmpInGeom.isGeosEmpty():
|
2016-01-18 14:20:41 +02:00
|
|
|
continue
|
2016-08-01 16:25:46 +10:00
|
|
|
tmpOutGeom = outFeat.geometry()
|
2016-03-21 21:30:23 +01:00
|
|
|
errors = tmpInGeom.validateGeometry()
|
|
|
|
if len(errors) != 0:
|
|
|
|
for error in errors:
|
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
|
2016-03-22 22:01:21 +01:00
|
|
|
self.tr('ValidateGeometry()'
|
|
|
|
'error:One or more input'
|
|
|
|
'features have invalid '
|
|
|
|
'geometry: ')
|
|
|
|
+ error.what())
|
2016-03-21 21:30:23 +01:00
|
|
|
continue
|
2012-09-15 18:25:25 +03:00
|
|
|
try:
|
2013-10-01 20:52:22 +03:00
|
|
|
tmpOutGeom = QgsGeometry(tmpOutGeom.combine(tmpInGeom))
|
2015-10-20 09:23:12 +02:00
|
|
|
outFeat.setGeometry(tmpOutGeom)
|
2012-09-15 18:25:25 +03:00
|
|
|
except:
|
2013-10-01 20:52:22 +03:00
|
|
|
raise GeoAlgorithmExecutionException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Geometry exception while dissolving'))
|
2015-10-20 09:23:12 +02:00
|
|
|
outFeat.setAttributes(attrs)
|
2014-03-12 10:49:54 +00:00
|
|
|
writer.addFeature(outFeat)
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2015-11-11 16:20:59 +01:00
|
|
|
fieldIdx = vlayerA.fieldNameIndex(fieldname)
|
|
|
|
unique = vector.getUniqueValues(vlayerA, int(fieldIdx))
|
|
|
|
nFeat = len(unique)
|
|
|
|
myDict = {}
|
|
|
|
attrDict = {}
|
2012-09-15 18:25:25 +03:00
|
|
|
for item in unique:
|
2015-11-11 16:20:59 +01:00
|
|
|
myDict[unicode(item).strip()] = []
|
|
|
|
attrDict[unicode(item).strip()] = None
|
|
|
|
|
|
|
|
unique = None
|
|
|
|
|
|
|
|
for inFeat in features:
|
|
|
|
attrs = inFeat.attributes()
|
|
|
|
tempItem = attrs[fieldIdx]
|
|
|
|
tmpInGeom = QgsGeometry(inFeat.geometry())
|
2016-03-21 21:30:23 +01:00
|
|
|
if tmpInGeom.isGeosEmpty():
|
2016-01-18 14:20:41 +02:00
|
|
|
continue
|
2016-03-21 21:30:23 +01:00
|
|
|
errors = tmpInGeom.validateGeometry()
|
|
|
|
if len(errors) != 0:
|
|
|
|
for error in errors:
|
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
|
2016-03-22 22:01:21 +01:00
|
|
|
self.tr('ValidateGeometry() '
|
|
|
|
'error: One or more input'
|
|
|
|
'features have invalid '
|
|
|
|
'geometry: ')
|
|
|
|
+ error.what())
|
2015-11-11 16:20:59 +01:00
|
|
|
|
2016-03-21 04:58:12 +01:00
|
|
|
if attrDict[unicode(tempItem).strip()] is None:
|
2015-11-11 16:20:59 +01:00
|
|
|
# keep attributes of first feature
|
|
|
|
attrDict[unicode(tempItem).strip()] = attrs
|
|
|
|
|
|
|
|
myDict[unicode(tempItem).strip()].append(tmpInGeom)
|
|
|
|
|
|
|
|
features = None
|
|
|
|
|
2016-03-15 11:46:05 +01:00
|
|
|
nElement = 0
|
2015-11-11 16:20:59 +01:00
|
|
|
for key, value in myDict.items():
|
|
|
|
nElement += 1
|
|
|
|
progress.setPercentage(int(nElement * 100 / nFeat))
|
2016-04-10 07:59:58 +10:00
|
|
|
try:
|
|
|
|
tmpOutGeom = QgsGeometry.unaryUnion(value)
|
|
|
|
except:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
|
|
|
self.tr('Geometry exception while dissolving'))
|
2015-11-11 16:20:59 +01:00
|
|
|
outFeat.setGeometry(tmpOutGeom)
|
|
|
|
outFeat.setAttributes(attrDict[key])
|
|
|
|
writer.addFeature(outFeat)
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
del writer
|