I am trying to run a python script to select 5 random points in an attribute table and to loop through every 100 points to do this. When I run the function SelectRandomByCOunt within a python shell, it works perfectly fine but I am trying to embed it in this For loop and it doesnt work. I keep getting this error mesg:
ExecuteError: Failed to execute. Parameters are not valid.
The value cannot be a feature class
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).
The code
import arcpy
import os
from arcpy import env
# path name of the feature class
ws = r"S:\Venu\APWRAGOEAProject\APWRAGOEA.gdb"
fcname = r"PracticePythonCode"
fc = os.path.join(ws, fcname)
# replace 'xxx' with file path
# get count of features
num = arcpy.GetCount_management(fc)
print("There are "+str(num)+ " rows in your feature class.")
# start counters
count = 0
next100 = 1
with arcpy.da.SearchCursor(fc, ['OBJECTID']) as scursor: ## column 1 name [0], column 2 name [1]
for srow in scursor: # iterate through rows
if count < (next100 * 100):
col1 = srow[0] ## grab value from column 1
# do processing of these 100 records here using col1, col2 variables
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"
return
oids = [oid for oid, in arcpy.da.SearchCursor (layer, "OID@")]
oidFldName = arcpy.Describe (layer).OIDFieldName
path = arcpy.Describe (layer).path
delimOidFld = arcpy.AddFieldDelimiters (path, oidFldName)
randOids = random.sample (oids, count)
oidsStr = ", ".join (map (str, randOids))
sql = "{0} IN ({1})".format (delimOidFld, oidsStr)
arcpy.SelectLayerByAttribute_management (layer, "", sql)
SelectRandomByCount (fc,5)
# increase count by 1
count = count + 1
else:
# move to next group of 100
next100 = next100 + 1
del srow, scursor