2014-10-02 18:59:12 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2014-10-03 10:29:54 +03:00
|
|
|
DeleteDuplicateGeometries.py
|
2014-10-02 18:59:12 +03:00
|
|
|
---------------------
|
|
|
|
Date : May 2010
|
|
|
|
Copyright : (C) 2010 by Michael Minn
|
|
|
|
Email : pyqgis at michaelminn 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__ = 'Michael Minn'
|
|
|
|
__date__ = 'May 2010'
|
|
|
|
__copyright__ = '(C) 2010, Michael Minn'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsFeatureRequest,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsApplication,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsProcessingUtils)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2014-10-02 18:59:12 +03:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class DeleteDuplicateGeometries(QgisAlgorithm):
|
2016-02-17 09:36:59 +02:00
|
|
|
|
2014-10-02 18:59:12 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector general tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
|
|
|
self.tr('Input layer')))
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Cleaned')))
|
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'deleteduplicategeometries'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Delete duplicate geometries')
|
2014-10-02 18:59:12 +03:00
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
2014-10-02 18:59:12 +03:00
|
|
|
|
2016-08-04 07:37:22 +10:00
|
|
|
fields = layer.fields()
|
2014-10-02 18:59:12 +03:00
|
|
|
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, layer.wkbType(), layer.crs(), context)
|
2014-10-02 18:59:12 +03:00
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2014-10-02 18:59:12 +03:00
|
|
|
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
2014-10-02 18:59:12 +03:00
|
|
|
geoms = dict()
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2016-08-01 16:25:46 +10:00
|
|
|
geoms[f.id()] = f.geometry()
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-10-02 18:59:12 +03:00
|
|
|
|
|
|
|
cleaned = dict(geoms)
|
|
|
|
|
2016-09-27 19:51:06 +02:00
|
|
|
for i, g in list(geoms.items()):
|
2016-09-21 18:24:26 +02:00
|
|
|
for j in list(cleaned.keys()):
|
2014-10-02 18:59:12 +03:00
|
|
|
if i == j or i not in cleaned:
|
|
|
|
continue
|
|
|
|
if g.isGeosEqual(cleaned[j]):
|
|
|
|
del cleaned[j]
|
|
|
|
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / len(cleaned) if cleaned else 1
|
2016-09-21 18:24:26 +02:00
|
|
|
request = QgsFeatureRequest().setFilterFids(list(cleaned.keys()))
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(layer.getFeatures(request)):
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(f, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-10-02 18:59:12 +03:00
|
|
|
|
|
|
|
del writer
|