<?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 randomly select polygons and stepwise omit overlapping polygons in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387051#M30541</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;hi there,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; I have ~300 buffer polygons (see attached) and want to randomly select ~50 of them. when one polygon is selected, I want to omit all other polygons that overlap that selected polygon from the next step of selection. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; I want to do this stepwise for all 50 polygons I select. in the end, none of my 50 selected polygons will overlap.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; any ideas? I am new to python.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; emil&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 24 Apr 2014 07:35:06 GMT</pubDate>
    <dc:creator>EmilLarsson</dc:creator>
    <dc:date>2014-04-24T07:35:06Z</dc:date>
    <item>
      <title>randomly select polygons and stepwise omit overlapping polygons</title>
      <link>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387051#M30541</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;hi there,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; I have ~300 buffer polygons (see attached) and want to randomly select ~50 of them. when one polygon is selected, I want to omit all other polygons that overlap that selected polygon from the next step of selection. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; I want to do this stepwise for all 50 polygons I select. in the end, none of my 50 selected polygons will overlap.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; any ideas? I am new to python.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; emil&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Apr 2014 07:35:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387051#M30541</guid>
      <dc:creator>EmilLarsson</dc:creator>
      <dc:date>2014-04-24T07:35:06Z</dc:date>
    </item>
    <item>
      <title>Re: randomly select polygons and stepwise omit overlapping polygons</title>
      <link>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387052#M30542</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi emil,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How are these polygons stored, in one feature class, in a variety of feature classes, shapefiles, etc.?&amp;nbsp; This will dramatically change the code, but it is certainly doable.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Apr 2014 14:03:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387052#M30542</guid>
      <dc:creator>AdamCox1</dc:creator>
      <dc:date>2014-04-24T14:03:09Z</dc:date>
    </item>
    <item>
      <title>Re: randomly select polygons and stepwise omit overlapping polygons</title>
      <link>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387053#M30543</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;hi adam,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;they are all stored in one single shapefile. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Apr 2014 14:50:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387053#M30543</guid>
      <dc:creator>EmilLarsson</dc:creator>
      <dc:date>2014-04-24T14:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: randomly select polygons and stepwise omit overlapping polygons</title>
      <link>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387054#M30544</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok.&amp;nbsp; I was working on a reply assuming a single feature class, but I've modified it so this should be fine. What I have in mind uses values from the FID field, SelectLayerByLocation and the randrange() function from the random module. I'll write it as a standalone script, but it wouldn't be hard to create a custom tool from it.&amp;nbsp; I'll add a lot of comments so it should be clear what's going on.&amp;nbsp; I got a little carried away.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import random import arcpy&amp;nbsp; ##a couple of quick useful functions def MakeQuery(fid_list): &amp;nbsp;&amp;nbsp;&amp;nbsp; templist = ['"FID" = ' + str(fid) for fid in fid_list] &amp;nbsp;&amp;nbsp;&amp;nbsp; return " OR ".join(templist) def TakeOutTrash(dataset): &amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Exists(dataset): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.management.Delete(dataset)&amp;nbsp; ##first get the feature class, and make feature layer for later use shp = r"C:\CLI_GIS\Intermountain Region\GRTE\850491\New_Shapefile.shp" TakeOutTrash("fl1") flayer = arcpy.management.MakeFeatureLayer(shp,"fl1")&amp;nbsp; ##begin with list of all FIDs rows = arcpy.SearchCursor(shp) fids_to_pick_from = [row.getValue("FID") for row in rows] del rows&amp;nbsp; #make empty list to hold the fids you'll use for final selection use_fids = []&amp;nbsp; ##use while loop to do the iterative selection process while True: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##get random index number for fid list which will point to a valid id &amp;nbsp;&amp;nbsp;&amp;nbsp; rand_index = random.randrange(0,len(fids_to_pick_from)+1) &amp;nbsp;&amp;nbsp;&amp;nbsp; rand_fid = fids_to_pick_from[rand_index] &amp;nbsp;&amp;nbsp;&amp;nbsp; use_fids.append(rand_fid) &amp;nbsp;&amp;nbsp;&amp;nbsp; fids_to_pick_from.remove(rand_fid)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##make feature layer using fids picked up to this point &amp;nbsp;&amp;nbsp;&amp;nbsp; q = MakeQuery(use_fids) &amp;nbsp;&amp;nbsp;&amp;nbsp; TakeOutTrash("fl2") &amp;nbsp;&amp;nbsp;&amp;nbsp; fl = arcpy.management.MakeFeatureLayer(shp,"fl2",q) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##selection by location to find which of the original features overlap with the polygons &amp;nbsp;&amp;nbsp;&amp;nbsp; ##in the use_fids list thus far, then remove them from the fids_to_pick_from list &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.management.SelectLayerByLocation(flayer,"INTERSECT",fl,"","NEW_SELECTION") &amp;nbsp;&amp;nbsp;&amp;nbsp; if not arcpy.management.GetCount(flayer).getOutput(0) ==\ &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.management.GetCount(fl).getOutput(0): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(flayer) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; all_overlapping = [row.getValue("FID") for row in rows] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del rows &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bad_overlapping = [i for i in all_overlapping if not i in use_fids] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fids_to_pick_from = [f for f in fids_to_pick_from if not f in bad_overlapping] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##break loop once 50 fids have been selected &amp;nbsp;&amp;nbsp;&amp;nbsp; if len(use_fids) == 50: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&amp;nbsp; ##make final selection final_q = MakeQuery(use_fids) arcpy.management.SelectLayerByAttribute(flayer,"NEW_SELECTION",final_q) print str(len(use_fids)) + " polygons have been chosen" print final_q&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm finding that running this in the python console takes forever.&amp;nbsp; It's much faster to create a little stand alone script from it, and just copy and paste the printed query at the end into the select by attribute dialogue box in ArcMap.&amp;nbsp; At any rate, this can serve as the core of the script.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Apr 2014 15:42:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387054#M30544</guid>
      <dc:creator>AdamCox1</dc:creator>
      <dc:date>2014-04-24T15:42:46Z</dc:date>
    </item>
    <item>
      <title>Re: randomly select polygons and stepwise omit overlapping polygons</title>
      <link>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387055#M30545</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;hi adam,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;this script worked like magic! I still ran it in the python window (did not understand the other option) and it took 10-15 minutes when selecting 50 polygons.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks a million!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;emil&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Apr 2014 13:15:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387055#M30545</guid>
      <dc:creator>EmilLarsson</dc:creator>
      <dc:date>2014-04-28T13:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: randomly select polygons and stepwise omit overlapping polygons</title>
      <link>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387056#M30546</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Emil, I was just testing this again, and realized that the line&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;rand_index = random.randrange(0,len(fids_to_pick_from)+1)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;should be modified to &lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;rand_index = random.randrange(0,len(fids_to_pick_from))&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;that + 1 will cause an error sometimes ("list index out of range").&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the other method, these steps should work just fine and it only takes a few seconds to run:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1. right-click on the desktop to make a new text file (.txt) and name it whatever you want.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. change the file extension to ".py" from ".txt"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3. right-click on the file and "Edit in IDLE"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4. paste in the code, and enter the correct path for the shapefile with the polygons.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;5. press F5 or click Run &amp;gt; Run Module&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;6. you should get the query when the script is done (takes only a couple of seconds) which you can paste into ArcMap, as a definition query or a Select By Attributes query.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;7. just rerun the module as many times as you want for new sets of 50 random polygons.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Apr 2014 13:34:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/randomly-select-polygons-and-stepwise-omit/m-p/387056#M30546</guid>
      <dc:creator>AdamCox1</dc:creator>
      <dc:date>2014-04-28T13:34:10Z</dc:date>
    </item>
  </channel>
</rss>

