Products & Services Solutions Academia Support User Community Company

Learn more about Mapping Toolbox   

distance - Distance between points on sphere or ellipsoid

Syntax

[dist,az] = distance(lat1,lon1,lat2,lon2)
[dist,az] = distance(lat1,lon1,lat2,lon2,ellipsoid)
[dist,az] = distance(lat1,lon1,lat2,lon2,units)
[dist,az] = distance(lat1,lon1,lat2,lon2,ellipsoid,units)
[dist,az] = distance(pt1,pt2)
[dist,az] = distance(pt1,pt2,ellipsoid)
[dist,az] = distance(pt1,pt2,units)
[dist,az] = distance(pt1,pt2,ellipsoid,units)
[dist,az] = distance(track,...)

Description

[dist,az] = distance(lat1,lon1,lat2,lon2) computes the great circle distance(s) and azimuth(s) between pairs of points on the surface of a sphere. The input latitudes and longitudes, lat1, lon1, lat2, and lon2, are in degrees and can be scalars or arrays of equal size. The distance dist is expressed in degrees of arc length and will have the same size as the input arrays. Azimuth az is clockwise from north, from the first point to the second point. When given a combination of scalar and array inputs, the scalar inputs are automatically expanded to match the size of the arrays.

[dist,az] = distance(lat1,lon1,lat2,lon2,ellipsoid) computes the geodesic distance and azimuth assuming that the points lie on the reference ellipsoid defined by the input ellipsoid. The ellipsoid vector is of the form [semimajor axis,eccentricity]. The output dist is expressed in the same distance units as the semimajor axis of the ellipsoid vector.

[dist,az] = distance(lat1,lon1,lat2,lon2,units) uses the string units to define the angle units of the input latitudes and longitudes and the outputs dist and az. The units string may equal 'degrees' (the default value) or 'radians'.

[dist,az] = distance(lat1,lon1,lat2,lon2,ellipsoid,units) uses the units string to specify the units of the latitude-longitude coordinates, but the output range has the same units as the semimajor axis of the ellipsoid vector.

[dist,az] = distance(pt1,pt2) accepts N-by-2 coordinate arrays pt1 and pt2 such that pt1 = [lat1 lon1] and pt2 = [lat2 lon2] where lat1, lon1, lat2, and lon2 are column vectors. It is equivalent to dist = distance(pt1(:,1),pt1(:,2),pt2(:,1),pt2(:,2)).

[dist,az] = distance(pt1,pt2,ellipsoid),

[dist,az] = distance(pt1,pt2,units), and

[dist,az] = distance(pt1,pt2,ellipsoid,units) are all valid calling forms.

[dist,az] = distance(track,...) specifies whether great circle distances or rhumb line distances are desired. Great circle distances, the default, are indicated with the standard track string 'gc'. Rhumb line distances are indicated with the standard track string 'rh'.

Examples

Using pt1,pt2 notation, find the distance from Norfolk, Virginia (37ºN, 76ºW), to Cape St. Vincent, Portugal (37ºN, 9ºW), just outside the Straits of Gibraltar. The distance between these two points depends upon the track string selected.

dist = distance('gc',[37,-76],[37,-9])

dist =
   52.3094

dist = distance('rh',[37,-76],[37,-9])

dist =
    53.5086

The difference between these two tracks is 1.1992 degrees, or about 72 nautical miles. This represents about 2% of the total trip distance. The trade-off is that at the cost of those 72 miles, the entire trip can be made on a rhumb line with a fixed course of 90º, due east, while in order to follow the shorter great circle path, the course must be changed continuously.

On a meridian and on the Equator, great circles and rhumb lines coincide, so the distances are the same. For example,

% great circle distance
dist = distance(37,-76,67,-76) 
dist =
     30.0000

% rhumb line distance
dist = distance('rh',37,-76,67,-76) 

dist =
     30.0000

The distances are the same, 30º, or about 1800 nautical miles (there are about 60 nautical miles in a degree of arc length).

Algorithm

Distance calculations for geodesics degrade slowly with increasing distance and may break down for points that are nearly antipodal, as well as when both points are very close to the Equator. In addition, for calculations on an ellipsoid, there is a small but finite input space, consisting of pairs of locations in which both the points are nearly antipodal and both points fall close to (but not precisely on) the Equator. In this case, a warning is issued and both dist and az are set to NaN for the "problem pairs."

Alternatives

Distance between two points can be calculated in two ways. For great circles (on the sphere) and geodesics (on the ellipsoid), the distance is the shortest surface distance between two points. For rhumb lines, the distance is measured along the rhumb line passing through the two points, which is not, in general, the shortest surface distance between them.

When you need to compute both distance and azimuth for the same point pair(s), it is more efficient to do so with a single call to distance. That is, use

[dist az] = distance(...);

rather than the slower

dist = distance(...)
az = azimuth(...)

To express the output dist as an arc length in either degrees or radians, omit the ellipsoid argument. This is possible only on a sphere. If ellipsoid is supplied, dist is a distance expressed in the same units as the semimajor axis of the ellipsoid. Specify ellipsoid as [R 0] to compute dist as a distance on a sphere of radius R, with dist having the same units as R.

See Also

almanac | azimuth | elevation | reckon | track | track1 | track2 | trackg

How To

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS