3

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).

  • Do your duplicate point need to have all the same attributes or only a some of them? – mgri Jan 23 '17 at 21:15
  • Yes, duplicates overlaps. – Artec Jan 23 '17 at 21:16
  • Thanks, but I don't understand if you need a spatial query or a query on the attributes. – mgri Jan 23 '17 at 21:19
  • The same attributes are only X and Y. The rest can be but not always. – Artec Jan 23 '17 at 21:20
  • In this case I need spatial query. "X,Y" in this table represents physical points wchich can be duplicated. I'm looking the way how to select points (with using geometry not attributes). – Artec Jan 23 '17 at 21:25
4

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])
  • Thanks for your quickly help but at last row I've got error File "<input>", line 7 layer.setSelectedFeatures([k.id() for k in selection]) ^ SyntaxError: invalid syntax – Artec Jan 23 '17 at 21:42
  • Which QGIS version are you using? It's working for me (QGIS 2.18.2) – mgri Jan 23 '17 at 21:44
  • Now I'm using 2.16.3 but sometimes use older (2.12 Lyon). – Artec Jan 23 '17 at 21:47
  • These function didn't change from your version... That's really weird, I'm pretty sure there is some problem with the copy&paste of the code. – mgri Jan 23 '17 at 21:50
  • You have right. I need to paste separately the last row layer.setSelectedFeatures([k.id() for k in selection]) and it works great ! – Artec Jan 23 '17 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.