From 4edac226fc9b7b759b6c71145aba8e0da31a3759 Mon Sep 17 00:00:00 2001 From: Ivan Vazhenin Date: Fri, 23 Feb 2024 18:18:57 +0300 Subject: [PATCH] fix --- lib/mbtiles.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/mbtiles.js b/lib/mbtiles.js index f204f39..befcf47 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -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 diff --git a/package.json b/package.json index d0fa676..7286653 100644 --- a/package.json +++ b/package.json @@ -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)",