I was wondering if someone could help me with something. This is probably an easy fix, but I do not have SDE nor have ever used it so I am unable to test for SDE data sources. A friend of mine asked me if I could write him a script to loop thru all layers in an mxd and export them out to a new user defined file gdb. This was a piece of cake. I used a test mxd that had data sources from layer files, shapefiels, and file geodatabase feature classes. It looped thru the mxd no problem and exported all the layers to feature classes in the new file gdb. Since I could not get it to produce any errors I went ahead and sent it to him.
However, he emailed me back with some error messages he received that were caused by layers that have a data source from an SDE geodatabase. I am wondering if someone who has SDE could test this for me and show me what needs tweaked so it can pull data sources from SDE geodatabases to export to the new file gdb using the FeatureClassToFeatureClass tool. Here is my code:
This works perfectly for me with all data sources that do not come from SDE geodatabases.
However, he emailed me back with some error messages he received that were caused by layers that have a data source from an SDE geodatabase. I am wondering if someone who has SDE could test this for me and show me what needs tweaked so it can pull data sources from SDE geodatabases to export to the new file gdb using the FeatureClassToFeatureClass tool. Here is my code:
Code:
# Loops through an MXD and exports all exportable layers to a new file gdb
import arcpy, os
from os import path as p
from arcpy import mapping as m
arcpy.env.overwriteOutput = True
def ExportLayersToGDB(mapDoc, gdb_path, gdb_name):
# create File GDB
if not gdb_name.endswith('.gdb'):
gdb_name += '.gdb'
gdb = p.join(gdb_path, gdb_name)
if arcpy.Exists(gdb):
arcpy.Delete_management(gdb)
arcpy.CreateFileGDB_management(gdb_path, gdb_name, 'CURRENT')
print 'Created "%s"\n' %gdb
# map document object
print 'Searching through "%s"\n' %mapDoc
mxd = m.MapDocument(mapDoc)
for df in m.ListDataFrames(mxd):
for lyr in m.ListLayers(mxd, '*', df):
if lyr.supports('DATASOURCE'):
infc = lyr.dataSource
outfc = p.basename(p.splitext(infc)[0])
try:
arcpy.FeatureClassToFeatureClass_conversion(infc, gdb, outfc)
print 'Exported \'%s\' layer to "%s"' %(lyr,gdb_name)
except:
print 'Could not export \'%s\' layer to "%s"' %(lyr,gdb_name)
print '\nAll layers in "%s" Successfully exported to "%s"' %(p.basename(mapDoc),gdb_name)
if __name__ == '__main__':
mxd = r'C:\Testing\Export_test.mxd'
path = r'C:\Testing'
gdb_name = 'Test.gdb'
ExportLayersToGDB(mxd, path, gdb_name)