Solved! Go to Solution.
import random import arcpy ##a couple of quick useful functions def MakeQuery(fid_list): templist = ['"FID" = ' + str(fid) for fid in fid_list] return " OR ".join(templist) def TakeOutTrash(dataset): if arcpy.Exists(dataset): arcpy.management.Delete(dataset) ##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") ##begin with list of all FIDs rows = arcpy.SearchCursor(shp) fids_to_pick_from = [row.getValue("FID") for row in rows] del rows #make empty list to hold the fids you'll use for final selection use_fids = [] ##use while loop to do the iterative selection process while True: ##get random index number for fid list which will point to a valid id rand_index = random.randrange(0,len(fids_to_pick_from)+1) rand_fid = fids_to_pick_from[rand_index] use_fids.append(rand_fid) fids_to_pick_from.remove(rand_fid) ##make feature layer using fids picked up to this point q = MakeQuery(use_fids) TakeOutTrash("fl2") fl = arcpy.management.MakeFeatureLayer(shp,"fl2",q) ##selection by location to find which of the original features overlap with the polygons ##in the use_fids list thus far, then remove them from the fids_to_pick_from list arcpy.management.SelectLayerByLocation(flayer,"INTERSECT",fl,"","NEW_SELECTION") if not arcpy.management.GetCount(flayer).getOutput(0) ==\ arcpy.management.GetCount(fl).getOutput(0): rows = arcpy.SearchCursor(flayer) all_overlapping = [row.getValue("FID") for row in rows] del rows bad_overlapping = [i for i in all_overlapping if not i in use_fids] fids_to_pick_from = [f for f in fids_to_pick_from if not f in bad_overlapping] ##break loop once 50 fids have been selected if len(use_fids) == 50: break ##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
import random import arcpy ##a couple of quick useful functions def MakeQuery(fid_list): templist = ['"FID" = ' + str(fid) for fid in fid_list] return " OR ".join(templist) def TakeOutTrash(dataset): if arcpy.Exists(dataset): arcpy.management.Delete(dataset) ##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") ##begin with list of all FIDs rows = arcpy.SearchCursor(shp) fids_to_pick_from = [row.getValue("FID") for row in rows] del rows #make empty list to hold the fids you'll use for final selection use_fids = [] ##use while loop to do the iterative selection process while True: ##get random index number for fid list which will point to a valid id rand_index = random.randrange(0,len(fids_to_pick_from)+1) rand_fid = fids_to_pick_from[rand_index] use_fids.append(rand_fid) fids_to_pick_from.remove(rand_fid) ##make feature layer using fids picked up to this point q = MakeQuery(use_fids) TakeOutTrash("fl2") fl = arcpy.management.MakeFeatureLayer(shp,"fl2",q) ##selection by location to find which of the original features overlap with the polygons ##in the use_fids list thus far, then remove them from the fids_to_pick_from list arcpy.management.SelectLayerByLocation(flayer,"INTERSECT",fl,"","NEW_SELECTION") if not arcpy.management.GetCount(flayer).getOutput(0) ==\ arcpy.management.GetCount(fl).getOutput(0): rows = arcpy.SearchCursor(flayer) all_overlapping = [row.getValue("FID") for row in rows] del rows bad_overlapping = [i for i in all_overlapping if not i in use_fids] fids_to_pick_from = [f for f in fids_to_pick_from if not f in bad_overlapping] ##break loop once 50 fids have been selected if len(use_fids) == 50: break ##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
rand_index = random.randrange(0,len(fids_to_pick_from)+1)
rand_index = random.randrange(0,len(fids_to_pick_from))