# coding=utf-8
import arcpy
import os
def sync_zgeo_schema(source_ds, target_ds):
"""
同步两个地理数据库的数据结构
参数:
source_ds: 源地理数据库zgeo 路径
target_ds: 目标地理数据库 zgeo 路径
"""
# 设置工作空间
arcpy.env.workspace = source_ds
# 获取源ds 中所有要素类和表
all_datasets = []
# 获取独立要素类
all_datasets.extend(arcpy.ListFeatureClasses())
# 遍历所有数据集
for ds in all_datasets:
if ds.endswith('_ZJ'):
continue
# if ds == u'C':
# print u'C'
# else:
# continue
ds_name = os.path.basename(ds)
source_path = os.path.join(source_ds, ds)
# 检查目标中是否存在
target_path = os.path.join(target_ds, ds_name)
exists = arcpy.Exists(target_path)
if not exists:
# 如果不存在,则创建整个要素类
try:
arcpy.FeatureClassToFeatureClass_conversion(
source_path,
target_ds,
ds_name
)
print('created featureclass: ' + ds_name)
except arcpy.ExecuteError as e:
print("创建 {} 失败: {}".format(ds_name ,e))
else:
# 如果已存在,比较字段
try:
# 获取源和目标字段列表(排除OID和几何字段)
source_fields = [f.name for f in arcpy.ListFields(source_path)
if not f.required]
target_fields = [f.name for f in arcpy.ListFields(target_path)
if not f.required]
# 找出缺失字段
missing_fields = set(source_fields) - set(target_fields)
if missing_fields:
# 添加缺失字段
for field in missing_fields:
# 获取源字段属性
src_field = [f for f in arcpy.ListFields(source_path)
if f.name == field][0]
# 创建字段
arcpy.AddField_management(
target_path,
field,
src_field.type,
field_length=src_field.length,
field_alias=src_field.aliasName,
field_is_nullable=src_field.isNullable
)
print("已添加字段 {} 到 {}".format(field,ds_name))
print("已同步字段: {}".format(ds_name))
except arcpy.ExecuteError as e:
print("同步 {} 字段失败: {}".format(ds_name,e))
def sync_table_schema(source_gdb, target_gdb):
"""
同步两个地理数据库的数据结构
参数:
source_gdb: 源地理数据库路径
target_gdb: 目标地理数据库路径
"""
# 设置工作空间
arcpy.env.workspace = source_gdb
# 获取源GDB中所有 表
all_tables = []
# 获取独立表
all_tables.extend(arcpy.ListTables())
# 遍历所有
for ds in all_tables:
ds_name = os.path.basename(ds)
source_path = os.path.join(source_gdb, ds)
# 检查目标中是否存在
target_path = os.path.join(target_gdb, ds_name)
exists = arcpy.Exists(target_path)
if not exists:
# 如果不存在,则创建整个要素类/表
try:
arcpy.TableToTable_conversion(
source_path,
target_gdb,
ds_name
)
print("已创建表: {}".format(ds_name))
except arcpy.ExecuteError as e:
print("创建 {} 失败: {}".format(ds_name ,e))
else:
# 如果已存在,比较字段
try:
# 获取源和目标字段列表(排除OID和几何字段)
source_fields = [f.name for f in arcpy.ListFields(source_path)
if not f.required]
target_fields = [f.name for f in arcpy.ListFields(target_path)
if not f.required]
# 找出缺失字段
missing_fields = set(source_fields) - set(target_fields)
if missing_fields:
# 添加缺失字段
for field in missing_fields:
# 获取源字段属性
src_field = [f for f in arcpy.ListFields(source_path)
if f.name == field][0]
# 创建字段
arcpy.AddField_management(
target_path,
field,
src_field.type,
field_length=src_field.length,
field_alias=src_field.aliasName,
field_is_nullable=src_field.isNullable
)
print("已添加字段 {} 到 {}".format(field,ds_name))
print("已同步字段: {}".format(ds_name))
except arcpy.ExecuteError as e:
print("同步 {} 字段失败: {}".format(ds_name,e))
# 使用示例
if __name__ == "__main__":
# source = u"E:\迁\TCGKNULL.gdb\zgeo" # type: unicode
# target = u'E:\迁\CK.gdb\zgeo'
# print("开始同步数据结构...")
# sync_zgeo_schema(source, target)
source = u"E:\迁\TCKNULL.gdb" # type: unicode
target = u'E:\迁\CK.gdb'
print("开始同步 表数据结构...")
sync_table_schema(source, target)
print("同步完成")