Created
May 23, 2018 06:55
-
-
Save zhuang-hao-ming/7f51fee9a7dc0fc9877a9c79b11d5a5e to your computer and use it in GitHub Desktop.
Use gdal to convert bin format to tiff format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import subprocess | |
file_list = [] | |
for f in os.listdir('.'): | |
if os.path.splitext(f)[1] == '.bin': | |
file_list.append(f) | |
for f in file_list: | |
out_filename = './out/' + os.path.splitext(f)[0] + '.tif' | |
command_str = 'gdal_translate {in_filename} {out_filename}'.format(in_filename=f, out_filename=out_filename) | |
print(command_str) | |
p = subprocess.Popen(command_str) | |
p.wait() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from osgeo import gdal, osr | |
import numpy as np | |
from time import time | |
import os | |
def array2raster(rasterfn, newRasterfn, array): | |
raster = gdal.Open(rasterfn) | |
geotransform = raster.GetGeoTransform() | |
originX = geotransform[0] | |
originY = geotransform[3] | |
pixelWidth = geotransform[1] | |
pixelHeight = geotransform[5] | |
print(originX, originY, pixelWidth, pixelHeight) | |
cols = raster.RasterXSize | |
rows = raster.RasterYSize | |
driver = gdal.GetDriverByName('GTiff') | |
outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32) | |
outRaster.SetGeoTransform((-180, 0.008333333333333, 0, 90, 0, -0.008333333333333)) | |
outband = outRaster.GetRasterBand(1) | |
outband.WriteArray(array) | |
outRasterSRS = osr.SpatialReference() | |
outRasterSRS.ImportFromEPSG(4326) | |
outRaster.SetProjection(outRasterSRS.ExportToWkt()) | |
outband.FlushCache() | |
def flip_raster(in_filename, out_filename): | |
begin_tick = time() | |
gtif = gdal.Open(in_filename) | |
band = gtif.GetRasterBand(1) | |
array = band.ReadAsArray() | |
flip_array = np.flipud(array) | |
print('finish flippint', in_filename) | |
array2raster(in_filename, out_filename, flip_array) | |
print('finish write', out_filename, time() - begin_tick) | |
if __name__ == '__main__': | |
file_list = [] | |
for f in os.listdir('./out'): | |
if os.path.splitext(f)[1] == '.tif': | |
file_list.append('./out/' + f) | |
for f in file_list: | |
out_filename = './result/' + os.path.split(f)[1] | |
print(f, out_filename) | |
flip_raster(f, out_filename) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
''' | |
1. 为每个文件生成头文件 | |
2. bin to tiff | |
3. 翻转tiff并添加坐标系 | |
''' | |
import os | |
meta = '''ENVI | |
samples = 43200 | |
lines = 21600 | |
data type = 4 | |
bands = 1 | |
byte order = 0 | |
''' | |
file_list = [] | |
for f in os.listdir('.'): | |
if os.path.splitext(f)[1] == '.bin': | |
file_list.append(f) | |
for f in file_list: | |
header_f = os.path.splitext(f)[0] + '.hdr' | |
print(header_f) | |
with open(header_f, 'w', encoding='utf-8') as f_handle: | |
f_handle.write(meta) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment