add tests around mbTile SQKM calculation
This commit is contained in:
@@ -11,6 +11,7 @@ var tiletype = require('@mapbox/tiletype');
|
||||
var ZXYStream = require('./zxystream');
|
||||
var queue = require('d3-queue').queue;
|
||||
var os = require('os');
|
||||
var utils = require('./utils.js');
|
||||
|
||||
if (process.env.BRIDGE_LOG_MAX_VTILE_BYTES_COMPRESSED) {
|
||||
var stats = { max:0, total:0, count:0 };
|
||||
@@ -192,6 +193,15 @@ MBTiles.prototype.getTile = function(z, x, y, callback) {
|
||||
if (stats.max < tileDataLength) {
|
||||
stats.max = tileDataLength;
|
||||
}
|
||||
var area = stats.hasOwnProperty(z)
|
||||
? stats[z] + utils.calculateTileArea(z, x, y)
|
||||
: utils.calculateTileArea(z, x, y);
|
||||
|
||||
stats = {
|
||||
...stats,
|
||||
[z]: area
|
||||
};
|
||||
|
||||
}
|
||||
return callback(null, row.tile_data, headers);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,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
|
||||
};
|
||||
Reference in New Issue
Block a user