arcpy: copy feature classes from sde to folder?

2014
9
Jump to solution
03-02-2018 02:36 PM
JaredPilbeam2
MVP Regular Contributor

Is there a way using arcpy to grab feature classes directly from an SDE and copy to a folder?

I have a real chunky way to do it, namely using these functions:

arcpy.mapping.ListDataFrames()
arcpy.mapping.ListLayers()
arcpy.mapping.AddLayer()
arcpy.FeatureClassToFeatureClass_conversion()‍‍

The script overall adds features from an SDE to an mxd then saves them as shapefiles in a folder. How can I avoid using the mxd as a sort of springboard?

0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Emeritus

Just fill in the blanks:

import arcpy
# full path to connection file:
infeatures = r'T:\GIS Documents\AddressPOC2017\AddIn\SLCOGIS_Joe.sde\slcogis.GDB_ADD.Quadrants'
outpath = r'C:\TestFolder'
outfeature = 'Quads.shp'
arcpy.FeatureClassToFeatureClass_conversion(infeatures,outpath,outfeature)
<Result 'C:\\TestFolder\\Quads.shp'>
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

From the help page:

FeatureClassToFeatureClass_conversion (in_features, out_path, out_name, {where_clause}, {field_mapping}, {config_keyword})

If you only want a few features, say the first 10 features, use a where clause like this:

where = 'OBJECTID < = 10'
arcpy.FeatureClassToFeatureClass_conversion(infeatures,outpath,outfeature,where)
‍‍‍‍
That should just about do it....

View solution in original post

9 Replies
JoeBorgione
MVP Emeritus

Just fill in the blanks:

import arcpy
# full path to connection file:
infeatures = r'T:\GIS Documents\AddressPOC2017\AddIn\SLCOGIS_Joe.sde\slcogis.GDB_ADD.Quadrants'
outpath = r'C:\TestFolder'
outfeature = 'Quads.shp'
arcpy.FeatureClassToFeatureClass_conversion(infeatures,outpath,outfeature)
<Result 'C:\\TestFolder\\Quads.shp'>
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

From the help page:

FeatureClassToFeatureClass_conversion (in_features, out_path, out_name, {where_clause}, {field_mapping}, {config_keyword})

If you only want a few features, say the first 10 features, use a where clause like this:

where = 'OBJECTID < = 10'
arcpy.FeatureClassToFeatureClass_conversion(infeatures,outpath,outfeature,where)
‍‍‍‍
That should just about do it....
JaredPilbeam2
MVP Regular Contributor

That worked great, thanks. I adapted it to save two files as you can see in the variables, and it works fine. However, after running the debugger on it I noticed it's looping through the whole thing numerous times and taking longer than it should to complete. Maybe you can spot why that is?

import arcpy

arcpy.env.overwriteOutput = True

# full path to connection file:
infeatures = ([r'Database Connections\Jared to Plainfield.sde\gisedit.DBO.MGU_Will\gisedit.DBO.Street',
               r'Database Connections\Jared to Plainfield.sde\gisedit.DBO.MGU_Will\gisedit.DBO.Address_Points'])
outpath = r'\\gisfile\GISstaff\Jared\Test'
outfeatures = ['WillCounty_Streets', 'WillCounty_AddressPoints']

for infs in infeatures:
    print infs
    for otfs in outfeatures:
        arcpy.FeatureClassToFeatureClass_conversion(infs,outpath,otfs)
        print otfs
0 Kudos
JoeBorgione
MVP Emeritus

Nothing jumps out at me; if you run it in regular mode, or even copy and paste it into a python window, is it still slow?

That should just about do it....
0 Kudos
JaredPilbeam2
MVP Regular Contributor

When I run it in regular mode both print statements are printed twice. I've attempted to use break after the for statements, but then it only runs the loop enough to print one of the two variables. I also tried to exit the loop with continue and pass, which didn't do anything.

0 Kudos
JoeBorgione
MVP Emeritus

I like to use the length of the list as the controller for the loop:

infeatures = ([r'Database Connections\Jared to Plainfield.sde\gisedit.DBO.MGU_Will\gisedit.DBO.Street',
               r'Database Connections\Jared to Plainfield.sde\gisedit.DBO.MGU_Will\gisedit.DBO.Address_Points'])
outpath = r'\\gisfile\GISstaff\Jared\Test'
outfeatures = ['WillCounty_Streets', 'WillCounty_AddressPoints']

#for infs in infeatures:
    #print infs
    #for otfs in outfeatures:
        #arcpy.FeatureClassToFeatureClass_conversion(infs,outpath,otfs)
        #print otfs

for i in range(len(infeatures)):
    print infeatures[i]
    for j in range(len(outfeatures)):
       print outfeatures[j]

 See if that makes a difference....

That should just about do it....
0 Kudos
JaredPilbeam2
MVP Regular Contributor

Joe,
Same result. I put the question to stack exchange, where they often like to do a lot of nagging rather than helping. From what they were saying, though, I guess it's repeating because the outer loop loops once for each variable and then loops for each variable again for the inner loop.

So, I guess that sort of draws out my main problem--I'm wondering how to use a list as a means to have one variable in the arcpy.FeatureClassToFeatureClass_conversion() function instead of repeating the block of code for each variable, basically. This wasn't ,my original question, so maybe I should post an additional one.

0 Kudos
JoeBorgione
MVP Emeritus

Now that you mention it, the stack exchange comments are spot on.  Start a new thread, and I'll look at it as others will I'm sure.  One way or the other we'll get things straightened out and you'll be golden....

That should just about do it....
0 Kudos
JamesMacKay3
Occasional Contributor

You can use the workspace environment variable and ListFeatureClasses() to step through your classes:

arcpy.env.workspace = r"Database Connections\YourConnection.sde"
for fc in arcpy.ListFeatureClasses():
    arcpy.FeatureClassToFeatureClass_conversion(fc, targetFolder, fc)‍‍‍

ListFeatureClasses has some optional parameters for filtering feature classes by name and type as well.  Depending on your naming conventions it may not be a bad idea to look into ValidateTableName() as well, you can call it before calling FC to FC to make sure the table name is valid for the target (usually is but doesn't hurt to check).