tile-retriever

0.0.7 • Public • Published

tile-retriever

tests

Load vector tiles from sources described in MapLibre style documents

tile-retriever inputs a source object from a MapLibre style document, and constructs a tile retriever function.

The retriever function inputs a set of tile coordinates of the form { z, x, y } and a callback function. It then retrieves the tile from the endpoints specified in the source, and executes the callback function with the tile's layers and features as a dictionary of GeoJSON FeatureCollections.

Using tile-retriever, you can play with the basic demo, build your own GIS code from scratch, or anything in between!

Initialization

A new tile retriever function is instantiated as follows:

import * as tileRetriever from "tile-retriever";

const retrieve = tileRetriever.init({ source, defaultID });

where the initialization parameters are:

  • .source (REQUIRED): A MapLibre source object. MUST be of type vector or geojson, with all external information (TileJSON description, or GeoJSON data) already retrieved and present in the object (NOT linked via URLs). You can ensure the data is already retrieved by loading the style with the .loadStyle method from tile-stencil
  • .defaultID: Only relevant for GeoJSON sources. Will be used as the layer name for the retrieved GeoJSON

Requesting a tile

The retriever function has the following signature:

const request = retrieve(tileCoords, callback);

where tileCoords has properties { z, x, y }, corresponding to the indices of the desired tile.

If you call the retrieve function, and then decide you don't need the tile, you can abort the request using the returned request object:

request.abort();

Handling the returned tile data

The callback supplied to the retrieve function has the following signature

function callback(error, json) {
  if (error) throw error;

  console.log(json);
}

The returned JSON data is a dictionary of layers, keyed on the original name from the source vector tile layer. (For geojson sources, there will only be one layer, with the key being the supplied defaultID.) The value of each layer is a GeoJSON FeatureCollection.

Example:

{
  "layer1": { "type": "FeatureCollection", "features": [...] },
  "layer2": { "type": "FeatureCollection", "features": [...] },
  ...
}

Package Sidebar

Install

npm i tile-retriever

Weekly Downloads

2

Version

0.0.7

License

MIT

Unpacked Size

167 kB

Total Files

6

Last publish

Collaborators

  • jjhembd