summaryrefslogtreecommitdiff
path: root/inst/rcurve.m
blob: 31ca78861982a4e2d948788f489162ea1cf20fa4 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
## Copyright (C) 2018-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{r} =} rcurve (@var{spheroid}, @var{lat})
## @deftypefnx {Function File} {@var{r} =} rcurve (@var{type}, @var{spheroid}, @var{lat})
## @deftypefnx {Function File} {@var{r} =} rcurve (@dots{}, @var{angleUnit})
## Return the length of a curve based on its type: meridian, parallel, or
## transverse.
##
## Optional input argument @var{type} is one of "meridian", "parallel", or
## "transverse; default (when left empty or skipped) is "parallel".
## @var{spheroid} is the spheroid of choice (default: "wgs84").  @var{lat}
## is the latitude at which the curve length should be computed and can be
## a numeric scalar, vector or matrix.  Output argument @var{r} will have the
## same size and dimension(s) as @var{lat}.
##
## Optional input argument @var{angleUnit} can be either "radians" or "degrees"
## (= default); just "r" or "d" will do.  All character input is
## case-insensitive.
##
## Examples:
##
## @example
## r = rcurve ("parallel", "wgs84", 45)
## => r =
## 4.5176e+06
## Note: this is in meters
## @end example
##
## @example
## r = rcurve ("", 45)
## => r =
## 4.5176e+06
## @end example
##
## @example
## r = rcurve ("", "", 45)
## => r =
## 4.5176e+06
## @end example
##
## @example
## r = rcurve ("", "", pi/4, "radians")
## => r =
## 4.5176e+06
## @end example
##
## @example
## r = rcurve ("meridian", "wgs84", 45)
## => r =
## 6.3674e+06
## @end example
##
## @example
## r = rcurve ("transverse", "wgs84", 45)
## => r =
## 6.3888e+06
## @end example
##
## Also can use structures as inputs:
## @example
## r = rcurve("", referenceEllipsoid ("venus"), 45)
## => r =
## 4.2793e+06
## @end example
## @end deftypefn

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

function r = rcurve (varargin)

  if (nargin < 2 || nargin > 4)
    print_usage ();
  elseif (nargin == 2)
    ## Neither type nor angleUnit specified
    type = "parallel";
    angleUnit = "degrees";
    spheroid = varargin{1};
    lat = varargin{2};
    ip = 1;
  elseif (nargin >= 3)
    if (isnumeric (varargin{2}) && isreal (varargin{2}))
      ## arg{1} = spheroid, type skipped
      type = "parallel";
      ip = 1;
    elseif (isnumeric (varargin{3}) && isreal (varargin{3}))
      ## arg{1} = type, no angleunit given
      angleUnit = "degrees";
      ip = 0;
    else
      error ("rcurve: real numeric input expected for Lat");
    endif
    type = varargin{ip + 1};
    spheroid = varargin{ip + 2};
    lat = varargin{ip + 3};
  endif
  if (nargin == 4)
    if (ischar (varargin{4}))
      angleUnit = varargin{4};
    else
      error ("rcurve: 'degrees' or 'radians' expected for angleUnits");
    endif
  endif

  if isempty (type)
    type = "parallel";
  endif

  if isempty (spheroid)
    E = wgs84Ellipsoid;
  elseif (isstruct (spheroid))
    E = spheroid;
  else
    E = referenceEllipsoid (spheroid);
  endif

  if (! ischar (angleUnit) || ! ismember (lower (angleUnit(1)), {"d", "r"}))
    error ("rcurve: angleUnit should be one of 'degrees' or 'radians'")
  endif

  if (strncmpi (lower (angleUnit), "r", 1) == 1)
    c_l = cos (lat);
    s_l = sin (lat);
  else
    c_l = cosd (lat);
    s_l = sind (lat);
  endif

  ## Insight From: Algorithms for Global Positioning pg 370-372

  e2 = E.Eccentricity ^ 2;
  R = E.SemimajorAxis;
  e_p = e2 / (1 - e2);

  N = (R * sqrt ( 1 + e_p) ./ (sqrt (1 + e_p * c_l .^ 2)));
  switch type
    case {"meridian"}
      w = sqrt (1 - e2 .* s_l .^ 2);
      r = R * (1 - e2 ) ./ (w .^ 3);
    case {"parallel"}
      r = N .* c_l;
    case {"transverse"}
      r = N;
    otherwise
      error ("rcurve: type should be one of 'meridian', 'parallel', or 'transverse'")
  endswitch

endfunction


%!test
%! assert (rcurve ("", 45), 4517590.87885, 10e-6)

%% Row vector
%!test
%! assert (rcurve ("", [45; 20]), [4517590.87885; 5995836.38390], 10e-6)

%% Column vector
%!test
%! assert (rcurve ("", [45, 20]), [4517590.87885, 5995836.38390], 10e-6)

%% Matrix
%!test
%! assert (rcurve ("", [60 45; 35 20]), [3197104.58692, 4517590.87885; 5230426.84020, 5995836.38390], 10e-6)

%!test
%! assert (rcurve ("", "", 45), 4517590.87885, 10e-6)

%!test
%! assert (rcurve ("transverse", "", 45), 6388838.29012, 10e-6)

%!test
%! assert (rcurve ("meridian", "", 45), 6367381.81562, 10e-6)

%!error <angleUnit> rcurve ("","", 45, "km")
%!error <numeric input expected>  rcurve ("", "", "A")
%!error <numeric input expected>  rcurve ("", "", 45i)
%!error <type> rcurve ('All', "", 45)