/** * Returns true if coordinates are within grid bounds. * * @param {array} grid * @param {Set} coordinateSet - set of [row,col] coordinates for given block */ export function isWithinGridBounds(grid, coordinateSet) { return coordinateSet.keysArray().every(([row, col]) => { return row >= 0 && row < grid.length && col >= 0 && col < grid[0].length; }); }
// Quadkey Schema async function quadkey() { const tile = '1' const db = new MBTiles('foo', 'quadkey') await db.findOne(tile) await db.save(tile, image) await db.update(options) await db.delete(tile) const tiles: string[] = await db.findAll() const hashes = await db.hashes() hashes.size const container: Container = {} container['foo'].findOne(tile) }
test('MBTiles -- plain_1', t => { const db = new MBTiles(directories.in + 'plain_1.mbtiles') Promise.all([ db.metadata().then(metadata => t.deepEqual(metadata, metadataPlain1, 'metadata')), db.tables().then(status => t.assert(status, 'tables')), db.count().then(count => t.equal(count, 285, 'count')), db.findAll().then(tiles => t.equal(tiles.length, 285, 'findAll')), db.findOne([0, 0, 0]).then(image => t.equal(image.byteLength, 7072, 'findOne')), db.findOne([15, 9, 4]).then(image => t.equal(image.byteLength, 1167, 'findOne - resolves correctly')), db.hashes().then(hashes => t.equal(hashes.size, 285, 'hashes')) ]).then(() => { t.equal(db.hash([0, 0, 0]), 1, 'hash') t.end() }) })
test('MBTiles -- hashes', t => { const db1 = new MBTiles(directories.out + 'hashes-quadkey.mbtiles', 'quadkey') const db2 = new MBTiles(directories.out + 'hashes-tms.mbtiles', 'tms') const db3 = new MBTiles(directories.out + 'hashes-xyz.mbtiles', 'xyz') Promise.all([ db1.save('021', Buffer.from([0, 1])), db2.save([1, 5, 3], Buffer.from([0, 1])), db3.save([1, 2, 3], Buffer.from([0, 1])) ]).then(() => { db1.hashes().then(hashes1 => { db2.hashes().then(hashes2 => { db3.hashes().then(hashes3 => { t.true(hashes1.has(hashes2.values().next().value), 'hashes1 contains hashes2') t.true(hashes2.has(hashes3.values().next().value), 'hashes2 contains hashes2') }) }) }) }) t.end() })
// Default (TMS/XYZ Schema) async function main() { const tile: Tile = [0, 0, 0] const db = new MBTiles(path.join(__dirname, 'test', 'in', 'plain_1.mbtiles'), 'tms') // Class properties db.db db.uri // Class methods await db.metadata() await db.count() await db.tables() await db.findAll() await db.findOne(tile) await db.index() await db.save(tile, image) await db.update(options) await db.delete(tile) const tiles: Tile[] = await db.findAll() const hashes = await db.hashes() hashes.size const container: Container = {} container['foo'].findOne(tile) // Sync functions db.metadataSync((error, metadata) => {}) db.findOneSync(tile, (error, image) => {}) }
test('MBTiles -- blank', t => { const db = new MBTiles(directories.in + 'blank.mbtiles') Promise.all([ db.metadata().then(metadata => t.deepEqual(metadata, baseMetadata, 'metadata')), db.count().then(count => t.equal(count, 0, 'count')), db.tables().then(status => t.true(status, 'tables')), db.findAll().then(tiles => t.equal(tiles.length, 0, 'findAll')), db.findOne([0, 0, 0]).then(image => t.not(image, 'findOne')), db.findOne([10, 0, 0]).then(image => t.not(image, 'findOne')), db.hashes().then(hashes => t.equal(hashes.size, 0, 'hashes')) ]) .then(() => { t.equal(db.hash([0, 0, 0]), 1, 'hash') t.end() }) })
/** * Returns true if the given block's position does not collide * with another block on the grid. * * Note: if the block is out of grid bounds, * it is technically NOT colliding with an existing block, * and so this will return true. * * @param {array} grid - matrix * @param {Set} coordinateSet - set of [row,col] coordinates for given block */ export function hasNoBlockCollisions(grid, coordinateSet) { return coordinateSet.keysArray().every(([row, col]) => { return grid[row] === undefined || grid[row][col] === null; }); }