<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python to select younger of two polygons with similar attributes in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-to-select-younger-of-two-polygons-with/m-p/133082#M10356</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Tom, how about the code below? That's what I would do in your situation.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14055460920169469 jive_text_macro" jivemacro_uid="_14055460920169469"&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;fc = 'c:/myfc.shp' # input feature class&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# object id column (FID), column with ids where some repeat, column with dates&lt;/P&gt;
&lt;P&gt;cols = ["OID@", "IDCOL", "DATECOL"]&lt;/P&gt;
&lt;P&gt;orderby = 'ORDER BY "DATECOL" DESC'&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;object_ids_to_delete = []&lt;/P&gt;
&lt;P&gt;last_id = None&lt;/P&gt;
&lt;P&gt;with arcpy.da.SearchCursor(fc, cols, sql_caluse=(None, orderby)) as sc:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in sc:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this_id = row[1]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if this_id == last_id:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this_object_id = row[0]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object_ids_to_delete.append(this_object_id)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; last_id = this_id&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# cleanup in ArcGIS &amp;lt;10.2&lt;/P&gt;
&lt;P&gt;del row&lt;/P&gt;
&lt;P&gt;del sc&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# one way to delete the old features where there is a newer feature:&lt;/P&gt;
&lt;P&gt;# create where clause like "FID" IN (1,2,3)&lt;/P&gt;
&lt;P&gt;# create layer containing only the features you need to delete&lt;/P&gt;
&lt;P&gt;# delete the features&lt;/P&gt;
&lt;P&gt;where = '"FID" IN (%s)' % ",".join(map(str, object_ids_to_delete))&lt;/P&gt;
&lt;P&gt;lr = arcpy.management.MakeFeatureLayer(fc, "tmp", where).getOutput(0)&lt;/P&gt;
&lt;P&gt;arcpy.management.DeleteFeatures(lr)&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# cleanup (delete the layer object)&lt;/P&gt;
&lt;P&gt;arcpy.management.Delete(lr)&lt;/P&gt;
&lt;P&gt;del lr&lt;/P&gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 16 Jul 2014 21:29:25 GMT</pubDate>
    <dc:creator>FilipKrál</dc:creator>
    <dc:date>2014-07-16T21:29:25Z</dc:date>
    <item>
      <title>Python to select younger of two polygons with similar attributes</title>
      <link>https://community.esri.com/t5/python-questions/python-to-select-younger-of-two-polygons-with/m-p/133081#M10355</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a .shp containing 31polygons with a date attribute identified by 28 unique (4 digit, string) numbers.&amp;nbsp; Three of the numbers have two polygons, I would like to select the younger of the two and delete the older.&amp;nbsp; Any suggestions?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have used the collections.Counter to identify the duplicated numbers.&amp;nbsp; 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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Jul 2014 20:27:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-to-select-younger-of-two-polygons-with/m-p/133081#M10355</guid>
      <dc:creator>TomKearns</dc:creator>
      <dc:date>2014-07-16T20:27:01Z</dc:date>
    </item>
    <item>
      <title>Re: Python to select younger of two polygons with similar attributes</title>
      <link>https://community.esri.com/t5/python-questions/python-to-select-younger-of-two-polygons-with/m-p/133082#M10356</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Tom, how about the code below? That's what I would do in your situation.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14055460920169469 jive_text_macro" jivemacro_uid="_14055460920169469"&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;fc = 'c:/myfc.shp' # input feature class&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# object id column (FID), column with ids where some repeat, column with dates&lt;/P&gt;
&lt;P&gt;cols = ["OID@", "IDCOL", "DATECOL"]&lt;/P&gt;
&lt;P&gt;orderby = 'ORDER BY "DATECOL" DESC'&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;object_ids_to_delete = []&lt;/P&gt;
&lt;P&gt;last_id = None&lt;/P&gt;
&lt;P&gt;with arcpy.da.SearchCursor(fc, cols, sql_caluse=(None, orderby)) as sc:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in sc:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this_id = row[1]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if this_id == last_id:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this_object_id = row[0]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object_ids_to_delete.append(this_object_id)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; last_id = this_id&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# cleanup in ArcGIS &amp;lt;10.2&lt;/P&gt;
&lt;P&gt;del row&lt;/P&gt;
&lt;P&gt;del sc&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# one way to delete the old features where there is a newer feature:&lt;/P&gt;
&lt;P&gt;# create where clause like "FID" IN (1,2,3)&lt;/P&gt;
&lt;P&gt;# create layer containing only the features you need to delete&lt;/P&gt;
&lt;P&gt;# delete the features&lt;/P&gt;
&lt;P&gt;where = '"FID" IN (%s)' % ",".join(map(str, object_ids_to_delete))&lt;/P&gt;
&lt;P&gt;lr = arcpy.management.MakeFeatureLayer(fc, "tmp", where).getOutput(0)&lt;/P&gt;
&lt;P&gt;arcpy.management.DeleteFeatures(lr)&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# cleanup (delete the layer object)&lt;/P&gt;
&lt;P&gt;arcpy.management.Delete(lr)&lt;/P&gt;
&lt;P&gt;del lr&lt;/P&gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Jul 2014 21:29:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-to-select-younger-of-two-polygons-with/m-p/133082#M10356</guid>
      <dc:creator>FilipKrál</dc:creator>
      <dc:date>2014-07-16T21:29:25Z</dc:date>
    </item>
    <item>
      <title>Re: Python to select younger of two polygons with similar attributes</title>
      <link>https://community.esri.com/t5/python-questions/python-to-select-younger-of-two-polygons-with/m-p/133083#M10357</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That looks great, I think it will work.&amp;nbsp; Even if the SQL command does not work, there should be a python counterpart.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Jul 2014 22:21:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-to-select-younger-of-two-polygons-with/m-p/133083#M10357</guid>
      <dc:creator>TomKearns</dc:creator>
      <dc:date>2014-07-16T22:21:29Z</dc:date>
    </item>
  </channel>
</rss>

