HowTo:  Automate copying datasets from one database to another

相关信息
Article ID: 40831
Software:
ArcGIS - ArcEditor 10
ArcGIS - ArcInfo 10
ArcGIS for Desktop Advanced 10.1, 10.2
Platforms:
Windows Server 2003, Vista, Server 2008, Windows 8

问题描述
GIS managers often need to copy datasets from one database to another.

Instructions provided describe how to copy feature datasets, feature classes and tables from one ArcSDE database to another.
已邀请:

易智瑞技术支持

赞同来自:

解决方案
The below Python script automates the process of copying feature datasets, feature classes and tables from one ArcSDE geodatabase to another.


Modify the below three parameters found under "__main_" towards the end of the script: 1) Set the 'start_db' parameter connection string to the origin database For example: start_db = r"Database Connections\Jie.sde" 2) Set the 'end_db' connection string to the database to which the datasets are to be copied. For example: end_db = r"Database Connections\MXD2.sde" 3) For the starting database, set the 'num' parameter to the number of characters seen from ArcCatalog that serves as the database and owner of schema, including the dots. For example, for a feature class with schema 'sde.sde.parcel', the number of characters that represents the prefix 'sde.sde.' is 8. num = 8


import arcpy, os, string


def CopyDatasets(start_db,end_db,num):

#Set workspaces
arcpy.env.workspace = start_db
wk2 = end_db
datasetList = arcpy.ListDatasets()

#for feature classes within datasets
for dataset in datasetList:
print "Reading: {0}".format(dataset)
name = arcpy.Describe(dataset)
new_data=name.name[num:]
if arcpy.Exists(wk2 + os.sep + new_data)==False:

arcpy.Copy_management(dataset, wk2 + os.sep + new_data)
print "Completed copy on {0}".format(new_data)

else:
print "Dataset {0} already exists in the end_db so skipping".format(new_data)
#Clear memory
del dataset

def CopyFeatureClasses(start_db,end_db,num):

#Set workspaces
arcpy.env.workspace = start_db
wk2 = end_db
datasetList = arcpy.ListDatasets()

#for feature classes within datasets
for fc in arcpy.ListFeatureClasses():
print "Reading: {0}".format(fc)
name = arcpy.Describe(fc)
new_data=name.name[num:]
if arcpy.Exists(wk2 + os.sep + new_data)==False:

arcpy.Copy_management(fc, wk2 + os.sep + new_data)
print "Completed copy on {0}".format(new_data)

else:
print "Feature class {0} already exists in the end_db so skipping".format(new_data)
#Clear memory
del fc

def CopyTables(start_db,end_db,num):

#Set workspaces
arcpy.env.workspace = start_db
wk2 = end_db
datasetList = arcpy.ListDatasets()

#for feature classes within datasets
for table in arcpy.ListTables():
print "Reading: {0}".format(table)
name = arcpy.Describe(table)
new_data=name.name[num:]
if arcpy.Exists(wk2 + os.sep + new_data)==False:

arcpy.Copy_management(table, wk2 + os.sep + new_data)
print "Completed copy on {0}".format(new_data)

else:
print "Table {0} already exists in the end_db so skipping".format(new_data)
#Clear memory
del table


if __name__== "__main__":

start_db = r"Database Connections\Jie.sde" #Origin Database
end_db = r"Database Connections\MXD2.sde" #To database
num = 8 #number of characters in schema (for example: sde.sde. is 8)
CopyDatasets(start_db,end_db,num)
CopyFeatureClasses(start_db,end_db,num)
CopyTables(start_db,end_db,num)




创建及修改时间
Created: 1/18/2013

Last Modified: 1/28/2013
原文链接
http://support.esri.com/en/kno ... 40831

要回复问题请先登录注册