当前位置: 首页 > news >正文

Matlab基于经纬度点并行提取指定日期的tiff栅格位置的值

文章目录

  • 前言
  • 一、基本说明
  • 二、代码


前言

该 MATLAB 代码用于从 GeoTIFF 文件中提取基于特定地理位置(经纬度)和日期的某个点的相关数据。代码首先读取一个包含事件数据(日期、经纬度)的 Excel 文件,然后根据日期和位置尝试从存储滑坡风险数据的 GeoTIFF 文件中提取相应的数值。为了加快数据提取过程,代码使用了并行处理。


一、基本说明

数据加载:从 Excel 文件中读取包含日期、经纬度的事件数据。
路径设置:指定 GeoTIFF 文件存储的文件夹路径。
GeoTIFF 数据提取:针对每个事件的日期和位置:
构建文件名并检查文件是否存在。
若文件存在,计算指定经纬度对应的行列索引并提取相应数值。
若坐标超出边界,则赋值为 NaN。
并行处理:使用 parfor 来加速处理每个日期-位置对。
结果存储:将提取的数值存储在数组 gpmresult 或 mswepresult 中。
该代码使得从大规模数据集中高效提取滑坡风险数据成为可能,并且具备处理缺失或越界数据的鲁棒性。

二、代码

% 清空环境、命令窗口,并关闭所有打开的图形窗口
clear all;
clc;
close all;% 加载滑坡事件数据,包括日期、纬度和经度
data = readtable('E:\data.xlsx');
longitude = data{:, 3};  % 提取经度数据
latitude = data{:, 2};   % 提取纬度数据
dates = data{:, 1};      % 提取日期,格式为 'yyyy/MM/dd'% 设置 GeoTIFF 文件夹路径
tif_folder = 'E:\';% 初始化结果数组
gpmresult = [];% 使用并行处理遍历每个数据点
parfor i = 1:length(dates)% 检查日期是单元格格式还是字符数组if iscell(dates)date_str = dates{i};elsedate_str = dates(i, :);end% 将日期字符串转换为 datetime 对象date_obj = datetime(date_str, 'InputFormat', 'yyyy/MM/dd');year_folder = datestr(date_obj, 'yyyy'); % 从日期中提取年份作为文件夹名称% 获取当前点的经纬度lon = longitude(i);lat = latitude(i);% 使用 'yyyymmdd' 格式构建 GeoTIFF 文件路径tif_file = fullfile(tif_folder, year_folder, sprintf('%s.tif', datestr(date_obj, 'yyyymmdd')));% 检查 GeoTIFF 文件是否存在if isfile(tif_file)% 获取 GeoTIFF 文件的元数据并读取数据info = geotiffinfo(tif_file);[data, R] = readgeoraster(tif_file);% 计算指定纬度和经度的行列索引row = ceil((max(R.LatitudeLimits) - lat) / R.CellExtentInLatitude);col = ceil((lon - min(R.LongitudeLimits)) / R.CellExtentInLongitude);% 检查计算的行列是否越界if row <= 0 || col <= 0 || row > size(data, 1) || col > size(data, 2)value30 = NaN; % 如果越界则设置为 NaNelsevalue30 = data(row, col); % 如果在范围内则提取数据值end% 将提取的值添加到结果数组中gpmresult = [gpmresult; value30];else% 如果文件不存在则输出提示信息fprintf('File not found: %s\n', tif_file);end
end
disp('GPM well done');%% 以下部分与第一个代码块类似,但用于另一个数据集
% 使用日期格式 'yyyydoy' (一年中的第几天)% % 设置 MSWEP GeoTIFF 文件夹路径
% tif_folder = 'H:\';
% mswepresult = [];
% 
% % 使用并行处理遍历每个点
% parfor i = 1:length(dates)
%     % 检查日期格式并转换
%     if iscell(dates)
%         date_str = dates{i};
%     else
%         date_str = dates(i, :);
%     end
%     
%     date_obj = datetime(date_str, 'InputFormat', 'yyyy/MM/dd');
%     year_folder = datestr(date_obj, 'yyyy');
%     day_of_year = day(date_obj, 'dayofyear'); % 获取一年中的第几天
%     
%     lon = longitude(i);
%     lat = latitude(i);
%     tif_file = fullfile(tif_folder, year_folder, sprintf('_%d%03d.tif', year(date_obj), day_of_year));
%     
%     if isfile(tif_file)
%         info = geotiffinfo(tif_file);
%         [data, R] = readgeoraster(tif_file);
%         
%         row = ceil((max(R.LatitudeLimits) - lat) / R.CellExtentInLatitude);
%         col = ceil((lon - min(R.LongitudeLimits)) / R.CellExtentInLongitude);
%         
%         if row <= 0 || col <= 0 || row > size(data, 1) || col > size(data, 2)
%             value30 = NaN; % 如果越界则设置为 NaN
%         else
%             value30 = data(row, col); % 如果在范围内则提取数据值
%         end
%         
%         mswepresult = [mswepresult; value30];
%     else
%         fprintf('File not found: %s\n', tif_file);
%     end
% end
% disp('MSWEP well done');
http://www.lryc.cn/news/476798.html

相关文章:

  • npm入门教程19:npm包管理
  • 【NOIP提高组】虫食算
  • 软件测试面试题个人总结
  • HTML 语法规范——代码注释、缩进与格式、标签与属性、字符编码等
  • 【Wi-Fi】WiFi中QAM及16-QAM、64-QAM、512-QAM、1024-QAM、2048-QAM、4096-QAM整理
  • 红黑树的平衡之舞:数据结构中的优雅艺术
  • angular实现list列表和翻页效果
  • 闯关leetcode——3285. Find Indices of Stable Mountains
  • 算法【Java】—— 动态规划之斐波那契数列模型
  • idea连接docker并构建镜像
  • 百度如何打造AI原生研发新范式?
  • RedisTemplate类中的常用方法粗解(简单明了,预计5分钟看完)
  • 鸿蒙ArkTS中的布局容器组件(Column、Row、Flex、 Stack、Grid)
  • 显存占用 显存测试
  • 快速入门CSS
  • AcWing 1073 树的中心 树形dp (详解)
  • modelscope下载Qwen2.5 72B 模型方法
  • 重学SpringBoot3-整合 Elasticsearch 8.x (二)使用Repository
  • 为什么说模拟电路的难点就在开通过程和关断过程?难在什么地方?
  • CubeIDE BUG-project‘hello‘has no explict encoding set hello
  • 在线PDF转图片网站
  • ps和top的区别
  • 自动驾驶上市潮中,会诞生下一个“英伟达”吗?
  • CSS 计数器:深入解析与高级应用
  • 【真题笔记】15年系统架构设计师要点总结
  • 斗破C++编程入门系列之三十九:多态性:纯虚函数和抽象类(四星斗师)
  • 目前美国的互联网环境
  • 从最小作用量原理推导牛顿三大定律
  • 【系统集成项目管理工程师教程】第4章 信息系统架构
  • docker下迁移elasticsearch的问题与解决方案