arcpy管线数据处理
一、需求
管线数据规整过程,需要依照标准给属性字段赋值,人工操作显然不现实,因此,需要借助arcpy开发小工具进行批量处理。
-
给gdb中的每个 要素数据集 的 要素属性表字段 赋值,这里的赋值主要是根据设定好的规则进行属性字段赋值;
-
管线表和管点表是有对应关系的,如:
管线表中的 起点 和 终点 对应管点表的 PllCode,所以PllCode码变了,StartPoint 和EndPoint也要跟着变。
二、代码
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import arcpy
import os
import datetime
import sys
import logging
reload(sys)
sys.setdefaultencoding('utf8')
isDone = []
# Step0:错误日志输出
# 错误日志
# logger = logging.getLogger() # 实例化一个logger对象
# logger.setLevel(logging.INFO) # 设置初始显示级别
# file_handle = logging.FileHandler("log", encoding="UTF-8")
# stream_handle = logging.StreamHandler()
# fmt = logging.Formatter('%(asctime)s %(filename)s %(levelname)s %(message)s')
# file_handle.setFormatter(fmt) # 文件句柄设置格式
# logger.addHandler(file_handle) # logger对象绑定文件句柄
# 得到gdb中各要素的路径
def getShpPath():
shpPath = []
shpName = []
for fs in arcpy.ListDatasets():
for shp in arcpy.ListFeatureClasses(feature_dataset=fs):
shpPath.append(fs + '/' + shp)
shpName.append(shp)
return shpPath, shpName
# 获取项目要素基础信息
def getShpInfo(shpPath):
# path = 'JZD2015SZ009/JS_JS_GHXK_L_2015_009'
fsName = shpPath.split('/', 1)[0] # 要素数据集名称
shpName = shpPath.split('/', 1)[1] # 要素名称
shpType = shpName.split('_')[3] # 要素类型
prjNo = fsName.split('JZD', 1)[1] # 工程项目编码
pllType = shpName.split('_')[0] # 管线类别
pllSubType = shpName.split('_')[1] # 管线子类
state = shpName.split('_')[2]
if state == "GHXK":
pipState = "规划管"
else:
pipState = "现状管"
licCode = '建字第' + shpName.split('_')[4] + '市政' + shpName.split('_')[5] + '号' # 规划许可证号
dbDate = datetime.datetime.now() # 数据库建库时间
info = {
"fsName": fsName,
"shpName": shpName,
"shpType": shpType,
"prjNo": prjNo,
"pllType": pllType,
"pllSubType": pllSubType,
"pipState": pipState,
"licCode": licCode,
"dbDate": dbDate.strftime("%Y-%m-%d %H:%M:%S")
} # 属性信息字典
return info
# 改正编码(赋予管点PllCode,连带修改管线的StartPoint、EndPoint)
def correctCode(info, shpNameArr, path):
if path in isDone:
print "已处理过"
if info["shpType"] == "L": # 已处理过且是线的话,直接可以给出
setLcode(path, info)
else:
if info["shpType"] == "L":
pShpName = info["shpName"].replace("_L_", "_P_")
if pShpName in shpNameArr:
ex = info["prjNo"] + info["pllType"] + info["pllSubType"]
lPath = path
pPath = path.split('/', 1)[0] + '/' + pShpName
p2lCodeUni(pPath, lPath, ex)
setLcode(path, info)
else:
print "不存在" + pShpName
if info["shpType"] == "P":
ex = info["prjNo"] + info["pllType"] + info["pllSubType"]
pPath = path
lPath = path.split('/', 1)[0] + '/' + info["shpName"].replace("_P_", "_L_")
p2lCodeUni(pPath, lPath, ex)
# 赋给点PllCode,并相应修改线 StartPoint、EndPoint
def p2lCodeUni(pPath, lPath, ex):
fileds = ["OBJECTID", "PllCode", "StartPoint", "EndPoint"]
pCursor = arcpy.UpdateCursor(pPath)
# lCursor = arcpy.UpdateCursor(lPath)
for pr in pCursor:
objId = pr.getValue(fileds[0])
newCode = ex + (5 - len(str(objId))) * '0' + str(objId)
pllCode = pr.getValue(fileds[1])
pr.setValue(fileds[1], newCode)
pCursor.updateRow(pr)
lCursor = arcpy.UpdateCursor(lPath)
for lr in lCursor:
# print pllCode, lr.getValue(fileds[2]), lr.getValue(fileds[3])
if lr.getValue(fileds[2]) == pllCode:
lr.setValue(fileds[2], newCode)
if lr.getValue(fileds[3]) == pllCode:
lr.setValue(fileds[3], newCode)
lCursor.updateRow(lr)
del lr, lCursor
isDone.append(pPath)
isDone.append(lPath)
# 设置属性值(管点和线)
def setAttr(info, p):
# fileds = ["PrjNo", "LicCode", "PllCode", "PllType", "PllSubtype", "DBDate", "PipState"]
inTable = p
arcpy.CalculateField_management(inTable, "PrjNo", "\'" + info["prjNo"] + "\'", "PYTHON_9.3")
arcpy.CalculateField_management(inTable, "LicCode", "\'" + info["licCode"] + "\'", "PYTHON_9.3")
arcpy.CalculateField_management(inTable, "PllType", "\'" + info["pllType"] + "\'", "PYTHON_9.3")
arcpy.CalculateField_management(inTable, "PllSubtype", "\'" + info["pllSubType"] + "\'", "PYTHON_9.3")
arcpy.CalculateField_management(inTable, "DBDate", "\'" + info["dbDate"] + "\'", "PYTHON_9.3")
arcpy.CalculateField_management(inTable, "PipState", "\'" + info["pipState"] + "\'", "PYTHON_9.3")
def setLcode(lPath, info):
ex = info["prjNo"] + info["pllType"] + info["pllSubType"]
filedList = ["StartPoint", "EndPoint", "PllCode"]
cursor = arcpy.UpdateCursor(lPath)
for row in cursor:
start_p = row.getValue(filedList[0])
end_p = row.getValue(filedList[1])
code = ex + start_p[-5:] + "_" + end_p[-5:]
row.setValue(filedList[2], code)
cursor.updateRow(row)
三、源码链接和工具箱下载
见我上传的资源:https://download.csdn.net/download/m0_37970224/20618507
四、遇到的问题
遇到的问题,这里也一并记录
-
arcmap 里面可以写arcpy ,这是没错,但不好用,我想用pycharm,怎么整?
打开pycharm,设置一下路径,ok
-
arcpy都有哪些函数方法,我咋用,
传送门:https://resources.arcgis.com/zh-cn/help/main/10.1/index.html#/na/018v0000006p000000/
3.我在pycharm里写好了脚本,怎么做成tbx工具?
教程:https://blog.csdn.net/sinat_36226553/article/details/105846731
运行时候报错unexpected indent,报脚本缩进错误,莫名奇妙
打开源代码,保存为ansi格式
参考:https://blog.51cto.com/u_12139363/3161608