diff --git a/lib/mbtiles.js b/lib/mbtiles.js index 8986428..d1737f2 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -268,7 +268,7 @@ MBTiles.prototype.insertGridTiles = function(map, callback) { stmt.finalize(callback); }; -// Select a tile from an mbtiles database. +// Select a tile from an mbtiles database. Scheme is XYZ. // // - @param {Number} x tile x coordinate. // - @param {Number} y tile y coordinate. @@ -277,6 +277,9 @@ MBTiles.prototype.insertGridTiles = function(map, callback) { MBTiles.prototype.getTile = function(z, x, y, callback) { if (typeof callback !== 'function') callback = noop; + // Flip Y coordinate because MBTiles files are TMS. + y = Math.pow(2, z) - 1 - y; + var mbtiles = this; this.db.get('SELECT tile_data FROM tiles WHERE ' + 'zoom_level = ? AND tile_column = ? AND tile_row = ?', @@ -297,7 +300,7 @@ MBTiles.prototype.getTile = function(z, x, y, callback) { }); }; -// Select a grid and its data from an mbtiles database. +// Select a grid and its data from an mbtiles database. Scheme is XYZ. // // - @param {Number} x tile x coordinate // - @param {Number} y tile y coordinate @@ -306,6 +309,9 @@ MBTiles.prototype.getTile = function(z, x, y, callback) { MBTiles.prototype.getGrid = function(z, x, y, callback) { if (typeof callback !== 'function') callback = noop; + // Flip Y coordinate because MBTiles files are TMS. + y = Math.pow(2, z) - 1 - y; + var that = this; Step( function() {