Learn how to perform data-driven styling to customize the appearance of vector tiles based on their underlying feature data.
You can use OpenLayers to display vector tile data using custom styles. ol-mapbox-style supports the MapLibre style specification, which 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.
The ol-mapbox-style library will allow you to style data and reference the attributes of vector tiles. Create a MapLibre style object that styles land parcels according to their UseType attribute.
Create a MapLibre style object and set the root properties. Set version to 8 and include a source of type vector. Do not include other source parameters, as you have already created a source using OpenLayers.
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.
Add a second vector tile layer to the style object to customize the parcel outlines.
Add a line layer to the layers list in the style object. Set the layer ID to parcels-outline, the source to parcels, and the source-layer to Santa_Monica_Mountains_Parcels to reference the original feature service.
Use ol-mapbox-style to apply the MapLibre style object to the vector tile layer.
Call olms.applyStyle() with the parcels layer and parcels style to customize the vector tiles. Set updateSource to false to preserve the OpenLayers source you provided.
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.