use mercator library for calculations
This commit is contained in:
parent
e422b0a799
commit
0c1aba58c6
@ -85,7 +85,7 @@ MercatorProjection.prototype.latLonToTilePoint = function(lat, lon, tileX, tileY
|
||||
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));
|
||||
return new Point(Math.round(pixelCoordinate.x-tilePixelPos.x), this._tileSize - Math.round(pixelCoordinate.y-tilePixelPos.y));
|
||||
};
|
||||
|
||||
module.exports = MercatorProjection;
|
||||
|
@ -1,10 +1,6 @@
|
||||
var torque = require('../');
|
||||
|
||||
|
||||
/** Converts numeric degrees to radians */
|
||||
Number.prototype.toRad = function () {
|
||||
return this * Math.PI / 180;
|
||||
};
|
||||
var mercatorUtils = new torque.Mercator();
|
||||
|
||||
/*
|
||||
* Options:
|
||||
@ -165,15 +161,13 @@ internal.prototype = {
|
||||
},
|
||||
prepareTile: function (tile) {
|
||||
/* Calculate tile bounds in lat/lon */
|
||||
var bounds = tileBoundsInMeters(tile.zoom, tile.x, tile.y);
|
||||
var mins = metersToLatLon(bounds[0]);
|
||||
var maxs = metersToLatLon(bounds[1]);
|
||||
var bounds = mercatorUtils.tileBBox(tile.x, tile.y, tile.zoom);
|
||||
|
||||
tile.latLonBounds = {
|
||||
minLat: mins[1],
|
||||
maxLat: maxs[1],
|
||||
minLon: mins[0],
|
||||
maxLon: maxs[0]
|
||||
minLat: bounds[0].lat,
|
||||
maxLat: bounds[1].lat,
|
||||
minLon: bounds[0].lon,
|
||||
maxLon: bounds[1].lon
|
||||
};
|
||||
|
||||
/* Update total bounds */
|
||||
@ -182,7 +176,6 @@ internal.prototype = {
|
||||
if (tile.latLonBounds.maxLon > this.options.bounds[1][1]) this.options.bounds[1][1] = tile.latLonBounds.maxLon;
|
||||
if (tile.latLonBounds.minLon < this.options.bounds[0][1]) this.options.bounds[0][1] = tile.latLonBounds.minLon;
|
||||
|
||||
|
||||
/* Function to find out if a point falls into this tile */
|
||||
tile.contains = function (point) {
|
||||
return point.lat < tile.latLonBounds.maxLat && point.lat > tile.latLonBounds.minLat && point.lon < tile.latLonBounds.maxLon && point.lon > tile.latLonBounds.minLon;
|
||||
@ -224,7 +217,8 @@ internal.prototype = {
|
||||
for (var pointIdx = 0; pointIdx < pointsInThisTile.length; pointIdx++) {
|
||||
var point = pointsInThisTile[pointIdx];
|
||||
|
||||
var tileXY = latLonToTileXY(point.lat, point.lon, tile.latLonBounds);
|
||||
var tileXY = mercatorUtils.latLonToTilePoint(point.lat, point.lon, tile.x, tile.y, tile.zoom);
|
||||
|
||||
var xInTile = tileXY.x;
|
||||
var yInTile = tileXY.y;
|
||||
x[pointIdx] = xInTile;
|
||||
@ -310,44 +304,4 @@ internal.prototype = {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function metersToLatLon(coord) {
|
||||
var lon = (coord[0] / (2 * Math.PI * 6378137 / 2.0)) * 180.0;
|
||||
|
||||
var lat = (coord[1] / (2 * Math.PI * 6378137 / 2.0)) * 180.0;
|
||||
lat = 180 / Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180.0)) - Math.PI / 2.0);
|
||||
|
||||
return [lon, lat]
|
||||
}
|
||||
|
||||
function tileBoundsInMeters(z, x, y) {
|
||||
var mins = pixelsToMeters(z, x * 256, (y + 1) * 256);
|
||||
var maxs = pixelsToMeters(z, (x + 1) * 256, y * 256);
|
||||
|
||||
return [mins, maxs];
|
||||
}
|
||||
|
||||
|
||||
function pixelsToMeters(z, x, y) {
|
||||
var res = (2 * Math.PI * 6378137 / 256) / (Math.pow(2, z));
|
||||
var mx = x * res - (2 * Math.PI * 6378137 / 2.0);
|
||||
var my = y * res - (2 * Math.PI * 6378137 / 2.0);
|
||||
my = -my;
|
||||
return [mx, my];
|
||||
}
|
||||
|
||||
/*
|
||||
* Convert lat and lon into pixels offsets inside the tile
|
||||
*/
|
||||
function latLonToTileXY(lat, lon, latLonBounds) {
|
||||
return {
|
||||
x: parseInt(256 * (lon - latLonBounds.minLon) / (latLonBounds.maxLon - latLonBounds.minLon)),
|
||||
y: parseInt(256 * (lat - latLonBounds.minLat) / (latLonBounds.maxLat - latLonBounds.minLat))
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = internal;
|
||||
|
Loading…
Reference in New Issue
Block a user