use new tilesource interface

This commit is contained in:
Konstantin Käfer
2011-06-27 15:31:56 +02:00
parent 4d8da2dbae
commit 4a8b9ad111
2 changed files with 40 additions and 40 deletions

View File

@@ -3,7 +3,6 @@ var _ = require('underscore'),
Step = require('step'),
crypto = require('crypto'),
zlib = require('zlib'),
get = require('get'),
path = require('path'),
sm = new (require('sphericalmercator')),
sqlite3 = require('sqlite3');
@@ -13,16 +12,40 @@ var _ = require('underscore'),
// MBTiles class for doing common operations (schema setup, tile reading,
// insertion, etc.)
module.exports = MBTiles;
function MBTiles(filename, callback) {
var mbtiles = this;
this.filename = filename;
this.db = new sqlite3.Database(filename, function(err) {
// Provides access to an mbtiles database file.
// - uri: A parsed URL hash, the only relevant part is `pathname`.
// - 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);
mbtiles.metadata('online', function(err, value) {
// Ignore not-existant key.
mbtiles.online = value;
if (callback) callback(null);
});
else callback(null, this);
}.bind(this));
};
// 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} z tile z coordinate.
// - @param {Function} callback
MBTiles.prototype.tile = function(x, y, z, callback) {
MBTiles.prototype.getTile = function(x, y, z, callback) {
var mbtiles = this;
this.db.get('SELECT tile_data FROM tiles WHERE ' +
'zoom_level = ? AND tile_column = ? AND tile_row = ?',
z, x, y,
function(err, row) {
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);
});
};
// 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.
//
// - @param {Number} x tile x coordinate
// - @param {Number} y tile y coordinate
// - @param {Number} z tile z coordinate
// - @param {Function} callback
MBTiles.prototype.grid = function(x, y, z, callback) {
MBTiles.prototype.getGrid = function(x, y, z, callback) {
var that = this;
Step(
function() {
@@ -312,7 +313,7 @@ MBTiles.prototype.metadata = function(key, callback) {
// Extend `MBTiles` class with an `info` method for retrieving metadata and
// performing fallback queries if certain keys (like `bounds`, `minzoom`,
// `maxzoom`) have not been provided.
MBTiles.prototype.info = function(callback) {
MBTiles.prototype.getInfo = function(callback) {
var that = this;
var info = {};
info.basename = path.basename(that.filename);
@@ -394,9 +395,9 @@ MBTiles.prototype.info = function(callback) {
// Return info
function(err) {
if (err) return callback(err);
var range = parseInt(info.maxzoom) - parseInt(info.minzoom);
info.minzoom = parseInt(info.minzoom);
info.maxzoom = parseInt(info.maxzoom);
var range = parseInt(info.maxzoom, 10) - parseInt(info.minzoom, 10);
info.minzoom = parseInt(info.minzoom, 10);
info.maxzoom = parseInt(info.maxzoom, 10);
info.bounds = _(info.bounds.split(',')).map(parseFloat);
info.center = [
(info.bounds[2] - info.bounds[0]) / 2 + info.bounds[0],