1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| function tif2grd(input_abspath, output_abspath) %{ 读取tif数据并转化为txt文件,主要用于读取InSAR处理后的形变数据。 input_abspath: str 输入的tif文件的绝对/相对路径,'E:\tif2txt\disp.tiff'; output_abspath:str 输出txt文件的绝对/相对路径,'E:\tif2txt\outputdata.txt'; example: tif2grd('.\disp.tiff', '.\outputdata.txt') %}
[value_m, geoinfo]= geotiffread(input_abspath); [m_row, m_col] = size(value_m); ngrid = m_row * m_col; disp_value = reshape(value_m, ngrid,1); lonlat_spacing = geoinfo.SampleSpacingInLatitude; lon_lim = geoinfo.LongitudeLimits; lat_lim = geoinfo.LatitudeLimits;
lon = lon_lim(1):lonlat_spacing:lon_lim(2); lat = lat_lim(1):lonlat_spacing:lat_lim(2);
if length(lon) ~= m_col || length(lat) ~= m_row disp('number of lon or lat error.'); exit; end
[glon,glat] = meshgrid(lon,lat); glon_l = reshape(glon, ngrid, 1); glat_l = reshape(glat, ngrid, 1);
output_m = [glon_l, glat_l, disp_value]; dlmwrite(output_abspath, output_m, 'delimiter', '\t'); disp('save .txt data successfule') end
|