AnsweredAssumed Answered

Random Selection of Preexisting Points w/ Definition Query

Question asked by nynjtc_gis on Nov 28, 2018
Latest reply on Jan 7, 2019 by nynjtc_gis

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:

import arcpy
def SelectRandomByCount (layer, count):
import random
layerCount = int (arcpy.GetCount_management (layer).getOutput (0))
if layerCount < count:
print "input count is greater than layer count"
oids = [oid for oid, in arcpy.da.SearchCursor (layer, "OID@")]
oidFldName = arcpy.Describe (layer).OIDFieldName
delimOidFld = arcpy.AddFieldDelimiters (layer, oidFldName)
randOids = random.sample (oids, count)
oidsStr = ", ".join (map (str, randOids))
sql = "{0} IN ({1})".format (delimOidFld, oidsStr)
arcpy.SelectLayerByAttribute_management (layer, "", sql)
SelectRandomByPercent ("layer", num)

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