Python+GDAL实现ASCII文件转Geotiff

变强没有任何其他原因,只是为了让这个世界待你以善意。


前言

1. 版本

   1.1 天津,2022年10月11日,Version 1

2. 摘要

Python+GDAL实现ASCII文件转Geotiff

3. 微信公众号GISRSGeography

  • 公众号 GISRSGeography的内容涉及GIS,遥感和作物模型等的内容,会坚持更新,
    欢迎大家关注,谢谢!。
    GISRSGeography

一、程序

# -*- coding: utf-8 -*-
"""
1. 程序目的
   将ASCII文件转换为Geotiff文件
   
2. 版本
   2.1 天津 2022年10月11日

3. 数据
   3.1 输入数据
   
   3.2 输出数据
   
4. 经验积累
  (1)
   
5. 参考资料
"""
# %% 相关包的导入
import os
import glob
import shutil
from tqdm import tqdm # 运行计时
from osgeo import osr,gdal

# %% 函数定义
  # 1. 转换方式定义
def ascii2tif(
        ascdir: str,
        ) -> None:
    '''
    (1) 功能:ASCII\txt文件(txt)转Geotiff文件
    ------------
    
    (2) 输入参数
        ascdir: str, 存储ascii\txt文件的路径, 需要满足python的路径格式要求
              示例: 'a\\b\\'
    
    (3) 输出
        无
    '''
    
    # (1) 创建输出文件夹
    outpath = ascdir + 'TiffFile'
    if os.path.exists(outpath):
        shutil.rmtree(outpath)
        os.mkdir(outpath)
    else:
        os.mkdir(outpath)
    
    asciifile_all = glob.glob(ascdir+'*.txt') 
    #print(asciifile_all)
    #print('共计%s个ASCII\\txt文件需要进行格式转换.\r\n'%str(len(asciifile_all)))
    
    # (2) 格式转换
    for ascii_file in tqdm(asciifile_all):
        ascii_file_folder,ascii_file_name = os.path.split(ascii_file)
        tif_file_path = outpath + '\\' + \
            ascii_file_name.replace('.txt','.tif') # 输出文件
        ds_in = gdal.Open(ascii_file) # 打开ascii文件
        gtiff_driver = gdal.GetDriverByName('GTiff') # 启动Gtiff驱动
        ds_out = gtiff_driver.CreateCopy(tif_file_path,ds_in)
        
        # 空间参考设置
        srs = osr.SpatialReference()
        # https://www.spatialreference.org/ref/epsg/4326/
        srs.ImportFromEPSG(4326)
        ds_out.SetProjection(srs.ExportToWkt())
        
        ds_in = None
        ds_out = None
        gtiff_driver = None # 关闭驱动
   
    #print('ASCII2TIF格式转换完成.\r\n')     

# %%
if __name__ == '__main__':
    
   asciidir = 'F:\\ASCII2TIF\\'
   ascii2tif(asciidir)
   
   print('Finished')

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

EWBA_GIS_RS_ER

如有帮助,赏杯茶吧。

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

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

打赏作者

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

抵扣说明:

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

余额充值