时间:2020-11-30 13:26:15 | 栏目:Python代码 | 点击:次
Case:需要给一个现有的shp数据创建一个字段,并将属性表中原有的一个文本类型的属性转换为整型后填入新创建的字段。
Problem:新字段创建成功,但是赋值操作无效,即无法成功给字段写入值。
solution:对字段进行赋值后需要,重新写入Feature,否则赋值无效,即layer0.SetFeature(feature)。
特别注意:在对数据进行读写操作,一定要以读写的方式打开,即Open(filePath,1),该方法的原型为Open(pszName,int bUpdate = false),并且具有返回值,参数说明如下:
名称 | 说明 |
pszName | 需要打开文件或数据源的路径 |
bUpdate | 是否需要更新数据集,默认为只读,如果需要对数据进行读写操作,需要给此参数赋值1 |
返回值 | 返回数据集的指针,如果为NULL,则表明打开数据集失败 |
以下将给出创建字段和获取字段值、子段赋值的实例。
1.为矢量数据创建字段
# 添加字段 defn = layer.GetLayerDefn() fieldIndex=defn.GetFieldIndex('SSSS') if fieldIndex<0: # 添加字段 fieldDefn = ogr.FieldDefn('SSSS', ogr.OFTInteger) fieldDefn.SetPrecision(9) layer0.CreateField(fieldDefn,1); fieldIndex2 = defn.GetFieldIndex('SSSS') if fieldIndex2>0: print("字段创建成功:",fieldIndex)
2.获取字段值及字段赋值
feature = layer.GetNextFeature() indexA = defn.GetFieldIndex('code') indexB = defn.GetFieldIndex('SSSS') oField = defn.GetFieldDefn(indexB) fieldName = oField.GetNameRef() while feature is not None: valueA= feature.GetFieldAsInteger(indexA) if valueA is None: feature.SetFieldNull(indexB) continue feature.SetField2(fieldName, valueA) layer0.SetFeature(feature) feature = layer0.GetNextFeature() #feature.Destroy() ds.Destroy()
补充知识:Python批量修改shapefile属性表字段名(arcpy增删字段)
尝试了3种方法,时间紧迫屡败屡战,最后终于成功。
方法1和2是失败记录,希望有类似经历成功的同学分享下经验。
方法3是成功记录,修改 = 新增 + 计算 +删除相关字段,因为没有删除改名前字段需求,故删除部分没有另做。
方法1 ―― 【将shapefile的dbf文件按csv文件读写】
――失败,dbf中有空值及编码问题
不同文件间通过改后缀简单粗暴改写,操作方法存在风险
过于依赖熟悉的领域,由于时间紧迫没有试用dbf第三方库
#-*- coding: utf-8 -*- import os import shutil import csv #批量修改shp中dbf文件中的字段名 #LANE_WIDTH改为LANEWIDTHL #LANE_WID_1改为LANEWIDTHR #ORIGIN_LIN改为ORG_LINKID roadDir = r"D:\20Q1\00DATA\ModifyTitle\ROAD_TEST\\" modifyRoadDir = r"D:\20Q1\00DATA\ModifyTitle\ModifiedLink\\" csvDir = r"D:\20Q1\00DATA\ModifyTitle\ROAD_CSV\\" fileList = os.listdir(roadDir) num = 0 for file in fileList: #新建修改后的城市文件夹 city = file.split("_")[0] os.mkdir(csvDir + city) os.mkdir(modifyRoadDir + city) shutil.copy(roadDir + file + "/RD_LINK.dbf", csvDir + city + "/RD_LINK.csv") print("正在生成{}新dbf文件......".format(city)) dbfData = [] #读取dbf数据为csv文件,读存内容部分 csvFile = open(csvDir + city + "/RD_LINK.csv",encoding='gbk',errors='ignore') csvReader = csv.reader(csvFile) for row in csvReader: titleLine = [] if csvReader.line_num == 1: titleLine.append(row) continue #跳过第1行――列名 dbfData.append(row) #生成正确的字段名行 newTitleLine = [] modifyDic = {"LANE_WIDTH":"LANEWIDTHL","LANE_WID_1":"LANEWIDTHR","ORIGIN_LIN":"ORG_LINKID"} for fieldName in titleLine: if fieldName in modifyDic: newTitleLine.append(modifyDic[fieldName]) else: newTitleLine.append(fieldName) #写入新dbf文件 newDBF = open(modifyRoadDir + city + "/RD_LINK.dbf",'w') csvWriter = csv.writer(newDBF) csvWriter.writerow(newTitleLine) for row in dbfData: csvWriter.writerow(row) newDBF.close() print("{}新dbf文件已生成!".format(city)) print("{}城市全部完成".format(num))
方法2――直接使用修改字段名函数――失败,arcpy模块没有AlterField_management方法
――失败,但发现直接探寻官方方法还是比网搜野路子要节约时间
使用Arcgis10.2 - Advanced浮动版,符合许可信息但Arcpy调用函数失败,存疑。附官网AlterField函数用法介绍:
https://pro.arcgis.com/zh-cn/pro-app/tool-reference/data-management/alter-field-properties.htm
#-*- coding: utf-8 -*- import os import arcpy #批量修改shp中dbf文件中的字段名 #LANE_WIDTH改为LANEWIDTHL #LANE_WID_1改为LANEWIDTHR #ORIGIN_LIN改为ORG_LINKID roadDir = r"D:\20Q1\00DATA\ModifyTitle\ROAD_TEST\\" fileList = os.listdir(roadDir) num = 0 for file in fileList: #截取城市名 city = file.split("_")[0] print("正在修改{}的shp字段......".format(city)) #修改前后字段名对照字典 modifyDic = {"LANE_WIDTH":"LANEWIDTHL","LANE_WID_1":"LANEWIDTHR","ORIGIN_LIN":"ORG_LINKID"} #读取shp数据 dbfFile = roadDir + file + "/RD_LINK.shp" fieldList = arcpy.ListFields(dbfFile) for field in fieldList: #遍历字段名 if field.name.upper() in modifyDic: #找到待修改字段名 arcpy.AlterField_management(dbfFile, field = field.name, new_field_name = modifyDic[field.name]) print("{}字段修改成功!".format(city)) print("{}城市全部完成".format(num))
方法3――添加改名后新字段(字段属性与前保持一致)、计算改名后字段值 = 改名前字段值,成功!
#-*- coding: utf-8 -*- import os import arcpy #批量修改shp属性表中的字段名 #LANE_WIDTH改为LANEWIDTHL #LANE_WID_1改为LANEWIDTHR #ORIGIN_LIN改为ORG_LINKID roadDir = r"D:\20Q1\00DATA\ModifyTitle\ROAD_TEST\\" fileList = os.listdir(roadDir) num = 0 for file in fileList: city = file.split("_")[0] #截取城市名 print("正在添加和计算{}的shp正确字段......".format(city)) #修改前后字段名对照属性字典 modifyDic = {"LANE_WIDTH":["LANEWIDTHL","1"],#错误字段名:[正确字段名,长度] "LANE_WID_1":["LANEWIDTHR","1"], "ORIGIN_LIN":["ORG_LINKID","10"]} #读取shp文件 shpFile = roadDir + file + "/RD_LINK.shp" for wrongfieldName in modifyDic: correctfieldName = modifyDic[wrongfieldName][0] field_length = modifyDic[wrongfieldName][1] # Process: 添加字段 arcpy.AddField_management(shpFile, correctfieldName, "TEXT", "", "", field_length) # Process: 计算字段字段 arcpy.CalculateField_management(shpFile, correctfieldName, "["+wrongfieldName+"]", "VB", "") ## # Process: 添加字段――LANEWIDTHL ## arcpy.AddField_management(shpFile, "LANEWIDTHL", "TEXT", "", "", "1") ## # Process: 计算字段字段――LANEWIDTHL ## arcpy.CalculateField_management(shpFile, "LANEWIDTHL", "["++]", "VB", "") ## ## # Process: 添加字段――LANEWIDTHR ## arcpy.AddField_management(shpFile, "LANEWIDTHR", "TEXT", "", "", "1") ## # Process: 计算字段字段――LANEWIDTHR ## arcpy.CalculateField_management(shpFile, "LANEWIDTHR", "[LANE_WID_1]", "VB", "") ## ## # Process: 添加字段――ORG_LINKID ## arcpy.AddField_management(shpFile, "ORG_LINKID", "TEXT", "", "", "10") ## # Process: 计算字段字段――ORG_LINKID ## arcpy.CalculateField_management(shpFile, "ORG_LINKID", "[ORIGIN_LIN]", "VB", "") ## ## # Process: 删除字段 ## #arcpy.DeleteField_management(in_table, "LANE_WIDTH") print("{}修改完成!请确认!".format(city)) num += 1 print("{}城市全部完成!".format(num))