Thursday 25 January 2018

qgis - Selecting duplicate records in PyQGIS


I have a lot of coordinates in attribute table like this one:


enter image description here


I'm looking the way how to find and select on active layer duplicated points using Python in QGIS ? I needn't delete them but only find and show (or select).




Answer



This code should do the job (you need to have your layer activated before running it from the Python Console):


layer = iface.activeLayer()

allfeatures={}
index = QgsSpatialIndex()
for ft in layer.getFeatures():
allfeatures[ft.id()] = ft
index.insertFeature(ft)


selection = []
for feat in layer.getFeatures():
inGeom = feat.geometry()
idsList = index.intersects(inGeom.boundingBox())
if len(idsList) > 1:
for id in idsList:
selection.append(allfeatures[id])
layer.setSelectedFeatures([k.id() for k in selection])

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...