利用Matlab读取DEM和对DEM的可视化相比OpenGL要简单很多。本文就中国标准格式的DEM,对DEM数据进行读取与可视化。
标准格式DEM数据由数据表头与数据两部份组成,如下图所示。数据表头描述了DEM数据的基本信息,包括格式,版本,单位,起点,取样精度,行数列数,数据类型,放缩倍数等信息。 数据项则是一行十个数据值,DEM的一行组成数据文件的一个数据块儿,在下图实例中1200个数据值(数据文件中的120行)组成DEM的一行数据,即一个数据块儿。这在读取的过程中需要对数据的序号进行合理的转换。
代码如下
file = fopen('实习dem-中国格式.dem','r'); frame = fgetl(file); %检查数据格式 if ~strcmp(frame,'DataMark: CNSDTF-DEM') warning('数据格式错误'); exit(); end %读取本例未使用的几行数据 fgetl(file); fgetl(file); fgetl(file); fgetl(file); %读取必要的数据 while(~feof(file)) line = fgetl(file); S = regexp(line, ':+', 'split'); if strcmp(S(1),'X0') X0 = str2double(S(2)); end if strcmp(S(1),'Y0') Y0 = str2double(S(2)); end if strcmp(S(1),'DX'); DX = str2double(S(2)); end if strcmp(S(1),'DY'); DY = str2double(S(2)); end if strcmp(S(1),'Row'); Row = str2double(S(2)); end if strcmp(S(1),'Col'); Col = str2double(S(2)); break; end end %读取本例不需要的数据行 fgetl(file); fgetl(file); fgetl(file); %开始读取数据 Height = zeros(Row,Col); for i = 1:Row for j=1:Col/10 line = fgetl(file); S = regexp(line, '\s+', 'split'); Height(i,(j-1)*10+1:j*10) = str2double(S(1:10))'; end end Y = DY:DY:Row*DY; X = DX:DX:Col*DX; %绘制三维Dem meshz(X,Y,Height);rotate3d, %绘制热力图 figure(2) surf(X,Y,Height),rotate3d shading interp; colormap(hot); colorbar; %绘制等高线 figure(3); [C,h]=contour(X,Y,Height./100000,16); %绘制三维等高线 figure(4); contour3(X,Y,Height./100000,16);