torque/lib/torque/mercator.js
2014-12-09 15:55:52 +01:00

92 lines
2.9 KiB
JavaScript

var Point = function(x, y) {
this.x = x || 0;
this.y = y || 0;
};
function clamp(value, optMin, optMax) {
if (optMin !== null) value = Math.max(value, optMin);
if (optMax !== null) value = Math.min(value, optMax);
return value;
}
function degreesToRadians(deg) {
return deg * (Math.PI / 180);
}
function radiansToDegrees(rad) {
return rad / (Math.PI / 180);
}
var MercatorProjection = function() {
// this._tileSize = L.Browser.retina ? 512 : 256;
this._tileSize = 256;
this._pixelOrigin = new Point(this._tileSize / 2, this._tileSize / 2);
this._pixelsPerLonDegree = this._tileSize / 360;
this._pixelsPerLonRadian = this._tileSize / (2 * Math.PI);
};
MercatorProjection.prototype._fromLatLonToPoint = function(lat, lon) {
var point = new Point(0, 0);
var origin = this._pixelOrigin;
point.x = origin.x + lon * this._pixelsPerLonDegree;
// NOTE(appleton): Truncating to 0.9999 effectively limits latitude to
// 89.189. This is about a third of a tile past the edge of the world
// tile.
var siny = clamp(Math.sin(degreesToRadians(lat)), -0.9999, 0.9999);
point.y = origin.y + 0.5 * Math.log((1 + siny) / (1 - siny)) * -this._pixelsPerLonRadian;
return point;
};
MercatorProjection.prototype._fromPointToLatLon = function(point) {
var me = this;
var origin = me._pixelOrigin;
var lon = (point.x - origin.x) / me._pixelsPerLonDegree;
var latRadians = (point.y - origin.y) / -me._pixelsPerLonRadian;
var lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2);
return { lat:lat, lon:lon };
};
MercatorProjection.prototype._tilePixelPos = function(tileX, tileY) {
return {
x: tileX*this._tileSize,
y: tileY*this._tileSize
};
};
MercatorProjection.prototype.tilePixelBBox = function(x, y, zoom, px, py, res) {
res = res || 1.0;
var numTiles = 1 <<zoom;
var inc = res/numTiles;
px = (x*this._tileSize + px)/numTiles;
py = (y*this._tileSize + py)/numTiles;
return [
this._fromPointToLatLon(new Point(px, py + inc)),
this._fromPointToLatLon(new Point(px + inc, py))
];
};
MercatorProjection.prototype.tileBBox = function(x, y, zoom, bufferSize) {
var numTiles = 1 <<zoom;
bufferSize = bufferSize || 0;
var inc = (this._tileSize + bufferSize*2)/numTiles;
var px = (x*this._tileSize - bufferSize )/numTiles;
var py = (y*this._tileSize - bufferSize )/numTiles;
return [
this._fromPointToLatLon(new Point(px, py + inc)),
this._fromPointToLatLon(new Point(px + inc, py))
];
};
MercatorProjection.prototype.latLonToTilePoint = function(lat, lon, tileX, tileY, zoom) {
var numTiles = 1 <<zoom;
var worldCoordinate = this._fromLatLonToPoint(lat, lon);
var pixelCoordinate = new Point(worldCoordinate.x*numTiles, worldCoordinate.y*numTiles);
var tilePixelPos = this._tilePixelPos(tileX, tileY);
return new Point(Math.round(pixelCoordinate.x-tilePixelPos.x), Math.round(pixelCoordinate.y-tilePixelPos.y));
};
module.exports = MercatorProjection;