Git Product home page Git Product logo

tile-stencil's Introduction

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

tile-stencil's People

Contributors

ananya-roy avatar dependabot[bot] avatar jjhembd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tile-stencil's Issues

Improve color parsing

Colors are currently parsed with csscolorparser. This dependency forces a bundling step with rollup-plugin-commonjs. Also, default and constant values are left as CSS strings, requiring a second parsing step during rendering.

Proposed changes:

  • Switch to d3-color for parsing. This is a modern, well-maintained ES module, that will eliminate the need for a build step
  • Consider d3-interpolate for interpolation? (or is this too heavy?)
  • Convert all colors (including constants and defaults) to 4-element arrays of clamped floats, suitable for use in WebGL

Improve checking of a loaded style document

Following on from #4 : we now check that:

  1. The loaded style is valid JSON
  2. The required root-level properties are present

But there are no checks whether the supplied sources or layers (for example) are conforming to the specification

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.