DataProcessToolProgram/tools/DataFormatConversion/tiff2bin.py

41 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import argparse
import os
from osgeo import gdal, osr
import numpy as np
def tiff_to_bin_with_metadata(tiff_path, bin_path):
# 转换格式为ENVI格式的BIN自动生成.hdr元数据文件
options = gdal.TranslateOptions(
format='ENVI', # ENVI格式生成.hdr文件存储地理信息
)
# 临时输出ENVI格式文件含.hdr
# temp_path = bin_path.replace('.bin', '_temp.bin')
gdal.Translate(bin_path, tiff_path, options=options)
# # 重命名文件并清理临时文件
# os.rename(temp_path, bin_path) # 主数据文件
# os.rename(temp_path + '.hdr', bin_path + '.hdr') # 元数据文件
# 可选:额外保存投影文件(.prj
dataset = gdal.Open(tiff_path)
if dataset.GetProjection():
with open(bin_path.replace('.bin', '.prj'), 'w') as f_prj:
f_prj.write(dataset.GetProjection())
dataset = None
def getParams():
parser = argparse.ArgumentParser()
parser.add_argument('-i','--infile',default=r'K:\风场数据\样例风场数据\4254_20240801095415\s1a-iw-grd-vv-20240801t095415-20240801t095444-055018-06b3e9-001-DB-GEO.tif', help='输入原始tiff')
parser.add_argument('-o', '--outfile',default=r'K:\风场数据\样例风场数据\4254_20240801095415\s1a-iw-grd-vv-20240801t095415-20240801t095444-055018-06b3e9-001-DB-GEO.bin', help='输出Bin文件路径')
args = parser.parse_args()
return args
if __name__ == '__main__':
parser = getParams()
intiffPath=parser.infile
outbinPath=parser.outfile
print('infile=',intiffPath)
print('outfile=',outbinPath)
tiff_to_bin_with_metadata(intiffPath, outbinPath)
print("convert tiff to bin file successfully")