-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGeoid.cpp
509 lines (486 loc) · 16.7 KB
/
Geoid.cpp
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/**
* \file Geoid.cpp
* \brief Implementation for GeographicLib::Geoid class
*
* Copyright (c) Charles Karney (2009-2015) <[email protected]> and licensed
* under the MIT/X11 License. For more information, see
* https://geographiclib.sourceforge.io/
**********************************************************************/
#include <GeographicLib/Geoid.hpp>
// For getenv
#include <cstdlib>
#include <GeographicLib/Utility.hpp>
#if !defined(GEOGRAPHICLIB_DATA)
# if defined(_WIN32)
# define GEOGRAPHICLIB_DATA "C:/ProgramData/GeographicLib"
# else
# define GEOGRAPHICLIB_DATA "/usr/local/share/GeographicLib"
# endif
#endif
#if !defined(GEOGRAPHICLIB_GEOID_DEFAULT_NAME)
# define GEOGRAPHICLIB_GEOID_DEFAULT_NAME "egm96-5"
#endif
#if defined(_MSC_VER)
// Squelch warnings about unsafe use of getenv
# pragma warning (disable: 4996)
#endif
namespace GeographicLib {
using namespace std;
// This is the transfer matrix for a 3rd order fit with a 12-point stencil
// with weights
//
// \x -1 0 1 2
// y
// -1 . 1 1 .
// 0 1 2 2 1
// 1 1 2 2 1
// 2 . 1 1 .
//
// A algorithm for n-dimensional polynomial fits is described in
// F. H. Lesh,
// Multi-dimensional least-squares polynomial curve fitting,
// CACM 2, 29-30 (1959).
//
// Here's the Maxima code to generate this matrix:
//
// /* The stencil and the weights */
// xarr:[
// 0, 1,
// -1, 0, 1, 2,
// -1, 0, 1, 2,
// 0, 1]$
// yarr:[
// -1,-1,
// 0, 0, 0, 0,
// 1, 1, 1, 1,
// 2, 2]$
// warr:[
// 1, 1,
// 1, 2, 2, 1,
// 1, 2, 2, 1,
// 1, 1]$
//
// /* [x exponent, y exponent] for cubic fit */
// pows:[
// [0,0],
// [1,0],[0,1],
// [2,0],[1,1],[0,2],
// [3,0],[2,1],[1,2],[0,3]]$
//
// basisvec(x,y,pows):=map(lambda([ex],(if ex[1]=0 then 1 else x^ex[1])*
// (if ex[2]=0 then 1 else y^ex[2])),pows)$
// addterm(x,y,f,w,pows):=block([a,b,bb:basisvec(x,y,pows)],
// a:w*(transpose(bb).bb),
// b:(w*f) * bb,
// [a,b])$
//
// c3row(k):=block([a,b,c,pows:pows,n],
// n:length(pows),
// a:zeromatrix(n,n),
// b:copylist(part(a,1)),
// c:[a,b],
// for i:1 thru length(xarr) do
// c:c+addterm(xarr[i],yarr[i],if i=k then 1 else 0,warr[i],pows),
// a:c[1],b:c[2],
// part(transpose( a^^-1 . transpose(b)),1))$
// c3:[]$
// for k:1 thru length(warr) do c3:endcons(c3row(k),c3)$
// c3:apply(matrix,c3)$
// c0:part(ratsimp(
// genmatrix(yc,1,length(warr)).abs(c3).genmatrix(yd,length(pows),1)),2)$
// c3:c0*c3$
const int Geoid::c0_ = 240; // Common denominator
const int Geoid::c3_[stencilsize_ * nterms_] = {
9, -18, -88, 0, 96, 90, 0, 0, -60, -20,
-9, 18, 8, 0, -96, 30, 0, 0, 60, -20,
9, -88, -18, 90, 96, 0, -20, -60, 0, 0,
186, -42, -42, -150, -96, -150, 60, 60, 60, 60,
54, 162, -78, 30, -24, -90, -60, 60, -60, 60,
-9, -32, 18, 30, 24, 0, 20, -60, 0, 0,
-9, 8, 18, 30, -96, 0, -20, 60, 0, 0,
54, -78, 162, -90, -24, 30, 60, -60, 60, -60,
-54, 78, 78, 90, 144, 90, -60, -60, -60, -60,
9, -8, -18, -30, -24, 0, 20, 60, 0, 0,
-9, 18, -32, 0, 24, 30, 0, 0, -60, 20,
9, -18, -8, 0, -24, -30, 0, 0, 60, 20,
};
// Like c3, but with the coeffs of x, x^2, and x^3 constrained to be zero.
// Use this at the N pole so that the height in independent of the longitude
// there.
//
// Here's the Maxima code to generate this matrix (continued from above).
//
// /* figure which terms to exclude so that fit is indep of x at y=0 */
// mask:part(zeromatrix(1,length(pows)),1)+1$
// for i:1 thru length(pows) do
// if pows[i][1]>0 and pows[i][2]=0 then mask[i]:0$
//
// /* Same as c3row but with masked pows. */
// c3nrow(k):=block([a,b,c,powsa:[],n,d,e],
// for i:1 thru length(mask) do if mask[i]>0 then
// powsa:endcons(pows[i],powsa),
// n:length(powsa),
// a:zeromatrix(n,n),
// b:copylist(part(a,1)),
// c:[a,b],
// for i:1 thru length(xarr) do
// c:c+addterm(xarr[i],yarr[i],if i=k then 1 else 0,warr[i],powsa),
// a:c[1],b:c[2],
// d:part(transpose( a^^-1 . transpose(b)),1),
// e:[],
// for i:1 thru length(mask) do
// if mask[i]>0 then (e:endcons(first(d),e),d:rest(d)) else e:endcons(0,e),
// e)$
// c3n:[]$
// for k:1 thru length(warr) do c3n:endcons(c3nrow(k),c3n)$
// c3n:apply(matrix,c3n)$
// c0n:part(ratsimp(
// genmatrix(yc,1,length(warr)).abs(c3n).genmatrix(yd,length(pows),1)),2)$
// c3n:c0n*c3n$
const int Geoid::c0n_ = 372; // Common denominator
const int Geoid::c3n_[stencilsize_ * nterms_] = {
0, 0, -131, 0, 138, 144, 0, 0, -102, -31,
0, 0, 7, 0, -138, 42, 0, 0, 102, -31,
62, 0, -31, 0, 0, -62, 0, 0, 0, 31,
124, 0, -62, 0, 0, -124, 0, 0, 0, 62,
124, 0, -62, 0, 0, -124, 0, 0, 0, 62,
62, 0, -31, 0, 0, -62, 0, 0, 0, 31,
0, 0, 45, 0, -183, -9, 0, 93, 18, 0,
0, 0, 216, 0, 33, 87, 0, -93, 12, -93,
0, 0, 156, 0, 153, 99, 0, -93, -12, -93,
0, 0, -45, 0, -3, 9, 0, 93, -18, 0,
0, 0, -55, 0, 48, 42, 0, 0, -84, 31,
0, 0, -7, 0, -48, -42, 0, 0, 84, 31,
};
// Like c3n, but y -> 1-y so that h is independent of x at y = 1. Use this
// at the S pole so that the height in independent of the longitude there.
//
// Here's the Maxima code to generate this matrix (continued from above).
//
// /* Transform c3n to c3s by transforming y -> 1-y */
// vv:[
// v[11],v[12],
// v[7],v[8],v[9],v[10],
// v[3],v[4],v[5],v[6],
// v[1],v[2]]$
// poly:expand(vv.(c3n/c0n).transpose(basisvec(x,1-y,pows)))$
// c3sf[i,j]:=coeff(coeff(coeff(poly,v[i]),x,pows[j][1]),y,pows[j][2])$
// c3s:genmatrix(c3sf,length(vv),length(pows))$
// c0s:part(ratsimp(
// genmatrix(yc,1,length(warr)).abs(c3s).genmatrix(yd,length(pows),1)),2)$
// c3s:c0s*c3s$
const int Geoid::c0s_ = 372; // Common denominator
const int Geoid::c3s_[stencilsize_ * nterms_] = {
18, -36, -122, 0, 120, 135, 0, 0, -84, -31,
-18, 36, -2, 0, -120, 51, 0, 0, 84, -31,
36, -165, -27, 93, 147, -9, 0, -93, 18, 0,
210, 45, -111, -93, -57, -192, 0, 93, 12, 93,
162, 141, -75, -93, -129, -180, 0, 93, -12, 93,
-36, -21, 27, 93, 39, 9, 0, -93, -18, 0,
0, 0, 62, 0, 0, 31, 0, 0, 0, -31,
0, 0, 124, 0, 0, 62, 0, 0, 0, -62,
0, 0, 124, 0, 0, 62, 0, 0, 0, -62,
0, 0, 62, 0, 0, 31, 0, 0, 0, -31,
-18, 36, -64, 0, 66, 51, 0, 0, -102, 31,
18, -36, 2, 0, -66, -51, 0, 0, 102, 31,
};
Geoid::Geoid(const std::string& name, const std::string& path, bool cubic,
bool threadsafe)
: _name(name)
, _dir(path)
, _cubic(cubic)
, _a( Constants::WGS84_a() )
, _e2( (2 - Constants::WGS84_f()) * Constants::WGS84_f() )
, _degree( Math::degree() )
, _eps( sqrt(numeric_limits<real>::epsilon()) )
, _threadsafe(false) // Set after cache is read
{
GEOGRAPHICLIB_STATIC_ASSERT(sizeof(pixel_t) == pixel_size_,
"pixel_t has the wrong size");
if (_dir.empty())
_dir = DefaultGeoidPath();
_filename = _dir + "/" + _name + (pixel_size_ != 4 ? ".pgm" : ".pgm4");
_file.open(_filename.c_str(), ios::binary);
if (!(_file.good()))
throw GeographicErr("File not readable " + _filename);
string s;
if (!(getline(_file, s) && s == "P5"))
throw GeographicErr("File not in PGM format " + _filename);
_offset = numeric_limits<real>::max();
_scale = 0;
_maxerror = _rmserror = -1;
_description = "NONE";
_datetime = "UNKNOWN";
while (getline(_file, s)) {
if (s.empty())
continue;
if (s[0] == '#') {
istringstream is(s);
string commentid, key;
if (!(is >> commentid >> key) || commentid != "#")
continue;
if (key == "Description" || key =="DateTime") {
string::size_type p =
s.find_first_not_of(" \t", unsigned(is.tellg()));
if (p != string::npos)
(key == "Description" ? _description : _datetime) = s.substr(p);
} else if (key == "Offset") {
if (!(is >> _offset))
throw GeographicErr("Error reading offset " + _filename);
} else if (key == "Scale") {
if (!(is >> _scale))
throw GeographicErr("Error reading scale " + _filename);
} else if (key == (_cubic ? "MaxCubicError" : "MaxBilinearError")) {
// It's not an error if the error can't be read
is >> _maxerror;
} else if (key == (_cubic ? "RMSCubicError" : "RMSBilinearError")) {
// It's not an error if the error can't be read
is >> _rmserror;
}
} else {
istringstream is(s);
if (!(is >> _width >> _height))
throw GeographicErr("Error reading raster size " + _filename);
break;
}
}
{
unsigned maxval;
if (!(_file >> maxval))
throw GeographicErr("Error reading maxval " + _filename);
if (maxval != pixel_max_)
throw GeographicErr("Incorrect value of maxval " + _filename);
// Add 1 for whitespace after maxval
_datastart = (unsigned long long)(_file.tellg()) + 1ULL;
_swidth = (unsigned long long)(_width);
}
if (_offset == numeric_limits<real>::max())
throw GeographicErr("Offset not set " + _filename);
if (_scale == 0)
throw GeographicErr("Scale not set " + _filename);
if (_scale < 0)
throw GeographicErr("Scale must be positive " + _filename);
if (_height < 2 || _width < 2)
// Coarsest grid spacing is 180deg.
throw GeographicErr("Raster size too small " + _filename);
if (_width & 1)
// This is so that longitude grids can be extended thru the poles.
throw GeographicErr("Raster width is odd " + _filename);
if (!(_height & 1))
// This is so that latitude grid includes the equator.
throw GeographicErr("Raster height is even " + _filename);
_file.seekg(0, ios::end);
if (!_file.good() ||
_datastart + pixel_size_ * _swidth * (unsigned long long)(_height) !=
(unsigned long long)(_file.tellg()))
// Possibly this test should be "<" because the file contains, e.g., a
// second image. However, for now we are more strict.
throw GeographicErr("File has the wrong length " + _filename);
_rlonres = _width / real(360);
_rlatres = (_height - 1) / real(180);
_cache = false;
_ix = _width;
_iy = _height;
// Ensure that file errors throw exceptions
_file.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
if (threadsafe) {
CacheAll();
_file.close();
_threadsafe = true;
}
}
Math::real Geoid::height(real lat, real lon) const {
lat = Math::LatFix(lat);
if (Math::isnan(lat) || Math::isnan(lon)) {
return Math::NaN();
}
lon = Math::AngNormalize(lon);
real
fx = lon * _rlonres,
fy = -lat * _rlatres;
int
ix = int(floor(fx)),
iy = min((_height - 1)/2 - 1, int(floor(fy)));
fx -= ix;
fy -= iy;
iy += (_height - 1)/2;
ix += ix < 0 ? _width : (ix >= _width ? -_width : 0);
real v00 = 0, v01 = 0, v10 = 0, v11 = 0;
real t[nterms_];
if (_threadsafe || !(ix == _ix && iy == _iy)) {
if (!_cubic) {
v00 = rawval(ix , iy );
v01 = rawval(ix + 1, iy );
v10 = rawval(ix , iy + 1);
v11 = rawval(ix + 1, iy + 1);
} else {
real v[stencilsize_];
int k = 0;
v[k++] = rawval(ix , iy - 1);
v[k++] = rawval(ix + 1, iy - 1);
v[k++] = rawval(ix - 1, iy );
v[k++] = rawval(ix , iy );
v[k++] = rawval(ix + 1, iy );
v[k++] = rawval(ix + 2, iy );
v[k++] = rawval(ix - 1, iy + 1);
v[k++] = rawval(ix , iy + 1);
v[k++] = rawval(ix + 1, iy + 1);
v[k++] = rawval(ix + 2, iy + 1);
v[k++] = rawval(ix , iy + 2);
v[k++] = rawval(ix + 1, iy + 2);
const int* c3x = iy == 0 ? c3n_ : (iy == _height - 2 ? c3s_ : c3_);
int c0x = iy == 0 ? c0n_ : (iy == _height - 2 ? c0s_ : c0_);
for (unsigned i = 0; i < nterms_; ++i) {
t[i] = 0;
for (unsigned j = 0; j < stencilsize_; ++j)
t[i] += v[j] * c3x[nterms_ * j + i];
t[i] /= c0x;
}
}
} else { // same cell; used cached coefficients
if (!_cubic) {
v00 = _v00;
v01 = _v01;
v10 = _v10;
v11 = _v11;
} else
copy(_t, _t + nterms_, t);
}
if (!_cubic) {
real
a = (1 - fx) * v00 + fx * v01,
b = (1 - fx) * v10 + fx * v11,
c = (1 - fy) * a + fy * b,
h = _offset + _scale * c;
if (!_threadsafe) {
_ix = ix;
_iy = iy;
_v00 = v00;
_v01 = v01;
_v10 = v10;
_v11 = v11;
}
return h;
} else {
real h = t[0] + fx * (t[1] + fx * (t[3] + fx * t[6])) +
fy * (t[2] + fx * (t[4] + fx * t[7]) +
fy * (t[5] + fx * t[8] + fy * t[9]));
h = _offset + _scale * h;
if (!_threadsafe) {
_ix = ix;
_iy = iy;
copy(t, t + nterms_, _t);
}
return h;
}
}
void Geoid::CacheClear() const {
if (!_threadsafe) {
_cache = false;
try {
_data.clear();
// Use swap to release memory back to system
vector< vector<pixel_t> >().swap(_data);
}
catch (const exception&) {
}
}
}
void Geoid::CacheArea(real south, real west, real north, real east) const {
if (_threadsafe)
throw GeographicErr("Attempt to change cache of threadsafe Geoid");
if (south > north) {
CacheClear();
return;
}
south = Math::LatFix(south);
north = Math::LatFix(north);
west = Math::AngNormalize(west); // west in [-180, 180)
east = Math::AngNormalize(east);
if (east <= west)
east += 360; // east - west in (0, 360]
int
iw = int(floor(west * _rlonres)),
ie = int(floor(east * _rlonres)),
in = int(floor(-north * _rlatres)) + (_height - 1)/2,
is = int(floor(-south * _rlatres)) + (_height - 1)/2;
in = max(0, min(_height - 2, in));
is = max(0, min(_height - 2, is));
is += 1;
ie += 1;
if (_cubic) {
in -= 1;
is += 1;
iw -= 1;
ie += 1;
}
if (ie - iw >= _width - 1) {
// Include entire longitude range
iw = 0;
ie = _width - 1;
} else {
ie += iw < 0 ? _width : (iw >= _width ? -_width : 0);
iw += iw < 0 ? _width : (iw >= _width ? -_width : 0);
}
int oysize = int(_data.size());
_xsize = ie - iw + 1;
_ysize = is - in + 1;
_xoffset = iw;
_yoffset = in;
try {
_data.resize(_ysize, vector<pixel_t>(_xsize));
for (int iy = min(oysize, _ysize); iy--;)
_data[iy].resize(_xsize);
}
catch (const bad_alloc&) {
CacheClear();
throw GeographicErr("Insufficient memory for caching " + _filename);
}
try {
for (int iy = in; iy <= is; ++iy) {
int iy1 = iy, iw1 = iw;
if (iy < 0 || iy >= _height) {
// Allow points "beyond" the poles to support interpolation
iy1 = iy1 < 0 ? -iy1 : 2 * (_height - 1) - iy1;
iw1 += _width/2;
if (iw1 >= _width)
iw1 -= _width;
}
int xs1 = min(_width - iw1, _xsize);
filepos(iw1, iy1);
Utility::readarray<pixel_t, pixel_t, true>
(_file, &(_data[iy - in][0]), xs1);
if (xs1 < _xsize) {
// Wrap around longitude = 0
filepos(0, iy1);
Utility::readarray<pixel_t, pixel_t, true>
(_file, &(_data[iy - in][xs1]), _xsize - xs1);
}
}
_cache = true;
}
catch (const exception& e) {
CacheClear();
throw GeographicErr(string("Error filling cache ") + e.what());
}
}
std::string Geoid::DefaultGeoidPath() {
string path;
char* geoidpath = getenv("GEOGRAPHICLIB_GEOID_PATH");
if (geoidpath)
path = string(geoidpath);
if (!path.empty())
return path;
char* datapath = getenv("GEOGRAPHICLIB_DATA");
if (datapath)
path = string(datapath);
return (!path.empty() ? path : string(GEOGRAPHICLIB_DATA)) + "/geoids";
}
std::string Geoid::DefaultGeoidName() {
string name;
char* geoidname = getenv("GEOGRAPHICLIB_GEOID_NAME");
if (geoidname)
name = string(geoidname);
return !name.empty() ? name : string(GEOGRAPHICLIB_GEOID_DEFAULT_NAME);
}
} // namespace GeographicLib