Learn how to perform data-driven styling to customize the appearance of vector tiles based on their underlying feature data.
You can use MapLibre GL JS to display vector tile data using custom styles. The MapLibre style specification allows you to customize the fill, outline, opacity and other properties of vector tiles to display data effectively. If your vector tiles are published from a feature service, you can also perform data-driven visualizations based on attributes of the original feature service.
In this tutorial, you style land parcels from a public vector tile service according to their use type.
This tutorial uses the Santa Monica Mountains Parcels vector tile service. This vector tile service was created by publishing a feature service as vector tiles using ArcGIS.com. It contains the attributes of the original feature service, which can be accessed to style layers in your application. Find the original feature service in ArcGIS.com to view the names and values of different attributes.
Under Details, find the Created from property. Follow the link to view the item page for the original feature layer, Santa_Monica_Mountains_Parcels.
Click the Data tab to view the layer's features and attributes. Each feature represents a land parcel and has attributes such as an address, use code, and number of square feet.
Review the values of the UseType field. You will use this field to style vector tiles in your application.
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.
In CodePen, update apiKey to use your key.
Use dark colors for code blocks
Change line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const apiKey = "YOUR_API_KEY";
const basemapEnum = "arcgis/streets";
const map = new maplibregl.Map({
container: "map", // the id of the div elementstyle: `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapEnum}?token=${apiKey}`,
zoom: 12, // starting zoomcenter: [-118.805, 34.027] // starting location [longitude, latitude]});
Inside the load event handler, add a vector tile source with id parcels. The source tells MapLibre GL JS how to access the data for the layer, but does not visually add it to the map.
Use a layer of type fill to style land parcels according to their UseType category.
Add a fill layer that references the parcels source you created. Set source-layer to Santa_Monica_Mountains_Parcels to reference the original feature service.
Add a custom fill-color to the layer. Use get to retrieve UseType attribute values, viewed in the previous step. Use case to create a conditional expression that assigns a unique color to each use type.
The MapLibre style specification has limited options for customizing polygon outlines. Add a second vector tile layer to customize the parcel outlines.
Add a line layer that also references the parcels source. Style the color, width, and opacity of the parcel outlines.
You should see the styled vector tile layer with parcels displayed on the basemap layer. Parcels shouold be styled in different colors based on their use type, and thin outlines should be present around each parcel.