[GIS] Using Select By Location in ArcPy

arcpyselect-by-location

How do you create a new feature class from the select by location tool in python?
This is my script that works but I just don't know what the next step is.

arcpy.SelectLayerByLocation_management("Buildings", "WITHIN_A_DISTANCE", "Hydrography", "50 feet", "NEW_SELECTION","NOT_INVERT") 

Best Answer

To do this with python you need to create some variables first so you can call the selection and with the arcpy.CopyFeatures tool copy the selection to a new feature class.

# Define output feature class location    
fc = "C:\Users\Documents\ArcGIS\Default.gdb\Testers"
# Define Selection criteria
Selection = arcpy.SelectLayerByLocation_management('Points', "WITHIN", 'Trajectory')
# Define output selection and fc
arcpy.CopyFeatures_management(Selection, fc)

This example was used within the Python interpreter in ArcMap. You can see that by using variables it makes everything easier to use and understand.

The example you provide should be something like this:

import arcpy 
#Set geoprocessing environments 
arcpy.env.workspace = "C:/Student/PythonBasics10_0/Westerville.gdb"
arcpy.env.overwriteOutput = True 

# Set name of output fc and select buildings by location
Outputfc =  "BuildingsWithin50ft"
Selection = arcpy.SelectLayerByLocation_management("Buildings", "WITHIN_A_DISTANCE", "Hydrography", "50 feet", "NEW_SELECTION","NOT_INVERT") 

arcpy.CopyFeatures_management(Selection, Outputfc) 
Related Question