summaryrefslogtreecommitdiff
path: root/inst/meridianarc.m
blob: 13b8cf73492f38c0ebb7d76ad3dd90b007fd0b55 (plain)
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
89
90
91
92
93
94
95
96
97
## Copyright (C) 2019-2020 Philip Nienhuis
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSEll. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn  {Function File} {@var{s} =}  meridianarc (@var{phi}, @var{phi_2}, @var{spheroid}, @var{angleUnit}) 
## Returns the meridian arch length given two latitudes @var{phi} and @var{phi_2}.
##
## If phi_2 is larger than phi the value will be negative.
##
## If no spheroid is given the default is wgs84.
## The angleunit can be degrees or radians (the latter is default).
##
## Examples
## Full options:
## @example
## s = meridianarc (0, 56, "int24", "degrees")
## => s =
## 6.2087e+06 
## @end example
## Short version:
## @example
## s = meridianarc (0, pi/4)
## => s =
## 4.9849e+06
## @end example
## If want different units:
## @example
## s = meridianarc (0, 56, referenceEllipsoid ("int24", "km"), "degrees")
## => s =
## 6208.7
## @end example
## @seealso{referenceEllipsoid} 
## @end deftypefn

## Function supplied by anonymous contributor, see:
## https://savannah.gnu.org/patch/index.php?9720

function s = meridianarc (phi, phi_2, spheroid="wgs84", angleUnit="radians")

  persistent intv = "-pi/2, pi/2";
  persistent degintv = "-90, 90";

  if (nargin < 2)
    print_usage ();
  endif

  if (strncmpi (lower (angleUnit), "d", 1) == 1)
    phi = deg2rad (phi);
    phi_2 = deg2rad (phi_2);
    intv = degintv;
  endif
  if (abs (phi) > pi / 2 || abs (phi_2) > pi / 2)
    error ("meridianarc: latitudes must lie in interval [%s]", intv);
  endif

  if isempty (spheroid)
    spheroid = "wgs84";
  end

  ## From: Algorithms for global positioning. Kai Borre and Gilbert Strang pg 373
  ## Note: Using integral instead of Taylor Expansion 
  if (isstruct (spheroid))
    E = spheroid;
  elseif (ischar (spheroid))
    E = referenceEllipsoid (spheroid);
  else
    error ("meridianarc.m: spheroid must be a string or a stucture");
  endif

  e_sq = E.Eccentricity ^ 2;
  F = @(x) ((1 - e_sq * sin(x) ^ 2) ^ (-3 / 2));
  s = E.SemimajorAxis * (1 - e_sq) * quad ( F, phi, phi_2, 1.0e-12);

endfunction

%!test
%! s = meridianarc (0, 56, "int24", "degrees");
%! assert (s, 6208700.08662672, 10e-6)

%!error <spheroid> meridianarc ( 0, pi/4, 7)
%!error <spheroid> meridianarc ( 0, pi/4, 7i)
%!error <latitudes> meridianarc (-2, 2)
%!error <latitudes> meridianarc (-91, 91, "", "d")