#coding=utf8
import datetime
import os
import sys
import time
import arcpy
from arcpy import env
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
def u(s):
if isinstance(s, unicode):
return s
elif isinstance(s, str):
try:
return s.decode('utf-8')
except Exception, e:
pass
try:
return s.decode('cp936')
except Exception, e:
print('Unknown coding of ', s)
else:
return u(str(s))
# 创建日志文件
def createlogfile():
txtfolder = u(os.path.dirname(os.path.realpath(sys.argv[1]))) # os.path.dirname(self.dataSource)
log_file_name = time.strftime("%Y-%m-%d_log.txt", time.localtime())
out_folder = os.path.abspath(txtfolder + os.sep + ".")
log_file_path = os.path.join(out_folder, log_file_name)
arcpy.AddMessage("日志文件位置:" + log_file_path)
return log_file_path
# 写日志
def writelog(log_path, message):
if log_path == '':
return
try:
f = file(log_path, 'a+')
message = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + message
message = (message + "\n").encode("gbk")
f.write(message)
f.close()
except Exception, e:
arcpy.AddMessage(e.message)
pass
if __name__ == '__main__':
try:
logFilePath = createlogfile()
# 获取传入参数,目标图层或者表,源图层或者表,数据类型,过滤条件
# 获取目标图层或表 E:\测试数据\Python测试的数据\数据库.mdb\XZQ"
tDataPath = u(sys.argv[2])
writelog(logFilePath,"目标图层路径:" + tDataPath)
# 获取目标图层数据过滤条件 "BSM = '440117121000000049' OR BSM = '440117121000000037'"
tfilter = u(sys.argv[3])
writelog(logFilePath, "目标图层过滤条件:" + tfilter)
# 获取源图层或者表 "E:\测试数据\Python测试的数据\更新包.gdb\XZQ"
sDataPath = u(sys.argv[4])
writelog(logFilePath, "源图层路径:" + sDataPath)
# 过滤条件 "BSM = '440117121000000049' OR BSM = '440117121000000037'"
sfilter = u(sys.argv[5])
writelog(logFilePath, "源图层过滤条件:" + sfilter)
# 删除目标图层中满足查询条件的数据
writelog(logFilePath, "判断目标条件和目标图层过滤条件是否满足要求")
if arcpy.Exists(tDataPath) and tfilter != '':
tcursor = arcpy.da.UpdateCursor(tDataPath, "*", tfilter)
for trow in tcursor:
tcursor.deleteRow()
del tcursor
else:
print "Failed: 目标图层路径",tDataPath,"或者目标过滤条件",tfilter,"不满足条件"
writelog(logFilePath, "判断目标条件和目标图层过滤条件不满足要求")
# 从源图层中查询满足条件的数据插入到目标图层
desc = arcpy.Describe(tDataPath)
fields = object
if desc.dataType == "FeatureClass":
fields = ["*", "SHAPE@"]
else:
fields = ["*"]
writelog(logFilePath, "数据类型:" + desc.dataType)
writelog(logFilePath, "从源图层中查询满足条件的数据插入到目标图层,目标图层" + tDataPath + "源图层:" + sDataPath + "源条件:" + sfilter)
if arcpy.Exists(tDataPath) and arcpy.Exists(sDataPath) and sfilter != '':
tcursor = arcpy.da.InsertCursor(tDataPath, fields)
writelog(logFilePath, "获取到目标图层的游标,路径:" + tDataPath)
scursor = arcpy.da.SearchCursor(sDataPath, fields, sfilter)
writelog(logFilePath, "获取到源图层的游标,路径:" + sDataPath)
for srow in scursor:
tcursor.insertRow(srow)
del scursor
del tcursor
print "Ok: Update Data Success!"
writelog(logFilePath,"Ok: Update Data Success!")
else:
print "Failed: 目标图层路径:", tDataPath, "源图层路径:",sDataPath, "源图层过滤条件", sfilter, "不满足条件"
writelog(logFilePath, "Failed: 目标图层路径:" + tDataPath+ "源图层路径:" + sDataPath + "源图层过滤条件" + sfilter + "不满足条件")
except Exception, ex:
arcpy.AddMessage("异常:" + ex.message)
print "Failed: 异常,",ex.message
writelog(logFilePath, "异常:" + ex.message)
exit(0)
【基于arcpy实现矢量数据入库sde】
最新推荐文章于 2023-07-14 16:06:45 发布