proj4
Version:
Proj4js is a JavaScript library to transform point coordinates from one coordinate system to another, including datum transformations.
101 lines (90 loc) • 2.66 kB
JavaScript
import adjust_lon from '../common/adjust_lon';
import asinz from '../common/asinz';
import { EPSLN, HALF_PI } from '../constants/values';
/**
* @typedef {Object} LocalThis
* @property {number} sin_p14
* @property {number} cos_p14
*/
/** @this {import('../defs.js').ProjectionDefinition & LocalThis} */
export function init() {
// double temp; /* temporary variable */
/* Place parameters in static storage for common use
------------------------------------------------- */
this.sin_p14 = Math.sin(this.lat0 || 0);
this.cos_p14 = Math.cos(this.lat0 || 0);
}
/* Orthographic forward equations--mapping lat,long to x,y
--------------------------------------------------- */
export function forward(p) {
var sinphi, cosphi; /* sin and cos value */
var dlon; /* delta longitude value */
var coslon; /* cos of longitude */
var ksp; /* scale factor */
var g, x, y;
var lon = p.x;
var lat = p.y;
/* Forward equations
----------------- */
dlon = adjust_lon(lon - (this.long0 || 0), this.over);
sinphi = Math.sin(lat);
cosphi = Math.cos(lat);
coslon = Math.cos(dlon);
g = this.sin_p14 * sinphi + this.cos_p14 * cosphi * coslon;
ksp = 1;
if ((g > 0) || (Math.abs(g) <= EPSLN)) {
x = this.a * ksp * cosphi * Math.sin(dlon);
y = (this.y0 || 0) + this.a * ksp * (this.cos_p14 * sinphi - this.sin_p14 * cosphi * coslon);
}
p.x = x;
p.y = y;
return p;
}
export function inverse(p) {
var rh; /* height above ellipsoid */
var z; /* angle */
var sinz, cosz; /* sin of z and cos of z */
var con;
var lon, lat;
var long0, lat0;
/* Inverse equations
----------------- */
p.x -= this.x0 || 0;
p.y -= this.y0 || 0;
rh = Math.sqrt(p.x * p.x + p.y * p.y);
z = asinz(rh / this.a);
sinz = Math.sin(z);
cosz = Math.cos(z);
long0 = this.long0 || 0;
lat0 = this.lat0 || 0;
lon = long0;
if (Math.abs(rh) <= EPSLN) {
lat = lat0;
p.x = lon;
p.y = lat;
return p;
}
lat = asinz(cosz * this.sin_p14 + (p.y * sinz * this.cos_p14) / rh);
con = Math.abs(lat0) - HALF_PI;
if (Math.abs(con) <= EPSLN) {
if (lat0 >= 0) {
lon = adjust_lon(long0 + Math.atan2(p.x, -p.y), this.over);
} else {
lon = adjust_lon(long0 - Math.atan2(-p.x, p.y), this.over);
}
p.x = lon;
p.y = lat;
return p;
}
lon = adjust_lon(long0 + Math.atan2((p.x * sinz), rh * this.cos_p14 * cosz - p.y * this.sin_p14 * sinz), this.over);
p.x = lon;
p.y = lat;
return p;
}
export var names = ['ortho'];
export default {
init: init,
forward: forward,
inverse: inverse,
names: names
};