15#if !defined(GEOGRAPHICLIB_DATA)
17# define GEOGRAPHICLIB_DATA "C:/ProgramData/GeographicLib"
19# define GEOGRAPHICLIB_DATA "/usr/local/share/GeographicLib"
23#if !defined(GEOGRAPHICLIB_GEOID_DEFAULT_NAME)
24# define GEOGRAPHICLIB_GEOID_DEFAULT_NAME "egm96-5"
29# pragma warning (disable: 4996 5055)
101 const int Geoid::c0_ = 240;
102 const int Geoid::c3_[stencilsize_ * nterms_] = {
103 9, -18, -88, 0, 96, 90, 0, 0, -60, -20,
104 -9, 18, 8, 0, -96, 30, 0, 0, 60, -20,
105 9, -88, -18, 90, 96, 0, -20, -60, 0, 0,
106 186, -42, -42, -150, -96, -150, 60, 60, 60, 60,
107 54, 162, -78, 30, -24, -90, -60, 60, -60, 60,
108 -9, -32, 18, 30, 24, 0, 20, -60, 0, 0,
109 -9, 8, 18, 30, -96, 0, -20, 60, 0, 0,
110 54, -78, 162, -90, -24, 30, 60, -60, 60, -60,
111 -54, 78, 78, 90, 144, 90, -60, -60, -60, -60,
112 9, -8, -18, -30, -24, 0, 20, 60, 0, 0,
113 -9, 18, -32, 0, 24, 30, 0, 0, -60, 20,
114 9, -18, -8, 0, -24, -30, 0, 0, 60, 20,
151 const int Geoid::c0n_ = 372;
152 const int Geoid::c3n_[stencilsize_ * nterms_] = {
153 0, 0, -131, 0, 138, 144, 0, 0, -102, -31,
154 0, 0, 7, 0, -138, 42, 0, 0, 102, -31,
155 62, 0, -31, 0, 0, -62, 0, 0, 0, 31,
156 124, 0, -62, 0, 0, -124, 0, 0, 0, 62,
157 124, 0, -62, 0, 0, -124, 0, 0, 0, 62,
158 62, 0, -31, 0, 0, -62, 0, 0, 0, 31,
159 0, 0, 45, 0, -183, -9, 0, 93, 18, 0,
160 0, 0, 216, 0, 33, 87, 0, -93, 12, -93,
161 0, 0, 156, 0, 153, 99, 0, -93, -12, -93,
162 0, 0, -45, 0, -3, 9, 0, 93, -18, 0,
163 0, 0, -55, 0, 48, 42, 0, 0, -84, 31,
164 0, 0, -7, 0, -48, -42, 0, 0, 84, 31,
185 const int Geoid::c0s_ = 372;
186 const int Geoid::c3s_[stencilsize_ * nterms_] = {
187 18, -36, -122, 0, 120, 135, 0, 0, -84, -31,
188 -18, 36, -2, 0, -120, 51, 0, 0, 84, -31,
189 36, -165, -27, 93, 147, -9, 0, -93, 18, 0,
190 210, 45, -111, -93, -57, -192, 0, 93, 12, 93,
191 162, 141, -75, -93, -129, -180, 0, 93, -12, 93,
192 -36, -21, 27, 93, 39, 9, 0, -93, -18, 0,
193 0, 0, 62, 0, 0, 31, 0, 0, 0, -31,
194 0, 0, 124, 0, 0, 62, 0, 0, 0, -62,
195 0, 0, 124, 0, 0, 62, 0, 0, 0, -62,
196 0, 0, 62, 0, 0, 31, 0, 0, 0, -31,
197 -18, 36, -64, 0, 66, 51, 0, 0, -102, 31,
198 18, -36, 2, 0, -66, -51, 0, 0, 102, 31,
201 Geoid::Geoid(
const std::string& name,
const std::string& path,
bool cubic,
208 , _degree(
Math::degree() )
209 , _eps( sqrt(numeric_limits<real>::epsilon()) )
212 static_assert(
sizeof(pixel_t) == pixel_size_,
"pixel_t has the wrong size");
215 _filename = _dir +
"/" + _name + (pixel_size_ != 4 ?
".pgm" :
".pgm4");
216 _file.open(_filename.c_str(), ios::binary);
220 if (!(getline(_file, s) && s ==
"P5"))
222 _offset = numeric_limits<real>::max();
224 _maxerror = _rmserror = -1;
225 _description =
"NONE";
226 _datetime =
"UNKNOWN";
227 while (getline(_file, s)) {
232 string commentid, key;
233 if (!(is >> commentid >> key) || commentid !=
"#")
235 if (key ==
"Description" || key ==
"DateTime") {
236 string::size_type p =
237 s.find_first_not_of(
" \t",
unsigned(is.tellg()));
238 if (p != string::npos)
239 (key ==
"Description" ? _description : _datetime) = s.substr(p);
240 }
else if (key ==
"Offset") {
241 if (!(is >> _offset))
243 }
else if (key ==
"Scale") {
246 }
else if (key == (_cubic ?
"MaxCubicError" :
"MaxBilinearError")) {
249 }
else if (key == (_cubic ?
"RMSCubicError" :
"RMSBilinearError")) {
255 if (!(is >> _width >> _height))
256 throw GeographicErr(
"Error reading raster size " + _filename);
262 if (!(_file >> maxval))
264 if (maxval != pixel_max_)
265 throw GeographicErr(
"Incorrect value of maxval " + _filename);
267 _datastart = (
unsigned long long)(_file.tellg()) + 1ULL;
268 _swidth = (
unsigned long long)(_width);
270 if (_offset == numeric_limits<real>::max())
276 if (_height < 2 || _width < 2)
285 _file.seekg(0, ios::end);
287 _datastart + pixel_size_ * _swidth * (
unsigned long long)(_height) !=
288 (
unsigned long long)(_file.tellg()))
291 throw GeographicErr(
"File has the wrong length " + _filename);
293 _rlatres = (_height - 1) / real(
Math::hd);
298 _file.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
309 if (isnan(lat) || isnan(lon)) {
315 fy = -lat * _rlatres;
318 iy = min((_height - 1)/2 - 1,
int(floor(fy)));
321 iy += (_height - 1)/2;
322 ix += ix < 0 ? _width : (ix >= _width ? -_width : 0);
323 real v00 = 0, v01 = 0, v10 = 0, v11 = 0;
326 if (_threadsafe || !(ix == _ix && iy == _iy)) {
328 v00 = rawval(ix , iy );
329 v01 = rawval(ix + 1, iy );
330 v10 = rawval(ix , iy + 1);
331 v11 = rawval(ix + 1, iy + 1);
333 real v[stencilsize_];
335 v[k++] = rawval(ix , iy - 1);
336 v[k++] = rawval(ix + 1, iy - 1);
337 v[k++] = rawval(ix - 1, iy );
338 v[k++] = rawval(ix , iy );
339 v[k++] = rawval(ix + 1, iy );
340 v[k++] = rawval(ix + 2, iy );
341 v[k++] = rawval(ix - 1, iy + 1);
342 v[k++] = rawval(ix , iy + 1);
343 v[k++] = rawval(ix + 1, iy + 1);
344 v[k++] = rawval(ix + 2, iy + 1);
345 v[k++] = rawval(ix , iy + 2);
346 v[k++] = rawval(ix + 1, iy + 2);
348 const int* c3x = iy == 0 ? c3n_ : (iy == _height - 2 ? c3s_ : c3_);
349 int c0x = iy == 0 ? c0n_ : (iy == _height - 2 ? c0s_ : c0_);
350 for (
unsigned i = 0; i < nterms_; ++i) {
352 for (
unsigned j = 0; j < stencilsize_; ++j)
353 t[i] += v[j] * c3x[nterms_ * j + i];
364 copy(_t, _t + nterms_, t);
368 a = (1 - fx) * v00 + fx * v01,
369 b = (1 - fx) * v10 + fx * v11,
370 c = (1 - fy) * a + fy * b,
371 h = _offset + _scale * c;
382 real h = t[0] + fx * (t[1] + fx * (t[3] + fx * t[6])) +
383 fy * (t[2] + fx * (t[4] + fx * t[7]) +
384 fy * (t[5] + fx * t[8] + fy * t[9]));
385 h = _offset + _scale * h;
389 copy(t, t + nterms_, _t);
401 vector< vector<pixel_t> >().
swap(_data);
403 catch (
const exception&) {
410 throw GeographicErr(
"Attempt to change cache of threadsafe Geoid");
422 iw = int(floor(west * _rlonres)),
423 ie = int(floor(east * _rlonres)),
424 in = int(floor(-north * _rlatres)) + (_height - 1)/2,
425 is =
int(floor(-south * _rlatres)) + (_height - 1)/2;
426 in = max(0, min(_height - 2, in));
427 is = max(0, min(_height - 2, is));
436 if (ie - iw >= _width - 1) {
441 ie += iw < 0 ? _width : (iw >= _width ? -_width : 0);
442 iw += iw < 0 ? _width : (iw >= _width ? -_width : 0);
444 int oysize = int(_data.size());
445 _xsize = ie - iw + 1;
446 _ysize = is - in + 1;
451 _data.resize(_ysize, vector<pixel_t>(_xsize));
452 for (
int iy = min(oysize, _ysize); iy--;)
453 _data[iy].resize(_xsize);
455 catch (
const bad_alloc&) {
457 throw GeographicErr(
"Insufficient memory for caching " + _filename);
461 for (
int iy = in; iy <= is; ++iy) {
462 int iy1 = iy, iw1 = iw;
463 if (iy < 0 || iy >= _height) {
465 iy1 = iy1 < 0 ? -iy1 : 2 * (_height - 1) - iy1;
470 int xs1 = min(_width - iw1, _xsize);
472 Utility::readarray<pixel_t, pixel_t, true>
473 (_file, &(_data[iy - in][0]), xs1);
477 Utility::readarray<pixel_t, pixel_t, true>
478 (_file, &(_data[iy - in][xs1]), _xsize - xs1);
483 catch (
const exception& e) {
485 throw GeographicErr(
string(
"Error filling cache ") + e.what());
491 char* geoidpath = getenv(
"GEOGRAPHICLIB_GEOID_PATH");
493 path = string(geoidpath);
496 char* datapath = getenv(
"GEOGRAPHICLIB_DATA");
498 path = string(datapath);
504 char* geoidname = getenv(
"GEOGRAPHICLIB_GEOID_NAME");
506 name = string(geoidname);
GeographicLib::Math::real real
#define GEOGRAPHICLIB_DATA
#define GEOGRAPHICLIB_GEOID_DEFAULT_NAME
Header for GeographicLib::Geoid class.
Header for GeographicLib::Utility class.
Constants needed by GeographicLib
Exception handling for GeographicLib.
static std::string DefaultGeoidPath()
static std::string DefaultGeoidName()
void CacheArea(real south, real west, real north, real east) const
Mathematical functions needed by GeographicLib.
static T AngNormalize(T x)
@ hd
degrees per half turn
Namespace for GeographicLib.
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)