首先设置输入文件路径 input_file_path,表示待处理的CSV文件所在的路径。
调用 readtable 函数读入CSV文件数据,并赋值给变量 data 。
从 data 中获取经度(Longitude)和纬度(Latitude)列的数据,存储到变量 lon 和 lat 中。
计算相邻两个航线点之间的距离,存储到 distances 数组中。
定义阈值 threshold 来判断哪些点为航线的特征点。遍历整个航线数据,找出每个距离大于阈值的点的索引,并记录在 feature_points 数组中。
根据特征点数量确定输出的特征点数量。如果特征点数量少于20个,则直接使用 feature_points 数组作为输出;否则,根据特征点数量平均分配特征点索引,并补充到20个左右,存储在 feature_indices 数组中。
指定输出文件路径 output_file_path。将特征点的坐标和其他数据导出到CSV文件中,使用 writetable 函数实现
%设置输入文件路径
input_file_path = 'C:/CATHERINE QUIGG_1.csv';
% 导入csv文件数据
data = readtable(input_file_path);
%获取经度(Longitude)和纬度(Latitude)列的数据
lon = data.LON;
lat = data.LAT;
%计算相邻两个航线点之间的距离
distances = distance2gps(lat(1:end-1), lon(1:end-1), lat(2:end), lon(2:end));
%定义阈值,判断哪些点为航线的特征点
threshold = 0.01; % 单位:海里
feature_points = [1];
for i = 2:length(lat)-1
if distances(i-1) > threshold && distances(i) > threshold
feature_points(end+1) = i;
end
end
feature_points(end+1) = length(lat); %加入最后一个点
%获取整条航线的二十个左右特征点
num_feature_points = numel(feature_points);
if num_feature_points <= 20 %特征点少于20个的情况
feature_indices = feature_points;
else %特征点多于20个的情况
interval = floor(num_feature_points/20);
feature_indices = [1:interval:num_feature_points];
if length(feature_indices) < 20
feature_indices(end+1) = num_feature_points;
end
end
%定义输出的文件路径
output_file_path = 'D:\特征4(9).csv';
%导出特征点的坐标和其他数据到csv文件中
writetable(data(feature_points(feature_indices),:), output_file_path);