use new tilesource interface
This commit is contained in:
+40
-39
@@ -3,7 +3,6 @@ var _ = require('underscore'),
|
|||||||
Step = require('step'),
|
Step = require('step'),
|
||||||
crypto = require('crypto'),
|
crypto = require('crypto'),
|
||||||
zlib = require('zlib'),
|
zlib = require('zlib'),
|
||||||
get = require('get'),
|
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
sm = new (require('sphericalmercator')),
|
sm = new (require('sphericalmercator')),
|
||||||
sqlite3 = require('sqlite3');
|
sqlite3 = require('sqlite3');
|
||||||
@@ -13,16 +12,40 @@ var _ = require('underscore'),
|
|||||||
// MBTiles class for doing common operations (schema setup, tile reading,
|
// MBTiles class for doing common operations (schema setup, tile reading,
|
||||||
// insertion, etc.)
|
// insertion, etc.)
|
||||||
module.exports = MBTiles;
|
module.exports = MBTiles;
|
||||||
function MBTiles(filename, callback) {
|
|
||||||
var mbtiles = this;
|
// Provides access to an mbtiles database file.
|
||||||
this.filename = filename;
|
// - uri: A parsed URL hash, the only relevant part is `pathname`.
|
||||||
this.db = new sqlite3.Database(filename, function(err) {
|
// - callback: Will be called when the resources have been acquired
|
||||||
|
// or acquisition failed.
|
||||||
|
function MBTiles(uri, callback) {
|
||||||
|
this.filename = uri.pathname;
|
||||||
|
this.db = new sqlite3.cached.Database(uri.pathname, function(err) {
|
||||||
if (err) return callback(err);
|
if (err) return callback(err);
|
||||||
mbtiles.metadata('online', function(err, value) {
|
else callback(null, this);
|
||||||
// Ignore not-existant key.
|
}.bind(this));
|
||||||
mbtiles.online = value;
|
};
|
||||||
if (callback) callback(null);
|
|
||||||
});
|
// Finds all mbtiles file in the filepath and returns their tilesource URI.
|
||||||
|
MBTiles.list = function(filepath, callback) {
|
||||||
|
filepath = path.resolve(filepath);
|
||||||
|
fs.readdir(filepath, function(err, files) {
|
||||||
|
if (err) return callback(err);
|
||||||
|
for (var result = {}, i = 0; i < files.length; i++) {
|
||||||
|
var name = files[i].match(/^([\w-]+)\.mbtiles$/);
|
||||||
|
if (name) result[name[1]] = 'mbtiles://' + path.join(filepath, name[0]);
|
||||||
|
}
|
||||||
|
callback(null, result);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Finds an mbtiles file with the given ID in the filepath and returns a
|
||||||
|
// tilesource URI.
|
||||||
|
MBTiles.findID = function(filepath, id, callback) {
|
||||||
|
filepath = path.resolve(filepath);
|
||||||
|
var file = path.join(filepath, id + '.mbtiles');
|
||||||
|
fs.stat(file, function(err, stats) {
|
||||||
|
if (err) callback(err);
|
||||||
|
else callback(null, 'mbtiles://' + file);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -219,47 +242,25 @@ MBTiles.prototype.insertGridTiles = function(map, callback) {
|
|||||||
// - @param {Number} y tile y coordinate.
|
// - @param {Number} y tile y coordinate.
|
||||||
// - @param {Number} z tile z coordinate.
|
// - @param {Number} z tile z coordinate.
|
||||||
// - @param {Function} callback
|
// - @param {Function} callback
|
||||||
MBTiles.prototype.tile = function(x, y, z, callback) {
|
MBTiles.prototype.getTile = function(x, y, z, callback) {
|
||||||
var mbtiles = this;
|
var mbtiles = this;
|
||||||
this.db.get('SELECT tile_data FROM tiles WHERE ' +
|
this.db.get('SELECT tile_data FROM tiles WHERE ' +
|
||||||
'zoom_level = ? AND tile_column = ? AND tile_row = ?',
|
'zoom_level = ? AND tile_column = ? AND tile_row = ?',
|
||||||
z, x, y,
|
z, x, y,
|
||||||
function(err, row) {
|
function(err, row) {
|
||||||
if (err) callback(err);
|
if (err) callback(err);
|
||||||
else if (!row || !row.tile_data) mbtiles.onlineTile(x, y, z, callback);
|
else if (!row || !row.tile_data) callback(new Error('Tile does not exist'));
|
||||||
else callback(null, row.tile_data);
|
else callback(null, row.tile_data);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Requests a tile from an online source and cache it to the database.
|
|
||||||
//
|
|
||||||
// - @param {Number} x tile x coordinate.
|
|
||||||
// - @param {Number} y tile y coordinate.
|
|
||||||
// - @param {Number} z tile z coordinate.
|
|
||||||
// - @param {Function} callback
|
|
||||||
MBTiles.prototype.onlineTile = function(x, y, z, callback) {
|
|
||||||
var mbtiles = this;
|
|
||||||
if (!this.online) {
|
|
||||||
return callback(new Error('Tile does not exist'));
|
|
||||||
}
|
|
||||||
|
|
||||||
var url = this.online.replace(/%x/g, x).replace(/%y/g, Math.pow(2, z) - 1 - y).replace(/%z/g, z);
|
|
||||||
new get(url).asBuffer(function(err, result) {
|
|
||||||
if (err) return callback(new Error(err));
|
|
||||||
|
|
||||||
// Cache image.
|
|
||||||
mbtiles.insertTiles([{ x: x, y: y, z: z, data: result }], function() {});
|
|
||||||
callback(null, result);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Select a grid and its data from an mbtiles database.
|
// Select a grid and its data from an mbtiles database.
|
||||||
//
|
//
|
||||||
// - @param {Number} x tile x coordinate
|
// - @param {Number} x tile x coordinate
|
||||||
// - @param {Number} y tile y coordinate
|
// - @param {Number} y tile y coordinate
|
||||||
// - @param {Number} z tile z coordinate
|
// - @param {Number} z tile z coordinate
|
||||||
// - @param {Function} callback
|
// - @param {Function} callback
|
||||||
MBTiles.prototype.grid = function(x, y, z, callback) {
|
MBTiles.prototype.getGrid = function(x, y, z, callback) {
|
||||||
var that = this;
|
var that = this;
|
||||||
Step(
|
Step(
|
||||||
function() {
|
function() {
|
||||||
@@ -312,7 +313,7 @@ MBTiles.prototype.metadata = function(key, callback) {
|
|||||||
// Extend `MBTiles` class with an `info` method for retrieving metadata and
|
// Extend `MBTiles` class with an `info` method for retrieving metadata and
|
||||||
// performing fallback queries if certain keys (like `bounds`, `minzoom`,
|
// performing fallback queries if certain keys (like `bounds`, `minzoom`,
|
||||||
// `maxzoom`) have not been provided.
|
// `maxzoom`) have not been provided.
|
||||||
MBTiles.prototype.info = function(callback) {
|
MBTiles.prototype.getInfo = function(callback) {
|
||||||
var that = this;
|
var that = this;
|
||||||
var info = {};
|
var info = {};
|
||||||
info.basename = path.basename(that.filename);
|
info.basename = path.basename(that.filename);
|
||||||
@@ -394,9 +395,9 @@ MBTiles.prototype.info = function(callback) {
|
|||||||
// Return info
|
// Return info
|
||||||
function(err) {
|
function(err) {
|
||||||
if (err) return callback(err);
|
if (err) return callback(err);
|
||||||
var range = parseInt(info.maxzoom) - parseInt(info.minzoom);
|
var range = parseInt(info.maxzoom, 10) - parseInt(info.minzoom, 10);
|
||||||
info.minzoom = parseInt(info.minzoom);
|
info.minzoom = parseInt(info.minzoom, 10);
|
||||||
info.maxzoom = parseInt(info.maxzoom);
|
info.maxzoom = parseInt(info.maxzoom, 10);
|
||||||
info.bounds = _(info.bounds.split(',')).map(parseFloat);
|
info.bounds = _(info.bounds.split(',')).map(parseFloat);
|
||||||
info.center = [
|
info.center = [
|
||||||
(info.bounds[2] - info.bounds[0]) / 2 + info.bounds[0],
|
(info.bounds[2] - info.bounds[0]) / 2 + info.bounds[0],
|
||||||
|
|||||||
@@ -27,7 +27,6 @@
|
|||||||
"sqlite3": "2.0.x",
|
"sqlite3": "2.0.x",
|
||||||
"step": "0.0.x",
|
"step": "0.0.x",
|
||||||
"sphericalmercator": "1.0.x",
|
"sphericalmercator": "1.0.x",
|
||||||
"get": "0.3.x",
|
|
||||||
"underscore": "1.1.x",
|
"underscore": "1.1.x",
|
||||||
"zlib": "1.0.x"
|
"zlib": "1.0.x"
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user