Skip to content

Commit

Permalink
Port delete duplicate geometries to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 28, 2017
1 parent 5b6696f commit 620d4e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
40 changes: 22 additions & 18 deletions python/plugins/processing/algs/qgis/DeleteDuplicateGeometries.py
Expand Up @@ -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):
Expand All @@ -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'
Expand All @@ -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
Expand All @@ -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}
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -192,7 +192,6 @@ def getAlgs(self):
# SelectByLocation(),
# ExtractByLocation(),
# SpatialJoin(),
# DeleteDuplicateGeometries(),
# GridLine(), Gridify(), HubDistancePoints(),
# HubDistanceLines(), HubLines(),
# GeometryConvert(), FieldsCalculator(),
Expand All @@ -204,7 +203,6 @@ def getAlgs(self):
# PointsFromLines(), PointsToPaths(),
# SetVectorStyle(), SetRasterStyle(),
# HypsometricCurves(),
#
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# OrientedMinimumBoundingBox(),
# DefineProjection(),
Expand All @@ -230,6 +228,7 @@ def getAlgs(self):
CreateConstantRaster(),
Delaunay(),
DeleteColumn(),
DeleteDuplicateGeometries(),
DeleteHoles(),
DensifyGeometries(),
DensifyGeometriesInterval(),
Expand Down

0 comments on commit 620d4e0

Please sign in to comment.