MATLAB function to calculate distance between two coordinates (latitude and longitude)

半世苍凉 提交于 2019-12-23 02:25:29

问题


How can I calculate distance between two world map coordinates (latitude and longitude) using MATLAB R2015a (in meters)?


回答1:


If you don't have access to the MATLAB Mapping toolbox then a simple approximation is to use the Haversine formula. Here is an excerpt from the link:

The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical triangles.

Here is a MATLAB implementation:

function rad = radians(degree) 
% degrees to radians
    rad = degree .* pi / 180;
end; 

function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2)
% HAVERSINE_FORMULA.AWK - converted from AWK 
    dlat = radians(lat2-lat1);
    dlon = radians(lon2-lon1);
    lat1 = radians(lat1);
    lat2 = radians(lat2);
    a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2;
    c = 2 .* asin(sqrt(a));
    arrayfun(@(x) printf("distance: %.4f km\n",6372.8 * x), c);
end;

[a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40); % BNA to LAX



回答2:


If you have access to Mapping toolbox then may be function described on this page could help you.

In that case, you can use the function distance(LAT1,LON1,LAT2,LON2) to get the length (in degrees) of the great circle arc connecting both points. Then you can convert this to either km or miles using the deg2km, deg2nm or deg2sm.

A one liner to get the distance in km would be:

deg2km(distance(lat1, lon1, lat2, lon2))


来源:https://stackoverflow.com/questions/31975639/matlab-function-to-calculate-distance-between-two-coordinates-latitude-and-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!