iwmlib/lib/pixi/maps/projections/mercator.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

import Projection from './projection.js'
/**
* This is a projection file, that grants access to the
* MERCATOR projection.
*
* Regulary only few Projections will be used in one
* project, therefore only required one's should be
* loaded.
*/
export default class Mercator extends Projection {
forward(coords) {
let lat = coords.x
let lng = coords.y
const PI_180 = Math.PI / 180.0
const PI_4 = Math.PI * 4
const sinLatitude = Math.sin(lat * PI_180)
let y = 0.5 - Math.log((1 + sinLatitude) / (1 - sinLatitude)) / PI_4
let x = (lng + 180) / 360
y = y < 0 ? 0 : y > 1 ? 1 : y
return new PIXI.Point(x, y)
}
backward(point) {
let lng = point.x * 360 - 180
let lat = (Math.asin(-2 / (Math.exp(4 * Math.PI * (0.5 - point.y)) + 1) + 1) * 180) / Math.PI
return new PIXI.Point(lat, lng)
}
toString() {
return 'Mercator Projection'
}
get maxViewport() {
return { min: new PIXI.Point(-85, -180), max: new PIXI.Point(85, 180) }
}
}