What is @mapbox/mapbox-gl-style-spec?
@mapbox/mapbox-gl-style-spec is a library for working with Mapbox GL style specifications. It allows you to validate, format, and manipulate Mapbox GL styles, which are used to define the visual appearance of maps rendered with Mapbox GL JS.
What are @mapbox/mapbox-gl-style-spec's main functionalities?
Style Validation
This feature allows you to validate a Mapbox GL style JSON object to ensure it conforms to the Mapbox GL style specification. The `validate` function returns an array of errors if the style is invalid.
const validate = require('@mapbox/mapbox-gl-style-spec').validate;
const style = { /* your style JSON here */ };
const errors = validate(style);
if (errors.length) {
console.error('Style has errors:', errors);
} else {
console.log('Style is valid!');
}
Style Formatting
This feature allows you to format a Mapbox GL style JSON object to make it more readable. The `format` function returns a formatted version of the style.
const format = require('@mapbox/mapbox-gl-style-spec').format;
const style = { /* your style JSON here */ };
const formattedStyle = format(style);
console.log('Formatted Style:', formattedStyle);
Style Manipulation
This feature allows you to migrate a Mapbox GL style JSON object to the latest version of the style specification. The `migrate` function returns the migrated style.
const migrate = require('@mapbox/mapbox-gl-style-spec').migrate;
const style = { /* your style JSON here */ };
const migratedStyle = migrate(style);
console.log('Migrated Style:', migratedStyle);
Other packages similar to @mapbox/mapbox-gl-style-spec
mapbox-gl
The `mapbox-gl` package is the core library for rendering interactive maps using WebGL. While it includes functionalities for working with styles, it is more focused on rendering and interaction, whereas `@mapbox/mapbox-gl-style-spec` is specifically for style validation and manipulation.
mapbox-gl-utils
The `mapbox-gl-utils` package provides utility functions for working with Mapbox GL JS, including style manipulation and layer management. It offers a broader range of utilities compared to `@mapbox/mapbox-gl-style-spec`, which is more focused on style validation and formatting.
Mapbox GL style specification & utilities
This directory contains code and reference files that define the Mapbox GL style specification and provides some utilities for working with Mapbox styles.
npm package
The Mapbox GL style specification and utilities are published as a seperate npm
package so that they can be installed without the bulk of GL JS.
npm install @mapbox/mapbox-gl-style-spec
CLI Tools
If you install this package globally, you will have access to several CLI tools.
npm install @mapbox/mapbox-gl-style-spec --global
gl-style-composite
$ gl-style-composite style.json
Will take a non-composited style and produce a composite style.
gl-style-migrate
This repo contains scripts for migrating GL styles of any version to the latest version
(currently v8). Migrate a style like this:
$ gl-style-migrate bright-v7.json > bright-v8.json
To migrate a file in place, you can use the sponge
utility from the moreutils
package:
$ brew install moreutils
$ gl-style-migrate bright.json | sponge bright.json
gl-style-format
$ gl-style-format style.json
Will format the given style JSON to use standard indentation and sorted object keys.
gl-style-validate
$ gl-style-validate style.json
Will validate the given style JSON and print errors to stdout. Provide a
--json
flag to get JSON output.
To validate that a style can be uploaded to the Mapbox Styles API, use the --mapbox-api-supported
flag.