Learn how to use data-driven styling to apply symbol colors and styles to feature layers.
A feature layer is a dataset in a feature service hosted in ArcGIS. Each feature layer contains features with a single geometry type (point, line, or polygon), and a set of attributes. Layers in OpenLayers can contain style functions, which use attribute values to change the appearance of features. This allows you to create complex, data-driven visualizations by relating visual variables to data attributes.
In this tutorial, you apply different styles to enhance the visualization of the Trailheads, Trails and Parks and Open Spaces feature layers.
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.
A Style in OpenLayers has several possible components: an image, text, stroke, fill and so on. To display a hiker icon for the trailheads layer, you use an Icon style as the image component. Specify which image file as the src, and the size as the scale parameter.
To display the trailhead name, you use a Text style. Use a left value for textAlign with an offsetX of 10 to display the labels to the right of the icons. Use Fill and Stroke styles as the fill and stroke properties to give the labels white text with a teal outline. The font property is a CSS font definition. Since the label for each trailhead is different, use a function to return a Style for a given feature. Set the text property using feature's TRL_NAME attribute.
Add a map load handler to the olms initialization. Inside, create a trailheadStyle function that takes a feature and returns a Style containing an Icon style. Use http://static.arcgis.com/images/Symbols/NPS/npsPictograph_0231b.png for the src and set a scale of 25%.
Add a Text style to display labels with white text, teal outline, in an italic sans-serif font. Set the text property using the feature's TRL_NAME attribute.
Add a Vector layer with a Vector source to load and display the trailheads feature layer. Set declutter to be true to prevent label overlap. Pass the trailheadsStyle function as the style property. Use map.addLayer to add this layer to the map.
At the top right, click Run to test your map. You should see hiker icons and trailhead labels.
Style trail width by elevation gain
To visualize the elevation gain of a trail you can use the width of a Stroke style. To do this, create another function which returns a Style. The width of the style's stroke property will be a calculation converting feet of elevation gain to pixels of width.
Create a trailStyle function that takes a feature and returns a Style with a Stroke style. Set the color to pink, and calculate the width property from the feature's ELEV_GAIN attribute.
Add the Trails feature layer as a Vector layer with a GeoJSON Vector source. Pass your trailStyle function as the style property. Use insertAt to add this layer below the trailheads layer.
At the top right, click Run to test your map. You should see the hiking trails in pink with thicker lines for those with more elevation gain.
Add a filtered bike-only trails layer
To dipslay bike-only trails, you can add another style function to display a dashed line for trails that allow biking. You can display a dashed line by passing an array of alternating stroke and gap segments length as the lineDash property in a Stroke style.
The same source from the trailsLayer can be used.
Create a bikeTrailsStyle function. If the feature has a Yes value for USE_BIKE, return a white dashed Stroke style. Otherwise, do not return a Style, so the feature will not be displayed.
At the top right, click Run to test your map. You should now see the bike-accessible trails with a dashed line.
Style a polygon layer
You can use color to communicate the category of a feature, such as the type of a park or open space. Use a style function to returns a Fill style, in which you derive the color property from the feature's TYPE property.
Create a parksStyle function which returns a Fill style. Look up the feature's TYPE attribute in a table to give a different color for Natural Areas, Regionla Open Space, Local Park and Regional Recreation Park. If the type is not one of these, make the color transparent.
Create a Vector layer with a Vector source for the parks feature layer. Pass your parksStyle function as the style property, with a low opacity. Insert the parks layer below the trails layers.