This commit is contained in:
Ivan Vazhenin
2024-02-23 18:18:57 +03:00
parent 03220bc2fa
commit 4edac226fc
2 changed files with 56 additions and 1 deletions

View File

@@ -77,6 +77,7 @@ function MBTiles(uri, callback) {
var mbtiles = this;
this.setMaxListeners(0);
this.filename = uri.pathname;
this.mode = mode;
this._batchSize = +uri.query.batch;
mbtiles._db = new sqlite3.Database(mbtiles.filename, mode, function(err) {
if (err) return callback(err);
@@ -96,6 +97,14 @@ MBTiles.registerProtocols = function(tilelive) {
tilelive.protocols['mbtiles:'] = MBTiles;
};
MBTiles.prototype.reInit = function () {
this._db = new sqlite3.Database(this.filename, this.mode, (err) => {
if (err) {
console.log(`Error while initialization ${this.filename}`);
}
});
};
// Finds all mbtiles file in the filepath and returns their tilesource URI.
MBTiles.list = function(filepath, callback) {
filepath = path.resolve(filepath);
@@ -208,6 +217,52 @@ MBTiles.prototype.getTile = function(z, x, y, callback) {
});
};
MBTiles.prototype.getTileT = function(t, z, x, y, callback) {
if (typeof callback !== 'function') throw new Error('Callback needed');
if (!this.open) return callback(new Error('MBTiles not yet loaded'));
// Flip Y coordinate because MBTiles files are TMS.
y = (1 << z) - 1 - y;
var sql = 'SELECT tile_data FROM tiles WHERE t_coord = ? and zoom_level = ? AND tile_column = ? AND tile_row = ?';
var mbtiles = this;
this._db.get(sql, t, z, x, y, function(err, row) {
if ((!err && !row) || (err && err.errno == 1)) {
return callback(new Error('Tile does not exist'));
} else if (err) {
return callback(err);
} else if (!row.tile_data || !Buffer.isBuffer(row.tile_data)) {
var err = new Error('Tile is invalid');
err.code = 'EINVALIDTILE';
return callback(err);
} else {
var headers = tiletype.headers(row.tile_data);
headers['Last-Modified'] = new Date(mbtiles._stat.mtime).toUTCString();
headers['ETag'] = mbtiles._stat.size + '-' + Number(mbtiles._stat.mtime);
if (process.env.BRIDGE_LOG_MAX_VTILE_BYTES_COMPRESSED) {
var tileDataLength = row.tile_data.length;
stats.count++;
stats.total = stats.total + (tileDataLength * 0.001);
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);
}
});
};
// Select a grid and its data from an mbtiles database. Scheme is XYZ.
//
// - @param {Number} z tile z coordinate

View File

@@ -1,6 +1,6 @@
{
"name": "@mapbox/mbtiles",
"version": "0.12.2",
"version": "0.15.1",
"description": "Utilities and tilelive integration for the MBTiles format.",
"url": "http://github.com/mapbox/node-mbtiles",
"author": "Mapbox (https://www.mapbox.com)",