134 lines
5.9 KiB
Python
Raw Normal View History

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'
2012-10-04 19:33:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-04 19:33:47 +02:00
__revision__ = '$Format:%H$'
2012-09-15 18:25:25 +03:00
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import \
GeoAlgorithmExecutionException
2013-08-12 20:44:27 +02:00
from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterBoolean import ParameterBoolean
from processing.parameters.ParameterTableField import ParameterTableField
from processing.outputs.OutputVector import OutputVector
from processing.tools import vector, dataobjects
2012-09-15 18:25:25 +03:00
class Dissolve(GeoAlgorithm):
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
FIELD = 'FIELD'
DISSOLVE_ALL = 'DISSOLVE_ALL'
2012-09-15 18:25:25 +03:00
#==========================================================================
#def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/icons/dissolve.png")
#==========================================================================
2012-09-15 18:25:25 +03:00
def processAlgorithm(self, progress):
2012-12-28 00:35:36 +01:00
useField = not self.getParameterValue(Dissolve.DISSOLVE_ALL)
2012-09-15 18:25:25 +03:00
fieldname = self.getParameterValue(Dissolve.FIELD)
vlayerA = dataobjects.getObjectFromUri(
self.getParameterValue(Dissolve.INPUT))
field = vlayerA.fieldNameIndex(fieldname)
2013-02-07 01:09:39 +01:00
vproviderA = vlayerA.dataProvider()
2012-09-15 18:25:25 +03:00
fields = vproviderA.fields()
writer = self.getOutputFromName(
Dissolve.OUTPUT).getVectorWriter(fields,
vproviderA.geometryType(),
vproviderA.crs())
2013-02-07 01:09:39 +01:00
outFeat = QgsFeature()
2012-09-15 18:25:25 +03:00
nElement = 0
2012-12-28 00:35:36 +01:00
nFeat = vproviderA.featureCount()
if not useField:
2012-09-15 18:25:25 +03:00
first = True
features = vector.features(vlayerA)
2012-12-28 00:35:36 +01:00
for inFeat in features:
2012-09-15 18:25:25 +03:00
nElement += 1
progress.setPercentage(int(nElement / nFeat * 100))
2012-12-28 00:35:36 +01:00
if first:
attrs = inFeat.attributes()
tmpInGeom = QgsGeometry(inFeat.geometry())
outFeat.setGeometry(tmpInGeom)
2012-09-15 18:25:25 +03:00
first = False
2012-12-28 00:35:36 +01:00
else:
tmpInGeom = QgsGeometry(inFeat.geometry())
tmpOutGeom = QgsGeometry(outFeat.geometry())
2012-09-15 18:25:25 +03:00
try:
tmpOutGeom = QgsGeometry(tmpOutGeom.combine(tmpInGeom))
outFeat.setGeometry(tmpOutGeom)
2012-09-15 18:25:25 +03:00
except:
raise GeoAlgorithmExecutionException(
'Geometry exception while dissolving')
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
2012-09-15 18:25:25 +03:00
else:
unique = vector.getUniqueValues(vlayerA, int(field))
nFeat = nFeat * len(unique)
2012-09-15 18:25:25 +03:00
for item in unique:
2012-12-28 00:35:36 +01:00
first = True
add = True
features = vector.features(vlayerA)
2012-12-28 00:35:36 +01:00
for inFeat in features:
nElement += 1
progress.setPercentage(int(nElement / nFeat * 100))
atMap = inFeat.attributes()
tempItem = atMap[field]
if unicode(tempItem).strip() == unicode(item).strip():
2012-12-28 00:35:36 +01:00
if first:
QgsGeometry(inFeat.geometry())
tmpInGeom = QgsGeometry(inFeat.geometry())
outFeat.setGeometry(tmpInGeom)
2012-12-28 00:35:36 +01:00
first = False
attrs = inFeat.attributes()
2013-05-01 23:45:20 +02:00
else:
tmpInGeom = QgsGeometry(inFeat.geometry())
tmpOutGeom = QgsGeometry(outFeat.geometry())
2013-05-01 23:45:20 +02:00
try:
tmpOutGeom = QgsGeometry(
tmpOutGeom.combine(tmpInGeom))
outFeat.setGeometry(tmpOutGeom)
2013-04-21 15:10:30 +02:00
except:
raise GeoAlgorithmExecutionException(
'Geometry exception while dissolving')
2012-12-28 00:35:36 +01:00
if add:
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
2012-09-15 18:25:25 +03:00
del writer
def defineCharacteristics(self):
self.name = 'Dissolve'
self.group = 'Vector geometry tools'
self.addParameter(ParameterVector(Dissolve.INPUT, 'Input layer',
[ParameterVector.VECTOR_TYPE_POLYGON]))
self.addParameter(ParameterBoolean(Dissolve.DISSOLVE_ALL,
'Dissolve all (do not use field)', True))
self.addParameter(ParameterTableField(Dissolve.FIELD, 'Unique ID field'
, Dissolve.INPUT, optional=True))
self.addOutput(OutputVector(Dissolve.OUTPUT, 'Dissolved'))