Hi everyone,
I want to use definition queries with this code, taken from this ESRI page, in order to randomly select a subset of preexisting points by count:
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">def</SPAN> SelectRandomByCount <SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">,</SPAN> count<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">import</SPAN> random
layerCount <SPAN class="operator token">=</SPAN> int <SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management <SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>getOutput <SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> layerCount <SPAN class="operator token"><</SPAN> count<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"input count is greater than layer count"</SPAN>
<SPAN class="keyword token">return</SPAN>
oids <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>oid <SPAN class="keyword token">for</SPAN> oid<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor <SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"OID@"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN>
oidFldName <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe <SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>OIDFieldName
delimOidFld <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddFieldDelimiters <SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">,</SPAN> oidFldName<SPAN class="punctuation token">)</SPAN>
randOids <SPAN class="operator token">=</SPAN> random<SPAN class="punctuation token">.</SPAN>sample <SPAN class="punctuation token">(</SPAN>oids<SPAN class="punctuation token">,</SPAN> count<SPAN class="punctuation token">)</SPAN>
oidsStr <SPAN class="operator token">=</SPAN> <SPAN class="string token">", "</SPAN><SPAN class="punctuation token">.</SPAN>join <SPAN class="punctuation token">(</SPAN>map <SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">,</SPAN> randOids<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
sql <SPAN class="operator token">=</SPAN> <SPAN class="string token">"{0} IN ({1})"</SPAN><SPAN class="punctuation token">.</SPAN>format <SPAN class="punctuation token">(</SPAN>delimOidFld<SPAN class="punctuation token">,</SPAN> oidsStr<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management <SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> sql<SPAN class="punctuation token">)</SPAN>
__________________________________________________________________
SelectRandomByPercent <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"layer"</SPAN><SPAN class="punctuation token">,</SPAN> num<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>The code works with a shapefile, but does not work for me with a personal geodatabase.
My desired scenario with using a definition query is as follows:
I have 27,000 points in one layer and 511 polygons in another. The points are draw over the polygons
and are only contained within the boundaries of the polygons.
Phase One:
1. I want to first filter out a subset of points, using a simple definition query (e.g. "ID = #") that
is set via the layer properties
2. I want to randomly select a max number of points from the layer that has the definition query set,
using the script above
3. Once that selection is made, I will assign the selected points a unique name in a field i've created.
Phase Two:
1. I want to set a definition query and repeat Phase One - Step 1. with the addition of excluding
the points assigned a name.
Can anyone help with this? I've attached a representation of the points and polygons. Thank you
Best,
Robert