3

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.

  • do your duplicates actually share an ID? – Ian Turton Feb 16 '17 at 17:01
  • I use the topology checker for these kind of problems, but you will still have to remove the duplicates yourself and that's maybe a lot of work for such a large file. – Konan Pruiksma Feb 16 '17 at 20:00
  • Yes they do. Looking like osgb1000000096774422 (string format and non consecutive) – PhilHey Feb 16 '17 at 20:03
3

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.

  • Defining the list outside of the function. I tried to have it between the qgisfunction and the def but this only caused a python error. Putting it outside of the qgisfunction I did not try... stupid me... Thank you very much for your help nash – PhilHey Feb 17 '17 at 11:39

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.