I have a .shp containing 31polygons with a date attribute identified by 28 unique (4 digit, string) numbers. Three of the numbers have two polygons, I would like to select the younger of the two and delete the older. Any suggestions?
I have used the collections.Counter to identify the duplicated numbers. I understand search and update cursors and am thinking I can use one to find the FID, but am not sure how to build a query that says if the numbers are duplicated choose the largest (newer) date.
Thanks
Solved! Go to Solution.
Hi Tom, how about the code below? That's what I would do in your situation.
I haven't tested it so you might need to tweak it a bit. I hope search cursors on shapefiles support the ORDER BY sql_clause, otherwise this won't work.
import arcpy
fc = 'c:/myfc.shp' # input feature class
# object id column (FID), column with ids where some repeat, column with dates
cols = ["OID@", "IDCOL", "DATECOL"]
orderby = 'ORDER BY "DATECOL" DESC'
object_ids_to_delete = []
last_id = None
with arcpy.da.SearchCursor(fc, cols, sql_caluse=(None, orderby)) as sc:
for row in sc:
this_id = row[1]
if this_id == last_id:
this_object_id = row[0]
object_ids_to_delete.append(this_object_id)
last_id = this_id
# cleanup in ArcGIS <10.2
del row
del sc
# one way to delete the old features where there is a newer feature:
# create where clause like "FID" IN (1,2,3)
# create layer containing only the features you need to delete
# delete the features
where = '"FID" IN (%s)' % ",".join(map(str, object_ids_to_delete))
lr = arcpy.management.MakeFeatureLayer(fc, "tmp", where).getOutput(0)
arcpy.management.DeleteFeatures(lr)
# cleanup (delete the layer object)
arcpy.management.Delete(lr)
del lr
Hi Tom, how about the code below? That's what I would do in your situation.
I haven't tested it so you might need to tweak it a bit. I hope search cursors on shapefiles support the ORDER BY sql_clause, otherwise this won't work.
import arcpy
fc = 'c:/myfc.shp' # input feature class
# object id column (FID), column with ids where some repeat, column with dates
cols = ["OID@", "IDCOL", "DATECOL"]
orderby = 'ORDER BY "DATECOL" DESC'
object_ids_to_delete = []
last_id = None
with arcpy.da.SearchCursor(fc, cols, sql_caluse=(None, orderby)) as sc:
for row in sc:
this_id = row[1]
if this_id == last_id:
this_object_id = row[0]
object_ids_to_delete.append(this_object_id)
last_id = this_id
# cleanup in ArcGIS <10.2
del row
del sc
# one way to delete the old features where there is a newer feature:
# create where clause like "FID" IN (1,2,3)
# create layer containing only the features you need to delete
# delete the features
where = '"FID" IN (%s)' % ",".join(map(str, object_ids_to_delete))
lr = arcpy.management.MakeFeatureLayer(fc, "tmp", where).getOutput(0)
arcpy.management.DeleteFeatures(lr)
# cleanup (delete the layer object)
arcpy.management.Delete(lr)
del lr
That looks great, I think it will work. Even if the SQL command does not work, there should be a python counterpart.
Thanks!