arcpy.SelectLayerByLocation_management("layer1","WITHIN_A_DISTANCE", "layer2","100 Meters", "ADD_TO_SELECTION")
# Select ancillary layer within 10km... arcpy.SelectLayerByLocation_management("AFOLyr",SelDist,"Wells2Lyr",10000,SelAddTo)
arcpy.SelectLayerByAttribute_management("AFOLyr",SelClear)
# 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
And does anyone know why arcpy vs. manual selection would produce different counts, and/or why my arcpy results were correct most of the time, but incorrect some of the time?
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.