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

tile-stencil

v0.4.12

Published

Load a MapLibre style document and parse it into Javascript functions

Downloads

80

Readme

tile-stencil

tests

Load a MapLibre style document and parse it into Javascript functions

MapLibre style documents describe how a map should be drawn. The document begins with input information, such as:

  • Data sources to be used (tiles, GeoJSON, etc.)
  • Where to get sprites (small images used as labels)

Then, it specifies a list of layers, in the order in which they should be drawn. Layers further down the list are drawn on top of the earlier layers. For each layer, the style document describes:

  • Which data source to use
  • A data filter, to select features from the source to draw in this layer
  • What style properties (colors, line thicknesses, fonts, etc) to use when drawing

The style properties (colors, etc) are specified as functions—i.e., they can vary depending on the zoom level or some property of the feature.

tile-stencil reads the document, loads relevant data about the source, loads the sprite data, and parses the specified filters and property functions into Javascript functions that can be used by a renderer.

Installation

tile-stencil is provided as an ESM import.

import * as tileStencil from 'tile-stencil';

tileStencil exposes three methods:

  • getStyleFuncs
  • loadStyle
  • buildFeatureFilter

getStyleFuncs

Syntax

const parsedLayer = tileStencil.getStyleFuncs(inputLayer);

Parameters

  • layer: An element from the [layers][] property of a MapLibre style document

Return value

A copy of inputLayer, where the .layout and .paint properties have been replaced by value getter dictionaries

Structure of returned .layout and .paint objects

The returned objects can be used to retrieve style properties as follows (where our example layer has layer.type === "line"):

var lineColor = paint["line-color"](zoom, feature);

where zoom is the zoom level of the map being drawn, and feature is the feature being drawn.

Some styles do not depend on feature properties, or even the zoom level. Each .layout and .paint property function has a defined .type, e.g.,

styleFunctionType = paint["style-property"].type;

where .type may take one of three values:

  • constant: Defines a style property that does not vary
  • zoom: Style value depends on the map zoom level
  • property: Style value depends on feature properties

loadStyle

Loads a style document and any linked information.

You can test this method live using the validator example.

Syntax

const loadedStyle = loadStyle(styleDoc, mapboxToken);

Parameters

  • styleDoc: A MapLibre Style document, OR a URL pointing to a style document
  • mapboxToken: Your Mapbox API key (Optional). This is only needed if your style document includes references to Mapbox-hosted resources, such as TileJSON or sprite data.

Return value

A Promise that resolves to a parsed style document.

The parsed document will have the following changes relative to the input:

  • styleDoc.sources: If a source was specified as a URL pointing to a TileJSON document, the properties of that source will be augmented by properties retrieved from the linked document
  • styleDoc.spriteData: This additional object contains the data pointed to by the URL in styleDoc.sprite. .spriteData has two properties:
    • .spriteData.image: A PNG image file containing the sprite data
    • .spriteData.meta: The JSON document containing the description of each image contained in the sprite
  • styleDoc.layers: Some MapLibre styles have non-standard layers that do not list all of the required properties, but rather 'reference' these properties from another layer. The layers in the parsed document will have these references resolved, so that the returned document is standards-compliant.

buildFeatureFilter

Converts the filter description from a MapLibre layer into a JavaScript function for filtering GeoJSON features.

The returned function can be used to filter features to the appropriate subset to be used in rendering this layer, e.g.,

const parsedFilter = buildFeatureFilter(layer.filter);
layerFeatures = features.filter(parsedFilter);

Note: the supplied filter description MUST follow the deprecated syntax!

Un-supported features

tile-stencil does not implement the following features of the style specification:

While expressions are not yet implemented, tile-stencil does implement the following older features:

  • functions for describing the dependence of a style value on zoom level or feature properties. But note: zoom-and-property functions are not implemented!
  • filters for defining the subset of a source-layer's features to be used in the current layer