I have a ArcView map with a few of layers, and I want to export them to one location?

999
4
08-15-2011 08:35 AM
qiangxue
New Contributor
the datasources of these layers are different, some of them are shapefile, and some are from geodatabase, Now I want to write a python script, and export all data into another new geodatabase, and relink them with map layers, and save a new copy. How can I do this? Could anybody give me a hint? I am new to python, and not very familiar with python functions. I did not find the functions I want. Thanks.
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
If all your shapefiles reside in one folder, and your feature classes within a single geodatabase you can use the 'arcpy.ListFeatureClasses' function to list all shapefiles and feature classes then append them to a list.

Next, you can use the 'arcpy.FeatureClassToGeodatabase_conversion' function to convert the shapefiles/feature classes to a new geodatabase.

Finally, you can run the 'replaceWorkspaces' and 'replaceDataSource' methods to remap the datasources and save it as a new MXD.

Ex:

import arcpy, os
from arcpy import env
from arcpy import mapping


folder = r"C:\temp\python"
env.workspace = folder

fcList = []

lstFCs = arcpy.ListFeatureClasses("*")
for fc in lstFCs:
    fcList.append(os.path.join(folder + "\\" + fc))

database = r"C:\temp\python\test.gdb"
env.workspace = database

lstFCs = arcpy.ListFeatureClasses("*")
for fc in lstFCs:
    fcList.append(database + "\\" + fc)

for fc in fcList:
    print fc
    arcpy.FeatureClassToGeodatabase_conversion(fc, r"C:\temp\test2.gdb")
    print "Successfully converted"
    

mxd = mapping.MapDocument(r"C:\temp\python\test.mxd")
mxd.replaceWorkspaces(r"C:\Temp\Python", "SHAPEFILE_WORKSPACE", r"C:\Temp\test2.gdb", "FILEGDB_WORKSPACE")
mxd.findAndReplaceWorkspacePaths(r"C:\temp\python\test.gdb", r"C:\temp\test2.gdb")
mxd.saveACopy(r"C:\Temp\Project2.mxd")
print "Save Successful"


If your data resides in multiple folders/geodatabase I would recommend looking into the os.walk function.
0 Kudos
qiangxue
New Contributor
thank you very much. I will try it later.
0 Kudos
qiangxue
New Contributor
but if I just want to export the data sources of these existing layers in my map to the new location instead of all files in the old location. Which function I can use? Thanks.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You can use the arcpy.mapping.ListLayers function to access the dataSource property.  Next you can append this to a list, convert the layers to the new geodatabase, and update the MXD.  Ex:

import arcpy
from arcpy import env
from arcpy import mapping

env.workspace = r"C:\temp\python"
 
list = []

mxd = mapping.MapDocument(r"C:\temp\python\test.mxd")
for df in mapping.ListDataFrames(mxd, "*"):
    for layers in mapping.ListLayers(mxd, "*", df):
        list.append(layers.dataSource)

for n in list:
    arcpy.FeatureClassToGeodatabase_conversion(n, r"C:\temp\Test2.gdb")

mxd.replaceWorkspaces(r"C:\Temp\Python", "SHAPEFILE_WORKSPACE", r"C:\Temp\test2.gdb", "FILEGDB_WORKSPACE")
mxd.findAndReplaceWorkspacePaths(r"C:\temp\python\test.gdb", r"C:\temp\test2.gdb")
# mxd.replaceWorkspaces(r"C:\temp\python\test.gdb", "FILEGDB_WORKSPACE", r"C:\temp\python\test.gdb", "FILEGDB_WORKSPACE")
mxd.saveACopy(r"C:\Temp\Project2.mxd")
print "Save Successful"
0 Kudos