QGIS/python/plugins/processing/algs/qgis/SinglePartsToMultiparts.py

101 lines
3.5 KiB
Python
Raw Normal View History

2012-10-04 19:33:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
SinglePartsToMultiparts.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. *
* *
***************************************************************************
"""
2016-09-21 18:24:26 +02:00
from builtins import str
2012-10-04 19:33:47 +02:00
__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$'
import os
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtGui import QIcon
2017-03-04 19:41:23 +01:00
from qgis.core import QgsFeature, QgsGeometry, QgsWkbTypes
2013-08-12 20:44:27 +02:00
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
2012-10-04 19:33:47 +02:00
class SinglePartsToMultiparts(GeoAlgorithm):
INPUT = 'INPUT'
FIELD = 'FIELD'
OUTPUT = 'OUTPUT'
2012-10-04 19:33:47 +02:00
def getIcon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'single_to_multi.png'))
2012-10-04 19:33:47 +02:00
def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Singleparts to multipart')
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
2012-10-04 19:33:47 +02:00
2015-01-15 20:41:15 +02:00
self.addParameter(ParameterVector(self.INPUT, self.tr('Input layer')))
self.addParameter(ParameterTableField(self.FIELD,
self.tr('Unique ID field'), self.INPUT))
2012-10-04 19:33:47 +02:00
self.addOutput(OutputVector(self.OUTPUT, self.tr('Multipart')))
2012-10-04 19:33:47 +02:00
def processAlgorithm(self, feedback):
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))
2012-10-04 19:33:47 +02:00
fieldName = self.getParameterValue(self.FIELD)
geomType = QgsWkbTypes.multiType(layer.wkbType())
2012-10-04 19:33:47 +02:00
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
layer.fields().toList(), geomType, layer.crs())
2012-10-04 19:33:47 +02:00
outFeat = QgsFeature()
inGeom = QgsGeometry()
2016-09-22 12:09:13 +02:00
index = layer.fields().lookupField(fieldName)
2012-10-04 19:33:47 +02:00
collection_geom = {}
collection_attrs = {}
features = vector.features(layer)
2016-11-11 00:28:27 +01:00
total = 100.0 / len(features)
for current, feature in enumerate(features):
atMap = feature.attributes()
idVar = atMap[index]
key = str(idVar).strip()
2017-03-04 19:41:23 +01:00
if key not in collection_geom:
collection_geom[key] = []
collection_attrs[key] = atMap
2016-11-11 00:28:27 +01:00
inGeom = feature.geometry()
collection_geom[key].append(inGeom)
feedback.setProgress(int(current * total))
for key, geoms in collection_geom.items():
outFeat.setAttributes(collection_attrs[key])
outFeat.setGeometry(QgsGeometry.collectGeometry(geoms))
writer.addFeature(outFeat)
del writer