Home > atmlab > geodetic > sphdist.m

sphdist

PURPOSE ^

SPHDIST The distance between two geograpgical positions

SYNOPSIS ^

function d = sphdist(lat1,lon1,lat2,lon2,r)

DESCRIPTION ^

 SPHDIST   The distance between two geograpgical positions

    "As-the-crow-flies" distance between two points, specified by their
    latitude and longitude. 

    If the optional argument *r* is given, the distance in m is returned.
    Otherwise the angular distance in degrees is returned.

 FORMAT   d = sphdist2(lat1,lon1,lat2,lon2[,r])
        
 OUT   d      Distance, either in degress or m.
 IN    lat1   Latitude of position 1.
       lon1   Longitude of position 1.
 IN    lat2   Latitude of position 2.
       lon2   Longitude of position 2.
 OPT   r      The radius (common for both points).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

sphdist.m

SOURCE CODE ^

0001 % SPHDIST   The distance between two geograpgical positions
0002 %
0003 %    "As-the-crow-flies" distance between two points, specified by their
0004 %    latitude and longitude.
0005 %
0006 %    If the optional argument *r* is given, the distance in m is returned.
0007 %    Otherwise the angular distance in degrees is returned.
0008 %
0009 % FORMAT   d = sphdist2(lat1,lon1,lat2,lon2[,r])
0010 %
0011 % OUT   d      Distance, either in degress or m.
0012 % IN    lat1   Latitude of position 1.
0013 %       lon1   Longitude of position 1.
0014 % IN    lat2   Latitude of position 2.
0015 %       lon2   Longitude of position 2.
0016 % OPT   r      The radius (common for both points).
0017 
0018 % 2012-04-05   Created by Patrick Eriksson.
0019 
0020 function d = sphdist(lat1,lon1,lat2,lon2,r)
0021 
0022 % Equations taken from http://www.movable-type.co.uk/scripts/latlong.html
0023   
0024 a = sind( (lat2-lat1)/2 ).^2 + cosd(lat1).*cosd(lat2).*sind( (lon2-lon1)/2 ).^2;
0025 c = 2 * atan2( sqrt(a), sqrt(1-a) );
0026 
0027 if nargin == 5
0028   d = r .* c;
0029 else
0030   d = constants('RAD2DEG') * c;
0031 end
0032

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005