(二十四)arcpy开发&修改arcgis中的字段长度

58 篇文章 19 订阅
52 篇文章 79 订阅

首先在测试这段代码之前,我们这里新建一个字段用于测试。

然后将FID的值相等过来。

最后我们的属性表如下图所示。

那么我们来看一下代码的实现过程,首先我们读取了一个要素类,然后设置我们需要修改长度的对应字段,然后设置对应的长度。在没有将字段值删除之前,我们需要将原有的数据给保存起来。然后再将我们的shapefile数据需要删除的字段给删除点,之后就是重新创建字段了。创建完字段后,接着将原来保存好的值添加进入。至此整个过程就完了。下面我们来一下实现的代码,主要利用了UpdateCursor、SearchCursor。

import arcpy


inputtable = 'D:/Data/中国国界和省界的SHP格式数据/省界/bou2_4p.shp'
target_field='CHKJ'
fld_length=15


inputDesc = arcpy.Describe(inputtable)
print "Feature Type:  " + inputDesc.featureType
print "Shape Type :   " + inputDesc.shapeType
print "Shape Type :   " + inputDesc.dataType


if inputDesc.dataType in ("FeatureClass", "Table","ShapeFile"):
    replace_fields = ['OID@', target_field]
    valueDict = {r[0]: r[1] for r in arcpy.da.SearchCursor(inputtable, replace_fields)}

    with arcpy.da.UpdateCursor(inputtable, replace_fields) as cursor:
        arcpy.AddMessage("delete {} field".format(target_field))
        arcpy.DeleteField_management(inputtable, target_field)
        arcpy.AddMessage("add {} field".format(target_field))
        arcpy.AddField_management(inputtable, target_field, field_type='TEXT', field_length=int(fld_length))
        arcpy.AddMessage("update field value")
        for row in cursor:
            row[1] = valueDict[row[0]]
            cursor.updateRow(row)
    arcpy.AddMessage("Finished")
else:
    arcpy.AddMessage("****** Input is not Feature Class or Table ******")

最后我们来看一下实现效果。


                           更多内容,请微信扫二维码关注公众号,或者加入arcpy开发qq学习群:487352121

                                                                 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yGIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值