Python+GDAL實現txt/csv轉.shp文件


需要的python包:

os,glob,pandas,gadl(osgeo)

gdal包下載:https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal,找到對應python版本的gdal版本,下載。cmd下cd進入文件路徑,pip install xxxxx.whl安裝。

pandas下載:cmd下,pip install -i 鏡像網址 pandas

GDAL使用參考:https://www.osgeo.cn/pygis/proj-osr.html

 

以txt為例:

##  txt\csv轉shp,txt需要加正則表達
# 導入相關庫
import os
from osgeo import ogr
import pandas as pd
from osgeo import osr
import glob

# 啟動異常報錯提示
ogr.UseExceptions()

# .shp文件保存路徑
shp_path = r'D:\Projects\wwlln\all\ceshi_num\tiqu_678\hb\txt2shp'
# 輸入的csv/txt文件路徑
txt_path = r'D:\Projects\wwlln\all\ceshi_num\tiqu_678\hb\txt'

for txt_filename in glob.glob(os.path.join(txt_path,'*.txt')):

    # 讀入csv/txt文件信息,設置點幾何的字段屬性
    txt_df = pd.read_table(txt_filename,header=None,sep='\s+')   ##  csv時不需要正則表達
    print(txt_df)

    # 利用.csv/.txt文件創建一個點shp文件
   
    # 獲取驅動
    driver = ogr.GetDriverByName('ESRI Shapefile')
    
    # 創建數據源
    shp_filename = os.path.basename(txt_filename)[:-4] + '.shp'
    # 檢查數據源是否已存在
    if os.path.exists(os.path.join(shp_path, shp_filename)):
        driver.DeleteDataSource(os.path.join(shp_path, shp_filename))    
    ds = driver.CreateDataSource(os.path.join(shp_path, shp_filename))
    
    # 圖層名
    layer_name = os.path.basename(txt_filename)[:-4]
    
    # 定義坐標系對象
    sr = osr.SpatialReference()
    # 使用WGS84地理坐標系
    sr.ImportFromEPSG(4326)
    
    # 創建點圖層, 並設置坐標系
    out_lyr = ds.CreateLayer(layer_name, srs = sr, geom_type=ogr.wkbPoint)
    
    # 創建圖層定義
    # 利用csv/txt文件中的字段創建對應數量的屬性字段
    ### zm:我的csv文件字段為:date1,time1,lat,lon,timingerror,statnumber,而我只需要date1,lat和lon字段
    # date字段
    date1_fld = ogr.FieldDefn('date1', ogr.OFTString)
    date1_fld.SetWidth(11)
    out_lyr.CreateField(date1_fld)
    
    #添加time字段
    # time1_fld = ogr.FieldDefn('time1', ogr.OFTString)
    # time1_fld.SetWidth(10)
    # out_lyr.CreateField(time1_fld)
    
    # Latitude字段
    lat_fld = ogr.FieldDefn('latitude', ogr.OFTReal)
    lat_fld.SetWidth(12)
    lat_fld.SetPrecision(6)
    out_lyr.CreateField(lat_fld)
    # Longitude字段
    lon_fld = ogr.FieldDefn('longitude', ogr.OFTReal)
    lon_fld.SetWidth(12)
    lon_fld.SetPrecision(6)
    out_lyr.CreateField(lon_fld)

    # timingerror字段
    # timingerror_fld = ogr.FieldDefn('timingerror',ogr.OFTReal)
    # timingerror_fld.SetWidth(4)
    # timingerror_fld.Setprecision(2)
    # out_lyr.CreateField(timingerror_fld)

    # statnumber 字段
    # statnum_fld = ogr.FieldDefn('timingerror',ogr.OFTINTEGER)
    # statnum_fld.Width(2)
    # out_lyr.CreateField(statnum_fld)   

    # 從layer中讀取相應的feature類型,並創建feature
    featureDefn = out_lyr.GetLayerDefn()
    feature = ogr.Feature(featureDefn)
    
    # 設定幾何形狀
    point = ogr.Geometry(ogr.wkbPoint)
    
    # 讀入csv/txt文件信息,設置點幾何的字段屬性
    for i in range(len(txt_df)):
        
        # 設置屬性值部分
        # 站點Id
        feature.SetField('date1', str(txt_df.iloc[i, 0]))
        # 時間
        # feature.SetField('time1',str(txt_df.iloc[i,1])
        # 緯度
        feature.SetField('latitude', float(txt_df.iloc[i, 2]))
        # 經度
        feature.SetField('longitude', float(txt_df.iloc[i, 3]))
        #   ZM:我這里為timingerror,statnum,不寫了
        # feature.SetField('timingerror', float(txt_df.iloc[i, 4]))
        
        # 設置幾何信息部分
        # 利用經緯度創建點, X為經度, Y為緯度
        point.AddPoint(float(txt_df.iloc[i, 3]), float(txt_df.iloc[i, 2]))
        feature.SetGeometry(point)
        
        # 將feature寫入layer
        out_lyr.CreateFeature(feature)
    
    # 從內存中清除 ds,將數據寫入磁盤中
    ds.Destroy()


參考文章:https://blog.csdn.net/lidahuilidahui/article/details/103223147

翻譯 朗讀 復制 正在查詢,請稍候…… 重試 朗讀 復制 復制 朗讀 復制 via 谷歌翻譯(國內)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM