Skip to content

Apply colormap renderer to raster

View on GitHub

Apply a colormap renderer to a raster.

Image of apply colormap renderer to raster sample

Use case

A colormap renderer transforms pixel values in a raster to display raster data based on specific colors, aiding in visual analysis of the data. For example, a forestry commission may want to quickly visualize areas above and below the tree-line line occurring at a known elevation on a raster containing elevation values. They could overlay a transparent colormap set to color those areas below the tree-line elevation green, and those above white.

How to use the sample

Pan and zoom to explore the effect of the colormap applied to the raster.

How it works

  1. Create a raster from a raster file using Raster.init(name:extension:bundle:).
  2. Create a raster layer with the raster using RasterLayer.init(raster:).
  3. Create an array of colors. Colors at the beginning of the array replace the darkest values in the raster and colors at the end of the array replace the brightest values of the raster.
  4. Create a colormap renderer with the color array using ColormapRenderer.init(colors:), and assign it to the raster layer using RasterLayer.renderer.

Relevant API

  • ColormapRenderer
  • Raster
  • RasterLayer

Offline data

This sample uses the ShastaBW raster. It is downloaded from ArcGIS Online automatically.

About the data

The raster used in this sample shows an area in the south of the Shasta-Trinity National Forest, California.

Tags

colormap, data, raster, renderer, visualization

Sample Code

ApplyColormapRendererToRasterView.swift
Use dark colors for code blocksCopy
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
// Copyright 2025 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import ArcGIS
import SwiftUI

struct ApplyColormapRendererToRasterView: View {
    /// Creates the raster layer used by this sample.
    static func makeRasterLayer() -> RasterLayer {
        // Creates a raster and raster layer.
        let raster = Raster(name: "ShastaBW/ShastaBW", extension: "tif", bundle: nil)!
        let rasterLayer = RasterLayer(raster: raster)

        // Creates a colormap renderer and assigns it to the raster layer.
        let colors = Array(repeating: UIColor.red, count: 150)
                   + Array(repeating: UIColor.yellow, count: 151)
        rasterLayer.renderer = ColormapRenderer(colors: colors)

        return rasterLayer
    }

    /// Creates the map for this sample.
    static func makeMap() -> Map {
        let map = Map(basemapStyle: .arcGISImageryStandard)
        map.addOperationalLayer(makeRasterLayer())
        return map
    }

    /// The map displayed by the map view.
    @State private var map = makeMap()
    /// The error if the raster layer load operation failed, otherwise `nil`.
    @State private var rasterLayerLoadError: Error?

    /// The raster layer from the map.
    var rasterLayer: RasterLayer { map.operationalLayers.first as! RasterLayer }

    var body: some View {
        MapViewReader { mapView in
            MapView(map: map)
                .task {
                    // Loads the raster layer to get the full extent.
                    do {
                        try await rasterLayer.load()
                        if let center = rasterLayer.fullExtent?.center {
                            let viewpoint = Viewpoint(center: center, scale: 80_000)
                            await mapView.setViewpoint(viewpoint)
                        }
                    } catch {
                        rasterLayerLoadError = error
                    }
                }
                .errorAlert(presentingError: $rasterLayerLoadError)
        }
    }
}

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