MATLAB读取TIF文件,并可视化
在GIS领域,TIF文件则常用于存储地图、地形图等地理空间数据,TIF文件用于地理信息系统时,它通常包含地理坐标、投影信息等地理元数据,这些元数据使得图像能够与地理信息系统无缝集成,便于进行地理定位和分析。
1.读取TIF数据
clc
clear
close all
% 设置工作路径
folder = [pwd,'\huatu'];
files = dir(fullfile(folder, '*.tif'));
info = geotiffinfo(fullfile(files(1).folder, files(1).name));
data = [];
for i = 1:length(files)
filename = fullfile(files(i).folder, files(i).name);
[temp, R] = readgeoraster(filename); % 读取栅格数据和空间参考
if isempty(data)
data = zeros([size(temp), length(files)]);
end
data(:,:,i) = temp;
end
2.数据处理及画图
meanpre= mean(data, 3, 'omitnan');
meanpre(meanpre < -99)=NaN;
figure;
lat = linspace(info.SpatialRef.LatitudeLimits(1),info.SpatialRef.LatitudeLimits(2),144);
lon = linspace(info.SpatialRef.LongitudeLimits(1),info.SpatialRef.LongitudeLimits(2),256);
[mlat,mlon] = meshgrid(lat,lon);
m_proj('Equidistant Cylindrical','long',[70.0000 140.0000],'lat',[15.0008 55.0008]);
m_gshhs('fb2','color','b'); % 满分辨率河流,蓝色
h = m_pcolor(mlon,mlat,meanpre','linestyle','none');%
title('中国区域降水量分布图');
m_grid('ytick',15:10:55,'xtick',70:10:140,'tickdir','out','linest','none','fontname','Times','fontsize',12,'linewidth',1.5);
colormap(flipud(slanCM('freeze')))
colorbar;
cbarrow;
运行结果如下:
3.绘制中国省界和国界
hold on
bordersl=shaperead('./bou2_4l.shp');% 读取中国国界数据
boux=[bordersl(:).X];
bouy=[bordersl(:).Y];
m_plot(boux,bouy,'k','linewidth',1);
运行结果如下:
4.完整程序
clc
clear
close all
folder = [pwd,'\huatu'];
files = dir(fullfile(folder, '*.tif'));
info = geotiffinfo(fullfile(files(1).folder, files(1).name));
data = [];
for i = 1:length(files)
filename = fullfile(files(i).folder, files(i).name);
[temp, R] = readgeoraster(filename); % 读取栅格数据和空间参考
if isempty(data)
data = zeros([size(temp), length(files)]);
end
data(:,:,i) = temp;
end
meanpre= mean(data, 3, 'omitnan');
meanpre(meanpre < -99)=NaN;
figure;
lat = linspace(info.SpatialRef.LatitudeLimits(1),info.SpatialRef.LatitudeLimits(2),144);
lon = linspace(info.SpatialRef.LongitudeLimits(1),info.SpatialRef.LongitudeLimits(2),256);
[mlat,mlon] = meshgrid(lat,lon);
m_proj('Equidistant Cylindrical','long',[70.0000 140.0000],'lat',[15.0008 55.0008]);
m_gshhs('fb2','color','b'); % 满分辨率河流,蓝色
h = m_pcolor(mlon,mlat,meanpre','linestyle','none');%
title('中国区域降水量分布图');
m_grid('ytick',15:10:55,'xtick',70:10:140,'tickdir','out','linest','none','fontname','Times','fontsize',12,'linewidth',1.5);
colormap(flipud(slanCM('freeze')))
colorbar;
cbarrow;
hold on
bordersl=shaperead('./bou2_4l.shp');% 读取中国国界数据
boux=[bordersl(:).X];
bouy=[bordersl(:).Y];
m_plot(boux,bouy,'k','linewidth',1);