var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // dist/index.js var dist_exports = {}; __export(dist_exports, { Geoid: () => Geoid, parsePGM: () => parsePGM }); module.exports = __toCommonJS(dist_exports); // dist/geoid.js var c0_ = 240; var c3_ = [ 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 ]; var c0n_ = 372; var c3n_ = [ 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 ]; var c0s_ = 372; var c3s_ = [ 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 ]; var stencilsize_ = 12; var nterms_ = (3 + 1) * (3 + 2) / 2; var PIXEL_SIZE = 2; var Geoid = class { /** * @constructs * Create a Geoid instance. * @param options - object which includes parameters parsed from *.pgm header * @param options.data - binary buffer of *.pgm file */ constructor(options) { this._v00 = 0; this._v01 = 0; this._v10 = 0; this._v11 = 0; this._t = []; this.options = options; this._ix = this.options._width; this._iy = this.options._height; } /** * Calculates difference between mean see level height and WGS84 ellipsoid height * Code is ported from /GeographicLib-1.50.1/src/Geoid.cpp * @param lat - latitude * @param lon - longitude * @returns height in meters */ // eslint-disable-next-line max-statements, complexity getHeight(lat, lon) { lat = Math.abs(lat) > 90 ? NaN : lat; if (isNaN(lat) || isNaN(lon)) { return NaN; } const rem = remainder(lon, 360); lon = rem !== -180 ? rem : 180; let fx = lon * this.options._rlonres; let fy = -lat * this.options._rlatres; let ix = Math.floor(fx); let iy = Math.min(Math.round((this.options._height - 1) / 2 - 1), Math.floor(fy)); fx -= ix; fy -= iy; iy += (this.options._height - 1) / 2; ix += ix < 0 ? this.options._width : ix >= this.options._width ? -this.options._width : 0; let v00 = 0; let v01 = 0; let v10 = 0; let v11 = 0; let t = new Array(nterms_); if (!(ix === this._ix && iy === this._iy)) { if (!this.options.cubic) { v00 = this._rawval(ix, iy); v01 = this._rawval(ix + 1, iy); v10 = this._rawval(ix, iy + 1); v11 = this._rawval(ix + 1, iy + 1); } else { const v = [ this._rawval(ix, iy - 1), this._rawval(ix + 1, iy - 1), this._rawval(ix - 1, iy), this._rawval(ix, iy), this._rawval(ix + 1, iy), this._rawval(ix + 2, iy), this._rawval(ix - 1, iy + 1), this._rawval(ix, iy + 1), this._rawval(ix + 1, iy + 1), this._rawval(ix + 2, iy + 1), this._rawval(ix, iy + 2), this._rawval(ix + 1, iy + 2) ]; let c3x = c3n_; if (iy !== 0) { c3x = iy === this.options._height - 2 ? c3s_ : c3_; } let c0x = c0n_; if (iy !== 0) { c0x = iy === this.options._height - 2 ? c0s_ : c0_; } for (let i = 0; i < nterms_; ++i) { t[i] = 0; for (let j = 0; j < stencilsize_; ++j) { t[i] += v[j] * c3x[nterms_ * j + i]; } t[i] /= c0x; } } } else if (!this.options.cubic) { v00 = this._v00; v01 = this._v01; v10 = this._v10; v11 = this._v11; } else { t = this._t; } if (!this.options.cubic) { const a = (1 - fx) * v00 + fx * v01; const b = (1 - fx) * v10 + fx * v11; const c = (1 - fy) * a + fy * b; const h2 = this.options._offset + this.options._scale * c; this._ix = ix; this._iy = iy; this._v00 = v00; this._v01 = v01; this._v10 = v10; this._v11 = v11; return h2; } let 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 = this.options._offset + this.options._scale * h; this._ix = ix; this._iy = iy; this._t = t; return h; } // PRIVATE /** * Method seeks for particular data in th pgm buffer * Code is ported from corresponding method * in /GeographicLib-1.50.1/include/GeographicLib/Geoid.hpp * @param {number} ix - longituge parameter * @param {number} iy - latitude parameter * @returns {number} data from pgm binary buffer */ _rawval(ix, iy) { if (ix < 0) { ix += this.options._width; } else if (ix >= this.options._width) { ix -= this.options._width; } if (iy < 0 || iy >= this.options._height) { iy = iy < 0 ? -iy : 2 * (this.options._height - 1) - iy; ix += (ix < this.options._width / 2 ? 1 : -1) * this.options._width / 2; } const bufferPosition = this.options._datastart + PIXEL_SIZE * (iy * this.options._swidth + ix); const a = this.options.data[bufferPosition]; const b = this.options.data[bufferPosition + 1]; const r = a << 8 | b; return r; } }; function remainder(x, y) { y = Math.abs(y); let z = fmod(x, y); if (2 * Math.abs(z) === y) z -= fmod(x, 2 * y) - z; else if (2 * Math.abs(z) > y) z += z < 0 ? y : -y; return z; } function fmod(x, y) { return x - Math.floor(x / y) * y; } // dist/parse-pgm.js var ENDL = 10; var PIXEL_MAX = 65535; function parsePGM(data, options) { const getline = _getLineGenerator(data); let currentLine = getline.next(); if (currentLine.done || currentLine.value.line !== "P5") { throw new Error("Geoid model file: File not in PGM format"); } let _offset = Number.MAX_VALUE; let _scale = 0; let _maxerror = -1; let _rmserror = -1; let _description = "NONE"; let _datetime = "UNKNOWN"; let _width = 0; let _height = 0; let _datastart = null; let _swidth = null; do { currentLine = getline.next(); const s = currentLine.value.line; if (!s.length) { continue; } if (s[0] === "#") { const sArr = s.split(" "); const commentId = sArr[0]; const key = sArr[1]; if (commentId !== "#" || !key) { continue; } const infoArr = sArr.length > 2 ? sArr.slice(2) : []; if (key === "Description") { _description = infoArr.join(" "); } else if (key === "DateTime") { _datetime = infoArr.join(" "); } else if (key === "Offset") { if (!sArr[2]) { throw new Error("Geoid model file: Error reading offset"); } _offset = parseInt(sArr[2], 10); } else if (key === "Scale") { if (!sArr[2]) { throw new Error("Geoid model file: Error reading scale"); } _scale = parseFloat(sArr[2]); } else if (key === (options.cubic ? "MaxCubicError" : "MaxBilinearError")) { if (isFinite(parseFloat(sArr[2]))) { _maxerror = parseFloat(sArr[2]); } } else if (key === (options.cubic ? "RMSCubicError" : "RMSBilinearError")) { if (isFinite(parseFloat(sArr[2]))) { _rmserror = parseFloat(sArr[2]); } } } else { let sArr = s.split(" "); sArr = sArr.filter((testString) => testString !== ""); _width = parseInt(sArr[0], 10); _height = parseInt(sArr[1], 10); if (!(_width && _height)) { throw new Error("Geoid model file: Error reading raster size"); } break; } } while (!currentLine.done); currentLine = getline.next(); const maxval = parseInt(currentLine.value.line, 10); if (currentLine.done) { throw new Error("Geoid model file: Error reading maxval"); } if (maxval !== PIXEL_MAX) { throw new Error("Geoid model file: Incorrect value of maxval"); } _datastart = currentLine.value.offset; _swidth = _width; if (_offset === Number.MAX_VALUE) { throw new Error("Geoid model file: Offset not set"); } if (_scale === 0) { throw new Error("Geoid model file: Scale not set"); } if (_scale < 0) { throw new Error("Geoid model file: Scale must be positive"); } if (_height < 2 || _width < 2) { throw new Error("Geoid model file: Raster size too small"); } if (_width & 1) { throw new Error("Geoid model file: Raster width is odd"); } if (!(_height & 1)) { throw new Error("Geoid model file: Raster height is even"); } const _rlonres = _width / 360; const _rlatres = (_height - 1) / 180; return new Geoid({ cubic: options.cubic, _width, _height, _rlonres, _rlatres, _offset, _scale, _swidth, _datastart, _maxerror, _rmserror, _description, _datetime, data }); } function* _getLineGenerator(data) { let offset = 0; do { const endLineIndex = data.indexOf(ENDL, offset); if (endLineIndex !== -1) { const line = data.subarray(offset, endLineIndex); offset = endLineIndex + 1; yield { offset, line: _getStringFromCharArray(line) }; } else { const line = data.subarray(offset, data.length); offset = data.length; yield { offset, line: _getStringFromCharArray(line) }; } } while (offset < data.length); return { offset, line: "" }; } function _getStringFromCharArray(array) { let s = ""; for (const char of array) { s += String.fromCharCode(char); } return s; } //# sourceMappingURL=index.cjs.map