Outputting same name as input using Copy Features

306
11
Jump to solution
07-08-2020 09:21 AM
AliciaShyu
New Contributor III

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!

Reply
0 Kudos
1 Solution

Accepted Solutions
JoshuaSharp-Heward
Occasional 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?

Reply
0 Kudos
AliciaShyu
New Contributor III

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())
Reply
0 Kudos
JoshuaSharp-Heward
Occasional 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.

Reply
0 Kudos
AliciaShyu
New Contributor III

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

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

Reply
0 Kudos
JoshuaSharp-Heward
Occasional 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

AliciaShyu
New Contributor III

That worked!! Thank you so much Josh!!

Reply
0 Kudos
JoshuaSharp-Heward
Occasional Contributor

No worries, glad that worked out!

Reply
0 Kudos
JoeBorgione
MVP Esteemed Contributor

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())‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
can't wait to retire....
JoeBorgione
MVP Esteemed Contributor

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.

can't wait to retire....
Reply
0 Kudos