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,
|
|
|
|
QgsProcessingUtils)
|
2014-10-02 18:59:12 +03:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteDuplicateGeometries(GeoAlgorithm):
|
2016-02-17 09:36:59 +02:00
|
|
|
|
2014-10-02 18:59:12 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
|
|
|
return QgsApplication.getThemeIcon("/providerQgis.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("providerQgis.svg")
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector general tools')
|
|
|
|
|
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-03-29 12:51:59 +10:00
|
|
|
def defineCharacteristics(self):
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer')))
|
2015-05-22 15:56:45 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Cleaned')))
|
2014-10-02 18:59:12 +03:00
|
|
|
|
2017-04-25 14:55:38 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
2017-04-05 18:35:55 +10:00
|
|
|
layer = dataobjects.getLayerFromString(
|
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(self.INPUT))
|
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
|
|
|
|
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
|
2015-08-22 14:29:41 +02:00
|
|
|
layer.wkbType(), layer.crs())
|
2014-10-02 18:59:12 +03:00
|
|
|
|
2017-04-25 15:57:24 +10:00
|
|
|
features = vector.features(layer, context)
|
2014-10-02 18:59:12 +03:00
|
|
|
|
2017-04-25 17:53:32 +10:00
|
|
|
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)
|
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]
|
|
|
|
|
2016-02-17 09:36:59 +02:00
|
|
|
total = 100.0 / len(cleaned)
|
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)):
|
2014-10-02 18:59:12 +03:00
|
|
|
writer.addFeature(f)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-10-02 18:59:12 +03:00
|
|
|
|
|
|
|
del writer
|