今天的这个例子是利用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