• 周四. 6月 30th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

SHP矢量属性字段操作

admin

11月 28, 2021

利用Python脚本往shp图层矢量添加、修改、删除属性字段。

添加字段:

import sys
import arcpy
from arcpy import env

#输入sde连接、要素类
print "shp add fields"
env.workspace = r"D:	est"
infc = "xxx.shp"
print "in workspace: " + env.workspace
print "in featureclass:" + infc

iid = 100000000

#添加字段
arcpy.AddField_management(infc, "BZ", "TEXT", "", "", 200)
arcpy.AddField_management(infc, "DATEFROM", "DATE")
arcpy.AddField_management(infc, "XH", "LONG", 38)
arcpy.AddField_management(infc, "IID", "LONG", 18)
arcpy.CalculateField_management(infc, "IID", iid)

print "done"

修改字段:

import time
import sys
import arcpy
from arcpy import env

#直接运行参数
print "shp add fields"
env.workspace = r"D:	est"
infc = "xxx.shp"
print "in workspace: " + env.workspace
print "in featureclass:" + infc

bz = "bzstring"

codeblock = """def StringToTime(stime):
  if stime == None :
    return None
  stime = stime.replace(" ", "")
  stime = stime.replace("T16:00:00.000Z", "")
  stime = stime.replace(".", "-")
  stime = stime.replace("-0", "-")
  if len(stime) == 0 :
    return None
  if stime.endswith("-") :
    stime = stime + "1"
  if stime.count("-") == 1 :
    stime = stime + "-1"
  return stime"""

#从字段1复制值到字段2
arcpy.CalculateField_management(infc, 'FIELD2', '!FIELD1!', 'PYTHON_9.3')

#转换字符串日期为标准日期格式(2021-7-20),然后直接插入
arcpy.AddField_management(infc, "FIELD2", "DATE")
expression = "StringToTime(!FIELD1!)"
arcpy.CalculateField_management(infc, 'FIELD2', expression, 'PYTHON_9.3', codeblock)

#填充字段值
arcpy.AddField_management(infc, "BZ", "TEXT", "", "", 200)
if len(fsrctable) > 0:
    arcpy.CalculateField_management(infc, "BZ", """ + bz + """)

print "done"

删除字段:

import time
import sys
import arcpy
from arcpy import env

#直接运行参数
print "shp add fields"
env.workspace = r"D:	est"
infc = "xxx.shp"
print "in workspace: " + env.workspace
print "in featureclass:" + infc

arcpy.DeleteField_management(infc, "FIELD1")

print "done"

发表评论

您的电子邮箱地址不会被公开。