Skip to content

Instantly share code, notes, and snippets.

@jflann
Created May 31, 2019 11:46
Show Gist options
  • Save jflann/2e94a32586a1e110a5abddde765505ed to your computer and use it in GitHub Desktop.
Save jflann/2e94a32586a1e110a5abddde765505ed to your computer and use it in GitHub Desktop.
Alt. Esri Find Identical
import arcpy
def select_identical(lyr,fld):
d = {}
f_lyr = arcpy.MakeFeatureLayer_management(lyr)
with arcpy.da.SearchCursor(f_lyr,['OID@',fld]) as cursor:
for row in cursor:
fld_value = row[1]
oid = row[0]
if fld_value not in d:
d[fld_value] = {'OIDs':[oid],
'multiple':False
}
elif fld_value in d:
d[fld_value]['OIDs'].append(oid)
d[fld_value]['multiple'] = True
selection_oids = []
for k, v in d.iteritems():
if v['multiple'] == True:
selection_oids.extend(v['OIDs'])
sql_set = '(' + ','.join(map(str, selection_oids)) + ')'
where_clause = 'OBJECTID in {}'.format(sql_set)
if len(selection_oids) > 0:
arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",where_clause)
else:
arcpy.AddMessage('No duplicates to select')
if __name__ == '__main__':
lyr = arcpy.GetParameter(0)
fld = arcpy.GetParameterAsText(1)
use_shape = arcpy.GetParameterAsText(2)
if use_shape == 'true':
fld = 'SHAPE@XY'
select_identical(lyr,fld)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment