Skip to content

Apply stretch renderer

View on GitHub

Use a stretch renderer to enhance the visual contrast of raster data for analysis.

Image of apply stretch renderer sample

Use case

An appropriate stretch renderer can enhance the contrast of raster imagery, allowing the user to control how their data is displayed for efficient imagery analysis.

How to use the sample

Choose one of the stretch parameter types:

  • Standard deviation - a linear stretch defined by the standard deviation of the pixel values
  • Min-max - a linear stretch based on minimum and maximum pixel values
  • Percent clip - a linear stretch between the defined percent clip minimum and percent clip maximum pixel values

Then configure the parameters.

How it works

  1. Create a raster from a raster file using Raster.init(fileURL:).
  2. Create a raster layer from the raster using RasterLayer.init(raster:).
  3. Add the layer to the map using Map.addOperationalLayer(_:).
  4. Create a stretch renderer, specifying the stretch parameters and other properties using StretchRenderer.init(parameters:gammas:estimatesStatistics:colorRamp:).
  5. Set the renderer on the raster layer using RasterLayer.renderer.

Relevant API

  • ColorRamp
  • MinMaxStretchParameters
  • PercentClipStretchParameters
  • Raster
  • RasterLayer
  • StandardDeviationStretchParameters
  • StretchParameters
  • StretchRenderer

Offline data

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

About the data

This sample uses a raster imagery tile of an area of forested mountainous terrain and rivers.

Additional information

See Stretch function in the ArcGIS Pro documentation for more information about the types of stretches that can be performed.

Tags

analysis, deviation, histogram, imagery, interpretation, min-max, percent clip, pixel, raster, stretch, symbology, visualization

Sample Code

ApplyStretchRendererView.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
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
// 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 ApplyStretchRendererView: View {
    /// Creates the raster layer used by this sample.
    static func makeRasterLayer() -> RasterLayer {
        let shastaURL = Bundle.main.url(
            forResource: "Shasta",
            withExtension: "tif",
            subdirectory: "raster-file/raster-file"
        )!
        let raster = Raster(fileURL: shastaURL)
        return RasterLayer(raster: raster)
    }

    /// 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?
    /// A Boolean value that indicates whether the settings sheet is presented.
    @State private var isSettingsFormPresented = false

    /// The settings for a stretch renderer.
    struct RendererSettings: Equatable {
        enum StretchType: CaseIterable { // swiftlint:disable:this nesting
            case minMax, percentClip, standardDeviation
        }

        var stretchType: StretchType = .minMax

        // MinMax

        var valueMin = 10.0
        var valueMax = 150.0

        // PercentClip

        var percentMin = 0.0
        var percentMax = 50.0

        // StdDeviation

        var factor = 0.5
    }

    /// The settings used to create the parameters of the stretch renderer.
    @State private var rendererSettings = RendererSettings()

    /// 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 {
                    do {
                        try await rasterLayer.load()
                        if let fullExtent = rasterLayer.fullExtent {
                            let viewpoint = Viewpoint(center: fullExtent.center, scale: 80_000)
                            await mapView.setViewpoint(viewpoint)
                        }
                    } catch {
                        rasterLayerLoadError = error
                    }
                }
                .errorAlert(presentingError: $rasterLayerLoadError)
                .onChange(of: rendererSettings, initial: true) {
                    let parameters: StretchParameters = switch rendererSettings.stretchType {
                    case .minMax:
                        MinMaxStretchParameters(
                            minValues: [rendererSettings.valueMin],
                            maxValues: [rendererSettings.valueMax]
                        )
                    case .percentClip:
                        PercentClipStretchParameters(
                            min: rendererSettings.percentMin,
                            max: rendererSettings.percentMax
                        )
                    case .standardDeviation:
                        StandardDeviationStretchParameters(
                            factor: rendererSettings.factor
                        )
                    }
                    rasterLayer.renderer = StretchRenderer(
                        parameters: parameters,
                        gammas: [],
                        estimatesStatistics: true,
                        colorRamp: nil
                    )
                }
                .toolbar {
                    ToolbarItem(placement: .bottomBar) {
                        Button("Renderer Settings") {
                            isSettingsFormPresented = true
                        }
                        .popover(isPresented: $isSettingsFormPresented) {
                            settingsForm
                                .frame(idealWidth: 320, idealHeight: 310)
                                .presentationCompactAdaptation(.popover)
                        }
                    }
                }
        }
    }

    var settingsForm: some View {
        Form {
            Section {
                Picker("Stretch Type", selection: $rendererSettings.stretchType) {
                    ForEach(RendererSettings.StretchType.allCases, id: \.self) { stretchType in
                        let label = switch stretchType {
                        case .minMax: "MinMax"
                        case .percentClip: "PercentClip"
                        case .standardDeviation: "StdDeviation"
                        }
                        Text(label)
                    }
                }
            }
            Section {
                switch rendererSettings.stretchType {
                case .minMax:
                    let valueRange = 0.0...255.0
                    let format = FloatingPointFormatStyle<Double>()
                        .precision(.fractionLength(0))
                    LabeledContent(
                        "Min Value",
                        value: rendererSettings.valueMin,
                        format: format
                    )
                    Slider(
                        value: $rendererSettings.valueMin,
                        in: valueRange.lowerBound...(rendererSettings.valueMax - 1)
                    ) {
                        Text("Min Value")
                    } minimumValueLabel: {
                        Text(valueRange.lowerBound, format: format)
                    } maximumValueLabel: {
                        Text(rendererSettings.valueMax - 1, format: format)
                    }
                    .listRowSeparator(.hidden, edges: .top)
                    LabeledContent(
                        "Max Value",
                        value: rendererSettings.valueMax,
                        format: format
                    )
                    Slider(
                        value: $rendererSettings.valueMax,
                        in: (rendererSettings.valueMin + 1)...valueRange.upperBound
                    ) {
                        Text("Max Value")
                    } minimumValueLabel: {
                        Text(rendererSettings.valueMin + 1, format: format)
                    } maximumValueLabel: {
                        Text(valueRange.upperBound, format: format)
                    }
                    .listRowSeparator(.hidden, edges: .top)
                case .percentClip:
                    let percentRange = 0.0...100.0
                    let format = FloatingPointFormatStyle<Double>.Percent()
                        .precision(.fractionLength(0))
                        .scale(1)
                    LabeledContent(
                        "Min",
                        value: rendererSettings.percentMin,
                        format: format
                    )
                    Slider(
                        value: $rendererSettings.percentMin,
                        in: percentRange.lowerBound...rendererSettings.percentMax
                    ) {
                        Text("Min")
                    } minimumValueLabel: {
                        Text(percentRange.lowerBound, format: format)
                    } maximumValueLabel: {
                        Text(rendererSettings.percentMax, format: format)
                    }
                    .listRowSeparator(.hidden, edges: .top)
                    LabeledContent(
                        "Max",
                        value: rendererSettings.percentMax,
                        format: format
                    )
                    Slider(
                        value: $rendererSettings.percentMax,
                        in: rendererSettings.percentMin...percentRange.upperBound
                    ) {
                        Text("Max")
                    } minimumValueLabel: {
                        Text(rendererSettings.percentMin, format: format)
                    } maximumValueLabel: {
                        Text(percentRange.upperBound, format: format)
                    }
                    .listRowSeparator(.hidden, edges: .top)
                case .standardDeviation:
                    let format = FloatingPointFormatStyle<Double>()
                        .precision(.fractionLength(0...2))
                    LabeledContent(
                        "Factor",
                        value: rendererSettings.factor,
                        format: format
                    )
                    let factorRange = 0.25...4.0
                    Slider(
                        value: $rendererSettings.factor,
                        in: factorRange
                    ) {
                        Text("Factor")
                    } minimumValueLabel: {
                        Text(factorRange.lowerBound, format: format)
                    } maximumValueLabel: {
                        Text(factorRange.upperBound, format: format)
                    }
                    .listRowSeparator(.hidden, edges: .top)
                }
            }
            .multilineTextAlignment(.trailing)
        }
        .animation(.default, value: rendererSettings.stretchType)
    }
}

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