Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import arcpy, random
- from collections import defaultdict
- points = r'points' #Layer name
- groupfield = 'fishnet_id' #Some id identifying each fishnet cell spatially joined
- sample_size = 1 #Points to select per group
- oidfield = arcpy.Describe(points).OIDFieldName
- groups = defaultdict(list)
- with arcpy.da.SearchCursor(points, [groupfield, oidfield]) as cursor:
- for key,value in cursor:
- groups[key].append(value)
- oidlist = []
- for key,value in groups.items():
- oidlist.append(random.sample(value,sample_size)[0])
- sql = """{0} IN({1})""".format(arcpy.AddFieldDelimiters(points,oidfield), ','.join([str(i) for i in oidlist]))
- arcpy.SelectLayerByAttribute_management(points, where_clause=sql)
Add Comment
Please, Sign In to add comment