【基于arcpy实现矢量数据入库sde】

#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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值