0

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
cursor.execute(cmd)
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"
cursor.execute(cmd)

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:
            discoveredFeatureIntegerIDs.append(row[0])

1 Answer 1

1

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

uniqueFeatureIntegerIDs = set(discoveredFeatureIntegerIDs)

place it after your for loop

2
  • 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
    Commented Sep 9, 2016 at 18:59
  • @George sorry so late... try this answer: gis.stackexchange.com/a/81130/72697
    – kttii
    Commented Jan 4, 2018 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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