1 # convert geotiff file to matlab file & print basic info on screen too
2 # usage: python geotiff2mat.py filename.tif
3 # limitation: for now, one band only
12 outfile
= file + '.mat'
14 # read the geotiff file
15 # see http://geoinformaticstutorial.blogspot.com/2012/09/reading-raster-data-with-python-and-gdal.html
18 from gdalconst
import *
19 print('Reading GeoTiff file',infile
)
20 dataset
= gdal
.Open(infile
, GA_ReadOnly
)
21 cols
= dataset
.RasterXSize
22 rows
= dataset
.RasterYSize
23 bands
= dataset
.RasterCount
25 print('columns ',cols
)
27 geotransform
= dataset
.GetGeoTransform()
28 print('top left X ',geotransform
[0])
29 print('W-E pixel resolution',geotransform
[1])
30 print('rotation, 0=North up',geotransform
[2])
31 print('top left Y ',geotransform
[3])
32 print('rotation, 0=North up',geotransform
[4])
33 print('N-S pixel resolution',geotransform
[5])
34 band
= dataset
.GetRasterBand(1)
35 data
= band
.ReadAsArray(0, 0, cols
, rows
)
38 # using http://docs.scipy.org/doc/scipy-0.14.0/reference/tutorial/io.html
39 import scipy
.io
as sio
40 print('writing Matlab file ',outfile
)
41 sio
.savemat(outfile
,{'data' : data
,'geotransform' : geotransform
})