Outputting same name as input using Copy Features

2771
11
Jump to solution
07-08-2020 09:21 AM
AliciaShyu
Frequent Contributor

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

# Select by location all layers that intersect with AirBound and copy selection to GBD
= aprx.listMaps("Test")[0]
for layer in m.listLayers():
    if layer.name != AirBound:
        arcpy.SelectLayerByLocation_management (layer, "INTERSECT", AirBound)
        print (arcpy.GetMessages())

Is there any way to use "Copy Features" tool  where the output name is the same as the input name?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
JoshuaSharp-Heward
Frequent Contributor

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. 

View solution in original post

11 Replies
JoshuaBixby
MVP Esteemed Contributor

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?

0 Kudos
AliciaShyu
Frequent Contributor

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.

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
= aprx.listMaps("Test")[0]
for layer in m.listLayers():
    if layer.name != AirBound:
        arcpy.SelectLayerByLocation_management (layer, "INTERSECT", AirBound)
        print (arcpy.GetMessages())
0 Kudos
JoshuaSharp-Heward
Frequent Contributor

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.

0 Kudos
AliciaShyu
Frequent Contributor

Thanks but that didn't work. Got the error below

ERROR 000733: Output Feature Class: Same as input Input Features
Failed to execute (CopyFeatures).

0 Kudos
JoshuaSharp-Heward
Frequent Contributor

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. 

AliciaShyu
Frequent Contributor

That worked!! Thank you so much Josh!!

0 Kudos
JoshuaSharp-Heward
Frequent Contributor

No worries, glad that worked out!

0 Kudos
JoeBorgione
MVP Emeritus

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())‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
That should just about do it....
JoeBorgione
MVP Emeritus

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.

That should just about do it....
0 Kudos