[GIS] Identify duplicate attributes (followup)

duplicationqgisqgis-2.18qgis-custom-function

I am processing a large map with several hundred thousand building polygons in QGIS 2.18. Unfortunately I had to puzzle this map together from several smaller and overlapping pieces and so quite a few of them appear multiple times. Since I need to aggregate the building data later I need to get rid of the multiple polygons now.

The Polygons all have ID’s (string, non-consecutive) and so I hoped it would be possible to filter out the duplicates in the attribute table. I found a python script under
Identify duplicate attributes
which (as far as I understand it) should be a simple and elegant solution

from qgis.core import *
from qgis.gui import *    

@qgsfunction(args='auto', group='Custom')

def FindDublicate(IDfield, feature, parent):
  uniqueList = []
  if IDfield in uniqueList:
    return 1
  else:
    uniqueList.append(IDfield)
    return 0

However, it does not work for me: The return is always ‘0’.

Can somebody tell me how the script should be such that it works under QGIS 2.18.1 or if there is an alternative solution to the problem?

Sidenote:
I also tried several methods to clean the geometry but none of them worked: GRASS GIS 7 function v.clean does not show any effect, and MMQGIS delete duplicate tool simply leads to QGIS crashing.

Best Answer

Your uniqueList = [] statement needs to be outside the function. Because every time your function is called, the uniqueList variable would be initialized as empty again. Try the below code, it should work -

from qgis.core import *
from qgis.gui import *    

uniqueList = []

@qgsfunction(args='auto', group='Custom')
def FindDublicate(IDfield, feature, parent):
  if IDfield in uniqueList:
    return 1
  else:
    uniqueList.append(IDfield)
    return 0

This script in the function editor would select the extra duplicates in the attribute table.

P.S. : Alternatively, if your final aim is to delete the duplicate geometries, you should try the Delete duplicate geometries algorithm under QGIS geoalgorithms >Vector general tools from the Processing Toolbox.

Related Question