Blend raster layer

View inC++QMLView on GitHubSample viewer app

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.

screenshot

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-null 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.
  6. Set the blend renderer to the raster layer.

Relevant API

  • BlendRenderer
  • ColorRamp
  • Raster
  • RasterLayer

Offline Data

To set up the sample's offline data, see the Use offline data in the samples section of the Qt Samples repository overview.

Link Local Location
Shasta.tif raster <userhome>/ArcGIS/Runtime/Data/raster/Shasta.tif
Shasta_Elevation.tif raster <userhome>/ArcGIS/Runtime/Data/raster/Shasta_Elevation.tif

Tags

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

Sample Code

BlendRasterLayer.qmlBlendRasterLayer.qmlColorRampModel.qmlSlopeTypeModel.qml
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// [WriteFile Name=BlendRasterLayer, Category=Layers]
// [Legal]
// Copyright 2017 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
// http://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.
// [Legal]

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Esri.ArcGISRuntime
import Esri.ArcGISExtras

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    readonly property url dataPath: {
        Qt.platform.os === "ios" ?
                    System.writableLocationUrl(System.StandardPathsDocumentsLocation) + "/ArcGIS/Runtime/Data/raster" :
                    System.writableLocationUrl(System.StandardPathsHomeLocation) + "/ArcGIS/Runtime/Data/raster"
    }
    property bool editingRenderer: false

    SlopeTypeModel {
        id: slopeTypeModel
    }

    ColorRampModel {
        id: colorRampModel
    }

    MapView {
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        Map {
            id: map
            basemap: basemap
        }
    }

    Basemap {
        id: basemap
        RasterLayer {
            id: rasterLayer
            Raster {
                id: baseRaster
                path: dataPath + "/Shasta.tif"
            }
        }
    }

    Basemap {
        id: basemapColorRamp
        RasterLayer {
            id: rasterLayerColorRamp

            Raster {
                path: dataPath + "/Shasta_Elevation.tif"
            }
        }
    }

    Raster {
        id: elevationRaster
        path: dataPath + "/Shasta_Elevation.tif"
    }

    Rectangle {
        visible: editButton.visible
        anchors.centerIn: editButton
        radius: 8
        height: editButton.height + (16)
        width: editButton.width + (16)
        color: "lightgrey"
        border.color: "darkgrey"
        border.width: 2
        opacity: 0.75
    }

    Button {
        id: editButton
        anchors {
            bottom: parent.bottom
            horizontalCenter: parent.horizontalCenter
            margins: 32
        }
        visible: rendererBox.width === 0
        text: "Edit Renderer"
        onClicked: editingRenderer = true;
    }

    Rectangle {
        id: rendererBox
        clip: true
        anchors {
            right: parent.right
            top: parent.top
            bottom: parent.bottom
        }

        color: "white"
        opacity: 0.75
        width: editingRenderer ? parent.width : 0

        GridLayout {
            anchors {
                centerIn: parent
                margins: 24
            }

            columns: 2

            Text {
                text: "altitude"
            }

            SpinBox {
                id: altSpinBox
                from: 0
                to: 90
                editable: true
                textFromValue: function(value) {
                    return value.toFixed(0) + "\u00B0";
                }
                valueFromText: function(text) {
                    return parseInt(text);
                }
            }

            Text {
                text: "azimuth"
            }

            SpinBox {
                id: azimuthSpinBox
                from: 0
                to: 360
                editable: true
                textFromValue: function(value) {
                    return value.toFixed(0) + "\u00B0";
                }
                valueFromText: function(text) {
                    return parseInt(text);
                }
            }

            Text {
                text: "slope type"
            }

            ComboBox {
                id: slopeCombo
                property int modelWidth: 0
                Layout.minimumWidth: modelWidth + leftPadding + rightPadding + (indicator ? indicator.width : 10)
                Layout.fillWidth: true
                textRole: "name"
                model: slopeTypeModel

                Component.onCompleted : {
                    for (let i = 0; i < model.count; ++i) {
                        metrics.text = model.get(i).name;
                        modelWidth = Math.max(modelWidth, metrics.width);
                    }
                }
                TextMetrics {
                    id: metrics
                    font: slopeCombo.font
                }
            }

            Text {
                text: "color ramp"
            }

            ComboBox {
                id: colorCombo
                property int modelWidth: 0
                Layout.minimumWidth: modelWidth + leftPadding + rightPadding
                Layout.fillWidth: true
                textRole: "name"
                model: colorRampModel

                Component.onCompleted : {
                    for (let i = 0; i < model.count; ++i) {
                        metrics2.text = model.get(i).name;
                        modelWidth = Math.max(modelWidth, metrics2.width);
                    }
                }
                TextMetrics {
                    id: metrics2
                    font: colorCombo.font
                }
            }

            Button {
                text: "Render"
                Layout.columnSpan: 2
                Layout.alignment: Qt.AlignHCenter
                onClicked: {
                    editingRenderer = false;
                    applyRendererSettings();
                }
            }
        }

        Behavior on width { PropertyAnimation { duration: 500 } }
    }

    function applyRendererSettings() {
        const blendRenderer = ArcGISRuntimeEnvironment.createObject("BlendRenderer");
        blendRenderer.elevationRaster = elevationRaster;
        blendRenderer.altitude = altSpinBox.value;
        blendRenderer.azimuth = azimuthSpinBox.value;
        blendRenderer.slopeType = slopeTypeModel.get(slopeCombo.currentIndex).value;
        blendRenderer.colorRamp = getColorRamp();
        blendRenderer.outputBitDepth = 8;
        blendRenderer.gammas = [];
        blendRenderer.noDataValues = [];
        blendRenderer.outputMaxValues = [255];
        blendRenderer.outputMinValues = [9];
        blendRenderer.sourceMaxValues = [];
        blendRenderer.sourceMinValues = [];

        applyRenderer(blendRenderer);

        map.basemap = (colorCombo.currentText !== "none") ? basemapColorRamp : basemap;
    }

    function applyRenderer(blendRenderer) {
        const val = colorRampModel.get(colorCombo.currentIndex).value;
        if (val.length === 0)
            rasterLayer.renderer = blendRenderer;
        else
            rasterLayerColorRamp.renderer = blendRenderer;
    }

    function getColorRamp() {
        const val = colorRampModel.get(colorCombo.currentIndex).value;
        if (val.length === 0)
            return null;

        const colorRamp = ArcGISRuntimeEnvironment.createObject(val);
        colorRamp.size = 800;

        return colorRamp;
    }
}

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