实现用一个shp文件中的不同要素一一对应裁剪不同的tiff数据
clc;
clear;
close all
[file,path]=uigetfile('*.shp','选择缓冲区');
bufferpath=[path,file];
buffer_shp=shaperead(bufferpath);%n*1的struct即n个struct,读取buffershp(n).X
shp_num=length(buffer_shp);
%建立结构体存放统计数据
STR='struct("SID",values,"TIME",values,"mean",values,"max",values)';
values = cell(shp_num, 1);
S=eval(STR);
%遍历buffer_shp制作每个buffer的掩膜并裁剪
for i=1:shp_num
tiffpath='C:\Users\44861\Desktop\2018tif\';
tiffname=[num2str(buffer_shp(i).year),num2str(buffer_shp(i).month,'%02d'),...
num2str(buffer_shp(i).day,'%02d'),num2str(buffer_shp(i).hour,'%02d'),'.tif'];
%读取tif文件加一个判断这个文件是否存在
if ~exist([tiffpath,tiffname],'file')
disp([tiffname,'not exist']);
continue;
end
[raintiff,R]=geotiffread([tiffpath,tiffname]);
%经纬度范围
lonlim=-180:0.25:179.75;
latlim=60:-0.25:-59.75;
%将图像划分格网
[glon,glat]=meshgrid(lonlim,latlim);
%判断在buffer内的点
inbuffer=inpolygon(glon,glat,buffer_shp(i).X,buffer_shp(i).Y);
raintiff(~inbuffer(:))=NaN;
%保存tif或计算
savepath=['C:\Users\44861\Desktop\2018TCP\',tiffname];
geotiffwrite(savepath,raintiff,R);
%保存统计数据
%名称,时间,统计数据(均值,最大值)
S(i).SID=buffer_shp(i).SID;
S(i).TIME=buffer_shp(i).ISO_TIME;
meandata=mean(reshape(raintiff, size(raintiff,1)*size(raintiff,2),1),'omitnan');
S(i).mean=meandata;
S(i).max=max(max(raintiff));
end