@sauskylark/potree
Version:
WebGL point cloud viewer
1,738 lines (1,564 loc) • 187 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('wkt-parser')) :
typeof define === 'function' && define.amd ? define(['wkt-parser'], factory) :
(global.proj4 = factory(global.wkt));
}(this, (function (wkt) { 'use strict';
wkt = wkt && wkt.hasOwnProperty('default') ? wkt['default'] : wkt;
var globals = function(defs) {
defs('EPSG:4326', "+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees");
defs('EPSG:4269', "+title=NAD83 (long/lat) +proj=longlat +a=6378137.0 +b=6356752.31414036 +ellps=GRS80 +datum=NAD83 +units=degrees");
defs('EPSG:3857', "+title=WGS 84 / Pseudo-Mercator +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs");
defs.WGS84 = defs['EPSG:4326'];
defs['EPSG:3785'] = defs['EPSG:3857']; // maintain backward compat, official code is 3857
defs.GOOGLE = defs['EPSG:3857'];
defs['EPSG:900913'] = defs['EPSG:3857'];
defs['EPSG:102113'] = defs['EPSG:3857'];
};
var PJD_3PARAM = 1;
var PJD_7PARAM = 2;
var PJD_WGS84 = 4; // WGS84 or equivalent
var PJD_NODATUM = 5; // WGS84 or equivalent
var SEC_TO_RAD = 4.84813681109535993589914102357e-6;
var HALF_PI = Math.PI/2;
// ellipoid pj_set_ell.c
var SIXTH = 0.1666666666666666667;
/* 1/6 */
var RA4 = 0.04722222222222222222;
/* 17/360 */
var RA6 = 0.02215608465608465608;
var EPSLN = 1.0e-10;
// you'd think you could use Number.EPSILON above but that makes
// Mollweide get into an infinate loop.
var D2R = 0.01745329251994329577;
var R2D = 57.29577951308232088;
var FORTPI = Math.PI/4;
var TWO_PI = Math.PI * 2;
// SPI is slightly greater than Math.PI, so values that exceed the -180..180
// degree range by a tiny amount don't get wrapped. This prevents points that
// have drifted from their original location along the 180th meridian (due to
// floating point error) from changing their sign.
var SPI = 3.14159265359;
var exports$1 = {};
exports$1.greenwich = 0.0; //"0dE",
exports$1.lisbon = -9.131906111111; //"9d07'54.862\"W",
exports$1.paris = 2.337229166667; //"2d20'14.025\"E",
exports$1.bogota = -74.080916666667; //"74d04'51.3\"W",
exports$1.madrid = -3.687938888889; //"3d41'16.58\"W",
exports$1.rome = 12.452333333333; //"12d27'8.4\"E",
exports$1.bern = 7.439583333333; //"7d26'22.5\"E",
exports$1.jakarta = 106.807719444444; //"106d48'27.79\"E",
exports$1.ferro = -17.666666666667; //"17d40'W",
exports$1.brussels = 4.367975; //"4d22'4.71\"E",
exports$1.stockholm = 18.058277777778; //"18d3'29.8\"E",
exports$1.athens = 23.7163375; //"23d42'58.815\"E",
exports$1.oslo = 10.722916666667; //"10d43'22.5\"E"
var units = {
ft: {to_meter: 0.3048},
'us-ft': {to_meter: 1200 / 3937}
};
var ignoredChar = /[\s_\-\/\(\)]/g;
function match(obj, key) {
if (obj[key]) {
return obj[key];
}
var keys = Object.keys(obj);
var lkey = key.toLowerCase().replace(ignoredChar, '');
var i = -1;
var testkey, processedKey;
while (++i < keys.length) {
testkey = keys[i];
processedKey = testkey.toLowerCase().replace(ignoredChar, '');
if (processedKey === lkey) {
return obj[testkey];
}
}
}
var parseProj = function(defData) {
var self = {};
var paramObj = defData.split('+').map(function(v) {
return v.trim();
}).filter(function(a) {
return a;
}).reduce(function(p, a) {
var split = a.split('=');
split.push(true);
p[split[0].toLowerCase()] = split[1];
return p;
}, {});
var paramName, paramVal, paramOutname;
var params = {
proj: 'projName',
datum: 'datumCode',
rf: function(v) {
self.rf = parseFloat(v);
},
lat_0: function(v) {
self.lat0 = v * D2R;
},
lat_1: function(v) {
self.lat1 = v * D2R;
},
lat_2: function(v) {
self.lat2 = v * D2R;
},
lat_ts: function(v) {
self.lat_ts = v * D2R;
},
lon_0: function(v) {
self.long0 = v * D2R;
},
lon_1: function(v) {
self.long1 = v * D2R;
},
lon_2: function(v) {
self.long2 = v * D2R;
},
alpha: function(v) {
self.alpha = parseFloat(v) * D2R;
},
lonc: function(v) {
self.longc = v * D2R;
},
x_0: function(v) {
self.x0 = parseFloat(v);
},
y_0: function(v) {
self.y0 = parseFloat(v);
},
k_0: function(v) {
self.k0 = parseFloat(v);
},
k: function(v) {
self.k0 = parseFloat(v);
},
a: function(v) {
self.a = parseFloat(v);
},
b: function(v) {
self.b = parseFloat(v);
},
r_a: function() {
self.R_A = true;
},
zone: function(v) {
self.zone = parseInt(v, 10);
},
south: function() {
self.utmSouth = true;
},
towgs84: function(v) {
self.datum_params = v.split(",").map(function(a) {
return parseFloat(a);
});
},
to_meter: function(v) {
self.to_meter = parseFloat(v);
},
units: function(v) {
self.units = v;
var unit = match(units, v);
if (unit) {
self.to_meter = unit.to_meter;
}
},
from_greenwich: function(v) {
self.from_greenwich = v * D2R;
},
pm: function(v) {
var pm = match(exports$1, v);
self.from_greenwich = (pm ? pm : parseFloat(v)) * D2R;
},
nadgrids: function(v) {
if (v === '@null') {
self.datumCode = 'none';
}
else {
self.nadgrids = v;
}
},
axis: function(v) {
var legalAxis = "ewnsud";
if (v.length === 3 && legalAxis.indexOf(v.substr(0, 1)) !== -1 && legalAxis.indexOf(v.substr(1, 1)) !== -1 && legalAxis.indexOf(v.substr(2, 1)) !== -1) {
self.axis = v;
}
}
};
for (paramName in paramObj) {
paramVal = paramObj[paramName];
if (paramName in params) {
paramOutname = params[paramName];
if (typeof paramOutname === 'function') {
paramOutname(paramVal);
}
else {
self[paramOutname] = paramVal;
}
}
else {
self[paramName] = paramVal;
}
}
if(typeof self.datumCode === 'string' && self.datumCode !== "WGS84"){
self.datumCode = self.datumCode.toLowerCase();
}
return self;
};
function defs(name) {
/*global console*/
var that = this;
if (arguments.length === 2) {
var def = arguments[1];
if (typeof def === 'string') {
if (def.charAt(0) === '+') {
defs[name] = parseProj(arguments[1]);
}
else {
defs[name] = wkt(arguments[1]);
}
} else {
defs[name] = def;
}
}
else if (arguments.length === 1) {
if (Array.isArray(name)) {
return name.map(function(v) {
if (Array.isArray(v)) {
defs.apply(that, v);
}
else {
defs(v);
}
});
}
else if (typeof name === 'string') {
if (name in defs) {
return defs[name];
}
}
else if ('EPSG' in name) {
defs['EPSG:' + name.EPSG] = name;
}
else if ('ESRI' in name) {
defs['ESRI:' + name.ESRI] = name;
}
else if ('IAU2000' in name) {
defs['IAU2000:' + name.IAU2000] = name;
}
else {
console.log(name);
}
return;
}
}
globals(defs);
function testObj(code){
return typeof code === 'string';
}
function testDef(code){
return code in defs;
}
var codeWords = ['PROJECTEDCRS', 'PROJCRS', 'GEOGCS','GEOCCS','PROJCS','LOCAL_CS', 'GEODCRS', 'GEODETICCRS', 'GEODETICDATUM', 'ENGCRS', 'ENGINEERINGCRS'];
function testWKT(code){
return codeWords.some(function (word) {
return code.indexOf(word) > -1;
});
}
var codes = ['3857', '900913', '3785', '102113'];
function checkMercator(item) {
var auth = match(item, 'authority');
if (!auth) {
return;
}
var code = match(auth, 'epsg');
return code && codes.indexOf(code) > -1;
}
function checkProjStr(item) {
var ext = match(item, 'extension');
if (!ext) {
return;
}
return match(ext, 'proj4');
}
function testProj(code){
return code[0] === '+';
}
function parse(code){
if (testObj(code)) {
//check to see if this is a WKT string
if (testDef(code)) {
return defs[code];
}
if (testWKT(code)) {
var out = wkt(code);
// test of spetial case, due to this being a very common and often malformed
if (checkMercator(out)) {
return defs['EPSG:3857'];
}
var maybeProjStr = checkProjStr(out);
if (maybeProjStr) {
return parseProj(maybeProjStr);
}
return out;
}
if (testProj(code)) {
return parseProj(code);
}
}else{
return code;
}
}
var extend = function(destination, source) {
destination = destination || {};
var value, property;
if (!source) {
return destination;
}
for (property in source) {
value = source[property];
if (value !== undefined) {
destination[property] = value;
}
}
return destination;
};
var msfnz = function(eccent, sinphi, cosphi) {
var con = eccent * sinphi;
return cosphi / (Math.sqrt(1 - con * con));
};
var sign = function(x) {
return x<0 ? -1 : 1;
};
var adjust_lon = function(x) {
return (Math.abs(x) <= SPI) ? x : (x - (sign(x) * TWO_PI));
};
var tsfnz = function(eccent, phi, sinphi) {
var con = eccent * sinphi;
var com = 0.5 * eccent;
con = Math.pow(((1 - con) / (1 + con)), com);
return (Math.tan(0.5 * (HALF_PI - phi)) / con);
};
var phi2z = function(eccent, ts) {
var eccnth = 0.5 * eccent;
var con, dphi;
var phi = HALF_PI - 2 * Math.atan(ts);
for (var i = 0; i <= 15; i++) {
con = eccent * Math.sin(phi);
dphi = HALF_PI - 2 * Math.atan(ts * (Math.pow(((1 - con) / (1 + con)), eccnth))) - phi;
phi += dphi;
if (Math.abs(dphi) <= 0.0000000001) {
return phi;
}
}
//console.log("phi2z has NoConvergence");
return -9999;
};
function init() {
var con = this.b / this.a;
this.es = 1 - con * con;
if(!('x0' in this)){
this.x0 = 0;
}
if(!('y0' in this)){
this.y0 = 0;
}
this.e = Math.sqrt(this.es);
if (this.lat_ts) {
if (this.sphere) {
this.k0 = Math.cos(this.lat_ts);
}
else {
this.k0 = msfnz(this.e, Math.sin(this.lat_ts), Math.cos(this.lat_ts));
}
}
else {
if (!this.k0) {
if (this.k) {
this.k0 = this.k;
}
else {
this.k0 = 1;
}
}
}
}
/* Mercator forward equations--mapping lat,long to x,y
--------------------------------------------------*/
function forward(p) {
var lon = p.x;
var lat = p.y;
// convert to radians
if (lat * R2D > 90 && lat * R2D < -90 && lon * R2D > 180 && lon * R2D < -180) {
return null;
}
var x, y;
if (Math.abs(Math.abs(lat) - HALF_PI) <= EPSLN) {
return null;
}
else {
if (this.sphere) {
x = this.x0 + this.a * this.k0 * adjust_lon(lon - this.long0);
y = this.y0 + this.a * this.k0 * Math.log(Math.tan(FORTPI + 0.5 * lat));
}
else {
var sinphi = Math.sin(lat);
var ts = tsfnz(this.e, lat, sinphi);
x = this.x0 + this.a * this.k0 * adjust_lon(lon - this.long0);
y = this.y0 - this.a * this.k0 * Math.log(ts);
}
p.x = x;
p.y = y;
return p;
}
}
/* Mercator inverse equations--mapping x,y to lat/long
--------------------------------------------------*/
function inverse(p) {
var x = p.x - this.x0;
var y = p.y - this.y0;
var lon, lat;
if (this.sphere) {
lat = HALF_PI - 2 * Math.atan(Math.exp(-y / (this.a * this.k0)));
}
else {
var ts = Math.exp(-y / (this.a * this.k0));
lat = phi2z(this.e, ts);
if (lat === -9999) {
return null;
}
}
lon = adjust_lon(this.long0 + x / (this.a * this.k0));
p.x = lon;
p.y = lat;
return p;
}
var names$1 = ["Mercator", "Popular Visualisation Pseudo Mercator", "Mercator_1SP", "Mercator_Auxiliary_Sphere", "merc"];
var merc = {
init: init,
forward: forward,
inverse: inverse,
names: names$1
};
function init$1() {
//no-op for longlat
}
function identity(pt) {
return pt;
}
var names$2 = ["longlat", "identity"];
var longlat = {
init: init$1,
forward: identity,
inverse: identity,
names: names$2
};
var projs = [merc, longlat];
var names = {};
var projStore = [];
function add(proj, i) {
var len = projStore.length;
if (!proj.names) {
console.log(i);
return true;
}
projStore[len] = proj;
proj.names.forEach(function(n) {
names[n.toLowerCase()] = len;
});
return this;
}
function get(name) {
if (!name) {
return false;
}
var n = name.toLowerCase();
if (typeof names[n] !== 'undefined' && projStore[names[n]]) {
return projStore[names[n]];
}
}
function start() {
projs.forEach(add);
}
var projections = {
start: start,
add: add,
get: get
};
var exports$2 = {};
exports$2.MERIT = {
a: 6378137.0,
rf: 298.257,
ellipseName: "MERIT 1983"
};
exports$2.SGS85 = {
a: 6378136.0,
rf: 298.257,
ellipseName: "Soviet Geodetic System 85"
};
exports$2.GRS80 = {
a: 6378137.0,
rf: 298.257222101,
ellipseName: "GRS 1980(IUGG, 1980)"
};
exports$2.IAU76 = {
a: 6378140.0,
rf: 298.257,
ellipseName: "IAU 1976"
};
exports$2.airy = {
a: 6377563.396,
b: 6356256.910,
ellipseName: "Airy 1830"
};
exports$2.APL4 = {
a: 6378137,
rf: 298.25,
ellipseName: "Appl. Physics. 1965"
};
exports$2.NWL9D = {
a: 6378145.0,
rf: 298.25,
ellipseName: "Naval Weapons Lab., 1965"
};
exports$2.mod_airy = {
a: 6377340.189,
b: 6356034.446,
ellipseName: "Modified Airy"
};
exports$2.andrae = {
a: 6377104.43,
rf: 300.0,
ellipseName: "Andrae 1876 (Den., Iclnd.)"
};
exports$2.aust_SA = {
a: 6378160.0,
rf: 298.25,
ellipseName: "Australian Natl & S. Amer. 1969"
};
exports$2.GRS67 = {
a: 6378160.0,
rf: 298.2471674270,
ellipseName: "GRS 67(IUGG 1967)"
};
exports$2.bessel = {
a: 6377397.155,
rf: 299.1528128,
ellipseName: "Bessel 1841"
};
exports$2.bess_nam = {
a: 6377483.865,
rf: 299.1528128,
ellipseName: "Bessel 1841 (Namibia)"
};
exports$2.clrk66 = {
a: 6378206.4,
b: 6356583.8,
ellipseName: "Clarke 1866"
};
exports$2.clrk80 = {
a: 6378249.145,
rf: 293.4663,
ellipseName: "Clarke 1880 mod."
};
exports$2.clrk58 = {
a: 6378293.645208759,
rf: 294.2606763692654,
ellipseName: "Clarke 1858"
};
exports$2.CPM = {
a: 6375738.7,
rf: 334.29,
ellipseName: "Comm. des Poids et Mesures 1799"
};
exports$2.delmbr = {
a: 6376428.0,
rf: 311.5,
ellipseName: "Delambre 1810 (Belgium)"
};
exports$2.engelis = {
a: 6378136.05,
rf: 298.2566,
ellipseName: "Engelis 1985"
};
exports$2.evrst30 = {
a: 6377276.345,
rf: 300.8017,
ellipseName: "Everest 1830"
};
exports$2.evrst48 = {
a: 6377304.063,
rf: 300.8017,
ellipseName: "Everest 1948"
};
exports$2.evrst56 = {
a: 6377301.243,
rf: 300.8017,
ellipseName: "Everest 1956"
};
exports$2.evrst69 = {
a: 6377295.664,
rf: 300.8017,
ellipseName: "Everest 1969"
};
exports$2.evrstSS = {
a: 6377298.556,
rf: 300.8017,
ellipseName: "Everest (Sabah & Sarawak)"
};
exports$2.fschr60 = {
a: 6378166.0,
rf: 298.3,
ellipseName: "Fischer (Mercury Datum) 1960"
};
exports$2.fschr60m = {
a: 6378155.0,
rf: 298.3,
ellipseName: "Fischer 1960"
};
exports$2.fschr68 = {
a: 6378150.0,
rf: 298.3,
ellipseName: "Fischer 1968"
};
exports$2.helmert = {
a: 6378200.0,
rf: 298.3,
ellipseName: "Helmert 1906"
};
exports$2.hough = {
a: 6378270.0,
rf: 297.0,
ellipseName: "Hough"
};
exports$2.intl = {
a: 6378388.0,
rf: 297.0,
ellipseName: "International 1909 (Hayford)"
};
exports$2.kaula = {
a: 6378163.0,
rf: 298.24,
ellipseName: "Kaula 1961"
};
exports$2.lerch = {
a: 6378139.0,
rf: 298.257,
ellipseName: "Lerch 1979"
};
exports$2.mprts = {
a: 6397300.0,
rf: 191.0,
ellipseName: "Maupertius 1738"
};
exports$2.new_intl = {
a: 6378157.5,
b: 6356772.2,
ellipseName: "New International 1967"
};
exports$2.plessis = {
a: 6376523.0,
rf: 6355863.0,
ellipseName: "Plessis 1817 (France)"
};
exports$2.krass = {
a: 6378245.0,
rf: 298.3,
ellipseName: "Krassovsky, 1942"
};
exports$2.SEasia = {
a: 6378155.0,
b: 6356773.3205,
ellipseName: "Southeast Asia"
};
exports$2.walbeck = {
a: 6376896.0,
b: 6355834.8467,
ellipseName: "Walbeck"
};
exports$2.WGS60 = {
a: 6378165.0,
rf: 298.3,
ellipseName: "WGS 60"
};
exports$2.WGS66 = {
a: 6378145.0,
rf: 298.25,
ellipseName: "WGS 66"
};
exports$2.WGS7 = {
a: 6378135.0,
rf: 298.26,
ellipseName: "WGS 72"
};
var WGS84 = exports$2.WGS84 = {
a: 6378137.0,
rf: 298.257223563,
ellipseName: "WGS 84"
};
exports$2.sphere = {
a: 6370997.0,
b: 6370997.0,
ellipseName: "Normal Sphere (r=6370997)"
};
function eccentricity(a, b, rf, R_A) {
var a2 = a * a; // used in geocentric
var b2 = b * b; // used in geocentric
var es = (a2 - b2) / a2; // e ^ 2
var e = 0;
if (R_A) {
a *= 1 - es * (SIXTH + es * (RA4 + es * RA6));
a2 = a * a;
es = 0;
} else {
e = Math.sqrt(es); // eccentricity
}
var ep2 = (a2 - b2) / b2; // used in geocentric
return {
es: es,
e: e,
ep2: ep2
};
}
function sphere(a, b, rf, ellps, sphere) {
if (!a) { // do we have an ellipsoid?
var ellipse = match(exports$2, ellps);
if (!ellipse) {
ellipse = WGS84;
}
a = ellipse.a;
b = ellipse.b;
rf = ellipse.rf;
}
if (rf && !b) {
b = (1.0 - 1.0 / rf) * a;
}
if (rf === 0 || Math.abs(a - b) < EPSLN) {
sphere = true;
b = a;
}
return {
a: a,
b: b,
rf: rf,
sphere: sphere
};
}
var exports$3 = {};
exports$3.wgs84 = {
towgs84: "0,0,0",
ellipse: "WGS84",
datumName: "WGS84"
};
exports$3.ch1903 = {
towgs84: "674.374,15.056,405.346",
ellipse: "bessel",
datumName: "swiss"
};
exports$3.ggrs87 = {
towgs84: "-199.87,74.79,246.62",
ellipse: "GRS80",
datumName: "Greek_Geodetic_Reference_System_1987"
};
exports$3.nad83 = {
towgs84: "0,0,0",
ellipse: "GRS80",
datumName: "North_American_Datum_1983"
};
exports$3.nad27 = {
nadgrids: "@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat",
ellipse: "clrk66",
datumName: "North_American_Datum_1927"
};
exports$3.potsdam = {
towgs84: "606.0,23.0,413.0",
ellipse: "bessel",
datumName: "Potsdam Rauenberg 1950 DHDN"
};
exports$3.carthage = {
towgs84: "-263.0,6.0,431.0",
ellipse: "clark80",
datumName: "Carthage 1934 Tunisia"
};
exports$3.hermannskogel = {
towgs84: "653.0,-212.0,449.0",
ellipse: "bessel",
datumName: "Hermannskogel"
};
exports$3.osni52 = {
towgs84: "482.530,-130.596,564.557,-1.042,-0.214,-0.631,8.15",
ellipse: "airy",
datumName: "Irish National"
};
exports$3.ire65 = {
towgs84: "482.530,-130.596,564.557,-1.042,-0.214,-0.631,8.15",
ellipse: "mod_airy",
datumName: "Ireland 1965"
};
exports$3.rassadiran = {
towgs84: "-133.63,-157.5,-158.62",
ellipse: "intl",
datumName: "Rassadiran"
};
exports$3.nzgd49 = {
towgs84: "59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993",
ellipse: "intl",
datumName: "New Zealand Geodetic Datum 1949"
};
exports$3.osgb36 = {
towgs84: "446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894",
ellipse: "airy",
datumName: "Airy 1830"
};
exports$3.s_jtsk = {
towgs84: "589,76,480",
ellipse: 'bessel',
datumName: 'S-JTSK (Ferro)'
};
exports$3.beduaram = {
towgs84: '-106,-87,188',
ellipse: 'clrk80',
datumName: 'Beduaram'
};
exports$3.gunung_segara = {
towgs84: '-403,684,41',
ellipse: 'bessel',
datumName: 'Gunung Segara Jakarta'
};
exports$3.rnb72 = {
towgs84: "106.869,-52.2978,103.724,-0.33657,0.456955,-1.84218,1",
ellipse: "intl",
datumName: "Reseau National Belge 1972"
};
function datum(datumCode, datum_params, a, b, es, ep2) {
var out = {};
if (datumCode === undefined || datumCode === 'none') {
out.datum_type = PJD_NODATUM;
} else {
out.datum_type = PJD_WGS84;
}
if (datum_params) {
out.datum_params = datum_params.map(parseFloat);
if (out.datum_params[0] !== 0 || out.datum_params[1] !== 0 || out.datum_params[2] !== 0) {
out.datum_type = PJD_3PARAM;
}
if (out.datum_params.length > 3) {
if (out.datum_params[3] !== 0 || out.datum_params[4] !== 0 || out.datum_params[5] !== 0 || out.datum_params[6] !== 0) {
out.datum_type = PJD_7PARAM;
out.datum_params[3] *= SEC_TO_RAD;
out.datum_params[4] *= SEC_TO_RAD;
out.datum_params[5] *= SEC_TO_RAD;
out.datum_params[6] = (out.datum_params[6] / 1000000.0) + 1.0;
}
}
}
out.a = a; //datum object also uses these values
out.b = b;
out.es = es;
out.ep2 = ep2;
return out;
}
function Projection(srsCode,callback) {
if (!(this instanceof Projection)) {
return new Projection(srsCode);
}
callback = callback || function(error){
if(error){
throw error;
}
};
var json = parse(srsCode);
if(typeof json !== 'object'){
callback(srsCode);
return;
}
var ourProj = Projection.projections.get(json.projName);
if(!ourProj){
callback(srsCode);
return;
}
if (json.datumCode && json.datumCode !== 'none') {
var datumDef = match(exports$3, json.datumCode);
if (datumDef) {
json.datum_params = datumDef.towgs84 ? datumDef.towgs84.split(',') : null;
json.ellps = datumDef.ellipse;
json.datumName = datumDef.datumName ? datumDef.datumName : json.datumCode;
}
}
json.k0 = json.k0 || 1.0;
json.axis = json.axis || 'enu';
json.ellps = json.ellps || 'wgs84';
var sphere_ = sphere(json.a, json.b, json.rf, json.ellps, json.sphere);
var ecc = eccentricity(sphere_.a, sphere_.b, sphere_.rf, json.R_A);
var datumObj = json.datum || datum(json.datumCode, json.datum_params, sphere_.a, sphere_.b, ecc.es, ecc.ep2);
extend(this, json); // transfer everything over from the projection because we don't know what we'll need
extend(this, ourProj); // transfer all the methods from the projection
// copy the 4 things over we calulated in deriveConstants.sphere
this.a = sphere_.a;
this.b = sphere_.b;
this.rf = sphere_.rf;
this.sphere = sphere_.sphere;
// copy the 3 things we calculated in deriveConstants.eccentricity
this.es = ecc.es;
this.e = ecc.e;
this.ep2 = ecc.ep2;
// add in the datum object
this.datum = datumObj;
// init the projection
this.init();
// legecy callback from back in the day when it went to spatialreference.org
callback(null, this);
}
Projection.projections = projections;
Projection.projections.start();
'use strict';
function compareDatums(source, dest) {
if (source.datum_type !== dest.datum_type) {
return false; // false, datums are not equal
} else if (source.a !== dest.a || Math.abs(source.es - dest.es) > 0.000000000050) {
// the tolerance for es is to ensure that GRS80 and WGS84
// are considered identical
return false;
} else if (source.datum_type === PJD_3PARAM) {
return (source.datum_params[0] === dest.datum_params[0] && source.datum_params[1] === dest.datum_params[1] && source.datum_params[2] === dest.datum_params[2]);
} else if (source.datum_type === PJD_7PARAM) {
return (source.datum_params[0] === dest.datum_params[0] && source.datum_params[1] === dest.datum_params[1] && source.datum_params[2] === dest.datum_params[2] && source.datum_params[3] === dest.datum_params[3] && source.datum_params[4] === dest.datum_params[4] && source.datum_params[5] === dest.datum_params[5] && source.datum_params[6] === dest.datum_params[6]);
} else {
return true; // datums are equal
}
} // cs_compare_datums()
/*
* The function Convert_Geodetic_To_Geocentric converts geodetic coordinates
* (latitude, longitude, and height) to geocentric coordinates (X, Y, Z),
* according to the current ellipsoid parameters.
*
* Latitude : Geodetic latitude in radians (input)
* Longitude : Geodetic longitude in radians (input)
* Height : Geodetic height, in meters (input)
* X : Calculated Geocentric X coordinate, in meters (output)
* Y : Calculated Geocentric Y coordinate, in meters (output)
* Z : Calculated Geocentric Z coordinate, in meters (output)
*
*/
function geodeticToGeocentric(p, es, a) {
var Longitude = p.x;
var Latitude = p.y;
var Height = p.z ? p.z : 0; //Z value not always supplied
var Rn; /* Earth radius at location */
var Sin_Lat; /* Math.sin(Latitude) */
var Sin2_Lat; /* Square of Math.sin(Latitude) */
var Cos_Lat; /* Math.cos(Latitude) */
/*
** Don't blow up if Latitude is just a little out of the value
** range as it may just be a rounding issue. Also removed longitude
** test, it should be wrapped by Math.cos() and Math.sin(). NFW for PROJ.4, Sep/2001.
*/
if (Latitude < -HALF_PI && Latitude > -1.001 * HALF_PI) {
Latitude = -HALF_PI;
} else if (Latitude > HALF_PI && Latitude < 1.001 * HALF_PI) {
Latitude = HALF_PI;
} else if (Latitude < -HALF_PI) {
/* Latitude out of range */
//..reportError('geocent:lat out of range:' + Latitude);
return { x: -Infinity, y: -Infinity, z: p.z };
} else if (Latitude > HALF_PI) {
/* Latitude out of range */
return { x: Infinity, y: Infinity, z: p.z };
}
if (Longitude > Math.PI) {
Longitude -= (2 * Math.PI);
}
Sin_Lat = Math.sin(Latitude);
Cos_Lat = Math.cos(Latitude);
Sin2_Lat = Sin_Lat * Sin_Lat;
Rn = a / (Math.sqrt(1.0e0 - es * Sin2_Lat));
return {
x: (Rn + Height) * Cos_Lat * Math.cos(Longitude),
y: (Rn + Height) * Cos_Lat * Math.sin(Longitude),
z: ((Rn * (1 - es)) + Height) * Sin_Lat
};
} // cs_geodetic_to_geocentric()
function geocentricToGeodetic(p, es, a, b) {
/* local defintions and variables */
/* end-criterium of loop, accuracy of sin(Latitude) */
var genau = 1e-12;
var genau2 = (genau * genau);
var maxiter = 30;
var P; /* distance between semi-minor axis and location */
var RR; /* distance between center and location */
var CT; /* sin of geocentric latitude */
var ST; /* cos of geocentric latitude */
var RX;
var RK;
var RN; /* Earth radius at location */
var CPHI0; /* cos of start or old geodetic latitude in iterations */
var SPHI0; /* sin of start or old geodetic latitude in iterations */
var CPHI; /* cos of searched geodetic latitude */
var SPHI; /* sin of searched geodetic latitude */
var SDPHI; /* end-criterium: addition-theorem of sin(Latitude(iter)-Latitude(iter-1)) */
var iter; /* # of continous iteration, max. 30 is always enough (s.a.) */
var X = p.x;
var Y = p.y;
var Z = p.z ? p.z : 0.0; //Z value not always supplied
var Longitude;
var Latitude;
var Height;
P = Math.sqrt(X * X + Y * Y);
RR = Math.sqrt(X * X + Y * Y + Z * Z);
/* special cases for latitude and longitude */
if (P / a < genau) {
/* special case, if P=0. (X=0., Y=0.) */
Longitude = 0.0;
/* if (X,Y,Z)=(0.,0.,0.) then Height becomes semi-minor axis
* of ellipsoid (=center of mass), Latitude becomes PI/2 */
if (RR / a < genau) {
Latitude = HALF_PI;
Height = -b;
return {
x: p.x,
y: p.y,
z: p.z
};
}
} else {
/* ellipsoidal (geodetic) longitude
* interval: -PI < Longitude <= +PI */
Longitude = Math.atan2(Y, X);
}
/* --------------------------------------------------------------
* Following iterative algorithm was developped by
* "Institut for Erdmessung", University of Hannover, July 1988.
* Internet: www.ife.uni-hannover.de
* Iterative computation of CPHI,SPHI and Height.
* Iteration of CPHI and SPHI to 10**-12 radian resp.
* 2*10**-7 arcsec.
* --------------------------------------------------------------
*/
CT = Z / RR;
ST = P / RR;
RX = 1.0 / Math.sqrt(1.0 - es * (2.0 - es) * ST * ST);
CPHI0 = ST * (1.0 - es) * RX;
SPHI0 = CT * RX;
iter = 0;
/* loop to find sin(Latitude) resp. Latitude
* until |sin(Latitude(iter)-Latitude(iter-1))| < genau */
do {
iter++;
RN = a / Math.sqrt(1.0 - es * SPHI0 * SPHI0);
/* ellipsoidal (geodetic) height */
Height = P * CPHI0 + Z * SPHI0 - RN * (1.0 - es * SPHI0 * SPHI0);
RK = es * RN / (RN + Height);
RX = 1.0 / Math.sqrt(1.0 - RK * (2.0 - RK) * ST * ST);
CPHI = ST * (1.0 - RK) * RX;
SPHI = CT * RX;
SDPHI = SPHI * CPHI0 - CPHI * SPHI0;
CPHI0 = CPHI;
SPHI0 = SPHI;
}
while (SDPHI * SDPHI > genau2 && iter < maxiter);
/* ellipsoidal (geodetic) latitude */
Latitude = Math.atan(SPHI / Math.abs(CPHI));
return {
x: Longitude,
y: Latitude,
z: Height
};
} // cs_geocentric_to_geodetic()
/****************************************************************/
// pj_geocentic_to_wgs84( p )
// p = point to transform in geocentric coordinates (x,y,z)
/** point object, nothing fancy, just allows values to be
passed back and forth by reference rather than by value.
Other point classes may be used as long as they have
x and y properties, which will get modified in the transform method.
*/
function geocentricToWgs84(p, datum_type, datum_params) {
if (datum_type === PJD_3PARAM) {
// if( x[io] === HUGE_VAL )
// continue;
return {
x: p.x + datum_params[0],
y: p.y + datum_params[1],
z: p.z + datum_params[2],
};
} else if (datum_type === PJD_7PARAM) {
var Dx_BF = datum_params[0];
var Dy_BF = datum_params[1];
var Dz_BF = datum_params[2];
var Rx_BF = datum_params[3];
var Ry_BF = datum_params[4];
var Rz_BF = datum_params[5];
var M_BF = datum_params[6];
// if( x[io] === HUGE_VAL )
// continue;
return {
x: M_BF * (p.x - Rz_BF * p.y + Ry_BF * p.z) + Dx_BF,
y: M_BF * (Rz_BF * p.x + p.y - Rx_BF * p.z) + Dy_BF,
z: M_BF * (-Ry_BF * p.x + Rx_BF * p.y + p.z) + Dz_BF
};
}
} // cs_geocentric_to_wgs84
/****************************************************************/
// pj_geocentic_from_wgs84()
// coordinate system definition,
// point to transform in geocentric coordinates (x,y,z)
function geocentricFromWgs84(p, datum_type, datum_params) {
if (datum_type === PJD_3PARAM) {
//if( x[io] === HUGE_VAL )
// continue;
return {
x: p.x - datum_params[0],
y: p.y - datum_params[1],
z: p.z - datum_params[2],
};
} else if (datum_type === PJD_7PARAM) {
var Dx_BF = datum_params[0];
var Dy_BF = datum_params[1];
var Dz_BF = datum_params[2];
var Rx_BF = datum_params[3];
var Ry_BF = datum_params[4];
var Rz_BF = datum_params[5];
var M_BF = datum_params[6];
var x_tmp = (p.x - Dx_BF) / M_BF;
var y_tmp = (p.y - Dy_BF) / M_BF;
var z_tmp = (p.z - Dz_BF) / M_BF;
//if( x[io] === HUGE_VAL )
// continue;
return {
x: x_tmp + Rz_BF * y_tmp - Ry_BF * z_tmp,
y: -Rz_BF * x_tmp + y_tmp + Rx_BF * z_tmp,
z: Ry_BF * x_tmp - Rx_BF * y_tmp + z_tmp
};
} //cs_geocentric_from_wgs84()
}
function checkParams(type) {
return (type === PJD_3PARAM || type === PJD_7PARAM);
}
var datum_transform = function(source, dest, point) {
// Short cut if the datums are identical.
if (compareDatums(source, dest)) {
return point; // in this case, zero is sucess,
// whereas cs_compare_datums returns 1 to indicate TRUE
// confusing, should fix this
}
// Explicitly skip datum transform by setting 'datum=none' as parameter for either source or dest
if (source.datum_type === PJD_NODATUM || dest.datum_type === PJD_NODATUM) {
return point;
}
// If this datum requires grid shifts, then apply it to geodetic coordinates.
// Do we need to go through geocentric coordinates?
if (source.es === dest.es && source.a === dest.a && !checkParams(source.datum_type) && !checkParams(dest.datum_type)) {
return point;
}
// Convert to geocentric coordinates.
point = geodeticToGeocentric(point, source.es, source.a);
// Convert between datums
if (checkParams(source.datum_type)) {
point = geocentricToWgs84(point, source.datum_type, source.datum_params);
}
if (checkParams(dest.datum_type)) {
point = geocentricFromWgs84(point, dest.datum_type, dest.datum_params);
}
return geocentricToGeodetic(point, dest.es, dest.a, dest.b);
};
var adjust_axis = function(crs, denorm, point) {
var xin = point.x,
yin = point.y,
zin = point.z || 0.0;
var v, t, i;
var out = {};
for (i = 0; i < 3; i++) {
if (denorm && i === 2 && point.z === undefined) {
continue;
}
if (i === 0) {
v = xin;
if ("ew".indexOf(crs.axis[i]) !== -1) {
t = 'x';
} else {
t = 'y';
}
}
else if (i === 1) {
v = yin;
if ("ns".indexOf(crs.axis[i]) !== -1) {
t = 'y';
} else {
t = 'x';
}
}
else {
v = zin;
t = 'z';
}
switch (crs.axis[i]) {
case 'e':
case 'w':
case 'n':
case 's':
out[t] = v;
break;
case 'u':
if (point[t] !== undefined) {
out.z = v;
}
break;
case 'd':
if (point[t] !== undefined) {
out.z = -v;
}
break;
default:
//console.log("ERROR: unknow axis ("+crs.axis[i]+") - check definition of "+crs.projName);
return null;
}
}
return out;
};
var toPoint = function (array){
var out = {
x: array[0],
y: array[1]
};
if (array.length>2) {
out.z = array[2];
}
if (array.length>3) {
out.m = array[3];
}
return out;
};
var checkSanity = function (point) {
checkCoord(point.x);
checkCoord(point.y);
};
function checkCoord(num) {
if (typeof Number.isFinite === 'function') {
if (Number.isFinite(num)) {
return;
}
throw new TypeError('coordinates must be finite numbers');
}
if (typeof num !== 'number' || num !== num || !isFinite(num)) {
throw new TypeError('coordinates must be finite numbers');
}
}
function checkNotWGS(source, dest) {
return ((source.datum.datum_type === PJD_3PARAM || source.datum.datum_type === PJD_7PARAM) && dest.datumCode !== 'WGS84') || ((dest.datum.datum_type === PJD_3PARAM || dest.datum.datum_type === PJD_7PARAM) && source.datumCode !== 'WGS84');
}
function transform(source, dest, point) {
var wgs84;
if (Array.isArray(point)) {
point = toPoint(point);
}
checkSanity(point);
// Workaround for datum shifts towgs84, if either source or destination projection is not wgs84
if (source.datum && dest.datum && checkNotWGS(source, dest)) {
wgs84 = new Projection('WGS84');
point = transform(source, wgs84, point);
source = wgs84;
}
// DGR, 2010/11/12
if (source.axis !== 'enu') {
point = adjust_axis(source, false, point);
}
// Transform source points to long/lat, if they aren't already.
if (source.projName === 'longlat') {
point = {
x: point.x * D2R,
y: point.y * D2R,
z: point.z || 0
};
} else {
if (source.to_meter) {
point = {
x: point.x * source.to_meter,
y: point.y * source.to_meter,
z: point.z || 0
};
}
point = source.inverse(point); // Convert Cartesian to longlat
if (!point) {
return;
}
}
// Adjust for the prime meridian if necessary
if (source.from_greenwich) {
point.x += source.from_greenwich;
}
// Convert datums if needed, and if possible.
point = datum_transform(source.datum, dest.datum, point);
// Adjust for the prime meridian if necessary
if (dest.from_greenwich) {
point = {
x: point.x - dest.from_greenwich,
y: point.y,
z: point.z || 0
};
}
if (dest.projName === 'longlat') {
// convert radians to decimal degrees
point = {
x: point.x * R2D,
y: point.y * R2D,
z: point.z || 0
};
} else { // else project
point = dest.forward(point);
if (dest.to_meter) {
point = {
x: point.x / dest.to_meter,
y: point.y / dest.to_meter,
z: point.z || 0
};
}
}
// DGR, 2010/11/12
if (dest.axis !== 'enu') {
return adjust_axis(dest, true, point);
}
return point;
}
var wgs84 = Projection('WGS84');
function transformer(from, to, coords) {
var transformedArray, out, keys;
if (Array.isArray(coords)) {
transformedArray = transform(from, to, coords) || {x: NaN, y: NaN};
if (coords.length > 2) {
if ((typeof from.name !== 'undefined' && from.name === 'geocent') || (typeof to.name !== 'undefined' && to.name === 'geocent')) {
if (typeof transformedArray.z === 'number') {
return [transformedArray.x, transformedArray.y, transformedArray.z].concat(coords.splice(3));
} else {
return [transformedArray.x, transformedArray.y, coords[2]].concat(coords.splice(3));
}
} else {
return [transformedArray.x, transformedArray.y].concat(coords.splice(2));
}
} else {
return [transformedArray.x, transformedArray.y];
}
} else {
out = transform(from, to, coords);
keys = Object.keys(coords);
if (keys.length === 2) {
return out;
}
keys.forEach(function (key) {
if ((typeof from.name !== 'undefined' && from.name === 'geocent') || (typeof to.name !== 'undefined' && to.name === 'geocent')) {
if (key === 'x' || key === 'y' || key === 'z') {
return;
}
} else {
if (key === 'x' || key === 'y') {
return;
}
}
out[key] = coords[key];
});
return out;
}
}
function checkProj(item) {
if (item instanceof Projection) {
return item;
}
if (item.oProj) {
return item.oProj;
}
return Projection(item);
}
function proj4$1(fromProj, toProj, coord) {
fromProj = checkProj(fromProj);
var single = false;
var obj;
if (typeof toProj === 'undefined') {
toProj = fromProj;
fromProj = wgs84;
single = true;
} else if (typeof toProj.x !== 'undefined' || Array.isArray(toProj)) {
coord = toProj;
toProj = fromProj;
fromProj = wgs84;
single = true;
}
toProj = checkProj(toProj);
if (coord) {
return transformer(fromProj, toProj, coord);
} else {
obj = {
forward: function (coords) {
return transformer(fromProj, toProj, coords);
},
inverse: function (coords) {
return transformer(toProj, fromProj, coords);
}
};
if (single) {
obj.oProj = toProj;
}
return obj;
}
}
/**
* UTM zones are grouped, and assigned to one of a group of 6
* sets.
*
* {int} @private
*/
var NUM_100K_SETS = 6;
/**
* The column letters (for easting) of the lower left value, per
* set.
*
* {string} @private
*/
var SET_ORIGIN_COLUMN_LETTERS = 'AJSAJS';
/**
* The row letters (for northing) of the lower left value, per
* set.
*
* {string} @private
*/
var SET_ORIGIN_ROW_LETTERS = 'AFAFAF';
var A = 65; // A
var I = 73; // I
var O = 79; // O
var V = 86; // V
var Z = 90; // Z
var mgrs = {
forward: forward$1,
inverse: inverse$1,
toPoint: toPoint$1
};
/**
* Conversion of lat/lon to MGRS.
*
* @param {object} ll Object literal with lat and lon properties on a
* WGS84 ellipsoid.
* @param {int} accuracy Accuracy in digits (5 for 1 m, 4 for 10 m, 3 for
* 100 m, 2 for 1000 m or 1 for 10000 m). Optional, default is 5.
* @return {string} the MGRS string for the given location and accuracy.
*/
function forward$1(ll, accuracy) {
accuracy = accuracy || 5; // default accuracy 1m
return encode(LLtoUTM({
lat: ll[1],
lon: ll[0]
}), accuracy);
}
/**
* Conversion of MGRS to lat/lon.
*
* @param {string} mgrs MGRS string.
* @return {array} An array with left (longitude), bottom (latitude), right
* (longitude) and top (latitude) values in WGS84, representing the
* bounding box for the provided MGRS reference.
*/
function inverse$1(mgrs) {
var bbox = UTMtoLL(decode(mgrs.toUpperCase()));
if (bbox.lat && bbox.lon) {
return [bbox.lon, bbox.lat, bbox.lon, bbox.lat];
}
return [bbox.left, bbox.bottom, bbox.right, bbox.top];
}
function toPoint$1(mgrs) {
var bbox = UTMtoLL(decode(mgrs.toUpperCase()));
if (bbox.lat && bbox.lon) {
return [bbox.lon, bbox.lat];
}
return [(bbox.left + bbox.right) / 2, (bbox.top + bbox.bottom) / 2];
}
/**
* Conversion from degrees to radians.
*
* @private
* @param {number} deg the angle in degrees.
* @return {number} the angle in radians.
*/
function degToRad(deg) {
return (deg * (Math.PI / 180.0));
}
/**
* Conversion from radians to degrees.
*
* @private
* @param {number} rad the angle in radians.
* @return {number} the angle in degrees.
*/
function radToDeg(rad) {
return (180.0 * (rad / Math.PI));
}
/**
* Converts a set of Longitude and Latitude co-ordinates to UTM
* using the WGS84 ellipsoid.
*
* @private
* @param {object} ll Object literal with lat and lon properties
* representing the WGS84 coordinate to be converted.
* @return {object} Object literal containing the UTM value with easting,
* northing, zoneNumber and zoneLetter properties, and an optional
* accuracy property in digits. Returns null if the conversion failed.
*/
function LLtoUTM(ll) {
var Lat = ll.lat;
var Long = ll.lon;
var a = 6378137.0; //ellip.radius;
var eccSquared = 0.00669438; //ellip.eccsq;
var k0 = 0.9996;
var LongOrigin;
var eccPrimeSquared;
var N, T, C, A, M;
var LatRad = degToRad(Lat);
var LongRad = degToRad(Long);
var LongOriginRad;
var ZoneNumber;
// (int)
ZoneNumber = Math.floor((Long + 180) / 6) + 1;
//Make sure the longitude 180.00 is in Zone 60
if (Long === 180) {
ZoneNumber = 60;
}
// Special zone for Norway
if (Lat >= 56.0 && Lat < 64.0 && Long >= 3.0 && Long < 12.0) {
ZoneNumber = 32;
}
// Special zones for Svalbard
if (Lat >= 72.0 && Lat < 84.0) {
if (Long >= 0.0 && Long < 9.0) {
ZoneNumber = 31;
}
else if (Long >= 9.0 && Long < 21.0) {
ZoneNumber = 33;
}
else if (Long >= 21.0 && Long < 33.0) {
ZoneNumber = 35;
}
else if (Long >= 33.0 && Long < 42.0) {
ZoneNumber = 37;
}
}
LongOrigin = (ZoneNumber - 1) * 6 - 180 + 3; //+3 puts origin
// in middle of
// zone
LongOriginRad = degToRad(LongOrigin);
eccPrimeSquared = (eccSquared) / (1 - eccSquared);
N = a / Math.sqrt(1 - eccSquared * Math.sin(LatRad) * Math.sin(LatRad));
T = Math.tan(LatRad) * Math.tan(LatRad);
C = eccPrimeSquared * Math.cos(LatRad) * Math.cos(LatRad);
A = Math.cos(LatRad) * (LongRad - LongOriginRad);
M = a * ((1 - eccSquared / 4 - 3 * eccSquared * eccSquared / 64 - 5 * eccSquared * eccSquared * eccSquared / 256) * LatRad - (3 * eccSquared / 8 + 3 * eccSquared * eccSquared / 32 + 45 * eccSquared * eccSquared * eccSquared / 1024) * Math.sin(2 * LatRad) + (15 * eccSquared * eccSquared / 256 + 45 * eccSquared * eccSquared * eccSquared / 1024) * Math.sin(4 * LatRad) - (35 * eccSquared * eccSquared * eccSquared / 3072) * Math.sin(6 * LatRad));
var UTMEasting = (k0 * N * (A + (1 - T + C) * A * A * A / 6.0 + (5 - 18 * T + T * T + 72 * C - 58 * eccPrimeSquared) * A * A * A * A * A / 120.0) + 500000.0);
var UTMNorthing = (k0 * (M + N * Math.tan(LatRad) * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * A * A * A * A / 24.0 + (61 - 58 * T + T * T + 600 * C - 330 * eccPrimeSquared) * A * A * A * A * A * A / 720.0)));
if (Lat < 0.0) {
UTMNorthing += 10000000.0; //10000000 meter offset for
// southern hemisphere
}
return {
northing: Math.round(UTMNorthing),
easting: Math.round(UTMEasting),
zoneNumber: ZoneNumber,
zoneLetter: getLetterDesignator(Lat)
};
}
/**
* Converts UTM coords to lat/long, using the WGS84 ellipsoid. This is a convenience
* class where the Zone can be specified as a single string eg."60N" which
* is then broken down into