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 collection....it 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:
<SPAN class="keyword token">def</SPAN> CleanSamplePlots <SPAN class="punctuation token">(</SPAN>inPoints<SPAN class="punctuation token">,</SPAN> standLayer<SPAN class="punctuation token">,</SPAN> outPoints<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#inPoints are the raw output of the desired point generation process</SPAN>
<SPAN class="comment token">#standLayer is the project stand dataset</SPAN>
<SPAN class="comment token">#outPoints are the cleaned, validated points</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>inPoints<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ptLayer"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation_management <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ptLayer"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"intersect"</SPAN><SPAN class="punctuation token">,</SPAN> standLayer<SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'INVERT'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> int<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ptLayer"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>getOutput<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>DeleteFeatures_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ptLayer"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ptLayer"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"CLUSTER_ID"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
count_of_items <SPAN class="operator token">=</SPAN> collections<SPAN class="punctuation token">.</SPAN>Counter<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> item <SPAN class="keyword token">in</SPAN> sorted<SPAN class="punctuation token">(</SPAN>count_of_items<SPAN class="punctuation token">.</SPAN>items<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> key<SPAN class="operator token">=</SPAN><SPAN class="keyword token">lambda</SPAN> x<SPAN class="punctuation token">:</SPAN>x<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> count_of_items <SPAN class="operator token"><</SPAN> <SPAN class="number token">8</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Cluster:{0} Points{1}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>item<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> item<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
Cluster<SPAN class="punctuation token">:</SPAN><SPAN class="number token">4</SPAN> Points4
Cluster<SPAN class="punctuation token">:</SPAN><SPAN class="number token">14</SPAN> Points4
Cluster<SPAN class="punctuation token">:</SPAN><SPAN class="number token">6</SPAN> Points7
Cluster<SPAN class="punctuation token">:</SPAN><SPAN class="number token">7</SPAN> Points8
Cluster<SPAN class="punctuation token">:</SPAN><SPAN class="number token">12</SPAN> Points8
Cluster<SPAN class="punctuation token">:</SPAN><SPAN class="number token">21</SPAN> Points9
Cluster<SPAN class="punctuation token">:</SPAN><SPAN class="number token">1</SPAN> Points9
Cluster<SPAN class="punctuation token">:</SPAN><SPAN class="number token">2</SPAN> Points9
Cluster<SPAN class="punctuation token">:</SPAN><SPAN class="number token">20</SPAN> Points10<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>etc
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>