0

I am using ArcGIS Pro and have a feature class of approx. 30.000 points and a polygon grid. Within each grid cell I want to select XY% (50, 70 or any other percentage) of the points (see picture).

enter image description here

I am referring to this - what would I change in the code to select a dynamic number of points within the grid cell?

2
  • 1
    This would be a single query in a relational database like PostgreSQL. Using a file-based feature class, you'd need to transfer the grid attribute onto the points, then select for each cell ID, determine the count, then randomly select if the feature count demanded. There's a ton of ways for keeping track of the selected features, including adding an attribute of "selected", keeping the list in memory, and writing the selected IDs to a table (and reselecting by ID later), so that after you finish all the cells you can generate the full selection set.
    – Vince
    Commented Aug 2, 2022 at 13:46
  • The phrase, "select a dynamic number of points," makes me think you want to select/re-select points every time the screen is refreshed. Is that what you mean? If so, what requirement is driving it?
    – bixb0012
    Commented Aug 4, 2022 at 15:10

1 Answer 1

0

Based on the link you provided, you'd want to define a sample_percent instead of a sample_size. Then, when iterating over groups.items(), you'd define sample_size by rounding up the number of points times your percent. Something like this:

import arcpy, random
from collections import defaultdict
from math import ceil

points = 'points' #Layer name
groupfield = 'fishnet_id' #Some id identifying each fishnet cell
sample_percent = 0.5  #Percent (0-1) of points in each cell to select

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():
    sample_size = ceil(len(value)*sample_percent)
    oidlist.extend(random.sample(value,sample_size))

sql = """{0} IN({1})""".format(arcpy.AddFieldDelimiters(points,oidfield), ','.join([str(i) for i in oidlist]))
arcpy.SelectLayerByAttribute_management(points, where_clause=sql)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.