Python to select younger of two polygons with similar attributes

583
2
Jump to solution
07-16-2014 01:27 PM
TomKearns
Occasional Contributor II

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

0 Kudos
1 Solution

Accepted Solutions
FilipKrál
Occasional Contributor III

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

View solution in original post

0 Kudos
2 Replies
FilipKrál
Occasional Contributor III

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

0 Kudos
TomKearns
Occasional Contributor II

That looks great, I think it will work.  Even if the SQL command does not work, there should be a python counterpart.

Thanks!

0 Kudos