add docs and remove old write methods
This commit is contained in:
193
lib/mbtiles.js
193
lib/mbtiles.js
@@ -125,162 +125,11 @@ MBTiles.prototype._setup = function(callback) {
|
||||
});
|
||||
};
|
||||
|
||||
// // Generic object insert.
|
||||
// //
|
||||
// // - `table` String. The table to which objects should be inserted.
|
||||
// // - `objects` Array. Objects to be inserted, where each object attribute
|
||||
// // has key/value pairs as a hash corresponding to column name and row value.
|
||||
// // - `callback` Function.
|
||||
// MBTiles.prototype._insert = function(table, objects, callback) {
|
||||
// if (typeof callback !== 'function') callback = noop;
|
||||
//
|
||||
// if (!objects.length) return callback(null);
|
||||
// var keys = _(objects[0]).keys();
|
||||
// var placeholders = [];
|
||||
// _(keys).each(function(k) { placeholders.push('?'); });
|
||||
// var stmt = this._db.prepare(
|
||||
// 'INSERT OR IGNORE INTO ' + table + ' ' +
|
||||
// '(' + keys.join(',') + ') ' +
|
||||
// 'VALUES (' + placeholders.join(',') + ')'
|
||||
// );
|
||||
// for (var i = 0; i < objects.length; i++) {
|
||||
// stmt.run.apply(stmt, _(objects[i]).values());
|
||||
// }
|
||||
// stmt.finalize(callback);
|
||||
// };
|
||||
//
|
||||
// // Insert metadata into the mbtiles database.
|
||||
// //
|
||||
// // - @param {Object} metadata key, value hash of metadata to be inserted.
|
||||
// // - @param {Function} callback
|
||||
// MBTiles.prototype._insertMetadata = function(data, callback) {
|
||||
// if (typeof callback !== 'function') callback = noop;
|
||||
//
|
||||
// var metadata = _(data).map(function(value, key) {
|
||||
// return { name: key, value: value};
|
||||
// });
|
||||
// this._insert('metadata', metadata, callback);
|
||||
// };
|
||||
//
|
||||
// // Insert a set of tiles into an mbtiles database. Scheme is TMS.
|
||||
// //
|
||||
// // - @param {Array} renders array of images to be inserted. Each item should
|
||||
// // be an object of the form { z: z, x: x, y: y, data: [Image buffer] }.
|
||||
// // - @param {Function} callback
|
||||
// MBTiles.prototype._insertTiles = function(data, callback) {
|
||||
// var that = this,
|
||||
// map = [],
|
||||
// images = [],
|
||||
// ids = [];
|
||||
// for (var i = 0; i < data.length; i++) {
|
||||
// var tile_id = crypto
|
||||
// .createHash('md5')
|
||||
// .update(data[i].data)
|
||||
// .digest('hex');
|
||||
// !_(ids).include(tile_id) && ids.push(tile_id) && images.push({
|
||||
// tile_id: tile_id,
|
||||
// tile_data: data[i].data
|
||||
// });
|
||||
// map.push({
|
||||
// tile_id: tile_id,
|
||||
// zoom_level: data[i].z,
|
||||
// tile_column: data[i].x,
|
||||
// tile_row: data[i].y
|
||||
// });
|
||||
// }
|
||||
// Step(
|
||||
// function() {
|
||||
// var group = this.group();
|
||||
// that._insert('images', images, group());
|
||||
// that._insert('map', map, group());
|
||||
// },
|
||||
// callback
|
||||
// );
|
||||
// };
|
||||
//
|
||||
// // Insert a set of grids into an mbtiles database.
|
||||
// //
|
||||
// // - @param {Array} renders array of grids to be inserted. Each item should
|
||||
// // be an object of the form { z: z, x: x, y: y, data: [Image buffer], keys: [] }.
|
||||
// // - @param {Function} callback
|
||||
MBTiles.prototype._insertGrids = function(data, callback) {
|
||||
if (typeof callback !== 'function') callback = noop;
|
||||
|
||||
var that = this,
|
||||
map = [],
|
||||
grids = [],
|
||||
grid_keys = [],
|
||||
features = {},
|
||||
ids = [];
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
var json = JSON.stringify({
|
||||
grid: data[i].grid,
|
||||
keys: data[i].keys
|
||||
});
|
||||
var grid_id = crypto
|
||||
.createHash('md5')
|
||||
.update(json)
|
||||
.digest('hex');
|
||||
!_(ids).include(grid_id) && ids.push(grid_id) && grids.push({
|
||||
grid_id: grid_id,
|
||||
grid_utfgrid: zlib.deflate(new Buffer(json, 'utf8'))
|
||||
});
|
||||
data[i].keys.forEach(function(k) {
|
||||
grid_keys.push({
|
||||
grid_id: grid_id,
|
||||
key_name: k
|
||||
});
|
||||
});
|
||||
map.push({
|
||||
grid_id: grid_id,
|
||||
zoom_level: data[i].z,
|
||||
tile_column: data[i].x,
|
||||
tile_row: data[i].y
|
||||
});
|
||||
_(features).extend(data[i].data);
|
||||
}
|
||||
features = _(features).map(function(value, key) {
|
||||
return { key_name: key, key_json: JSON.stringify(value) };
|
||||
});
|
||||
Step(
|
||||
function() {
|
||||
var group = this.group();
|
||||
that._insert('grid_utfgrid', grids, group());
|
||||
that._insert('grid_key', grid_keys, group());
|
||||
that._insert('keymap', features, group());
|
||||
that._insertGridTiles(map, group());
|
||||
},
|
||||
callback
|
||||
);
|
||||
};
|
||||
//
|
||||
// // Insert grids into the mbtiles database.
|
||||
// //
|
||||
// // - @param {Object} tile tile object to be inserted.
|
||||
// // - @param {Function} callback
|
||||
// MBTiles.prototype._insertGridTiles = function(map, callback) {
|
||||
// if (typeof callback !== 'function') callback = noop;
|
||||
//
|
||||
// var stmt = this._db.prepare('UPDATE OR REPLACE map SET grid_id = ? WHERE ' +
|
||||
// ' zoom_level = ? AND tile_column = ? AND tile_row = ?');
|
||||
//
|
||||
// for (var i = 0; i < map.length; i++) {
|
||||
// stmt.run(
|
||||
// map[i].grid_id,
|
||||
// map[i].zoom_level,
|
||||
// map[i].tile_column,
|
||||
// map[i].tile_row
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// stmt.finalize(callback);
|
||||
// };
|
||||
|
||||
// Select a tile from an mbtiles database. Scheme is XYZ.
|
||||
//
|
||||
// - @param {Number} z tile z coordinate.
|
||||
// - @param {Number} x tile x coordinate.
|
||||
// - @param {Number} y tile y coordinate.
|
||||
// - @param {Number} z tile z coordinate.
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.getTile = function(z, x, y, callback) {
|
||||
if (typeof callback !== 'function') throw new Error('Callback needed');
|
||||
@@ -311,9 +160,9 @@ MBTiles.prototype.getTile = function(z, x, y, callback) {
|
||||
|
||||
// Select a grid and its data from an mbtiles database. Scheme is XYZ.
|
||||
//
|
||||
// - @param {Number} z tile z coordinate
|
||||
// - @param {Number} x tile x coordinate
|
||||
// - @param {Number} y tile y coordinate
|
||||
// - @param {Number} z tile z coordinate
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.getGrid = function(z, x, y, callback) {
|
||||
if (typeof callback !== 'function') throw new Error('Callback needed');
|
||||
@@ -487,6 +336,9 @@ MBTiles.prototype.getInfo = function(callback) {
|
||||
});
|
||||
};
|
||||
|
||||
// Puts the MBTiles tilestore into write mode.
|
||||
//
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.startWriting = function(callback) {
|
||||
if (typeof callback !== 'function') throw new Error('Callback needed');
|
||||
if (!this.open) return callback(new Error('MBTiles not yet loaded'));
|
||||
@@ -510,12 +362,10 @@ MBTiles.prototype.startWriting = function(callback) {
|
||||
}
|
||||
};
|
||||
|
||||
// (private) Commits the cached changes to the database.
|
||||
//
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype._commit = function(callback) {
|
||||
// Only commit when we can insert at least 100 rows.
|
||||
if (++this._pending < 100) {
|
||||
return callback(null);
|
||||
}
|
||||
|
||||
var mbtiles = this;
|
||||
mbtiles._db.serialize(function() {
|
||||
mbtiles._db.run('BEGIN');
|
||||
@@ -601,6 +451,9 @@ MBTiles.prototype._commit = function(callback) {
|
||||
});
|
||||
};
|
||||
|
||||
// Leaves write mode.
|
||||
//
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.stopWriting = function(callback) {
|
||||
if (typeof callback !== 'function') throw new Error('Callback needed');
|
||||
if (!this.open) return callback(new Error('MBTiles not yet loaded'));
|
||||
@@ -617,7 +470,13 @@ MBTiles.prototype.stopWriting = function(callback) {
|
||||
});
|
||||
};
|
||||
|
||||
// Insert a tile. Scheme is XYZ.
|
||||
// Inserts a 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 {Buffer} buffer tile image data
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.putTile = function(z, x, y, data, callback) {
|
||||
if (typeof callback !== 'function') throw new Error('Callback needed');
|
||||
if (!this.open) return callback(new Error('MBTiles not yet loaded'));
|
||||
@@ -639,10 +498,18 @@ MBTiles.prototype.putTile = function(z, x, y, data, callback) {
|
||||
if (!this._mapCache[coords]) this._mapCache[coords] = { z: z, x: x, y: y };
|
||||
this._mapCache[coords].tile_id = id;
|
||||
|
||||
this._commit(callback);
|
||||
// Only commit when we can insert at least 100 rows.
|
||||
if (++this._pending < 100) return this._commit(callback);
|
||||
else return callback(null);
|
||||
};
|
||||
|
||||
// Insert a tile. Scheme is XYZ.
|
||||
// Inserts a grid into the MBTiles store. Scheme is XYZ.
|
||||
//
|
||||
// - @param {Number} z grid z coordinate
|
||||
// - @param {Number} x grid x coordinate
|
||||
// - @param {Number} y grid y coordinate
|
||||
// - @param {Object} data grid object
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.putGrid = function(z, x, y, data, callback) {
|
||||
if (typeof callback !== 'function') throw new Error('Callback needed');
|
||||
if (!this.open) return callback(new Error('MBTiles not yet loaded'));
|
||||
@@ -669,5 +536,7 @@ MBTiles.prototype.putGrid = function(z, x, y, data, callback) {
|
||||
if (!this._mapCache[coords]) this._mapCache[coords] = { z: z, x: x, y: y };
|
||||
this._mapCache[coords].grid_id = id;
|
||||
|
||||
this._commit(callback);
|
||||
// Only commit when we can insert at least 100 rows.
|
||||
if (++this._pending < 100) return this._commit(callback);
|
||||
else return callback(null);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user