I have created a code that iterates through a geodatabase, intersecting each feature with a road network. I am not sure how to make it so the intersected features are output into a new geodatabase. Here is my code so far:
import arcpy
import os
arcpy.env.overwriteOutput = True
# Location of the data you want to import
arcpy.env.workspace = r"D:\Data\OriginalFCs.gdb"
# Output Location
out_workspace = r"D:\Data\IntersectedFCs.gdb"
# Lists all feature classes in the workspace
fc_list = arcpy.ListFeatureClasses()
# Iterating intersect through GDB
for fc in fc_list:
arcpy.Intersect_analysis(["RoadNetwork", fc], fc + "_Intersect", "NO_FID")
arcpy.CopyFeatures_management(fc, out_workspace)
I want the output intersected features to be saved into the geodatabase specified by out_workspace, which is what I attempted to do with arcpy.CopyFeatures_Management in the final line. However, the intersected features are still output to the input workspace and nothing is created in the output location.
How can I modify my code to place the intersected features created in the FOR loop to my output workspace?
1 Answer 1
this will do it
# Iterating intersect through GDB
for fc in fc_list:
outfc = os.path.join(out_workspace, fc + "_Intersect")
arcpy.Intersect_analysis(["RoadNetwork", fc], outfc, "NO_FID")
#arcpy.CopyFeatures_management(fc, outfc) # NOT NEEDED
there is no need for an intermediate output which you copyfeatures to the output gdb, just have intersect write directly to the output gdb.
the reason CopyFeatures doesn't work in you original case is that you have to specify the output location + fc name. Your code specified the out_workspace only.
Explore related questions
See similar questions with these tags.
arcpy.CopyFeatures_management(fc, os.path.join(out_workspace, 'some_name')
? You'd have to make sure to change'some_name'
every loop.fc_list2 = arcpy.ListFeatureClasses("*_Intersect") for fc2 in fc_list2: arcpy.CopyFeatures_management(fc, os.path.join(out_workspace, fc2))
If anyone has a more elegant solution that deals with the first for loop directly, I'd love to hear it!arcpy.CopyFeatures_management(fc, os.path.join(out_workspace, fc + '_intersect')
be an option?