Skip to content

Apply blend renderer to hillshade

View on GitHub

Blend a hillshade with a raster by specifying the elevation data. The resulting raster looks similar to the original raster, but with some terrain shading, giving it a textured look.

Image of apply blend renderer to hillshade sample

Use case

BlendRenderer can be used to apply a color ramp to a hillshade to emphasize areas of high or low elevation. A BlendRenderer can also be used to add a hillshade effect to aerial or satellite imagery, thereby making changes in elevation more visible.

How to use the sample

Choose and adjust the altitude, azimuth, slope type and color ramp type settings to update the image.

How it works

  1. Create a Raster object from a raster file.
  2. Create a RasterLayer object from the raster.
  3. Create a Basemap object from the raster layer and set it to the map.
  4. Create another Raster object for elevation from a grayscale raster file.
  5. Create a BlendRenderer object, specifying the elevation raster, color ramp, and other properties.
  • If you specify a non-nil color ramp, use the elevation raster as the base raster in addition to the elevation raster parameter. That way, the color ramp is used instead of the satellite imagery.
  1. Set the blend renderer to the raster layer.

Relevant API

  • BlendRenderer
  • ColorRamp
  • Raster
  • RasterLayer

Offline data

This sample uses the Shasta Raster and Shasta Elevation Raster. Both are downloaded from ArcGIS Online automatically.

Tags

color ramp, elevation, hillshade, image, raster, raster layer, visualization

Sample Code

ApplyBlendRendererToHillshadeView.swiftApplyBlendRendererToHillshadeView.swiftApplyBlendRendererToHillshadeView.SettingsView.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
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
// 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 ApplyBlendRendererToHillshadeView: View {
    /// A map with no specified style.
    @State private var map = Map()

    /// The imagery basemap.
    private let imageryBasemap: Basemap = {
        let raster = Raster(fileURL: .shasta)
        let rasterLayer = RasterLayer(raster: raster)
        return Basemap(baseLayer: rasterLayer)
    }()

    /// The elevation basemap.
    private let elevationBasemap: Basemap = {
        let raster = Raster(fileURL: .shastaElevation)
        let rasterLayer = RasterLayer(raster: raster)
        return Basemap(baseLayer: rasterLayer)
    }()

    /// The imagery basemap raster layer.
    private var imageryBasemapLayer: RasterLayer {
        imageryBasemap.baseLayers[0] as! RasterLayer
    }

    /// The elevation basemap raster layer.
    private var elevationBasemapLayer: RasterLayer {
        elevationBasemap.baseLayers[0] as! RasterLayer
    }

    /// The elevation raster.
    private let elevationRaster = Raster(fileURL: .shastaElevation)

    /// A Boolean value indicating whether the settings view should be presented.
    @State private var isShowingSettings = false

    /// The settings for a blend renderer.
    struct RendererSettings: Equatable {
        /// The renderer altitude.
        var altitude = 0.0
        /// The renderer azimuth.
        var azimuth = 0.0
        /// The renderer slope type.
        var slopeType: HillshadeRenderer.SlopeType?
        /// The renderer color ramp preset.
        var colorRampPreset: ColorRamp.Preset?

        /// The renderer color ramp.
        var colorRamp: ColorRamp? {
            colorRampPreset.map { ColorRamp(preset: $0, size: 800) }
        }
        ///  A Boolean value indicating whether the renderer has a color ramp.
        var hasColorRamp: Bool {
            colorRamp != nil
        }
    }

    /// The renderer settings.
    @State private var rendererSettings = RendererSettings()

    var body: some View {
        MapView(map: map)
            .onChange(of: rendererSettings, initial: true) {
                let rasterLayer = rendererSettings.hasColorRamp ? elevationBasemapLayer : imageryBasemapLayer
                rasterLayer.renderer = BlendRenderer(
                    elevationRaster: elevationRaster,
                    outputMinValues: [9],
                    outputMaxValues: [255],
                    sourceMinValues: [],
                    sourceMaxValues: [],
                    noDataValues: [],
                    gammas: [],
                    colorRamp: rendererSettings.colorRamp,
                    altitude: rendererSettings.altitude,
                    azimuth: rendererSettings.azimuth,
                    slopeType: rendererSettings.slopeType
                )
                map.basemap = rendererSettings.hasColorRamp ? elevationBasemap : imageryBasemap
            }
            .toolbar {
                ToolbarItem(placement: .bottomBar) {
                    Button("Renderer Settings") {
                        isShowingSettings = true
                    }
                    .popover(isPresented: $isShowingSettings) {
                        SettingsView(settings: $rendererSettings)
                            .presentationDetents([.fraction(0.5)])
                            .frame(idealWidth: 320, idealHeight: 320)
                    }
                }
            }
    }
}

private extension URL {
    /// The URL to the Shasta data.
    static var shasta: URL {
        Bundle.main.url(forResource: "Shasta", withExtension: "tif", subdirectory: "raster-file/raster-file")!
    }

    /// The URL to the Shasta elevation data.
    static var shastaElevation: URL {
        Bundle.main.url(forResource: "Shasta_Elevation", withExtension: "tif", subdirectory: "Shasta_Elevation")!
    }
}

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