Add features with contingent values

View on GitHub

Create and add features whose attribute values satisfy a predefined set of contingencies.

Image of add features with contingent values

Use case

Contingent values are a data design feature that allow you to make values in one field dependent on values in another field. Your choice for a value on one field further constrains the domain values that can be placed on another field. In this way, contingent values enforce data integrity by applying additional constraints to reduce the number of valid field inputs.

For example, a field crew working in a sensitive habitat area may be required to stay a certain distance away from occupied bird nests, but the size of that exclusion area differs depending on the bird's level of protection according to presiding laws. Surveyors can add points of bird nests in the work area and their selection of the size of the exclusion area will be contingent on the values in other attribute fields.

How to use the sample

Tap on the map to add a feature symbolizing a bird's nest. Then choose values describing the nest's status, protection, and buffer size. Notice how different values are available depending on the values of preceding fields. Once the contingent values are validated, tap "Done" to add the feature to the map.

How it works

  1. Create and load the Geodatabase from the mobile geodatabase location on file.
  2. Load the first GeodatabaseFeatureTable.
  3. Load the ContingentValuesDefinition from the feature table.
  4. Create a new FeatureLayer from the feature table and add it to the map.
  5. Create a new Feature from the feature table using makeFeature(attributes:geometry:).
  6. Get the first field from the feature table by name using field(named:).
  7. Then get the domain from the field as an CodedValueDomain.
  8. Get the coded value domain's codedValues to get an array of CodedValues.
  9. After selecting a value from the initial coded values for the first field, retrieve the remaining valid contingent values for each field as you select the values for the attributes. i. Get the ContingentValueResults by using contingentValues(with:field:) with the feature and the target field by name. ii. Get an array of valid ContingentValues from contingentValuesByFieldGroup dictionary with the name of the relevant field group. iii. Iterate through the array of valid contingent values to create an array of ContingentCodedValue names or the minimum and maximum values of a ContingentRangeValue depending on the type of ContingentValue returned.
  10. Validate the feature's contingent values by using validateContingencyConstraints(for:) with the current feature. If the resulting array is empty, the selected values are valid.

Relevant API

  • ArcGISFeatureTable
  • CodedValue
  • CodedValueDomain
  • ContingencyConstraintViolation
  • ContingentCodedValue
  • ContingentRangeValue
  • ContingentValuesDefinition
  • ContingentValuesResult

Offline data

This sample uses the Contingent values birds nests mobile geodatabase and the Fillmore topographic map vector tile package for the basemap.

About the data

The mobile geodatabase contains birds nests in the Fillmore area, defined with contingent values. Each feature contains information about its status, protection, and buffer size.

Additional information

Learn more about contingent values and how to utilize them on the ArcGIS Pro documentation.

Tags

coded values, contingent values, feature table, geodatabase

Sample Code

AddFeaturesWithContingentValuesView.swiftAddFeaturesWithContingentValuesView.swiftAddFeaturesWithContingentValuesView.Model.swiftAddFeaturesWithContingentValuesView.AddFeatureView.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
// Copyright 2024 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 AddFeaturesWithContingentValuesView: View {
    /// The view model for the sample.
    @StateObject private var model = Model()

    /// The point on the map where the user tapped.
    @State private var tapLocation: Point?

    /// A Boolean value indicating whether the add feature sheet is presented.
    @State private var addFeatureSheetIsPresented = false

    /// The error shown in the error alert.
    @State private var error: Error?

    var body: some View {
        ZStack {
            GeometryReader { geometryProxy in
                MapViewReader { mapViewProxy in
                    MapView(map: model.map, graphicsOverlays: [model.graphicsOverlay])
                        .onSingleTapGesture { _, mapPoint in
                            tapLocation = mapPoint
                        }
                        .task(id: tapLocation) {
                            // Add a feature representing a bird's nest when the map is tapped.
                            guard let tapLocation else { return }

                            do {
                                try await model.addFeature(at: tapLocation)
                                addFeatureSheetIsPresented = true

                                // Create an envelope from the screen's frame.
                                let viewRect = geometryProxy.frame(in: .local)
                                guard let viewExtent = mapViewProxy.envelope(
                                    fromViewRect: viewRect
                                ) else { return }

                                // Update the map's viewpoint with an offsetted tap location
                                // to center the feature in the top half of the screen.
                                let yOffset = (viewExtent.height / 2) / 2
                                let newViewpointCenter = Point(
                                    x: tapLocation.x,
                                    y: tapLocation.y - yOffset
                                )
                                await mapViewProxy.setViewpointCenter(newViewpointCenter)
                            } catch {
                                self.error = error
                            }
                        }
                        .task {
                            do {
                                // Load the features from the geodatabase when the sample loads.
                                try await model.loadFeatures()

                                // Zoom to the extent of the added layer.
                                guard let extent = model.map.operationalLayers.first?.fullExtent
                                else { return }
                                await mapViewProxy.setViewpointGeometry(extent, padding: 15)
                            } catch {
                                self.error = error
                            }
                        }
                }
            }

            VStack {
                Spacer()

                // A button that allows the popover to display.
                Button("") {}
                    .opacity(0)
                    .sheet(isPresented: $addFeatureSheetIsPresented, detents: [.medium]) {
                        NavigationView {
                            AddFeatureView(model: model)
                                .navigationTitle("Add Bird Nest")
                                .navigationBarTitleDisplayMode(.inline)
                                .toolbar {
                                    ToolbarItem(placement: .cancellationAction) {
                                        Button("Cancel", role: .cancel) {
                                            addFeatureSheetIsPresented = false
                                        }
                                    }

                                    ToolbarItem(placement: .confirmationAction) {
                                        Button("Done") {
                                            addFeatureSheetIsPresented = false
                                        }
                                        .disabled(!model.contingenciesAreValid)
                                    }
                                }
                        }
                        .navigationViewStyle(.stack)
                    }
                    .task(id: addFeatureSheetIsPresented) {
                        // When the sheet closes, remove the feature if it is invalid.
                        guard !addFeatureSheetIsPresented,
                              !model.contingenciesAreValid,
                              model.feature != nil
                        else { return }

                        do {
                            try await model.removeFeature()
                        } catch {
                            self.error = error
                        }
                    }
            }
        }
        .errorAlert(presentingError: $error)
    }
}

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