From 4a8b9ad111bb9214e93221b4f2a3f6e04c473b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 27 Jun 2011 15:31:56 +0200 Subject: [PATCH] use new tilesource interface --- lib/mbtiles.js | 79 +++++++++++++++++++++++++------------------------- package.json | 1 - 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/lib/mbtiles.js b/lib/mbtiles.js index 5dc0b45..22bcd43 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -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], diff --git a/package.json b/package.json index 0083842..8a17dbd 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "sqlite3": "2.0.x", "step": "0.0.x", "sphericalmercator": "1.0.x", - "get": "0.3.x", "underscore": "1.1.x", "zlib": "1.0.x" },