Random Selection of Ploygons from Current Selection

711
4
12-18-2013 08:34 AM
FredKellner1
New Contributor
Hello,

In my python script I am selecting by attribute, and after doing so I would like to make a random selection of 50 polygons from this selection. I am trying to do this in an efficient manner. I have tried a manner of different solutions, but these involve exporting files and slow for loops. Any suggestions for a fast and efficient way to perform this random selection.

Thanks

Fred
Tags (2)
0 Kudos
4 Replies
DouglasSands
Occasional Contributor II
Hello,

In my python script I am selecting by attribute, and after doing so I would like to make a random selection of 50 polygons from this selection. I am trying to do this in an efficient manner. I have tried a manner of different solutions, but these involve exporting files and slow for loops. Any suggestions for a fast and efficient way to perform this random selection.

Thanks

Fred


Off the top of my head I can think of something like the following working:

    Select the polygons.
  1. Get the IDs of the selected Polygons.

  2. Create a dictionary that has incremental keys (0 - (n - 1)) where n is the number of selected polygons (see arcpy.GetCount). Each key should reference a ploygon's ID.

  3. Import random and use random.sample(range(n), 50) to generate a list of 50 unique random numbers.

  4. For each random number, use the dictionary to get a list of all the corresponding polygon IDs

  5. Now use select by attribute to get all the polygons. This would be done using something like [ID_FIELD] IN (<IDs>)


The exact way you code it will depend a lot on what your data looks like. Hope this helps.
0 Kudos
FredKellner1
New Contributor
Thanks for the reply. I'm with you on steps 2-5, what I can't seem to figure out is how to extract the attribute data for the polygons such as object id into a python list that I can then randomly select values from. Seems easy enough but is eluding me in the arcpy documentation.

Fred
0 Kudos
DouglasSands
Occasional Contributor II
The easiest way to do this would be to use a Search Cursor on the feature or table layer you created (not the source datasets) that has the initial selection applied (although I haven't tried doing this before):

Cursors honor layer/table view definition queries and selections. The cursor object only contains the rows that would be used by any geoprocessing tool during an operation.


Your code could look something like this:


selected = [str(r[0]) for r in arcpy.da.SearchCursor('Layer Name', ['SOURCE_ID_FIELD'])]
query = 'SOURCE_ID_FIELD IN (' + ', '.join(selected) + ')'



Note that the exact syntax is going to depend on how you defined your feature or table layer, and whether or not this is shapefile, fgdb etc. The str(r[0]) is important because join() expects a list of strings.

Also if the IDs are text then...

query = "SOURCE_ID_FIELD IN ('" + "', '".join(selected) + "')"


... should work.

Tough to test any of this without your data, but hope this helps.
0 Kudos
DouglasSands
Occasional Contributor II
Oops. I kind of forgot what it was you were trying to do. I was also wrong about how random.sample() works. The dictionary isn't necessary and you can get it all in one line:

randomly_selected =  random.sample([str(r[0]) for r in arcpy.da.SearchCursor('Layer Name', ['SOURCE_ID_FIELD'])], 50)

query = "SOURCE_ID_FIELD IN ('" + "', '".join(randomly_selected) + "')"


ramdom.sample() is pulling random values from the input list. When you use the query, you will get your 50 random features.
0 Kudos