修改bug,解决工作路径存在中文时,decode报错的问题
This commit is contained in:
		
							parent
							
								
									cb86059c30
								
							
						
					
					
						commit
						ffb7888a49
					
				
							
								
								
									
										53
									
								
								README.md
								
								
								
								
							
							
						
						
									
										53
									
								
								README.md
								
								
								
								
							| 
						 | 
				
			
			@ -1,3 +1,54 @@
 | 
			
		|||
# 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.7,ArcGIS版本为10.2.2;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,10 +10,6 @@ import time
 | 
			
		|||
import ctypes
 | 
			
		||||
import urllib
 | 
			
		||||
import urllib2
 | 
			
		||||
from imp import reload
 | 
			
		||||
 | 
			
		||||
reload(sys)
 | 
			
		||||
sys.setdefaultencoding('utf8')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def run_command(command):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,10 +2,6 @@
 | 
			
		|||
import os
 | 
			
		||||
import sys
 | 
			
		||||
import logging
 | 
			
		||||
from imp import reload
 | 
			
		||||
 | 
			
		||||
reload(sys)
 | 
			
		||||
sys.setdefaultencoding('utf8')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Logger:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										3
									
								
								main.py
								
								
								
								
							
							
						
						
									
										3
									
								
								main.py
								
								
								
								
							| 
						 | 
				
			
			@ -1,8 +1,7 @@
 | 
			
		|||
# coding=utf-8
 | 
			
		||||
import inspect
 | 
			
		||||
import os
 | 
			
		||||
import sys
 | 
			
		||||
from imp import reload
 | 
			
		||||
 | 
			
		||||
from sde_sync import SdeSynchronous
 | 
			
		||||
from logger import Logger
 | 
			
		||||
from functions import *
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										32
									
								
								sde_sync.py
								
								
								
								
							
							
						
						
									
										32
									
								
								sde_sync.py
								
								
								
								
							| 
						 | 
				
			
			@ -3,13 +3,10 @@ import fnmatch
 | 
			
		|||
import os
 | 
			
		||||
import sys
 | 
			
		||||
import arcpy
 | 
			
		||||
from imp import reload
 | 
			
		||||
from functions import *
 | 
			
		||||
from sys_config import SysConfig
 | 
			
		||||
from logger import Logger
 | 
			
		||||
 | 
			
		||||
reload(sys)
 | 
			
		||||
sys.setdefaultencoding('utf8')
 | 
			
		||||
 | 
			
		||||
# EPS SDE 系统表列表
 | 
			
		||||
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!")
 | 
			
		||||
        else:
 | 
			
		||||
            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)
 | 
			
		||||
            # 开始编辑SDE
 | 
			
		||||
            edit = self.start_edit()
 | 
			
		||||
| 
						 | 
				
			
			@ -58,12 +55,9 @@ class SdeSynchronous:
 | 
			
		|||
            ds_list = arcpy.ListDatasets("*", "Feature")
 | 
			
		||||
            # 逐个数据集输出
 | 
			
		||||
            for DS in ds_list:
 | 
			
		||||
                str_msg = "Process Dataset: " + DS.encode("gb2312")
 | 
			
		||||
                print(str_msg)
 | 
			
		||||
                index = DS.rfind('.')
 | 
			
		||||
                ds_name = DS[index + 1:]
 | 
			
		||||
                desc = arcpy.Describe(DS)
 | 
			
		||||
                sr = desc.spatialReference
 | 
			
		||||
                print("Process Dataset: " + DS.encode("gb2312"))
 | 
			
		||||
                ds_name = DS[DS.rfind('.') + 1:]
 | 
			
		||||
                sr = arcpy.Describe(DS).spatialReference
 | 
			
		||||
                # 更改工作空间回到SDE
 | 
			
		||||
                arcpy.env.workspace = self.src_sde
 | 
			
		||||
                # 获取数据集中fc列表
 | 
			
		||||
| 
						 | 
				
			
			@ -71,7 +65,7 @@ class SdeSynchronous:
 | 
			
		|||
                # 输出增量
 | 
			
		||||
                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)
 | 
			
		||||
 | 
			
		||||
            bool_save = True if self.error_count == 0 else False
 | 
			
		||||
| 
						 | 
				
			
			@ -114,7 +108,7 @@ class SdeSynchronous:
 | 
			
		|||
        try:
 | 
			
		||||
            # 创建输出GDB
 | 
			
		||||
            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)
 | 
			
		||||
            self.out_gdb_files = {"Add": "addFeatures.gdb", "Edit": "editFeatures.gdb", "Delete": "deleteFeatures.gdb"}
 | 
			
		||||
            for key in self.out_gdb_files.keys():
 | 
			
		||||
| 
						 | 
				
			
			@ -129,13 +123,13 @@ class SdeSynchronous:
 | 
			
		|||
        """获取SDE连接"""
 | 
			
		||||
        for fileName in os.listdir(self.config.pathname + "\\" + sub_path.encode("gb2312")):
 | 
			
		||||
            if fnmatch.fnmatch(fileName, '*.sde'):
 | 
			
		||||
                return self.config.pathname + "\\" + sub_path + "\\" + fileName
 | 
			
		||||
                return self.config.pathname + "\\" + sub_path.encode('gb2312') + "\\" + fileName
 | 
			
		||||
        return ""
 | 
			
		||||
 | 
			
		||||
    def start_edit(self):
 | 
			
		||||
        """开始编辑"""
 | 
			
		||||
        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.startEditing(False, False)
 | 
			
		||||
        edit.startOperation()
 | 
			
		||||
| 
						 | 
				
			
			@ -249,6 +243,8 @@ class SdeSynchronous:
 | 
			
		|||
        if index0 == 0:
 | 
			
		||||
            return
 | 
			
		||||
        fc_history = "history" + fc_name
 | 
			
		||||
        if not arcpy.Exists(fc_history):
 | 
			
		||||
            return
 | 
			
		||||
        expression_add, expression_edit, expression_delete = self.get_export_expression(fc_name)
 | 
			
		||||
        self.logger.log("Exporting Layer:" + fc_name.encode("gb2312"))
 | 
			
		||||
        try:
 | 
			
		||||
| 
						 | 
				
			
			@ -288,6 +284,8 @@ class SdeSynchronous:
 | 
			
		|||
        if index0 == 0:
 | 
			
		||||
            return
 | 
			
		||||
        tb_history = "history" + tb_name
 | 
			
		||||
        if not arcpy.Exists(tb_history):
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        entity_index = tb_name.find("ReEnt_")
 | 
			
		||||
        tb_type = "entity_rela" if entity_index == 0 else "elem_rela"
 | 
			
		||||
| 
						 | 
				
			
			@ -455,10 +453,10 @@ class SdeSynchronous:
 | 
			
		|||
        """启动python子进程输出feature或者table到gdb"""
 | 
			
		||||
        python_path = str(self.config.python_path() + u' ').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')
 | 
			
		||||
        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')
 | 
			
		||||
        exp_cmd = str(u'"' + expression + u'" ')
 | 
			
		||||
        # 启动命令参数
 | 
			
		||||
| 
						 | 
				
			
			@ -505,7 +503,7 @@ class SdeSynchronous:
 | 
			
		|||
        python_path = str(self.config.python_path() + u' ').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)
 | 
			
		||||
        self.logger.log("--End Translate TO old--")
 | 
			
		||||
        # 移动文件
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,10 +2,6 @@
 | 
			
		|||
import os
 | 
			
		||||
import sys
 | 
			
		||||
import ConfigParser
 | 
			
		||||
from imp import reload
 | 
			
		||||
 | 
			
		||||
reload(sys)
 | 
			
		||||
sys.setdefaultencoding('utf8')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BaseConfig(object):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			@ -16,3 +16,5 @@ class TestFunctions(unittest.TestCase):
 | 
			
		|||
        rst, out = run_command(command_string)
 | 
			
		||||
        print(out)
 | 
			
		||||
        self.assertEqual(rst, True)
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue