I've written some code in Python and PostGIS to find duplicate geometric features within the same feature layer.

That part is a success. The problem I have is the dupes as well as the original feature IDs are part of the result of my cursor.execute command.

What's the best way for me to segregate the first occurrence of a feature geometry from subsequent occurrences of the same geometry?

#Select all features within current layer (i.e. HydrographySrf)
cmd = "SELECT * FROM " + layer
numLayer = cursor.rowcount

#find all duplicates in the current layer
cmd = "SELECT b.ogc_fid FROM " + layer + " AS a, " + layer + " AS b WHERE ST_Equals(a.geom, b.geom) AND a.ctid <> b.ctid"

if (cursor.rowcount > 0):

    #Store Suspect Feature IDs for Reporting at End of Logfile
    for row in sorted(cursor):
        if row[0] not in discoveredFeatureIntegerIDs:

Use the Python set() function on a list to remove duplicates:

uniqueFeatureIntegerIDs = set(discoveredFeatureIntegerIDs)

place it after your for loop

  • Each feature will already have a unique feature ID even though it may be a dupe so the "set" command will not work in this case. I'm trying to separate GEOMETRIC feature duplicates from their corresponding originals (lowest feature ID for any given set of dupes). I wonder if there is a way to exclude the lowest number ID for any set of dupes and capture the lowest number as an "original" while all others will be called "dupes". Do you have any ideas? – George Sep 9 '16 at 18:59
  • @George sorry so late... try this answer: gis.stackexchange.com/a/81130/72697 – kttii Jan 4 '18 at 14:42

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.