Python - select layer by location.

3125
1
05-11-2015 09:13 AM
AlexanderHaussman
New Contributor

Hi, I am looking for help correcting my python script. I am having trouble with the select layer by location from a buffer feature layer. Thanks!

0 Kudos
1 Reply
DarrenWiens2
MVP Honored Contributor

Check the help page. Look at the example. Note the quotes, particularly around the overlap type.

Also, please review how to post code blocks on GeoNet so we don't have to download a potentially harmful Python script. Here is the uncorrected script, for future reference:

import arcpy, os

arcpy.env.workspace = "F:\\GEO443\\Final"
arcpy.env.overwriteOutput = True

brown = "F:\\GEO443\\Final\\BRO_ADDS\\BRO_ADDS.shp"
green = "F:\\GEO443\\Final\\GRE_ADDS\\GRE_ADDS.shp"
hancock = "F:\\GEO443\\Final\\HAN_ADDS\\HAN_ADDS.shp"
logan = "F:\\GEO443\\Final\\LOG_ADDS\\LOG_ADDS.shp" 
muskingum = "F:\\GEO443\\Final\\MUS_ADDS\\MUS_ADDS.shp"

try:
    ## CreateFileGDB_management (out_folder_path, out_name, {out_version})
    ## execute create file .gdb
    #arcpy.CreateFileGDB_management("F:\\GEO443\\Final","county.gdb")
    print "file GDB created"
   
    ##FeatureClassToGeodatabase_conversion (Input_Features, Output_Geodatabase)
    #arcpy.FeatureClassToGeodatabase_conversion([brown,green,hancock,logan,muskingum],"F:\\GEO443\\Final\\county.gdb")
    print "shape files moved to gdb"
    
    arcpy.env.overwriteOutput = True 
    arcpy.env.workspace = "F:\\GEO443\\Final\\county.gdb"

    kroger_selection = ('"COMMENT" = \'KROGER\'')    
    fcs = arcpy.ListFeatureClasses()
    for fc in fcs:
        print fc
        print ""
        fc_kroger = fc + "kroger"
        fc_lyr = arcpy.MakeFeatureLayer_management(fc,fc_kroger)
        print "Feature layer created"
        ## SelectLayerByAttribute_management (in_layer_or_view, {selection_type}, {where_clause})
        arcpy.SelectLayerByAttribute_management(fc_lyr,"NEW_SELECTION",kroger_selection)
        arcpy.CopyFeatures_management(fc_lyr,fc + "krogerselection")
        print "Kroger Selected"

        ## Buffer_analysis (in_features, out_feature_class, buffer_distance_or_field, {line_side}, {line_end_type}, {dissolve_option}, {dissolve_field})
    fcs = arcpy.ListFeatureClasses("*krogerselection")
    for fc in fcs:
        print fc
        arcpy.Buffer_analysis(fc, fc + "Buff", "1 Mile")
    
        print "Features Copied"
        print "Buffer Created"

        ## SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type})    
    fcs = arcpy.ListFeatureClasses("*krogerselection")
    for fc in fcs:
        print fc
        fc_location = fc + "location"
        fc_Addr = arcpy.MakeFeatureLayer_management(fc, fc_location)
        print "location fc created"
        arcpy.SelectLayerByLocation_management(fc_Addr, WITHIN, fc + "Buff")
        arcpy.CopyFeatures_management(fc_Addr, fc + "addresses")
        print "addresses selected" 
    
except:
    print arcpy.GetMessages(2)

Apparently, this is a hard assignment.