AnsweredAssumed Answered

Using Collections with da Search Cursor for validation

Question asked by LANDVEST on Oct 5, 2016
Latest reply on Oct 6, 2016 by LANDVEST

I have a tool which creates a number of points grouped together in 'clusters'. Inevitably, the tool will generate points that fall outside the project boundary. Removing these points is a simple task using select by location. However, I also want to remove any cluster of points that has less than a required threshold value. I'm having some success with using 'collections' much in the same way that Summary Stats would work. However, I'm running into difficulty using an IF statement with the seems to print anytime the value (regardless of value) is 'collection > X' and prints nothing when ' collection < X' and fails to work when I try ' collection = X'.

So it appears the collection is a 'Counter'. Am I on the wrong track here?I couldn't get 'GetCount' to summarize by each value in a field.


EDIT: it looks like 'sorted' may be something I can use here, but having a hard time finding examples of this.



I want to remove all points in the cluster if the particular cluster contains less than a threshold value.


Here is my code so far:


def CleanSamplePlots (inPoints, standLayer, outPoints):
    #inPoints are the raw output of the desired point generation process
    #standLayer is the project stand dataset
    #outPoints are the cleaned, validated points
    arcpy.MakeFeatureLayer_management(inPoints, "ptLayer")
    arcpy.SelectLayerByLocation_management ("ptLayer", "intersect", standLayer,"", "", 'INVERT')
    if int(arcpy.GetCount_management("ptLayer").getOutput(0)) > 0:
    with arcpy.da.SearchCursor("ptLayer", "CLUSTER_ID") as cursor:
        count_of_items = collections.Counter(row[0] for row in cursor)
        for item in sorted(count_of_items.items(), key=lambda x:x[1]):
            if count_of_items < 8:
                print "Cluster:{0} Points{1}".format(item[0], item[1])

Cluster:4 Points4
Cluster:14 Points4
Cluster:6 Points7
Cluster:7 Points8
Cluster:12 Points8
Cluster:21 Points9
Cluster:1 Points9
Cluster:2 Points9
Cluster:20 Points10....etc