npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-mbtiles

v1.0.2

Published

基于Mapbox Mbtiles扩展使其async/await封装,方便集成开发,又支持callback,兼容原来版本

Downloads

31

Readme

mbtiles

Node.js utilities and tilelive integration for the MBTiles format.

Build Status Build status

Installation

npm install node-mbtiles
var MBTiles = require('node-mbtiles');

API

Constructor

All MBTiles instances need to be constructed before any of the methods become available. NOTE: All methods described below assume you've taken this step.

let mbtiles=await new MBTiles('./path/to/file.mbtiles?mode={ro, rw, rwc}');

The mode query parameter is a opening flag of mbtiles. It is optional, default as rwc. Available flags are:

  • ro: readonly mode, will throw error if the mbtiles is not existed.
  • rw: read and write mode, will throw error if the mbtiles is not existed.
  • rwc: read, write and create mode, will create a new mbtiles if the mbtiles is not existed.

Reading

async getTile(z, x, y)

Get an individual tile from the MBTiles table. This can be a raster or gzipped vector tile. Also returns headers that are important for serving over HTTP. 原来@mapbox的callback(null,tile,headers) 更改为 callback(null,{ tile:tile, headers:headers });

let tileInfo=await mbtiles.getTile(z, x, y);

async getInfo()

Get info of an MBTiles file, which is stored in the metadata table. Includes information like zoom levels, bounds, vector_layers, that were created during generation. This performs fallback queries if certain keys like bounds, minzoom, or maxzoom have not been provided.

let info=await mbtiles.getInfo();

async getGrid(z, x, y)

Get a UTFGrid tile from the MBTiles table. 原来@mapbox的callback(null,grid,headers) 更改为 callback(null,{ grid:grid, headers:headers });

let gridInfo=await mbtiles.getGrid(z, x, y);

Writing

async startWriting AND async stopWriting

In order to write a new (or currently existing) MBTiles file you need to "start" and "stop" writing. First, construct the MBTiles object.

await mbtiles.startWriting();
await mbtiles.stopWriting();

async putTile(z, x, y, buffer)

Add a new tile buffer to a specific ZXY. This can be a raster tile or a gzipped vector tile (we suggest using require('zlib') to gzip your tiles).

var zlib = require('zlib');

zlib.gzip(fs.readFileSync('./path/to/file.mvt'), async function(err, buffer) {
  await mbtiles.putTile(0, 0, 0, buffer);
});

async putInfo(data)

Put an information object into the metadata table. Any nested JSON will be stringified and stored in the "json" row of the metadata table. This will replace any matching key/value fields in the table.

var exampleInfo = {
  "name": "hello-world",
  "description":"the world in vector tiles",
  "format":"pbf",
  "version": 2,
  "minzoom": 0,
  "maxzoom": 4,
  "center": "0,0,1",
  "bounds": "-180.000000,-85.051129,180.000000,85.051129",
  "type": "overlay",
  "json": `{"vector_layers": [ { "id": "${layername}", "description": "", "minzoom": 0, "maxzoom": 4, "fields": {} } ] }`
};

await mbtiles.putInfo(exampleInfo);

async putGrid(z, x, y, grid)

Inserts a UTFGrid tile into the MBTiles store. Grids are in JSON format.

var fs = require('fs');
var grid = JSON.parse(fs.readFileSync('./path/to/grid.json', 'utf8'));
await mbtiles.putGrid(0, 0, 0, grid);

Hook up to tilelive

When working at scale, node-mbtiles is meant to be used within a Tilelive ecosystem. For example, you could set up an MBTiles file as a "source" and an S3 destination as a "sink" (using tilelive-s3). Assuming you have a system set up with an mbtiles:// protocol that points to a specific file and authorized to write to the s3 bucket:

var tilelive = require('@mapbox/tilelive');
var MBTiles = require('@mapbox/mbtiles');
var s3 = require('@mapbox/tilelive-s3');

s3.registerProtocols(tilelive);
MBTiles.registerProtocols(tilelive);

var sourceUri = 'mbtiles:///User/hello/path/to/file.mbtiles';
var sinkUri = 's3://my-bucket/tiles/{z}/{x}/{y}';

// load the mbtiles source
tilelive.load(sourceUri, function(err, src) {
  // load the s3 sink
  tilelive.load(sinkUri, function(err, dest) {

    var options = {}; // prepare options for tilelive copy
    options.listScheme = src.createZXYStream(); // create ZXY stream from mbtiles

    // now copy all tiles to the destination
    tilelive.copy(src, dst, options, function(err) {
      console.log('tiles are now on s3!');
    });
  });
});

Test

npm test