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 Location Platform or ArcGIS Online account.
Steps
Create a new pen
- To get started, you can complete the Display a scene tutorial or use the .
Get an access token
You need an access token with the correct privileges to access the resources used in this tutorial.
-
Go to the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Item access
- Note: If you are using your own custom data layer for this tutorial, you need to grant the API key credentials access to the layer item. Learn more in Item access privileges.
- Privileges
-
Copy the API key access token to your clipboard when prompted.
-
In CodePen, update the
access
variable to use your access token.Token Use dark colors for code blocks const accessToken = "YOUR_ACCESS_TOKEN"; Cesium.ArcGisMapService.defaultAccessToken = accessToken; 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), });
-
Run the code to ensure the base layer is displayed in the scene.
To learn about the other types of authentication available, go to Types of authentication.
Add references to ArcGIS REST JS
-
In the
<head
element, reference the> feature-service
andrequest
packages from ArcGIS REST JS.Use dark colors for code blocks <script src="https://cesium.com/downloads/cesiumjs/releases/1.121/Build/Cesium/Cesium.js"></script> <link href="https://cesium.com/downloads/cesiumjs/releases/1.121/Build/Cesium/Widgets/widgets.css" rel="stylesheet"> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-feature-service@4/dist/bundled/feature-service.umd.js"></script>
-
In the
<body
, create an> arcgis
using your access token to authenticate requests to the feature service.Rest. Api Key Manager Use dark colors for code blocks const accessToken = "YOUR_ACCESS_TOKEN"; Cesium.ArcGisMapService.defaultAccessToken = accessToken; const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken);
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); }) })
-
Add the data attribution for the feature layer source.
- Go to the Trailheads (Santa Monica Mountains) item.
- Scroll down to the Credits (Attribution) section and copy its value.
- Create an
attribution
property and paste the attribution value from the item.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, // Attribution text retrieved from https://arcgis.com/home/item.html?id=883cedb8c9fe4524b64d47666ed234a7 credit: "Los Angeles GeoHub" }).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/ NP S/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, // Attribution text retrieved from https://arcgis.com/home/item.html?id=883cedb8c9fe4524b64d47666ed234a7 credit: "Los Angeles GeoHub" }).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); }) })
-
Add the data attribution for the feature layer source.
- Go to the Trails item.
- Scroll down to the Credits (Attribution) section and copy its value.
- Create an
attribution
property and paste the attribution value from the item.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, // Attribution text retrieved from https://arcgis.com/home/item.html?id=69e12682738e467eb509d8b54dc73cbd credit: "Los Angeles GeoHub" }).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, // Attribution text retrieved from https://arcgis.com/home/item.html?id=69e12682738e467eb509d8b54dc73cbd credit: "Los Angeles GeoHub" }).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); }) })
-
Add the data attribution for the feature layer source.
- Go to the Parks and Open Space item.
- Scroll down to the Credits (Attribution) section and copy its value.
- Create an
attribution
property and paste the attribution value from the item.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, // Attribution text retrieved from https://arcgis.com/home/item.html?id=f2ea5d874dad427294641d2d45097c0e credit: "Los Angeles GeoHub" }).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, // Attribution text retrieved from https://arcgis.com/home/item.html?id=f2ea5d874dad427294641d2d45097c0e credit: "Los Angeles GeoHub" }).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, // Attribution text retrieved from https://arcgis.com/home/item.html?id=f2ea5d874dad427294641d2d45097c0e credit: "Los Angeles GeoHub" }).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: