summaryrefslogtreecommitdiff
path: root/inst/referenceEllipsoid.m
blob: c7b819e5b7d17f8d8d5762337b2f758b426cf956 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
## 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} {} referenceEllipsoid (@var{name}, @var{unit})
## Returns the parameters of an ellipsoid.
##
## @var{Name} can be the name (e.g., "wgs84") or (integer) EPSG code of a
## reference ellipoid.  Case is not important.  If the number or code 0
## (zero) is specified, referenceEllipsoid echoes a list of implemented
## ellipsoids to the screen.
##
## @var{unit} can be the name of any unit accepted by function 
## validateLengthUnit.m.  Also here case is not important.
##
## The output consists of a scalar struct with fields "Code" (EPSG code of the
## ellipsoid), "Name" (name of the ellipsoid), "LengthUnit", "SemimajorAxis",
## "SemiminorAxis", "InverseFlattening", "Eccentricity", "Flattening", 
## "ThirdFlattening", "MeanRadius", "SurfaceArea", and "Volume".
##
## Examples:
##
## @example
## >> E = referenceEllipsoid ("wgs84")
## E =
##
##  scalar structure containing the fields:
##
##    Code =  7030
##    Name = World Geodetic System 1984
##    LengthUnit = meter
##    SemimajorAxis =  6378137
##    SemiminorAxis =    6.3568e+06
##    InverseFlattening =  298.26
##    Eccentricity =  0.081819
##    Flattening =  0.0033528
##    ThirdFlattening =  0.0016792
##    MeanRadius =    6.3710e+06
##    SurfaceArea =    5.1007e+14
##    Volume =    1.0832e+21
## @end example
##
## The code number can be used:
##
## @example
## >> E = referenceEllipsoid (7019)
## E = 
##
##  scalar structure containing the fields:
##
##    Code =       7019
##    Name = Geodetic Reference System 1980
##    LengthUnit = meter
##    SemimajorAxis = 6.3781e+06
##    SemiminorAxis = 6.3568e+06
##    InverseFlattening =     298.26
##    Eccentricity =   0.081819
##    Flattening =  0.0033528
##    ThirdFlattening =  0.0016792
##    MeanRadius =  6.371e+06
##    SurfaceArea = 5.1007e+14
##    Volume = 1.0832e+21
## @end example
##
## @example
## >> E = referenceEllipsoid ("wgs84", "km")
## E =
##
##  scalar structure containing the fields:
##
##    Code =  7030
##    Name = World Geodetic System 1984
##    LengthUnit = km
##    SemimajorAxis =  6378.1
##    SemiminorAxis =  6356.8
##    InverseFlattening =  298.26
##    Eccentricity =  0.081819
##    Flattening =  0.0033528
##    ThirdFlattening =  0.0016792
##    MeanRadius =  6371.0
##    SurfaceArea =    5.1007e+08
##    Volume =    1.0832e+12
## @end example
##
## @seealso{validateLengthUnit, wgs84Ellipsoid}
## @end deftypefn

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

function Ell = referenceEllipsoid (name="wgs84", unit="meter")

  ## List of implemented codes. To be updated if codes are added.
  persistent codes;
  if (isempty (codes))
    codes = {"7001", "Airy30",       " "; ...
             "7002", "Airy49",       " "; ...
             "7003", "Australia",    " "; ...
             "7004", "Bessel",       " "; ...
             "7008", "Clarke66",     " "; ...
             "7012", "Bessel 1880",  " "; ...
             "7015", "Everest 1830", " "; ...
             "7019", "grs80",        "1980"; ...
             "7022", "int24",        "1924"; ...
             "7024", "Kras40",       "Krasovsky"; ...
             "7030", "Wgs84",        "1984"; ...
             "7043", "Wgs72",        "1972"; ...
             " ",    "Sun",          " "; ...
             " ",    "Mercury",      " "; ...
             " ",    "Venus",        " "; ...
             " ",    "Earth",        " "; ...
             " ",    "Moon",         " "; ...
             " ",    "Mars",         " "; ...
             " ",    "Jupiter",      " "; ...
             " ",    "Saturn",       " "; ...
             " ",    "Uranus",       " "; ...
             " ",    "Neptune",      " "; ...
             " ",    "Pluto",        " "};
  endif

  if (isnumeric (name) && isreal (name))
    name = num2str (fix (name));
  elseif (! ischar (name))
    error ("referenceEllipsoid: value must be a string or real number");
  elseif (strcmp (name, " "))
    error ("referenceEllipsoid: name required");
  endif

  if (! ischar (unit))
    error ("referenceEllipsoid: length name expected for input arg. #2");
  endif

  switch lower (name)
    ## Semimajor axis and Inverse flattening from
    ## USER's HANDBOOK ON DATUM TRANSFORMATIONS INVOLVING WGS 84
    ## 3rd Edition, July 2003, (Last correction August 2008),
    ## Special Publication No. 60
    ## Codenames are from https://epsg.io/
    ## Planet values from Report of the IAU Working Group on
    ## CartographicCoordinates and Rotational Elements: 2015

    case lower (codes(1, :))
      Code = 7001;
      Name = "Airy 1830";
      SemimajorAxis = 6377563.396;
      InverseFlattening = 299.3249646;
        
    case lower (codes (2, :))
      Code = 7002;
      Name = "Airy Modified 1849";
      SemimajorAxis = 6377340.189;
      InverseFlattening = 299.3249646;

    case lower (codes (3, :))
      Code = 7003;
      Name = "Australian National";
      SemimajorAxis = 6378160;
      InverseFlattening = 298.25;

    case lower (codes (4, :))
      Code = 7004;
      Name = "Bessel 1841";
      SemimajorAxis = 6377397.155;
      InverseFlattening = 299.1528128;

    case lower (codes (5, :))
      Code = 7008;
      Name = "Clarke 1866";
      SemimajorAxis = 6378206.4;
      InverseFlattening = 294.9786982;

    case lower (codes (6, :))
      Code = 7012;
      Name = "Bessel 1880";
      SemimajorAxis = 6378249.145;
      InverseFlattening = 293.465;

    case lower (codes (7, :))
      Code = 7015;
      Name = "Everest 1830";
      SemimajorAxis = 6377276.34518;
      InverseFlattening = 300.8017;

    case lower (codes (8, :))
      Code = 7019;
      Name = "Geodetic Reference System 1980";
      SemimajorAxis = 6378137;
      InverseFlattening = 298.257222101;

    case lower (codes (9, :))
      Code = 7022;
      Name = "International 1924";
      SemimajorAxis = 6378388;
      InverseFlattening = 297;

    case lower (codes (10, :))
      Code = 7024;
      Name = "Krasovsky 1940";
      SemimajorAxis = 6378245;
      InverseFlattening = 298.3;

    case lower (codes (11, :))
      Code = 7030;
      Name = "World Geodetic System 1984";
      SemimajorAxis = 6378137;
      InverseFlattening = 298.2572235630;

    case lower (codes (12, :))
      Code = 7043;
      Name = "World Geodetic System 1972";
      SemimajorAxis = 6378135;
      InverseFlattening = 298.26;

    case lower (codes (13, :))
      Code = " ";
      Name = "Sun";
      SemimajorAxis = 695700000;
      InverseFlattening = 111111;

    case lower (codes (14, :))
      Code = " ";
      Name = "Mercury";
      SemimajorAxis = 2440530;
      InverseFlattening = 1075;

    case lower (codes (15, :))
      Code = " ";
      Name = "Venus";
      SemimajorAxis = 6051800;
      InverseFlattening = Inf;

    case lower (codes (16, :))
      Code = " ";
      Name = "Earth";
      SemimajorAxis = 6378137;
      InverseFlattening = 298.2572235630;

    case lower (codes (17, :))
      Code = " ";
      Name = "Moon";
      SemimajorAxis = 1738100;
      InverseFlattening = 833.33;

    case lower (codes (18, :))
      Code = " ";
      Name = "Mars";
      SemimajorAxis = 3396190;
      InverseFlattening =  169.894;

    case lower (codes (19, :))
      Code = " ";
      Name = "Jupiter";
      SemimajorAxis = 71492000;
      InverseFlattening =  15.4144;

    case lower (codes (20, :))
      Code = " ";
      Name = "Saturn";
      SemimajorAxis = 60268000;
      InverseFlattening = 10.208;

    case lower (codes (21, :))
      Code = " ";
      Name = "Uranus";
      SemimajorAxis = 25559000;
      InverseFlattening = 43.616;

    case lower (codes (22, :))
      Code = " ";
      Name = "Neptune";
      SemimajorAxis = 24764000;
      InverseFlattening = 58.5437;

    case lower (codes (23, :))
      Code = " ";
      Name = "Pluto";
      SemimajorAxis = 1188300;
      InverseFlattening = INF;

    case "0"
      ## Show list of codes
      printf ("\n referenceEllipsoid.m:\n list of implemented ellipsoids:\n");
      printf (" Code          Alias 1          Alias 2\n");
      printf (" ====          =======          =======\n");
      for ii=1:size (codes, 1)
        printf ("%5s  %15s  %15s\n", codes (ii, :){:});
      endfor
      return

    otherwise
      error ("referenceEllipsoid: ellipsoid %s has not been implemented", name)

  endswitch

  ## Calculations
  Ell = param_calc (Code, Name, SemimajorAxis, InverseFlattening, unit);

endfunction


function ell = param_calc (Code, Name, SemimajorAxis, InvF, unit)

  ell.Code              = Code;
  ell.Name              = Name;
  ratio   = unitsratio (unit, "Meters");
  ell.LengthUnit        = unit;
  Major   = SemimajorAxis * ratio;
  ell.SemimajorAxis     = Major;
  Inverse = InvF;
  ell.InverseFlattening = InvF;
  Ecc     = flat2ecc (1 / InvF);
  ell.Eccentricity      = Ecc;
  Minor   = minaxis (Major, Ecc);
  ell.SemiminorAxis     = Minor;
  ell.Flattening        = 1 / InvF;
  ell.ThirdFlattening   = (Major - Minor) / (Major + Minor);
  ell.MeanRadius        = (2 * Major + Minor) / 3;
  ## From Knud Thomsen this results in a max error of 1.061 %:
  P       = 1.6075;
  Surface = 4 * pi * ((Major^(2*P) +  2 * (Major * Minor)^P) / 3 )^(1/P);
  ell.SurfaceArea       = Surface;
  ell.Volume            = (4 * pi) / 3 * Major^2 * Minor;

endfunction


%!test
%!
%! E = referenceEllipsoid ("wgs84");
%! assert ( E.SemiminorAxis, 6356752.314245, 10e-7 )
%! assert ( E.Eccentricity, 0.081819221456, 10e-8)
%! assert ( E.Flattening, 1 / 298.2572235630, 10e-8 )

%!error <value must be a string> referenceEllipsoid ( 7i )
%!error <not been implemented> referenceEllipsoid ( "yy" )
%!error <name required> referenceEllipsoid ( " " )