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.
In this tutorial, you apply different styles to enhance the visualization of the Trailheads, Trails, and Parks and Open Spaces feature layers.
Prerequisites
You need an ArcGIS Developer or ArcGIS Online account to access the developer dashboard and create an API key.
Steps
Create a new pen
- To get started, either complete the Display a scene tutorial or .
Set the API key
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
api
to use your key. UpdateKey cesium
to use your Cesium ion access token.Access Token Use dark colors for code blocks const apiKey = "YOUR_API_KEY"; Cesium.ArcGisMapService.defaultAccessToken = apiKey; const cesiumAccessToken = "YOUR_CESIUM_ACCESS_TOKEN"; Cesium.Ion.defaultAccessToken = cesiumAccessToken; const arcGisImagery = Cesium.ArcGisMapServerImageryProvider.fromBasemapType(Cesium.ArcGisBaseMapType.SATELLITE); const viewer = new Cesium.Viewer("cesiumContainer", { baseLayer: Cesium.ImageryLayer.fromProviderAsync(arcGisImagery), });
Add references to ArcGIS REST JS
-
In the
<head>
element, reference thefeature-service
andrequest
packages from ArcGIS REST JS.Use dark colors for code blocks <script src="https://cesium.com/downloads/cesiumjs/releases/1.114/Build/Cesium/Cesium.js"></script> <link href="https://cesium.com/downloads/cesiumjs/releases/1.114/Build/Cesium/Widgets/widgets.css" rel="stylesheet"> <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-feature-service@4.0.0/dist/bundled/feature-service.umd.js"></script>
-
In the
<body>
, create anarcgis
using your API key to authenticate requests to the feature service.Rest.Api K e y Manager Use dark colors for code blocks const apiKey = "YOUR_API_KEY"; Cesium.ArcGisMapService.defaultAccessToken = apiKey; const authentication = arcgisRest.ApiKeyManager.fromKey(apiKey);
Remove the terrain provider
CesiumJS does not currently fully support layer styling in scenes with terrain.
-
Remove the
terrain
from your viewer to enable layer ordering and feature outlines.Provider Use dark colors for code blocks const viewer = new Cesium.Viewer("cesiumContainer", { imageryProvider: arcGISImageTileProvider, terrainProvider: Cesium.createWorldTerrain(), timeline: false, animation: false, geocoder:false });
Style trailheads (points) with icons
To style a point feature layer with custom icons, add the layer as a Geo
and iterate through each Entity
created by the layer. Set the Billboard
property to style the points with trailhead icons.
-
Use
arcgis
to make an authenticated request to the Trailheads URL. Format the response as GeoJSON, then add it to your scene as aRest.query Features Geo
.Json Data Source Use dark colors for code blocks const pointLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0"; arcgisRest.queryFeatures({ url: pointLayerURL, authentication, f:"geojson" }).then((response) => { Cesium.GeoJsonDataSource.load(response,{ clampToGround:true }).then((dataSource) => { dataSource.name="trailheads"; viewer.dataSources.add(dataSource); }) })
-
After the layer is loaded, iterate through each
Entity
in the layer and set eachBillboard
image to the URL for the hiker icon:http:
.//static.arcgis.com/images/Symbols/NPS/nps Pictograph_ 0231b.png Use dark colors for code blocks const pointLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0"; arcgisRest.queryFeatures({ url: pointLayerURL, authentication, f:"geojson" }).then((response) => { Cesium.GeoJsonDataSource.load(response,{ clampToGround:true }).then((dataSource) => { dataSource.name="trailheads"; viewer.dataSources.add(dataSource); const entities = dataSource.entities.values; for (let i=0; i<entities.length;i++) { entities[i].billboard.image = "http://static.arcgis.com/images/Symbols/NPS/npsPictograph_0231b.png" entities[i].billboard.scale=0.4; } }) })
Style trails (lines) by elevation gain
To visualize the elevation gain of trails in the Trails (lines) feature layer, add the layer as a Geo
and the width of each line based on its properties.
-
Call the
arcgis
operation to make an authenticated request to the Trails URL. Format the response as GeoJSON and add it to the scene as aRest.query Features Geo
.Json Data Source Use dark colors for code blocks const lineLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0"; arcgisRest.queryFeatures({ url: lineLayerURL, authentication, f:"geojson" }).then((response) => { Cesium.GeoJsonDataSource.load(response,{ clampToGround:true }).then((dataSource) => { dataSource.name="trails"; viewer.dataSources.add(dataSource); }) })
-
Iterate through all entities and set the
polyline
width of each entity based on theirELEV_
property.GAIN Use dark colors for code blocks const lineLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0"; arcgisRest.queryFeatures({ url: lineLayerURL, authentication, f:"geojson" }).then((response) => { Cesium.GeoJsonDataSource.load(response,{ clampToGround:true }).then((dataSource) => { dataSource.name="trails"; viewer.dataSources.add(dataSource); const entities = dataSource.entities.values; for (let i=0; i<entities.length;i++) { const trail = entities[i]; trail.polyline.width = 3 + (4* trail.properties.ELEV_GAIN._value)/2300; trail.polyline.material = Cesium.Color.fromCssColorString('#FCADF9'); trail.polyline.zIndex = 2; } }) })
Style parks (polygons) by type
You can create a categorical rendering of parks based on their attribute values. Render the polygon features from the Parks and Open Space feature layer with different colors based on the type of land they represent.
-
Call the
arcgis
opeartion to make an authenticated request to the Parks and Open Space URL. Format the response as GeoJSON and add it to the scene as aRest.query Features Geo
.Json Data Source Use dark colors for code blocks const polygonLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0"; arcgisRest.queryFeatures({ url: polygonLayerURL, authentication, f:"geojson" }).then((response) => { Cesium.GeoJsonDataSource.load(response,{ clampToGround:true }).then((dataSource) => { dataSource.name="parks" viewer.dataSources.add(dataSource); }) })
-
The
TYPE
attribute of the layer contains four different types of parks and open spaces. Define a unique color for each category.Use dark colors for code blocks const polygonLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0"; arcgisRest.queryFeatures({ url: polygonLayerURL, authentication, f:"geojson" }).then((response) => { Cesium.GeoJsonDataSource.load(response,{ clampToGround:true }).then((dataSource) => { dataSource.name="parks" viewer.dataSources.add(dataSource); const parkColors = { "Natural Areas": "#9E559C", "Regional Open Space": "#A7C636", "Local Park": "#149ECE", "Regional Recreation Park": "#ED5151" }; }) })
-
Iterate through all entities and set the
polygon
material of each entity based on theTYPE
of park.Use dark colors for code blocks const polygonLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0"; arcgisRest.queryFeatures({ url: polygonLayerURL, authentication, f:"geojson" }).then((response) => { Cesium.GeoJsonDataSource.load(response,{ clampToGround:true }).then((dataSource) => { dataSource.name="parks" viewer.dataSources.add(dataSource); const entities = dataSource.entities.values; for (let i=0; i<entities.length;i++) { const park = entities[i]; if (park.polygon == null) { continue; } const color = Cesium.Color.fromCssColorString(parkColors[park.properties.TYPE._value]); color.alpha = 0.7 park.polygon.material = color; park.polygon.zIndex=1; park.polygon.outline = false; } }) })
Run the app
In CodePen, run your code to display the map.
Your map should display the trailheads, trails, and open spaces in the Santa Monica Mountains:
- Each trailhead should be represented with a hiker icon.
- Each trail should have a custom width based on its total elevation gain.
- Each park should be assigned a color based on its type.
What's next?
Learn how to use additional ArcGIS location services in these tutorials: