Tutorial: Style features

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

  1. To get started, either complete the Display a scene tutorial or .

Get an access token

You need an access token with the correct privileges to access the resources used in this tutorial.

  1. 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.
  2. Copy the API key access token to your clipboard when prompted.

  3. In CodePen, update the accessToken variable to use your access token.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
          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),
    
          });
    

To learn about the other types of authentication available, go to Types of authentication.

Add references to ArcGIS REST JS

  1. In the <head> element, reference the feature-service and request packages from ArcGIS REST JS.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
        <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>
    
    
    Expand
  2. In the <body>, create an arcgisRest.ApiKeyManager using your access token to authenticate requests to the feature service.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
        const accessToken = "YOUR_ACCESS_TOKEN";
    
        Cesium.ArcGisMapService.defaultAccessToken = accessToken;
    
        const authentication = arcgisRest.ApiKeyManager.fromKey(accessToken);
    
    Expand

Remove the terrain provider

CesiumJS does not currently fully support layer styling in scenes with terrain.

  1. Remove the terrainProvider from your viewer to enable layer ordering and feature outlines.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
            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 GeoJsonDataSource and iterate through each Entity created by the layer. Set the Billboard property to style the points with trailhead icons.

  1. Use arcgisRest.queryFeatures to make an authenticated request to the Trailheads URL. Format the response as GeoJSON, then add it to your scene as a GeoJsonDataSource.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
        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);
    
          })
        })
    
    Expand
  2. After the layer is loaded, iterate through each Entity in the layer and set each Billboard image to the URL for the hiker icon: http://static.arcgis.com/images/Symbols/NPS/npsPictograph_0231b.png.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
        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;
            }
    
          })
        })
    
    Expand

Style trails (lines) by elevation gain

To visualize the elevation gain of trails in the Trails (lines) feature layer, add the layer as a GeoJsonDataSource and the width of each line based on its properties.

  1. Call the arcgisRest.queryFeatures operation to make an authenticated request to the Trails URL. Format the response as GeoJSON and add it to the scene as a GeoJsonDataSource.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
        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);
    
          })
        })
    
    Expand
  2. Iterate through all entities and set the polyline width of each entity based on their ELEV_GAIN property.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
        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;
            }
    
          })
        })
    
    Expand

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.

  1. Call the arcgisRest.queryFeatures 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 a GeoJsonDataSource.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
        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);
    
          })
        })
    
    Expand
  2. The TYPE attribute of the layer contains four different types of parks and open spaces. Define a unique color for each category.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
        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"
            };
    
          })
        })
    
    Expand
  3. Iterate through all entities and set the polygon material of each entity based on the TYPE of park.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
        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;
            }
    
          })
        })
    
    Expand

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:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.