Copy a list of feature classes from Enterprise to Geodatabase

2595
6
Jump to solution
07-19-2017 05:57 AM
AhmadSALEH1
Occasional Contributor III

Hi

I am trying to write a code that uses “arcpy.CopyFeatures_management” to copy all feature classes inside an Enterprise geodatabase to a Geodatabase.

The issue here happens when using FOR loop to copy the whole list of F.C’s to the geodatabase, I am trying to assign the same name for the features. But it seems that there is something that I am missing.

I tried different statements for “out_feature_class” with no luck, sometimes the code fails to run or it copies a single F.C only.

import arcpy ,sys
adminconn=r'C:\1\b2.sde'
gdbFile=r'c:\1\x.gdb'

arcpy.env.workspace=adminconn
arcpy.env.overwriteOutput=True


SDEfcList=arcpy.ListFeatureClasses()
arcpy.env.workspace=gdbFile
for dataitem in SDEfcList:
    fcdesc = arcpy.Describe(dataitem)
    arcpy.CopyFeatures_management(dataitem,gdbFile+fcdesc.name)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks

0 Kudos
1 Solution

Accepted Solutions
JonathanQuinn
Esri Frequent Contributor

The first thing that stands out is the path of the output data.  If you add

print(gdbFile+fcdesc.name)

You'll probably see that you're missing an OS separator between the gdbFile name and the data set name.

You can use the OS module to correct that:

os.path.join(gdbFile,fcdesc.name)

The other thing is that you're already getting the feature class name from the loop, stored in the dataitem variable, so the Describe function is not necessary.  You'll also likely need to split off the database name and data owner as the feature class name will likely come across as database.owner.table and periods are not supported in the name of a feature class in a FGDB.

import arcpy, os
adminconn=r'C:\1\b2.sde'
gdbFile=r'c:\1\x.gdb'
arcpy.env.workspace=adminconn
arcpy.env.overwriteOutput=True

SDEfcList=arcpy.ListFeatureClasses()
for dataitem in SDEfcList:
    arcpy.CopyFeatures_management(dataitem,os.path.join(gdbFile,dataitem.split("SDE.")[1]))‍‍‍‍‍‍‍‍‍

View solution in original post

6 Replies
JonathanQuinn
Esri Frequent Contributor

The first thing that stands out is the path of the output data.  If you add

print(gdbFile+fcdesc.name)

You'll probably see that you're missing an OS separator between the gdbFile name and the data set name.

You can use the OS module to correct that:

os.path.join(gdbFile,fcdesc.name)

The other thing is that you're already getting the feature class name from the loop, stored in the dataitem variable, so the Describe function is not necessary.  You'll also likely need to split off the database name and data owner as the feature class name will likely come across as database.owner.table and periods are not supported in the name of a feature class in a FGDB.

import arcpy, os
adminconn=r'C:\1\b2.sde'
gdbFile=r'c:\1\x.gdb'
arcpy.env.workspace=adminconn
arcpy.env.overwriteOutput=True

SDEfcList=arcpy.ListFeatureClasses()
for dataitem in SDEfcList:
    arcpy.CopyFeatures_management(dataitem,os.path.join(gdbFile,dataitem.split("SDE.")[1]))‍‍‍‍‍‍‍‍‍
XanderBakker
Esri Esteemed Contributor

If your data is stored in feature datasets inside the database, you will only get the featureclasses that are outside any feature dataset. To avoid that you will have to get the list of datasets and loop through that list and use it in the list Featureclasses. Example:

import arcpy
import os

ws = r'C:\Esri\Curso\Aguas\WaterUtilityNetworkEditing - Ejercicio 2\localgovernment.gdb'
arcpy.env.workspace = ws

fdss = arcpy.ListDatasets()
fdss.append('')

for fds in fdss:
    fc_names = arcpy.ListFeatureClasses(feature_dataset=fds)
    for fc_name in fc_names:
        fc = os.path.join(ws, fds, fc_name)
        print fc
AhmadSALEH1
Occasional Contributor III

Thanks Jonathan,

it worked fine with me now. another question I am trying do do the same method to use Table to table tool, it seems that  the split ("SDE.") fails to truncate database.owner. for the Tables!

arcpy.TableToTable_conversion(tableitem,os.path.join(gdbFile,tableitem.split("SDE.")[0]))‍

what do you think ?

0 Kudos
JonathanQuinn
Esri Frequent Contributor

Woops, it should be

arcpy.TableToTable_conversion(tableitem,os.path.join(gdbFile,tableitem.split("SDE.")[1]))

Which means split on SDE., (or whatever database owner you're using), and take the second item in the list which is the FC name.

AhmadSALEH1
Occasional Contributor III

Thanks again Jonathan,

I tried to change it form [0] to [1] but I keep getting the following message:

Failed to execute. Parameters are not valid. ERROR 00732: Output Location. Dataset .... dose not exist or not supported.

I think that the tool considers the output path (os.path.join(gdbFile,tableitem.split("DBO.")[1])) as a dataset and tries to store the table inside it!

TableList=arcpy.ListTables()
for tableitem in TableList :

      arcpy.TableToTable_conversion(tableitem,os.path.join(gdbFile,tableitem.split("DBO.")[1]))
0 Kudos
JonathanQuinn
Esri Frequent Contributor

The documentation for the Table to Table tool describes the usage:

Syntax

TableToTable_conversion (in_rows, out_path, out_name, {where_clause}, {field_mapping}, {config_keyword})

You need the in_table, (tableitem), out_path, (gdbFile), and out_name, (tableitem.split("DBO.")[1] as separate parameters.

The Copy Features tool requires only input and output data paths:

Syntax

CopyFeatures_management (in_features, out_feature_class, {config_keyword}, {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3})