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!
Code:
# Imports...
import arcpy
import sys
import traceback
from arcpy import env
# Environments...
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"
try:
# Delete fields if they exist...
#(left out)
# Create feature layer...
arcpy.MakeFeatureLayer_management(Wells2,"Wells2Lyr")
# Add count fields...
L2 = ["Count_10kmAFOs","Count_10kmConfmnts","Count_10kmFeedlots", \
"Count_10kmMixed","Count_10kmHogs"]
for item in L2:
arcpy.AddField_management("Wells2Lyr",item,"LONG")
# Create ancillary feature layer...
arcpy.MakeFeatureLayer_management(AFOs,"AFOLyr")
# 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 + "'"
arcpy.SelectLayerByAttribute_management("Wells2Lyr",SelNew,WhereClause)
# Select ancillary layer within 10km...
arcpy.SelectLayerByLocation_management("AFOLyr",SelDist,"Wells2Lyr",10000,SelAddTo)
# Count selected records in each layer and assign to appropriate fields...
row.Count_10kmAFOs = int(arcpy.GetCount_management("AFOLyr").getOutput(0))
cur1.updateRow(row)
# Clear all selected records...
arcpy.SelectLayerByAttribute_management("Wells2Lyr",SelClear)
arcpy.SelectLayerByAttribute_management("AFOLyr",SelClear)
# Create final feature class from results...
arcpy.CopyFeatures_management("Wells2Lyr",Wells3)
except: #(error handling specifics listed here)
finally:
del cur1, row
Bookmarks