-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpos2dist.m
More file actions
executable file
·88 lines (86 loc) · 3.29 KB
/
pos2dist.m
File metadata and controls
executable file
·88 lines (86 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function dist = pos2dist(lag1,lon1,lag2,lon2,method)
% function dist = pos2dist(lag1,lon1,lag2,lon2,method)
% calculate distance between two points on earth's surface
% given by their latitude-longitude pair.
% Input lag1,lon1,lag2,lon2 are in degrees, without 'NSWE' indicators.
% Input method is 1 or 2. Default is 1.
% Method 1 uses plane approximation,
% only for points within several tens of kilometers (angles in rads):
% d =
% sqrt(R_equator^2*(lag1-lag2)^2 + R_polar^2*(lon1-lon2)^2*cos((lag1+lag2)/2)^2)
% Method 2 calculates sphereic geodesic distance for points farther apart,
% but ignores flattening of the earth:
% d =
% R_aver * acos(cos(lag1)cos(lag2)cos(lon1-lon2)+sin(lag1)sin(lag2))
% Output dist is in km.
% Returns -99999 if input argument(s) is/are incorrect.
%
% Flora Sun, University of Toronto, Jun 12, 2004.
%
% Downloaded from Matlab File Exchange,
% https://www.mathworks.com/matlabcentral/fileexchange/5256-pos2dist
%
% Copyright 2004, Flora Sun
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in the
% documentation and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
% PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
% CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
% OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%
if nargin < 4
dist = -99999;
disp('Number of input arguments error! distance = -99999');
return;
end
if abs(lag1)>90 || abs(lag2)>90 || abs(lon1)>360 || abs(lon2)>360
dist = -99999;
disp('Degree(s) illegal! distance = -99999');
return;
end
if lon1 < 0
lon1 = lon1 + 360;
end
if lon2 < 0
lon2 = lon2 + 360;
end
if nargin == 4
method = 1; % Default method is 1.
end
if method == 1
km_per_deg_la = 111.3237;
km_per_deg_lo = 111.1350;
km_la = km_per_deg_la * (lag1-lag2);
% Always calculate the shorter arc.
if abs(lon1-lon2) > 180
dif_lo = abs(lon1-lon2)-180;
else
dif_lo = abs(lon1-lon2);
end
km_lo = km_per_deg_lo * dif_lo * cos((lag1+lag2)*pi/360);
dist = sqrt(km_la^2 + km_lo^2);
else
R_aver = 6374;
deg2rad = pi/180;
lag1 = lag1 * deg2rad;
lon1 = lon1 * deg2rad;
lag2 = lag2 * deg2rad;
lon2 = lon2 * deg2rad;
dist = R_aver * acos(cos(lag1)*cos(lag2)*cos(lon1-lon2) + sin(lag1)*sin(lag2));
end