Calculate distance between a point and a contour line

조회 수: 7 (최근 30일)
Tumelo Maja
Tumelo Maja 2018년 10월 16일
편집: jonas 2018년 10월 16일
I want to calculate distance between points of the trajectory(grey line) and the 1000m contour (dash black line) as shown on the attached image. I want the distance to be in km/m
Thanks for the help in advance
  댓글 수: 3
Tumelo Maja
Tumelo Maja 2018년 10월 16일
편집: Tumelo Maja 2018년 10월 16일
I have attached the trajectory vectors of the glider paths (greyline) and the contour matrix, and lon/lat vectors for the contour values
jonas
jonas 2018년 10월 16일
Perfect, I will take a look!

댓글을 달려면 로그인하십시오.

채택된 답변

jonas
jonas 2018년 10월 16일
편집: jonas 2018년 10월 16일
Here is my code so far. The units of the output is not entirely correct, but should be quite easy to fix.
% Trajectory
lat = lat_track;
lon = lon_track;
% Contour matrix for Z = 1000
C = contour(lon_cont,lat_cont,cont_vals,'levellist',-1000);hold on
close all;
% Remove some unnecessary data and save contour coordinates
[~,idx] = find(C(1,:)==-1000);
poly = [C(1,idx(1)+1:idx(2)-1);C(2,idx(1)+1:idx(2)-1)]'
C(:,C(1,:)==-1000) = [];
Clat = C(2,:)';
Clon = C(1,:)';
% Calculate closest distance between trajectory and contour
[K,d] = dsearchn([Clon,Clat],[lon,lat]);
% Check if the pts are inside or outside of the contour
in = inpolygon(lon,lat,poly(:,1),poly(:,2));
% Change sign of those inside
d(in) = -d(in);
% Plot contour
plot(Clon,Clat,'k.');hold on
% plot trajectory
scatter3(lon,lat,d,[],d)
% plot closest points
plot(Clon(K),Clat(K),'r.')
% Calculate arclength
[arclen,az] = distance(lon,lat,Clon(K),Clat(K),'rad')
The minimum distance in each point is stored in d and the index on the corresponding index in the contour matrix is stored in K. This means that the distance d(1) is the distance between [lon(1);lat(1)] and [Clon(K);Clat(K)]. The distance is currently in a weird unit of latitudes and longitudes, but you could calculate the actual distance by the distance function.
  댓글 수: 6
Tumelo Maja
Tumelo Maja 2018년 10월 16일
편집: Tumelo Maja 2018년 10월 16일
Thank you very much. The solution works perfectly :-)
jonas
jonas 2018년 10월 16일
My pleasure. Interesting question!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Earth and Planetary Science에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by