Skip to content
Permalink
Browse files

Port delete duplicate geometries to new API

  • Loading branch information
nyalldawson committed Jul 28, 2017
1 parent 5b6696f commit 620d4e0bcdbbd036a4bb51ad05bc926d952dfa4f
@@ -26,12 +26,10 @@
__revision__ = '$Format:%H$'

from qgis.core import (QgsFeatureRequest,
QgsApplication,
QgsFeatureSink,
QgsProcessingUtils)
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector


class DeleteDuplicateGeometries(QgisAlgorithm):
@@ -46,9 +44,9 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer')))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Cleaned')))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer')))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Cleaned')))

def name(self):
return 'deleteduplicategeometries'
@@ -57,23 +55,26 @@ def displayName(self):
return self.tr('Delete duplicate geometries')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
source = self.parameterAsSource(parameters, self.INPUT, context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), source.wkbType(), source.sourceCrs())

fields = layer.fields()

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, layer.wkbType(), layer.crs(), context)

features = QgsProcessingUtils.getFeatures(layer, context)

total = 100.0 / layer.featureCount() if layer.featureCount() else 0
features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]))
total = 100.0 / source.featureCount() if source.featureCount() else 0
geoms = dict()
for current, f in enumerate(features):
if feedback.isCanceled():
break

geoms[f.id()] = f.geometry()
feedback.setProgress(int(current * total))

cleaned = dict(geoms)

for i, g in list(geoms.items()):
if feedback.isCanceled():
break

for j in list(cleaned.keys()):
if i == j or i not in cleaned:
continue
@@ -82,8 +83,11 @@ def processAlgorithm(self, parameters, context, feedback):

total = 100.0 / len(cleaned) if cleaned else 1
request = QgsFeatureRequest().setFilterFids(list(cleaned.keys()))
for current, f in enumerate(layer.getFeatures(request)):
writer.addFeature(f, QgsFeatureSink.FastInsert)
for current, f in enumerate(source.getFeatures(request)):
if feedback.isCanceled():
break

sink.addFeature(f, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT: dest_id}
@@ -52,6 +52,7 @@
from .CreateConstantRaster import CreateConstantRaster
from .Delaunay import Delaunay
from .DeleteColumn import DeleteColumn
from .DeleteDuplicateGeometries import DeleteDuplicateGeometries
from .DeleteHoles import DeleteHoles
from .DensifyGeometries import DensifyGeometries
from .DensifyGeometriesInterval import DensifyGeometriesInterval
@@ -135,7 +136,6 @@
# from .RandomSelectionWithinSubsets import RandomSelectionWithinSubsets
# from .SelectByLocation import SelectByLocation
# from .SpatialJoin import SpatialJoin
# from .DeleteDuplicateGeometries import DeleteDuplicateGeometries
# from .GridLine import GridLine
# from .Gridify import Gridify
# from .HubDistancePoints import HubDistancePoints
@@ -192,7 +192,6 @@ def getAlgs(self):
# SelectByLocation(),
# ExtractByLocation(),
# SpatialJoin(),
# DeleteDuplicateGeometries(),
# GridLine(), Gridify(), HubDistancePoints(),
# HubDistanceLines(), HubLines(),
# GeometryConvert(), FieldsCalculator(),
@@ -204,7 +203,6 @@ def getAlgs(self):
# PointsFromLines(), PointsToPaths(),
# SetVectorStyle(), SetRasterStyle(),
# HypsometricCurves(),
#
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# OrientedMinimumBoundingBox(),
# DefineProjection(),
@@ -230,6 +228,7 @@ def getAlgs(self):
CreateConstantRaster(),
Delaunay(),
DeleteColumn(),
DeleteDuplicateGeometries(),
DeleteHoles(),
DensifyGeometries(),
DensifyGeometriesInterval(),

0 comments on commit 620d4e0

Please sign in to comment.
You can’t perform that action at this time.