From c7c4304567f3e4d0a75cc0850fa58e7119dc35c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Thu, 8 Mar 2012 21:56:57 +0100 Subject: [PATCH] add putDuplicateTile function --- lib/mbtiles.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/mbtiles.js b/lib/mbtiles.js index 85c87ff..fb2c900 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -562,6 +562,31 @@ MBTiles.prototype.putTile = function(z, x, y, data, callback) { else return callback(null); }; +// Inserts a duplicate tile into the MBTiles store. Scheme is XYZ. +// +// - @param {Number} z tile z coordinate +// - @param {Number} x tile x coordinate +// - @param {Number} y tile y coordinate +// - @param {Number} duplicate tile ID +// - @param {Function(err)} callback +MBTiles.prototype.putDuplicateTile = function(z, x, y, id, callback) { + if (typeof callback !== 'function') throw new Error('Callback needed'); + if (!this.open) return callback(new Error('MBTiles not yet loaded')); + if (!this._isWritable) return callback(new Error('MBTiles not in write mode')); + + // Flip Y coordinate because MBTiles files are TMS. + y = (1 << z) - 1 - y; + + // This corresponds to the map table. + var coords = hash(z, x, y); + if (!this._mapCache[coords]) this._mapCache[coords] = { z: z, x: x, y: y }; + this._mapCache[coords].tile_id = id; + + // Only commit when we can insert at least batchSize rows. + if (++this._pending >= this._batchSize) return this._commit(callback); + else return callback(null); +}; + // Inserts a grid into the MBTiles store. Scheme is XYZ. // // - @param {Number} z grid z coordinate