-
Notifications
You must be signed in to change notification settings - Fork 46
/
utils.js
35 lines (29 loc) · 923 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict'
var EARTH_RADIUS = 6371.0088;
function degToRad(degrees) {
return degrees * (Math.PI / 180);
}
function tileToLon(tileX, zoom) {
return ((tileX / 2**zoom) * 360.0) - 180.0;
}
function tileToLat(tileY, zoom) {
var n = Math.PI - 2 * Math.PI * tileY / 2**zoom;
return (180.0 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
}
/**
*
* @param {Number} zoom
* @param {Number} tileX
* @param {Number} tileY
* @returns {Number} SqKM of a given tile
*/
function calculateTileArea(zoom, tileX, tileY) {
var left = degToRad(tileToLon(tileX, zoom));
var top = degToRad(tileToLat(tileY, zoom));
var right = degToRad(tileToLon(tileX + 1, zoom));
var bottom = degToRad(tileToLat(tileY + 1, zoom));
return (Math.PI / degToRad(180)) * EARTH_RADIUS**2 * Math.abs(Math.sin(top) - Math.sin(bottom)) * Math.abs(left - right);
}
module.exports = {
calculateTileArea
};