(四十一)arcpy开发&创建gdb,复制shapefile与更改属性表

今天的这个例子是利用arcpy来创建一个gdb,然后将一个shapefile文件复制到该gdb中,最后我们还会更新该属性表的一些数据。实现这个例子,你将会使用到创建gdb函数,CreateFileGDB_management。临时图层复制函数,MakeFeatureLayer_management。将数据复制到到gdb中,FeatureClassToGeodatabase_conversion。以及要素更新函数,UpdateCursor。那么,我们事先准备好一份数据。如下图所示,是一个没有空间参考的shapefile数据。注意里面的【NAME】字段是没有数据的。

现在需要将数据复制到gdb数据库中,那么我们来看实现代码。特别需要注意里面的路径,在相应的函数中需要添加上output_location完整路径,否则会报参数错误。

#encoding:utf-8
import arcpy

arcpy.env.overwriteOutput = True
input_data="D:/Data/polylineTest/test.shp"
output_location="C:/Users/qin/Desktop/shp"
arcpy.AddMessage("starting progress")
arcpy.CreateFileGDB_management(
    output_location, "test2018_5_16.gdb")


arcpy.MakeFeatureLayer_management(input_data, "input_data")
arcpy.FeatureClassToGeodatabase_conversion(
    ["input_data"], output_location+"/"+"test2018_5_16.gdb")

arcpy.MakeFeatureLayer_management(
    output_location+"/"+"test2018_5_16.gdb/input_data", "lyr_input_data")

with arcpy.da.UpdateCursor("lyr_input_data",["NAME"]) as update_cursor:
    index=1
    for row in update_cursor:
        row[0] = index
        index+=1
        update_cursor.updateRow(row)
del update_cursor
arcpy.Delete_management('lyr_input_data')
arcpy.Delete_management('input_data')

print 'finished'
我们来看一下在指定目录下创建好的gdb文件。

然后,用ArcGIS Desktop来打开移动、和修改的文件。

好了,本次就学习到这里。


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

                                                                     

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yGIS

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

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

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

打赏作者

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

抵扣说明:

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

余额充值