distance of random points to coastline

I have 18 global sites, and I want to find the shortest distance from each site to the coastline.
Can anybody provide a simple way to do this?
load coastlines.mat
Dlon = [-43.5 -44.5 -79.4 -110.5 59.8 90.4 -33.0 -15.9 -23.6 -118.4 -126.4 -90.8 -83.7 -21.0 8.9 113.3 116.6 -171.0];
Dlat = [4.2 5.5 11.5 0.2 16.6 5.4 41.0 57.5 60.4 32.3 41.0 -3.1 1.2 18.1 -42.9 9.4 18.8 -41.8]
plot(coastlon, coastlat)
hold on
scatter(Dlon, Dlat,'k','filled')

 Accepted Answer

If you can treat longitude and lattitude as X and Y, one straight-forward solution would be as follows.
Of course, if you want to calculate more accurate solution, you have to consider geoid model and calculate shortest path on the sphere.
load coastlines.mat
Dlon = [-43.5 -44.5 -79.4 -110.5 59.8 90.4 -33.0 -15.9 -23.6 -118.4 -126.4 -90.8 -83.7 -21.0 8.9 113.3 116.6 -171.0];
Dlat = [4.2 5.5 11.5 0.2 16.6 5.4 41.0 57.5 60.4 32.3 41.0 -3.1 1.2 18.1 -42.9 9.4 18.8 -41.8];
% Calculate euclid distance between each points
coastxy = [coastlon, coastlat];
Dxy = [Dlon', Dlat'];
d = pdist2(Dxy, coastxy);
% Visualize the result
figure
plot(coastlon, coastlat)
hold on
h1 = scatter(Dlon, Dlat, 'rx');
for kk = 1:numel(Dlat)
[~, pt] = min(d(kk, :));
lon = [Dlon(kk), coastlon(pt)];
lat = [Dlat(kk), coastlat(pt)];
h2 = plot(lon, lat, 'm-');
end
legend([h1 h2], ["18 Global Site", "Shortest path to coast"])

2 Comments

LC
LC on 7 Oct 2024
Moved: John D'Errico on 7 Oct 2024
Thank you. How do I extract the raw min distance values from that for each point?
Well, in the above code, raw min distance value is meaningless because it only calculates Euclid distance beteen two points on lon-lat plane.
As mentioned in my comment, actual min distance shall be calculated considering geoid model. If you have Mapping Toolbox, you can do this task as follows:
load coastlines.mat
Dlon = [-43.5 -44.5 -79.4 -110.5 59.8 90.4 -33.0 -15.9 -23.6 -118.4 -126.4 -90.8 -83.7 -21.0 8.9 113.3 116.6 -171.0];
Dlat = [4.2 5.5 11.5 0.2 16.6 5.4 41.0 57.5 60.4 32.3 41.0 -3.1 1.2 18.1 -42.9 9.4 18.8 -41.8];
% Calculate euclid distance between each points
coastxy = [coastlon, coastlat];
Dxy = [Dlon', Dlat'];
d = pdist2(Dxy, coastxy);
% Use WGS84 reference ellipsoid
wgs84 = wgs84Ellipsoid("kilometer");
figure
plot(coastlon, coastlat)
hold on
scatter(Dlon, Dlat, 'rx')
for kk = 1:numel(Dlat)
[~, pt] = min(d(kk, :));
lon = [Dlon(kk), coastlon(pt)];
lat = [Dlat(kk), coastlat(pt)];
% Calculate distance in km
l = distance(lat(1), lon(1), lat(2), lon(2), wgs84);
plot(lon, lat, 'm-')
text(lon(1)+5, lat(1), compose("%.1f km", l))
end

Sign in to comment.

More Answers (0)

Asked:

LC
on 7 Oct 2024

Moved:

on 7 Oct 2024

Community Treasure Hunt

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

Start Hunting!