ArcPy 拷贝数据库

使用Python脚本进行图形数据库的拷贝。

原始帖子地址:https://www.2cto.com/database/201302/187391.html

整理Python代码:

 1 # -*- coding: utf-8 -*-
 2 # nightroad
 3 import arcpy, os, string,sys
 4 reload(sys)
 5 sys.setdefaultencoding( "utf-8" )
 6 
 7 def CopyDatasets(start_db,end_db,num):
 8     try:
 9         #Set workspaces 
10         arcpy.env.workspace = start_db
11         wk2 = end_db
12         datasetList = arcpy.ListDatasets()
13         #for feature classes within datasets
14         for dataset in datasetList:
15             print "Reading: {0}".format(dataset)
16             name = arcpy.Describe(dataset)
17             new_data=name.name[num:]
18             if (not arcpy.Exists(wk2 + os.sep + new_data)):
19                 arcpy.Copy_management(dataset, wk2 + os.sep + new_data)
20                 print "Completed copy on {0}".format(new_data)
21             else:
22                 print "Dataset {0} already exists in the end_db so skipping".format(new_data) #如果有同名的就Skip
23         #Clear memory
24         del dataset
25     except Exception as e:
26         arcpy.AddError(e.message)
27 
28 def CopyFeatureClasses(start_db,end_db,num):  
29     try:
30         #Set workspaces
31         arcpy.env.workspace = start_db
32         wk2 = end_db
33         datasetList = arcpy.ListDatasets()
34         #for feature classes within datasets
35         for fc in arcpy.ListFeatureClasses():
36             print "Reading: {0}".format(fc)
37             name = arcpy.Describe(fc)
38             new_data=name.name[num:]
39             if arcpy.Exists(wk2 + os.sep + new_data)==False:
40                 arcpy.Copy_management(fc, wk2 + os.sep + new_data)
41                 print "Completed copy on {0}".format(new_data)
42             else:
43                 print "Feature class {0} already exists in the end_db so skipping".format(new_data)
44         #Clear memory
45         del fc  
46     except Exception as e:  
47         arcpy.AddError(e.message)
48 
49 def CopyTables(start_db,end_db,num):
50     try:
51     #Set workspaces
52         arcpy.env.workspace = start_db
53         wk2 = end_db
54         datasetList = arcpy.ListDatasets()
55         #for feature classes within datasets
56         for table in arcpy.ListTables():
57             print "Reading: {0}".format(table)
58             name = arcpy.Describe(table)
59             new_data=name.name[num:]
60             if arcpy.Exists(wk2 + os.sep + new_data)==False:
61                 arcpy.Copy_management(table, wk2 + os.sep + new_data)
62                 print "Completed copy on {0}".format(new_data)
63             else:
64                 print "Table {0} already exists in the end_db so skipping".format(new_data)
65         #Clear memory
66         del table
67     except Exception as e:
68         arcpy.AddError(e.message)
69         
70 if __name__== "__main__":  
71     start_db =r'C:\Users\nightroad\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\test.sde' #源工作空间  
72     end_db = r'E:\Test\Test.gdb'  #目的工作空间  
73     num =0                              # (例如: sde.sde. is 8)  
74     CopyDatasets(start_db,end_db,num)  
75     CopyFeatureClasses(start_db,end_db,num)  
76     CopyTables(start_db,end_db,num)  

Python脚本的目的就是将获得源工作空间和目的工作空间,里面有一个Num的选择,根据数据库不一样来记录要素类或者数据集前的字符串:
SQL Server:sde.sde.featureclass或者sde.dbo.featureclass
那么只记录sde.sde.(sde.dbo.)一共8个字符,num=8
Oracle:test.featureclass
那么只记录test.一共5个字符串,num=5
FileGDB,没有前缀,num=0

转载于:https://www.cnblogs.com/nightroad/p/9627027.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值