Python地學分析 — 向量資料輸出 03
阿新 • • 發佈:2018-12-08
歡迎關注博主的微信公眾號:“智慧遙感”。
該公眾號將為您奉上Python地學分析、爬蟲、資料分析、Web開發、機器學習、深度學習等熱門原始碼。
Python的小夥伴們,咱們又見面啦!
本節給大家帶來得知識點仍然是GDAL的向量資料操作,主要是向量資料的輸出,好了,廢話不多說,直接上程式碼!
~~~~~~~~~~~~~~~~~~~~~~~~~~
程式設計環境:
作業系統:windows
Python版本:2.7
IDE版本:PyCharm 2018.2.4專業版
~~~~~~~~~~~~~~~~~~~~~~~~~~
# _*_ coding: utf-8 _*_ __author__ = 'xbr' __date__ = '2018/11/2 11:36' import sys from osgeo import ogr # 獲取該問夾下下所有shp資料集 ds = ogr.Open(r'D:\osgeopy-data\global', 1) # 如果獲取失敗,則程式報錯,不執行 if ds is None: sys.exit('Could not open folder.') in_lyr = ds.GetLayer('ne_50m_populated_places') # 獲取該shp檔案 # 如果該檔案下存在獲shp檔案capital_cities,則刪除,為了後面重新建立該shp檔案 if ds.GetLayer('capital_cities'): ds.DeleteLayer('capital_cities') out_lyr = ds.CreateLayer('capital_cities', in_lyr.GetSpatialRef(), ogr.wkbPoint) out_lyr.CreateFields(in_lyr.schema) # 建立shp屬性表字段 out_defn = out_lyr.GetLayerDefn() out_feat = ogr.Feature(out_defn) for in_feat in in_lyr: if in_feat.GetField('FEATURECLA') == 'Admin-0 capital': geom = in_feat.geometry() out_feat.SetGeometry(geom) # 建立幾何資訊 # 將輸入shp檔案中的屬性值賦給輸出shp檔案中的屬性值 for i in range(in_feat.GetFieldCount()): value = in_feat.GetField(i) out_feat.SetField(i, value) out_lyr.CreateFeature(out_feat) del ds
~~~~~~~~~~~~~~~~~~~~~~~~~~~
由於百度網盤上傳資料開始抽風,小編決定放棄使用,後面會把資料和原始碼上傳到我的GitHub上,大家敬請關注!
哦,還有一件事,我的CSDN部落格也在同步更新,有什麼好的想法和建議,可以在我的部落格下方留言(微信公眾號留言功能目前已取消,希望騰訊快點兒開通吧 *^_^*),相互學習與交流,歡迎您的參與!部落格連結:
https://blog.csdn.net/XBR_2014
~~~~~~~~~~~~~~~~~~~~~~~~~~~