I have a shapefile that contains many polygons(>200,000). Some of these polygons are assigned the same ID number. None of the polygons overlap other polygons. I need to find a way to select out all of the polygons that have the same ID number as another polygon for further processing.

I have looked into finding all the duplicates in an Excel sheet and then joining this to the shapefile attribute table but this would take almost as long as manually selecting each shape. I would like a way to select all these out only using QGIS.

  • Did you check this: gis.stackexchange.com/questions/70886/… – WKT Mar 8 '16 at 14:52
  • I did to no avail. My end game here is to save the features with duplicated IDs to a separate file so that I can carry out further processing to determine which is the one I want to keep. from what I saw "Group Stats" will just show me the duplicates. As there may be hundreds this would not be an efficient way to identify them. If I am missing some functionality of that plugin please let me know. – Kingfisher Mar 8 '16 at 17:37

The following script (in Python console) should work. It will select all the features that have the same ID that another feature:

from qgis.utils import iface

layer = iface.mapCanvas().currentLayer() # Selected layer
dataProvider = layer.dataProvider()

# Index of field for faster lookup
idIdx = dataProvider.fields().indexFromName( 'ID' ) # Edit fieldname if necessary

featureList = list( layer.getFeatures() )

#idMax = max(f.attributes()[idIdx] for f in featureList) # if id is integer
idList = list(set([f.attributes()[idIdx] for f in featureList])) # if id is string

selection = [] # selected features

#for i in range(0,idMax+1): # if id is integer
for i in idList: # if id is string
    fSubList = [f.id() for f in featureList if f.attributes()[idIdx] == i]
    #print i, len(fSubList)

    if len(fSubList) > 1:
        selection += fSubList

layer.setSelectedFeatures( selection )

This takes around 30 seconds on a 200k+ features layer.

Edit: script adapted for QString ids. Also works for integers by commenting/uncommenting designated lines.

| improve this answer | |
  • Unfortunately the id field is a string that contains letters and numbers. – Kingfisher Mar 8 '16 at 17:32
  • Ok, script edited. – ArMoraer Mar 8 '16 at 18:04

I dissolved the polygons by ID. This turned all the duplicated into multi-part polygons. I then applied the python code below to select all the multi-part geometries from the layer and saved them to a separate file.

# find all multipart features in the active layer
l = iface.activelayer()
iter = l.getFeatures()
geoms = []
for feature in iter:
   geom = feature.geometry()
   if geom.isMultipart():

Thanks to http://kartoza.com/a-quick-hack-to-select-all-multipart-features-in-qgis/ for the python code that allowed me to select the desired features.

| improve this answer | |

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.