4.0.1 • Published 7 years ago

mbtiles-offline v4.0.1

Weekly downloads
14
License
MIT
Repository
-
Last release
7 years ago

MBTiles Offline

Build Status Coverage Status npm version MIT licensed

Standard - JavaScript Style Guide

MBTiles binding for NodeJS 4+ using Callbacks and/or Promises.

Install

$ npm install --save mbtiles-offline

Usage

const MBTiles = require('mbtiles-offline')
const db = new MBTiles('example.mbtiles')

db.metadata()
//= Promise{ JSON }

db.save([1, 2, 3], Buffer([0, 1]))
db.save([2, 2, 3], Buffer([3, 4]))

db.findOne([1, 2, 3])
//= Promise { <Buffer 00 01> }

db.findAll()
//= Promise{ [[1, 2, 3], [2, 2, 3]] }

db.count()
//= Promise { 2 }

db.delete([1, 2, 3])
db.delete([2, 2, 3])

db.count()
//= Promise { 0 }

Features

NameDescription
metadataRetrieve Metadata from MBTiles
updateUpdate Metadata
saveSave buffer data to individual Tile
deleteDelete individual Tile
countCount total tiles
findOneFinds one Tile and returns buffer
findAllFinds all Tiles
tablesBuild SQL Tables
indexBuild SQL Index
getMinZoomRetrieves Minimum Zoom level
getMaxZoomRetrieves Maximum Zoom level
getFormatRetrieves Image Format
getBoundsRetrieves Bounds
validateValidate MBTiles according to the specifications
hashCreates hash from a single Tile
hashesCreates a hash table for all tiles

NodeJS Support

Windows, MacOSX, Linux & Electron

  • Node.js v8
  • Node.js v7
  • Node.js v6
  • Node.js v5
  • Node.js v4

Schemas

XYZ

Slippy Map is the most commonly used Tile schema for service maps as tiles, providers such as Google/ArcGIS & OpenStreetMap use this schema.

const tile1 = [1, 2, 3]
const tile2 = [2, 2, 3]
const tile3 = [1, 3, 3]
const tile4 = [2, 3, 3]
const img = Buffer([0, 1])
const db = new MBTiles('xyz.mbtiles', 'xyz')

db.save(tile1, img)
db.save(tile1, img)
db.findOne(tile1)
//= Promise { <Buffer 00 01> }
db.findAll()
//= Promise{ [[1, 2, 3], [2, 2, 3]] }

TMS

Tile Map Service is an OGC protocol for serving maps as tiles. MBTiles uses TMS as it's internal tile storage schema (TMS has an inverse Y compared to the XYZ schema).

const tile1 = [1, 5, 3]
const tile2 = [2, 5, 3]
const tile3 = [1, 4, 3]
const tile4 = [2, 4, 3]
const img = Buffer([0, 1])
const db = new MBTiles('tms.mbtiles', 'tms')

db.save(tile1, img)
db.save(tile2, img)
db.findOne(tile1)
//= Promise { <Buffer 00 01> }
db.findAll()
//= Promise{ [[1, 5, 3], [2, 5, 3]] }

Quadkey

Bing Map Tile System, a quadkey is defined as a string which represent a Tile x,y,z.

const tile1 = '021'
const tile2 = '030'
const tile3 = '023'
const tile4 = '032'
const img = Buffer([0, 1])
const db = new MBTiles('quadkey.mbtiles', 'quadkey')

db.save(tile1, img)
db.save(tile2, img)
db.findOne(tile1)
//= Promise { <Buffer 00 01> }
db.findAll()
//= Promise { ['021', '030'] }

API

index

MBTiles

constructor

MBTiles

Parameters

  • uri string Path to MBTiles
  • schema string Tile schema ('xyz', 'tms', 'quadkey') (optional, default 'xyz')

Examples

const db = new MBTiles('example.mbtiles')
//= mbtiles

Returns MBTiles MBTiles

save

Save buffer data to individual Tile

Parameters

Examples

db.save([x, y, z], buffer).then(status => {
  //= status
}).catch(error => {
  //= error
})

Returns Promise<boolean> true/false

metadata

Retrieves Metadata from MBTiles

Examples

db.metadata().then(metadata => {
  //= metadata
}).catch(error => {
  //= error
})

Returns Promise<Metadata> Metadata as an Object

metadataSync

Sync: Retrieves Metadata from MBTiles

Parameters

  • callback Function a method that takes (error: {Error}, metadata: {Object})

Examples

db.metadata((error, metadata) => {
  //= error
  //= metadata
})

Returns void

delete

Delete individual Tile

Parameters

Examples

db.delete([x, y, z]).then(status => {
  //= status
}).catch(error => {
  //= error
})

Returns Promise<boolean> true/false

getMinZoom

Retrieves Minimum Zoom level

Examples

db.getMinZoom().then(minZoom => {
  //= minZoom
}).catch(error => {
  //= error
})

Returns Promise<number>

getMaxZoom

Retrieves Maximum Zoom level

Examples

db.getMaxZoom().then(maxZoom => {
  //= maxZoom
}).catch(error => {
  //= error
})

Returns Promise<number>

getFormat

Retrieves Image Format

Examples

db.getFormat().then(format => {
  //= format
}).catch(error => {
  //= error
})

Returns Promise<Formats>

getBounds

Retrieves Bounds

Parameters

Examples

db.getBounds().then(bbox => {
  //= bbox
}).catch(error => {
  //= error
})

Returns Promise<BBox>

count

Count the amount of Tiles

Parameters

  • tiles Array<Tile>? Only find given tiles

Examples

db.count().then(count => {
  //= count
}).catch(error => {
  //= error
})

Returns Promise<number>

update

Update Metadata

Parameters

  • metadata Metadata Metadata according to MBTiles spec 1.1.0 (optional, default {})
    • metadata.attribution string Attribution
    • metadata.bounds BBox BBox west, south, east, north or Polygon GeoJSON
    • metadata.center Center Center lng, lat or lng, lat, height
    • metadata.description string Description
    • metadata.format Formats Format 'png' | 'jpg' | 'webp' | 'pbf'
    • metadata.minzoom number Minimum zoom level
    • metadata.maxzoom number Maximum zoom level
    • metadata.name string Name
    • metadata.url string URL source or tile scheme
    • metadata.type Types Type 'baselayer' | 'overlay' (optional, default 'baselayer')
    • metadata.version Versions Version '1.0.0' | '1.1.0' | '1.2.0' (optional, default '1.1.0')

Examples

const options = {
  name: 'Foo',
  description: 'Bar',
  minzoom: 1,
  maxzoom: 3,
  format: 'png',
  bounds: [-110, -40, 95, 50]
}
db.update(options).then(metadata => {
  //= metadata
}).catch(error => {
  //= error
})

Returns Promise<Metadata> Metadata

validate

Validate MBTiles according to the specifications

Examples

db.validate().then(status => {
  //= status
}).catch(error => {
  //= error
})

Returns Promise<boolean> true/false

findAll

Finds all Tile unique hashes

Parameters

  • tiles Array<Tile>? Only find given tiles (optional, default [])

Examples

const tile1 = [33, 40, 6]
const tile2 = [20, 50, 7]
db.findAll([tile1, tile2]).then(tiles => {
  //= tiles
}).catch(error => {
  //= error
})

Returns Promise<Array<Tile>> An array of Tiles x, y, z

findOneSync

Sync: Finds one Tile and returns Buffer

Parameters

  • tile Tile Tile x, y, z
  • callback Function a method that takes (image: {Buffer})

Examples

db.findOneSync([x, y, z], (error, image) => {
  //= error
  //= image
})

Returns void

findOne

Finds one Tile and returns Buffer

Parameters

Examples

db.findOne([x, y, z]).then(image => {
  //= image
}).catch(error => {
  //= error
})

Returns Promise<Buffer> Tile Data

tables

Build SQL tables

Examples

db.tables().then(status => {
  //= status
}).catch(error => {
  //= error
})

Returns Promise<boolean> true/false

index

Build SQL index

Examples

db.index().then(status => {
  //= status
}).catch(error => {
  //= error
})

Returns Promise<boolean> true/false

hash

Creates hash from a single Tile

Parameters

  • tile Tile

Examples

const hash = db.hash([5, 25, 12])
//= 16797721

Returns number hash

hashes

Creates a hash table for all tiles

Parameters

  • tiles Array<Tile>? Only find given tiles

Examples

await db.save([0, 0, 3], Buffer([0, 1]))
await db.save([0, 1, 3], Buffer([2, 3]))
db.hashes()
//= Promise { Set { 64, 65 } }

Returns Promise<Set<number>> hashes

4.0.1

7 years ago

4.0.0

7 years ago

3.2.0

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.9.0

7 years ago

2.8.0

7 years ago

2.7.0

7 years ago

2.6.0

7 years ago

2.5.2

7 years ago

2.5.1

7 years ago

2.5.0

7 years ago

2.3.5

7 years ago

2.3.4

7 years ago

2.3.3

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.3

7 years ago

2.2.2

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.12

7 years ago

2.0.11

7 years ago

2.0.10

7 years ago

2.0.9

7 years ago

2.0.8

7 years ago

2.0.7

7 years ago

2.0.6

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.3.5

7 years ago

1.3.4

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago