Arcpy - Select by location - iterate through gdb feature class and export to new fc

04-05-2020 08:06 PM
New Contributor II

Hi, I am trying to use arcpy to run a select by location and export the resulting selection (intersect) as a new feature class. I have set my workspace to the gdb containing the featureclasses I want to select by, and the features I want to select are a tile index feature class called 'tile_index'. The code will run but doesn't have any output. Any help would be great.


# Import arcpy and set path to data
import arcpy
arcpy.env.workspace = r"C:\folder\Data.gdb\"

# Get the list of the featureclasses to process
fc_tables = arcpy.ListFeatureClasses()

for fc in fc_tables:
arcpy.SelectLayerByLocation_management('fc', 'intersect', 'tile_index')
arcpy.CopyFeatures_management(fc_tables, r"C:\Data.gdb\%Name%_selection")‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
1 Reply
Regular Contributor

Don't know if I'm up to date but I think you have to use make feature layer infront of your select by location. Out of the help files:

import arcpy
arcpy.env.workspace = "c:/data/mexico.gdb"
# Make a layer and select cities which overlap the chihuahua polygon
arcpy.MakeFeatureLayer_management('cities', 'cities_lyr')
arcpy.SelectLayerByLocation_management('cities_lyr', 'intersect', 'chihuahua')

you should use:

arcpy.env.workspace = r"C:\folder\Data.gdb" and not
arcpy.env.workspace = r"C:\folder\Data.gdb\"

r"C:\Data.gdb\%Name%_selection" will not work in python code, 
you will need to create a unique name for your output file.

0 Kudos