I have a script that uses the sql orderby parameter to sort my arcpy cursors by a user defined field or fields and deletes duplicates, but keeps the duplicate with the highest value. I'd like to be able to sort by geometry to identify/group features that are spatial duplicates and delete those that are duplicates (but keeping the duplicate with the highest value in another chosen field which is why I can't just use the Delete Identical tool).

I have tried several ways of doing this, but as I understand, shape tokens such as SHAPE@, SHAPE@WKT, etc. cannot be used in the sql clause. Is there another way to do this or a workaround? I also tried to add a field and fill with the SHAPE@WKT, but it didn't fill the field.

Here is the code I've got that works for other fields, other than the geometry field.

import arcpy
from itertools import groupby
from operator import itemgetter

input_layer = arcpy.GetParameterAsText(0)
case_fields = arcpy.GetParameterAsText(1)
max_field = arcpy.GetParameterAsText(2)
case_fields = case_fields.split(";")
case_fields = [str(x) for x in case_fields]

sql_orderby = "ORDER BY {}, {} DESC".format(",".join(case_fields), max_field)

with arcpy.da.UpdateCursor(input_layer, "*", sql_clause=(None, sql_orderby)) as cursor:
    case_func = itemgetter(*(cursor.fields.index(fld) for fld in case_fields))
    for key, group in groupby(cursor, case_func):
        for extra in group:
  • Though "Delete Identical" GP tool won't do what you need, couldn't you use "Find Identical" GP tool to output a table of spatial duplicates. Then just join the output table to your source data (using MakeFeatureLayer), and then input the layer file, with join, to the cursor, so you can access the FEAT_SEQ field in the Find Dup output table to sort on. Haven't used joined layer in update cursor, but it works great in search cursor, so presume update works too. – John Oct 2 '18 at 15:40

You dont need to use cursors, instead:

  1. Sort descending by value field
  2. Delete Identical on shape field. The first duplicate, which is the one with highest value field, will be kept.
| 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.