修改bug,解决工作路径存在中文时,decode报错的问题

This commit is contained in:
HKS 2023-05-29 17:27:23 +08:00
parent cb86059c30
commit ffb7888a49
8 changed files with 100 additions and 32 deletions

View File

@ -1,3 +1,54 @@
# shsmi_sysdb_nex # shsmi_sysdb_nex
重构代码 ## Description
本程序用于将SDE数据库中的增量入库数据同步到本地mdo文件中用于更新上海院的版本库。
## 同步原理
1. 从SDE数据库中读取增量入库数据到本地gdb包括新增、修改、删除三个gdb数据文件
2. 将本地gdb数据文件同步到mdo文件中包括新增、修改、删除三个mdo数据文件这步转换需要EPS;
3. 将gdb三文件通过标准转换py脚本转换为gdb二文件的旧标准格式
4. 将gdb二文件的旧标准格式进行秘密信息处理生产保密的gdb二文件
## 使用方法
1. 将程序部署到服务器上例如D:\shsmi_sysdb_nex
2. 在D:\shsmi_sysdb_nex\SdeSyncSetting.config中配置同步参数例如
```
[System]
# 配置EPS.EXE的文件路径
eps_path=C:/EPS20221017/eps.exe
# 配置python.exe的路径要用arcgis默认安装的2.7
python_path=C:/Python27/ArcGIS10.2/python.exe
# 配置EPS的模板文件名用于mdo转换使用
template_name="上海新型测绘_地理实体.mdt"
# 配置EPS的vbs文件名用于mdo转换使用
eps_vbs=.\上海数据库同步\自动输出MDO.vbs
# 配置SDE数据库的scheme名称
sde_scheme=SHDLG500
# 配置EPS输出mdo是是否显示EPS子进程界面
eps_show=show
# 配置SDE同步gdb时只同步图元数据还是同步所有数据
sync_type=ALL
[UpdateRegion]
# 配置SDE库中的更新区域图层名称
layer_name=UpdateRegion_A
# 配置SDE库中的更新区域图层的更新时间字段名称
updatetime_field=UpdateTime
# 配置SDE库中的更新区域历史图层的更新时间字段名称即删除时间
updatetime_field_history=DeleteTime
```
3. 在D:\shsmi_sysdb_nex\ExportGdbSetting.config 中配置新标准转换旧标准的对照规则
4. 创建一个bat文件例如D:\shsmi_sysdb_nex\run.bat内容如下
```
@echo off
cd /d %~dp0
C:/Python27/ArcGIS10.2/python.exe main.py
pause
```
5. 在windows计划任务中配置定时执行run.bat文件例如每天凌晨1点执行一次。(或者使用消息队列监管程序定时执行run.bat文件)
## 注意事项
1. 本程序需要EPS软件的支持EPS软件需要单独购买EPS软件的安装路径需要在SdeSyncSetting.config中配置
2. 本程序需要ArcGIS软件的支持ArcGIS软件需要单独购买
3. 本程序需要ArcGIS的python环境ArcGIS的python环境需要单独安装ArcGIS的python环境的安装路径需要在SdeSyncSetting.config中配置
4. 本程序基于ArcGIS的python环境开发python版本为2.7ArcGIS版本为10.2.2

View File

@ -10,10 +10,6 @@ import time
import ctypes import ctypes
import urllib import urllib
import urllib2 import urllib2
from imp import reload
reload(sys)
sys.setdefaultencoding('utf8')
def run_command(command): def run_command(command):

View File

@ -2,10 +2,6 @@
import os import os
import sys import sys
import logging import logging
from imp import reload
reload(sys)
sys.setdefaultencoding('utf8')
class Logger: class Logger:

View File

@ -1,8 +1,7 @@
# coding=utf-8 # coding=utf-8
import inspect import inspect
import os
import sys
from imp import reload from imp import reload
from sde_sync import SdeSynchronous from sde_sync import SdeSynchronous
from logger import Logger from logger import Logger
from functions import * from functions import *

View File

@ -3,13 +3,10 @@ import fnmatch
import os import os
import sys import sys
import arcpy import arcpy
from imp import reload
from functions import * from functions import *
from sys_config import SysConfig from sys_config import SysConfig
from logger import Logger from logger import Logger
reload(sys)
sys.setdefaultencoding('utf8')
# EPS SDE 系统表列表 # EPS SDE 系统表列表
g_sys_tb = ['SS_BDCDYHINFO', 'SS_GISDBLINKINFO', 'SS_LOGINFO', 'SS_ROLEINFO', 'SS_ROLEMXDINFO', g_sys_tb = ['SS_BDCDYHINFO', 'SS_GISDBLINKINFO', 'SS_LOGINFO', 'SS_ROLEINFO', 'SS_ROLEMXDINFO',
@ -36,7 +33,7 @@ class SdeSynchronous:
self.logger.error("src_sde Connection file not exist!") self.logger.error("src_sde Connection file not exist!")
else: else:
self.create_gdb_files() self.create_gdb_files()
txt_path = self.out_gdb_path + "\\" + str(u"图层数量统计.txt").encode("utf-8") txt_path = self.out_gdb_path + "\\" + str(u"图层数量统计.txt").encode("gb2312")
self.txt_ct_file = open(txt_path, "wb+", buffering=0) self.txt_ct_file = open(txt_path, "wb+", buffering=0)
# 开始编辑SDE # 开始编辑SDE
edit = self.start_edit() edit = self.start_edit()
@ -58,12 +55,9 @@ class SdeSynchronous:
ds_list = arcpy.ListDatasets("*", "Feature") ds_list = arcpy.ListDatasets("*", "Feature")
# 逐个数据集输出 # 逐个数据集输出
for DS in ds_list: for DS in ds_list:
str_msg = "Process Dataset: " + DS.encode("gb2312") print("Process Dataset: " + DS.encode("gb2312"))
print(str_msg) ds_name = DS[DS.rfind('.') + 1:]
index = DS.rfind('.') sr = arcpy.Describe(DS).spatialReference
ds_name = DS[index + 1:]
desc = arcpy.Describe(DS)
sr = desc.spatialReference
# 更改工作空间回到SDE # 更改工作空间回到SDE
arcpy.env.workspace = self.src_sde arcpy.env.workspace = self.src_sde
# 获取数据集中fc列表 # 获取数据集中fc列表
@ -71,7 +65,7 @@ class SdeSynchronous:
# 输出增量 # 输出增量
self.sde2gdb_feature_list(fc_list) self.sde2gdb_feature_list(fc_list)
# 获取关联关系表列表 # 获取关联关系表列表
re_table_list = arcpy.ListTables("SHDLG500.*", "all") re_table_list = arcpy.ListTables("{}.*".format(self.config.sde_scheme()), "all")
self.sde2gdb_table_list(re_table_list) self.sde2gdb_table_list(re_table_list)
bool_save = True if self.error_count == 0 else False bool_save = True if self.error_count == 0 else False
@ -114,7 +108,7 @@ class SdeSynchronous:
try: try:
# 创建输出GDB # 创建输出GDB
tt = self.config.curr_sync_datetime tt = self.config.curr_sync_datetime
self.out_gdb_path = self.config.pathname + "\\" + u"NEWGDB\\" + tt[:tt.find(' ')] self.out_gdb_path = self.config.pathname + "\\" + "NEWGDB\\" + tt[:tt.find(' ')]
create_dir(self.out_gdb_path) create_dir(self.out_gdb_path)
self.out_gdb_files = {"Add": "addFeatures.gdb", "Edit": "editFeatures.gdb", "Delete": "deleteFeatures.gdb"} self.out_gdb_files = {"Add": "addFeatures.gdb", "Edit": "editFeatures.gdb", "Delete": "deleteFeatures.gdb"}
for key in self.out_gdb_files.keys(): for key in self.out_gdb_files.keys():
@ -129,13 +123,13 @@ class SdeSynchronous:
"""获取SDE连接""" """获取SDE连接"""
for fileName in os.listdir(self.config.pathname + "\\" + sub_path.encode("gb2312")): for fileName in os.listdir(self.config.pathname + "\\" + sub_path.encode("gb2312")):
if fnmatch.fnmatch(fileName, '*.sde'): if fnmatch.fnmatch(fileName, '*.sde'):
return self.config.pathname + "\\" + sub_path + "\\" + fileName return self.config.pathname + "\\" + sub_path.encode('gb2312') + "\\" + fileName
return "" return ""
def start_edit(self): def start_edit(self):
"""开始编辑""" """开始编辑"""
arcpy.env.workspace = self.src_sde arcpy.env.workspace = self.src_sde
print(str(self.src_sde).encode('gb2312')) print(self.src_sde.decode("gb2312"))
edit = arcpy.da.Editor(self.src_sde) edit = arcpy.da.Editor(self.src_sde)
edit.startEditing(False, False) edit.startEditing(False, False)
edit.startOperation() edit.startOperation()
@ -249,6 +243,8 @@ class SdeSynchronous:
if index0 == 0: if index0 == 0:
return return
fc_history = "history" + fc_name fc_history = "history" + fc_name
if not arcpy.Exists(fc_history):
return
expression_add, expression_edit, expression_delete = self.get_export_expression(fc_name) expression_add, expression_edit, expression_delete = self.get_export_expression(fc_name)
self.logger.log("Exporting Layer:" + fc_name.encode("gb2312")) self.logger.log("Exporting Layer:" + fc_name.encode("gb2312"))
try: try:
@ -288,6 +284,8 @@ class SdeSynchronous:
if index0 == 0: if index0 == 0:
return return
tb_history = "history" + tb_name tb_history = "history" + tb_name
if not arcpy.Exists(tb_history):
return
entity_index = tb_name.find("ReEnt_") entity_index = tb_name.find("ReEnt_")
tb_type = "entity_rela" if entity_index == 0 else "elem_rela" tb_type = "entity_rela" if entity_index == 0 else "elem_rela"
@ -455,10 +453,10 @@ class SdeSynchronous:
"""启动python子进程输出feature或者table到gdb""" """启动python子进程输出feature或者table到gdb"""
python_path = str(self.config.python_path() + u' ').encode('gb2312') python_path = str(self.config.python_path() + u' ').encode('gb2312')
script = str(u'sde_fc2fc.py ').encode('gb2312') script = str(u'sde_fc2fc.py ').encode('gb2312')
src_sde = str(self.src_sde + u' ') src_sde = str(self.src_sde + ' ')
type_cmd = str(fc_type + u' ').encode('gb2312') type_cmd = str(fc_type + u' ').encode('gb2312')
fc_name_cmd = str(fc_name + u' ').encode('gb2312') fc_name_cmd = str(fc_name + u' ').encode('gb2312')
out_cmd = str(out + u' ').encode('gb2312') out_cmd = str(out + ' ')
fc_cur_cmd = str(fc_cur + u' ').encode('gb2312') fc_cur_cmd = str(fc_cur + u' ').encode('gb2312')
exp_cmd = str(u'"' + expression + u'" ') exp_cmd = str(u'"' + expression + u'" ')
# 启动命令参数 # 启动命令参数
@ -505,7 +503,7 @@ class SdeSynchronous:
python_path = str(self.config.python_path() + u' ').encode('gb2312') python_path = str(self.config.python_path() + u' ').encode('gb2312')
script = str(u'translate2old.py ').encode('gb2312') script = str(u'translate2old.py ').encode('gb2312')
# 启动命令参数 # 启动命令参数
command1 = python_path + script + self.out_gdb_path.encode('gb2312') command1 = python_path + script + self.out_gdb_path
run_command(command1) run_command(command1)
self.logger.log("--End Translate TO old--") self.logger.log("--End Translate TO old--")
# 移动文件 # 移动文件

View File

@ -2,10 +2,6 @@
import os import os
import sys import sys
import ConfigParser import ConfigParser
from imp import reload
reload(sys)
sys.setdefaultencoding('utf8')
class BaseConfig(object): class BaseConfig(object):

30
test_arcpy.py Normal file
View File

@ -0,0 +1,30 @@
# coding=utf-8
import arcpy
from functions import *
# Set the workspace
arcpy.env.workspace = u"C:\\Users\\Administrator\\PycharmProjects\\shsmi_sysdb_nex\\源库SDE连接\\shdlg500.sde"
# Get the list of the feature classes in the workspace
# fc_list = arcpy.ListFeatureClasses()
# # 循环验证每个fc是否为有效的sde创建的fc
# for fc in fc_list:
# fc = fc[fc.find(".")+1:]
# if fc.find("history") == 0:
# continue
# fc_his = "history" + fc.lower()
# if not arcpy.Exists(fc_his):
# print("Not exists: " + fc_his)
# else:
# print(fc_his)
tb_list = arcpy.ListTables()
for tb in tb_list:
tb = tb[tb.find(".")+1:]
if tb.find("history") == 0:
continue
tb_his = "history" + tb.lower()
if not arcpy.Exists(tb_his):
print("Not exists: " + tb_his)
else:
print(tb_his)

View File

@ -16,3 +16,5 @@ class TestFunctions(unittest.TestCase):
rst, out = run_command(command_string) rst, out = run_command(command_string)
print(out) print(out)
self.assertEqual(rst, True) self.assertEqual(rst, True)