View on GitHub Sample viewer app

Analyze terrain suitability from an elevation raster by deriving slope and aspect.

Analyze terrain suitability from slope and aspect sample

Use case

Terrain suitability analysis is a common way to narrow a larger elevation surface down to areas that match a specific set of conditions. Slope and aspect are derived from elevation datasets to show how steep the terrain is and which direction it faces. Both of these factors can determine whether an area is suitable for a given purpose, for example, finding areas which are more sheltered from weather versus areas with more exposed terrain.

How to use the sample

When the sample opens, the map shows the results of a preconfigured terrain suitability analysis which finds southward facing lowland slopes on the Isle of Arran, Scotland. The areas matching the criteria are rendered in green, and those not, in white. Open the settings panel to choose another preconfigured scenario, that of a west to north facing slope in upland terrains. Areas matching this criteria are rendered in purple.

How it works

  1. Create a ContinuousField from a raster file.
  2. Create a ContinuousFieldFunction from the continuous field.
  3. Derive a slope function and an aspect function from the continuous field function.
  4. Create BooleanFieldFunction masks for slope, aspect, and elevation using range checks with map algebra.
  5. Combine the masks with logicalAnd and apply a land-only mask to exclude areas below sea level.
  6. Convert the final result to a DiscreteFieldFunction.
  7. Create a FieldAnalysis from the discrete field function.
  8. Apply a ColormapRenderer with a color for areas not matching the terrain suitability criteria, and a color for matching areas.
  9. Add the analysis to an AnalysisOverlay.

Relevant API

  • AnalysisOverlay
  • BooleanFieldFunction
  • ColormapRenderer
  • ContinuousField
  • ContinuousFieldFunction
  • DiscreteFieldFunction
  • FieldAnalysis

About the data

The sample uses a 10m resolution digital terrain elevation raster of the Isle of Arran, Scotland (Data Copyright Scottish Government and SEPA (2014)).

Tags

aspect, elevation, field analysis, map algebra, raster, slope, spatial reference, terrain

Sample code

AnalyzeTerrainSuitabilityFromSlopeAndAspectView.swiftAnalyzeTerrainSuitabilityFromSlopeAndAspectView.swiftAnalyzeTerrainSuitabilityFromSlopeAndAspectView.Model.swift
// Copyright 2026 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 AnalyzeTerrainSuitabilityFromSlopeAndAspectView: View {
/// The view model for the sample.
@State private var model = Model()
/// A Boolean value indicating whether the settings are showing.
@State private var isShowingSettings = false
/// The identifier for the currently displayed scenario toast. This is needed
/// to trigger the toast dismissal task when the scenario changes.
@State private var scenarioToastID: UUID?
/// The error shown in the error alert.
@State private var error: (any Error)?
var body: some View {
MapView(
map: model.map,
viewpoint: model.viewpoint,
analysisOverlays: [model.analysisOverlay]
)
.onAnalysisViewStateChanged { analysis, viewState in
guard let activeAnalysis = model.activeAnalysis,
analysis === activeAnalysis else {
return
}
if let analysisError = viewState.error {
error = analysisError
}
}
.overlay(alignment: .top) {
if scenarioToastID != nil {
Text(model.selectedScenario.description)
.font(.footnote)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
.padding()
.background(.regularMaterial)
.clipShape(.rect(cornerRadius: 8))
.padding()
.transition(
.move(edge: .top).combined(with: .opacity)
)
}
}
.overlay(alignment: .bottom) {
Text(
"Raster data copyright Scottish Government and SEPA (2014)"
)
.font(.caption)
.frame(maxWidth: .infinity)
.padding(.vertical, 6)
.background(
.thinMaterial,
ignoresSafeAreaEdges: .horizontal
)
}
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button("Settings", systemImage: "gear") {
isShowingSettings.toggle()
}
.popover(isPresented: $isShowingSettings) {
TerrainSuitabilitySettings(model: model)
.presentationCompactAdaptation(.popover)
.frame(idealWidth: 320, idealHeight: 340)
}
}
}
.task {
do {
try await model.configureTerrainSuitabilityAnalyses()
scenarioToastID = UUID()
} catch {
self.error = error
}
}
.task(id: scenarioToastID) {
guard scenarioToastID != nil else { return }
try? await Task.sleep(for: .seconds(3))
guard !Task.isCancelled else { return }
scenarioToastID = nil
}
.onChange(of: model.selectedScenario) {
isShowingSettings = false
scenarioToastID = UUID()
}
.animation(.default, value: scenarioToastID)
.errorAlert(presentingError: $error)
}
}
extension AnalyzeTerrainSuitabilityFromSlopeAndAspectView {
/// A view for selecting a terrain suitability scenario.
struct TerrainSuitabilitySettings: View {
/// The model containing the selected terrain suitability scenario.
@Bindable var model: Model
var body: some View {
NavigationStack {
Form {
Picker("Scenario", selection: $model.selectedScenario) {
ForEach(SiteScenario.allCases) { scenario in
Text(scenario.label)
}
}
.pickerStyle(.inline)
}
.navigationTitle("Terrain Suitability")
}
}
}
}