I'm writing a script that has 30+ layers and a boundary layer. I need to copy all the layers that intersect with the boundary to a GDB.
I have the following for the "Select layer by location" tool
Is there any way to use "Copy Features" tool where the output name is the same as the input name?
Thanks!
Solved! Go to Solution.
Interesting, let's make a slight tweak to that then:
outname = os.path.join(GDB, layer.name)
arcpy.CopyFeatures_management(layer, outname)
just to be sure it's writing to the geodatabase that we want it to.
A layer name doesn't have to match the data set name, so what name are you wanting to use?
Where do you want to copy the features to exactly?
The features would go into the new GDB. I want to use their original names from SDE
I'm trying to avoid creating 30+ output names using variables.
Sorry, should have copy the entire script.
Are the layer names the same as the names in the SDE? If so the following line should do the trick:
arcpy.CopyFeatures_management(layer, layer.name)
just below the selection.
Thanks but that didn't work. Got the error below
ERROR 000733: Output Feature Class: Same as input Input Features
Failed to execute (CopyFeatures).
Interesting, let's make a slight tweak to that then:
outname = os.path.join(GDB, layer.name)
arcpy.CopyFeatures_management(layer, outname)
just to be sure it's writing to the geodatabase that we want it to.
That worked!! Thank you so much Josh!!
No worries, glad that worked out!
I took the liberty of copying and pasting your code into the syntax highlighter. Easier to read and note issues with line numbers...
import arcpy, os, sys, math, time, smtplib
arcpy.env.orverwriteOutput = True
# Project Variables
aprx = arcpy.mp.ArcGISProject ("CURRENT")
GDB = "\\\\cdmgis\\d$\\Scripts\\Python\\AirportFTP\\Airport.gdb"
AirportBound = "\\\\cdmgis\\d$\\Scripts\\Python\\AirportFTP\\AirportBoundary.gdb"
AirBound = AirportBound +"\\CloseGISBoundary"
ExtAirBound = AirportBound + "\\ExtendedGISBoundary"
# # Delete old GDB and creates new one
if arcpy.Exists (GDB):
arcpy.Delete_management (GDB)
arcpy.AddMessage(arcpy.GetMessages())
if not os.path.exists (os.path.dirname(GDB)):
os.mkdir (os.path.dirname(GDB))
arcpy.CreateFileGDB_management (os.path.dirname(GDB), os.path.basename(GDB))
print (arcpy.GetMessages())
arcpy.env.workspace = GDB
# Select by location all layers that intersect with AirBound and copy selection to GBD
m = aprx.listMaps("Test")[0]
for layer in m.listLayers():
if layer.name != AirBound:
arcpy.SelectLayerByLocation_management (layer, "INTERSECT", AirBound)
print (arcpy.GetMessages())
I'm a little confused with GDB; seems like you are all over the map checking for it, deleting it, creating it etc: lines 11 through 17.