Sorry - I knew it would be difficult to read, but I didn't know how to get around it (i.e., I didn't know about the # button ...I'm new to posting forum code). My apologies! Here's another go at it...
NOTE: In the code below I have simplified what's happening. This code is where I began, and produced the same count discrepancies as the more complex code. Perhaps this is a better to start since it produced the same problematic result...
CURTIS: I use SelectLayerByLocation in my code and then use SelectByLocation manually in ArcMap for the check.
DUNCAN: Regarding the selection type, I use "Add to" because I have also selected one row in the table. So, for each row in the table, I select the row (i.e. a single feature), add to this selection by selecting ancillary data within 10,000m of this feature, then count the selected ancillary data and write this value to the appropriate field in the row. Below I have included only one ancillary data layer, because as stated above, this produces the same count discrepancies. When selecting features within 10,000m from multiple ancillary data layers, my "GetCount" tool is specific to each ancillary data layer - therefore it returns the count for each layer and not the count of everything selected. Hope this helps clear up my reasoning for using "Add to" - if not just let me know!
I appreciate your continued help with this!
from arcpy import env
env.workspace = "C:/AF_WorkingFiles/AgHealth_Iowa/Databases/AHS_NitrateWells.mdb"
# User-defined variables...
Wells2 = "C:/.../FC_Wells2" #Input feature class
Wells3 = "C:/.../FC_Wells3" #Output feature class
AFOs = "C:/.../AFOs"
# Delete fields if they exist...
# Create feature layer...
# Add count fields...
L2 = ["Count_10kmAFOs","Count_10kmConfmnts","Count_10kmFeedlots", \
for item in L2:
# Create ancillary feature layer...
# Set variables...
SelNew = "NEW_SELECTION"
SelDist = "WITHIN_A_DISTANCE"
SelAddTo = "ADD_TO_SELECTION"
SelClear = "CLEAR_SELECTION"
# Assign counts to each row...
cur1 = arcpy.UpdateCursor("Wells2Lyr")
for row in cur1:
UnqKey = row.Concat_DatasetPrimkey
# Select feature at row...
WhereClause = '[Concat_DatasetPrimkey]' + " = '" + UnqKey + "'"
# Select ancillary layer within 10km...
# Count selected records in each layer and assign to appropriate fields...
row.Count_10kmAFOs = int(arcpy.GetCount_management("AFOLyr").getOutput(0))
# Clear all selected records...
# Create final feature class from results...
except: #(error handling specifics listed here)
del cur1, row