Calculate distance between a point and a contour line

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 comentarios

jonas
jonas el 16 de Oct. de 2018
Could you upload the contour matrix and the trajectory?
Tumelo Maja
Tumelo Maja el 16 de Oct. de 2018
Editada: Tumelo Maja el 16 de Oct. de 2018
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 el 16 de Oct. de 2018
Perfect, I will take a look!

Iniciar sesión para comentar.

 Respuesta aceptada

jonas
jonas el 16 de Oct. de 2018
Editada: jonas el 16 de Oct. de 2018
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 comentarios

I think the levels are negative. try using -1000. I plot the contours with -1000.
e.g. m_contour(lon_cont,lat_cont,cont_vals,-1000,'color','k');
jonas
jonas el 16 de Oct. de 2018
Editada: jonas el 16 de Oct. de 2018
Aha! That is better, check the new code.
[arclen,az] = distance(lon,lat,Clon(K),Clat(K))
should give you the arclength and from there you can calculate the distance in units of [m].
Tumelo Maja
Tumelo Maja el 16 de Oct. de 2018
Editada: Tumelo Maja el 16 de Oct. de 2018
So K is the index for the contour matrix?
what are d, arclen and az? and their units
I want to have positive and negative displacements. i.e. right(left) side of the contour have positive(negative) values
jonas
jonas el 16 de Oct. de 2018
Editada: jonas el 16 de Oct. de 2018
K( n ) is the index of the closest point on the contour matrix to the trajectory point n. The d( n ) is the corresponding distance but in useless units, so you cannot use it. What you need to do is take the pairs of coordinates and calculate the arclength between the pts, as described in my previous comment. The arclength is the distance between two points on a sphere, and its unit is radians or degrees. You need to multiply by the radius of the earth to get the distance in units of [m]. Perhaps it would be easier to just use this FEX function
Negative distances? You could make a polygon of the contour and find points the points inside of it using inpolygon.
I've updated the code so that you have a subset of indices ( in ) that lies inside of the contour.
Tumelo Maja
Tumelo Maja el 16 de Oct. de 2018
Editada: Tumelo Maja el 16 de Oct. de 2018
Thank you very much. The solution works perfectly :-)
jonas
jonas el 16 de Oct. de 2018
My pleasure. Interesting question!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Contour Plots en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Oct. de 2018

Editada:

el 16 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by