shsmi_sysdb_nex/translate2old.py

349 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
import os
import shutil
import time
from imp import reload
import arcpy
import inspect
import ConfigParser
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# 增删改gdb
add_gdb = 'addFeatures.gdb'
edit_gdb = 'editFeatures.gdb'
delete_gdb = 'deleteFeatures.gdb'
# 输出gdb
out_folder = 'OldGDB'
out_addedit_gdb = 'addAndeditFeatures.gdb'
out_delete_gdb = 'deleteFeatures.gdb'
global add_gdb_path
global edit_gdb_path
global delete_gdb_path
global outFolderPath
global output
def add_log_info(msg):
"""添加日志记录"""
cur_date = time.strftime("%Y-%m-%d", time.localtime())
cur_time = time.strftime("%H-%M-%S", time.localtime())
output.write("[" + cur_date + " " + cur_time + "] " + msg + "\n")
output.flush()
def create_dir(dir_name):
if os.path.exists(dir_name):
shutil.rmtree(dir_name)
os.mkdir(dir_name)
def trim_tablespace_name(fc_name):
"""去除表空间名"""
index = fc_name.rfind('.')
if index > 0:
fc_name = fc_name[index + 1:]
return fc_name
def start_edit(src_sde):
"""开始编辑"""
arcpy.env.workspace = src_sde
# print str(src_sde).decode('gb2312')
edit = arcpy.da.Editor(src_sde)
edit.startEditing(False, False)
edit.startOperation()
return edit
def stop_edit(src_sde, edit, bool_save):
"""停止编辑"""
arcpy.env.workspace = src_sde
# 停止编辑
edit.stopOperation()
edit.stopEditing(bool_save)
def get_config(section, key):
"""从配置文件读取参数"""
config = ConfigParser.ConfigParser()
path = os.path.split(os.path.realpath(__file__))[0] + '/ExportGdbSetting.config'
config.read(path)
try:
return config.get(section, key)
except ConfigParser.Error:
return ""
def get_config_int(section, key):
"""从配置文件读取参数"""
config = ConfigParser.ConfigParser()
path = os.path.split(os.path.realpath(__file__))[0] + '/ExportGdbSetting.config'
config.read(path)
try:
return config.getint(section, key)
except ConfigParser.Error:
return 0
def get_config_float(section, key):
"""从配置文件读取参数"""
config = ConfigParser.ConfigParser()
path = os.path.split(os.path.realpath(__file__))[0] + '/ExportGdbSetting.config'
config.read(path)
try:
return config.getfloat(section, key)
except ConfigParser.Error:
return 0.0
def get_layer_relation_count(fc_name):
"""获取图层对照个数"""
layer_name = trim_tablespace_name(fc_name)
index0 = layer_name.find("history")
if index0 == 0:
layer_name = layer_name[index0 + 7:]
count = get_config_int("Layer_" + layer_name, "relationCount")
return count
def get_relation_layer(fc_name, index):
"""获取对照图层信息"""
layer_name = trim_tablespace_name(fc_name)
index0 = layer_name.find("history")
if index0 == 0:
layer_name = layer_name[index0 + 7:]
tag_layer_name = get_config("Layer_" + layer_name, "tagLayerName" + str(index))
expression = get_config("Layer_" + layer_name, "expression" + str(index))
remove_field_names = get_config("Layer_" + layer_name, "removeFieldNames" + str(index))
rename_field_names = get_config("Layer_" + layer_name, "renameFieldNames" + str(index))
return tag_layer_name, expression, remove_field_names, rename_field_names
def get_relation_code_count():
"""获取编码对照个数"""
code_section = "FeatureCode"
count = get_config_int(code_section, "relationCount")
return count
def get_relation_code(index):
"""获取对照编码"""
code_section = "FeatureCode"
name = get_config(code_section, "changeCode" + str(index))
relations = name.split(":")
try:
# old_code, new_code = relations[0], relations[1]
return relations[0], relations[1]
except BaseException as e:
errmsg = str(e.message.encode("gb2312"))
add_log_info(errmsg)
return "", ""
def get_change_code_layers():
"""获取换码图层列表"""
code_section = "FeatureCode"
rel_layers = get_config(code_section, "relationLayers")
layers = rel_layers.split(",")
return layers
def feature_class_to_feature_class(src_layer_name, out_location, tag_layer_name, where_clause,
remove_field_names, rename_field_names):
"""图层转换输出"""
field_mappings = arcpy.FieldMappings()
field_mappings.addTable(src_layer_name)
# 处理删除字段
remove_list_names = remove_field_names.split(",")
for name in remove_list_names:
index = field_mappings.findFieldMapIndex(name)
if index > -1:
field_mappings.removeFieldMap(index)
# 处理改名字段
rename_list_names = rename_field_names.split(",")
for name in rename_list_names:
relations = name.split(":")
index0 = field_mappings.findFieldMapIndex(relations[0])
if index0 > -1:
field_map = field_mappings.getFieldMap(index0)
f_name = field_map.outputField
f_name.name = relations[1]
f_name.aliasName = relations[1]
field_map.outputField = f_name
field_mappings.replaceFieldMap(field_mappings.findFieldMapIndex(relations[0]), field_map)
# 导出数据
arcpy.FeatureClassToFeatureClass_conversion(src_layer_name, out_location, tag_layer_name, where_clause,
field_mappings)
def transform_to_feature_class(fc_name, out_location):
"""根据图层对照转换一个fc"""
count = get_layer_relation_count(fc_name)
# 没有对照,按原图层输出
if count == 0:
return
# 有对照获取对照输出
for index in range(1, count + 1):
out_fc, expression, rm_fields, rn_fields = get_relation_layer(fc_name, index)
expression = "ObjectID>0" if expression == "" else expression
# 如果对照为空,则按原图层名
out_fc = fc_name if out_fc == "" else out_fc
try:
add_log_info(str(fc_name) + "-->" + str(out_fc))
if arcpy.Exists(os.path.join(out_location, out_fc)):
# 先转换到内存工作空间
# arcpy.FeatureClassToFeatureClass_conversion(fc_name, "in_memory", "buffer", expression)
feature_class_to_feature_class(fc_name, "in_memory", "buffer", expression, rm_fields, rn_fields)
# 再从内存工作空间转换输出到目标
arcpy.Append_management("in_memory/buffer", os.path.join(out_location, out_fc), "NO_TEST")
# 及时回收内存工作空间
arcpy.Delete_management("in_memory")
else:
# arcpy.FeatureClassToFeatureClass_conversion(fc_name, out_location, out_fc, expression)
feature_class_to_feature_class(fc_name, out_location, out_fc, expression, rm_fields, rn_fields)
except BaseException as e:
# error_code = arcpy.GetReturnCode(2)
add_log_info(str(e.message.encode("gb2312")))
def translate_one_gdb(src_gdb_path, gdb_type):
"""转换图层输出一个GDB"""
if src_gdb_path == "":
return
# gdb_type 大与0 add; 小于0 delete; 等于0 edit
str_type = "add" if gdb_type > 0 else "delete"
if gdb_type == 0:
str_type = "edit"
add_log_info("-----------<" + str_type + ">-----------")
arcpy.env.workspace = src_gdb_path
fc_list = arcpy.ListFeatureClasses("*", "all")
for FC in fc_list:
index = FC.rfind('.')
fc_name = FC[index + 1:]
# 跳过实体图层et_开头的
if fc_name.lower().find('et_') == 0:
continue
if gdb_type <= 0:
# 删除和修改输出到删除gdb
out_location = outFolderPath + "\\" + out_delete_gdb
transform_to_feature_class(FC, out_location)
if gdb_type >= 0:
# 新增和修改输出到增改gdb
out_location = outFolderPath + "\\" + out_addedit_gdb
transform_to_feature_class(FC, out_location)
def translate_to_old():
# 创建输出子目录
create_dir(outFolderPath)
# 创建增改gdb
arcpy.CreateFileGDB_management(outFolderPath, out_addedit_gdb)
# 创建删除gdb
arcpy.CreateFileGDB_management(outFolderPath, out_delete_gdb)
# 修改env变量改为覆盖输出
arcpy.env.overwriteOutput = True
# 1.新增数据转换输出
translate_one_gdb(add_gdb_path, 1)
# 2.删除数据转换输出
translate_one_gdb(delete_gdb_path, -1)
# 3.修改数据转换输出
translate_one_gdb(edit_gdb_path, 0)
def change_codes_onelayer(fc):
"""一个图层所有编码遍历换编码"""
try:
count = get_relation_code_count()
for index in range(1, count + 1):
old_code, new_code = get_relation_code(index)
if old_code == "":
continue
fields = ["FEATURECODE"]
cur = arcpy.da.UpdateCursor(fc, fields,
where_clause="FeatureCode='" + old_code + "'")
for row in cur:
row[0] = new_code
cur.updateRow(row)
print(old_code + "-->" + new_code)
return True
except BaseException as e:
add_log_info(str(e.message.encode("gb2312")))
return False
def change_codes():
"""逐层换编码"""
out_paths = [outFolderPath + "\\" + out_addedit_gdb, outFolderPath + "\\" + out_delete_gdb]
for path in out_paths:
if path != "":
err_count = 0
edit = start_edit(path)
fc_list = arcpy.ListFeatureClasses("*", "all")
add_log_info("----------change_codes----------")
for fc in fc_list:
# 换码图层过滤
if fc not in get_change_code_layers():
continue
add_log_info("Layer: " + str(fc))
if not change_codes_onelayer(fc):
err_count += 1
b_save = True if err_count == 0 else False
stop_edit(path, edit, b_save)
# 获取参数
args = sys.argv[1:]
if not args or len(args) < 1:
sys.exit(str(u"参数不足:需要文件路径").encode("gb2312"))
else:
try:
# 文件路径
filepath = args[0]
add_gdb_path = filepath + "\\" + add_gdb
edit_gdb_path = filepath + "\\" + edit_gdb
delete_gdb_path = filepath + "\\" + delete_gdb
outFolderPath = filepath + "\\" + out_folder
# 创建日志
Date = time.strftime("%Y-%m-%d", time.localtime())
Time = time.strftime("%I:%M:%S %p", time.localtime())
Time1 = time.strftime("%H-%M-%S", time.localtime())
caller_file = inspect.getfile(inspect.currentframe())
pathname = os.path.abspath(os.path.dirname(caller_file))
out_logfile = pathname + "\\log\\" + Date + " " + Time1 + "-Translate2Old.txt"
if not os.path.exists(pathname + "\\log"):
os.mkdir(pathname + "\\log")
# 打开日志
output = open(out_logfile, "w")
# 记录开始时间
add_log_info('################ Transe2Old ################\n\n')
Date = time.strftime("%Y-%m-%d", time.localtime())
Time = time.strftime("%H-%M-%S", time.localtime())
add_log_info("Process begin at " + str(Date) + " " + str(Time) + ".")
# 换层输出旧标准数据增删改3文件->增删2文件
translate_to_old()
# 逐层换编码
change_codes()
# 结束时间日志记录
Date = time.strftime("%Y-%m-%d", time.localtime())
Time = time.strftime("%I:%M:%S %p", time.localtime())
add_log_info("Process completed at " + str(Date) + " " + str(Time) + "." + "\n")
output.close()
except BaseException as e:
errmsg = str(e.message.encode("gb2312"))
add_log_info(errmsg)