(四十一)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

                                                                     

發佈了243 篇原創文章 · 獲贊 82 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章