Add features with contingent values

View on GitHubSample viewer app

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

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 ArcGISFeature using GeodatabaseFeatureTable.createFeature()
  6. Get the first field by name using ArcGISFeatureTable.fields.find{ }.
  7. Then get the Field.domain as an CodedValueDomain.
  8. Get the coded value domain's codedValues to get an array of CodedValue's.
  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 ArcGISFeatureTable.getContingentValues(ArcGISFeature, "field_name") with the feature and the target field by name.
    ii. Get an array of valid ContingentValues from ContingentValuesResult.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(feature) 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

About the data

This sample uses the Contingent values birds nests mobile geodatabase and the Fillmore topographic map vector tile package for the basemap. 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

MainActivity.ktMainActivity.ktDownloadActivity.kt
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* Copyright 2022 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.
 *
 */

package com.esri.arcgismaps.sample.addfeatureswithcontingentvalues

import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.Color
import com.arcgismaps.data.ArcGISFeature
import com.arcgismaps.data.ArcGISFeatureTable
import com.arcgismaps.data.CodedValue
import com.arcgismaps.data.CodedValueDomain
import com.arcgismaps.data.ContingentCodedValue
import com.arcgismaps.data.ContingentRangeValue
import com.arcgismaps.data.Feature
import com.arcgismaps.data.Geodatabase
import com.arcgismaps.data.QueryParameters
import com.arcgismaps.geometry.GeometryEngine
import com.arcgismaps.geometry.Point
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.Basemap
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.layers.ArcGISVectorTiledLayer
import com.arcgismaps.mapping.layers.FeatureLayer
import com.arcgismaps.mapping.symbology.SimpleFillSymbol
import com.arcgismaps.mapping.symbology.SimpleFillSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleLineSymbol
import com.arcgismaps.mapping.symbology.SimpleLineSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbol
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbolStyle
import com.arcgismaps.mapping.view.Graphic
import com.arcgismaps.mapping.view.GraphicsOverlay
import com.esri.arcgismaps.sample.addfeatureswithcontingentvalues.databinding.ActivityMainBinding
import com.esri.arcgismaps.sample.addfeatureswithcontingentvalues.databinding.AddFeatureLayoutBinding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.launch
import java.io.File

class MainActivity : AppCompatActivity() {

    // set up data binding for the activity
    private val activityMainBinding: ActivityMainBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_main)
    }

    private val bottomSheetBinding by lazy {
        AddFeatureLayoutBinding.inflate(layoutInflater)
    }

    private val mapView by lazy {
        activityMainBinding.mapView
    }

    private val provisionPath: String by lazy {
        getExternalFilesDir(null)?.path.toString() + File.separator + getString(R.string.app_name)
    }

    // mobile database containing offline feature data. geodatabase is closed on app exit
    private val geodatabase: Geodatabase by lazy {
        Geodatabase("${cacheDir.path}/ContingentValuesBirdNests.geodatabase")
    }

    // graphic overlay instance to add the feature graphic to the map
    private val graphicsOverlay = GraphicsOverlay()

    // instance of the contingent feature to be added to the map
    private var feature: ArcGISFeature? = null

    // instance of the feature table retrieved from the geodatabase, updates when new feature is added
    private var featureTable: ArcGISFeatureTable? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // authentication with an API key or named user is
        // required to access basemaps and other location services
        ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.API_KEY)
        lifecycle.addObserver(mapView)

        // use the offline vector tiled layer as a basemap
        val fillmoreVectorTiledLayer = ArcGISVectorTiledLayer(
            "$provisionPath/FillmoreTopographicMap.vtpk"
        )

        mapView.apply {
            // set the basemap layer and the graphic overlay to the MapView
            map = ArcGISMap(Basemap(fillmoreVectorTiledLayer))
            graphicsOverlays.add(graphicsOverlay)
        }

        // add a listener to the MapView to detect when
        // a user has performed a single tap to add a new feature
        lifecycleScope.launch {
            mapView.onSingleTapConfirmed.collect {
                // open a bottom sheet view to add the feature
                it.mapPoint?.let { mapPoint -> createBottomSheet(mapPoint) }
            }
        }

        // create a temporary directory to use the geodatabase file
        createGeodatabaseCacheDirectory()

        lifecycleScope.launch {
            // retrieve and load the offline mobile geodatabase file from the cache directory
            geodatabase.load().getOrElse {
                showError("Error loading GeoDatabase: ${it.message}")
            }

            // get the first geodatabase feature table
            val featureTable = geodatabase.featureTables.firstOrNull()
                ?: return@launch showError("No feature table found in geodatabase")
            // load the geodatabase feature table
            featureTable.load().getOrElse {
                return@launch showError(it.message.toString())
            }

            // create and load the feature layer from the feature table
            val featureLayer = FeatureLayer.createWithFeatureTable(featureTable)
            // add the feature layer to the map
            mapView.map?.operationalLayers?.add(featureLayer)

            // set the map's viewpoint to the feature layer's full extent
            val extent = featureLayer.fullExtent
                ?: return@launch showError("Error retrieving extent of the feature layer")
            mapView.setViewpoint(Viewpoint(extent))

            // keep the instance of the featureTable
            this@MainActivity.featureTable = featureTable

            // add buffer graphics for the feature layer
            queryFeatures()
        }
    }

    /**
     * Geodatabase creates and uses various temporary files while processing a database,
     * which will need to be cleared before looking up the [geodatabase] again.
     * A copy of the original geodatabase file is created in the cache folder.
     */
    private fun createGeodatabaseCacheDirectory() {
        // clear cache directory
        File(cacheDir.path).deleteRecursively()
        // copy over the original Geodatabase file to be used in the temp cache directory
        File("$provisionPath/ContingentValuesBirdNests.geodatabase").copyTo(
            File("${cacheDir.path}/ContingentValuesBirdNests.geodatabase")
        )
    }

    /**
     * Create buffer graphics for the features and adds the graphics to
     * the [graphicsOverlay]
     */
    private suspend fun queryFeatures() {
        // clear the existing graphics
        graphicsOverlay.graphics.clear()

        // create buffer graphics for the features
        val queryParameters = QueryParameters().apply {
            // set the where clause to filter for buffer sizes greater than 0
            whereClause = "BufferSize > 0"
        }

        // query the features using the queryParameters on the featureTable
        val featureQueryResult = featureTable?.queryFeatures(queryParameters)?.getOrThrow()
        // call get on the future to get the result
        val featureResultList = featureQueryResult?.toList()

        if (!featureResultList.isNullOrEmpty()) {
            // create list of graphics for each query result
            val graphics = featureResultList.map { createGraphic(it) }
            // add the graphics to the graphics overlay
            graphicsOverlay.graphics.addAll(graphics)
        } else {
            showError("No features found with BufferSize > 0")
        }
    }

    /**
     * Create a graphic for the given [feature] and returns a Graphic with the features attributes
     */
    private fun createGraphic(feature: Feature): Graphic {
        // get the feature's buffer size
        val bufferSize = feature.attributes["BufferSize"] as Int
        // get a polygon using the feature's buffer size and geometry
        val polygon = feature.geometry?.let { GeometryEngine.bufferOrNull(it, bufferSize.toDouble()) }
        // create the outline for the buffers
        val lineSymbol = SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.black, 2f)
        // create the buffer symbol
        val bufferSymbol = SimpleFillSymbol(
            SimpleFillSymbolStyle.ForwardDiagonal, Color.red, lineSymbol
        )
        // create an graphic using the geometry and fill symbol
        return Graphic(polygon, bufferSymbol)
    }

    /**
     * Creates a BottomSheetDialog view to handle contingent value interaction.
     * Once the contingent values have been set and the apply button is clicked,
     * the function will call validateContingency() to add the feature at the [mapPoint].
     */
    private fun createBottomSheet(mapPoint: Point) {
        // creates a new BottomSheetDialog
        val bottomSheet = BottomSheetDialog(this).apply {
            behavior.state = BottomSheetBehavior.STATE_EXPANDED
        }

        // set up the first content value attribute
        setUpStatusAttributes()

        // clear and set bottom sheet content view to layout,
        // to be able to set the content view on each bottom sheet draw
        if (bottomSheetBinding.root.parent != null) {
            (bottomSheetBinding.root.parent as ViewGroup).removeAllViews()
        }

        // reset feature to null since this is a new feature
        feature = null

        bottomSheetBinding.apply {

            // reset bottom sheet values, this is needed to showcase contingent values behavior
            statusInputLayout.editText?.setText("")
            protectionInputLayout.editText?.setText("")
            selectedBuffer.text = ""
            protectionInputLayout.isEnabled = false
            bufferSeekBar.isEnabled = false
            bufferSeekBar.value = bufferSeekBar.valueFrom

            // set apply button to validate and apply contingency feature on map
            applyTv.setOnClickListener {
                // check if the contingent features set is valid and set it to the map if valid
                validateContingency(mapPoint)
                bottomSheet.dismiss()
            }

            // dismiss on cancel clicked
            cancelTv.setOnClickListener { bottomSheet.dismiss() }
        }

        // set the content view to the root of the binding layout
        bottomSheet.setContentView(bottomSheetBinding.root)
        // display the bottom sheet view
        bottomSheet.show()
    }

    /**
     *  Retrieve the status fields, add the fields to a ContingentValueDomain, and set the values to the spinner
     *  When status attribute selected, createFeature() is called.
     */
    private fun setUpStatusAttributes() {
        // get the first field by name
        val statusField = featureTable?.fields?.find { field -> field.name == "Status" }
        // get the field's domains as coded value domain
        val codedValueDomain = statusField?.domain as CodedValueDomain
        // get the coded value domain's coded values
        val statusCodedValues = codedValueDomain.codedValues
        // get the selected index if applicable
        val statusNames = statusCodedValues.map { it.name }
        // get the items to be added to the spinner adapter
        val adapter = ArrayAdapter(bottomSheetBinding.root.context, R.layout.list_item, statusNames)
        (bottomSheetBinding.statusInputLayout.editText as AutoCompleteTextView).apply {
            setAdapter(adapter)
            setOnItemClickListener { _, _, position, _ ->
                // get the CodedValue of the item selected, and create a feature needed for feature attributes
                createFeature(statusCodedValues[position])
            }
        }
    }

    /**
     * Set up the [feature] using the status attribute's coded value
     * by loading the [featureTable]'s Contingent Value Definition.
     * This function calls setUpProtectionAttributes() once the [feature] has been set
     */
    private fun createFeature(codedValue: CodedValue) {
        // get the contingent values definition from the feature table
        val contingentValueDefinition = featureTable?.contingentValuesDefinition
        if (contingentValueDefinition != null) {
            lifecycleScope.launch {
                // load the contingent values definition
                contingentValueDefinition.load().getOrElse {
                    showError("Error loading the ContingentValuesDefinition")
                }
                // create a feature from the feature table and set the initial attribute
                feature = featureTable?.createFeature() as ArcGISFeature
                feature?.attributes?.set("Status", codedValue.code)
                setUpProtectionAttributes()
            }
        } else {
            showError("Error retrieving ContingentValuesDefinition from the FeatureTable")
        }
    }

    /**
     *  Retrieve the protection attribute fields, add the fields to a ContingentCodedValue, and set the values to the spinner
     *  When status attribute selected, showBufferSeekbar() is called.
     */
    private fun setUpProtectionAttributes() {
        // set the bottom sheet view to enable the Protection attribute, and disable input elsewhere
        bottomSheetBinding.apply {
            protectionInputLayout.isEnabled = true
            bufferSeekBar.isEnabled = false
            bufferSeekBar.value = bufferSeekBar.valueFrom
            protectionInputLayout.editText?.setText("")
            selectedBuffer.text = ""
        }

        // get the contingent value results with the feature for the protection field
        val contingentValuesResult = feature?.let {
            featureTable?.getContingentValuesOrNull(it, "Protection")
        }

        // get the list of contingent values by field group
        val contingentValues = contingentValuesResult?.byFieldGroup?.get("ProtectionFieldGroup")

        // convert the list of ContingentValues to a list of CodedValue
        val protectionCodedValues =
            contingentValues?.map { (it as ContingentCodedValue).codedValue }
                ?: return showError("Error getting coded values by field group")

        // get the attribute names for each coded value
        val protectionNames = protectionCodedValues.map { it.name }

        // set the items to be added to the spinner adapter
        val adapter = ArrayAdapter(
            bottomSheetBinding.root.context, R.layout.list_item, protectionNames
        )

        // set the choices of protection attribute values
        (bottomSheetBinding.protectionInputLayout.editText as AutoCompleteTextView).apply {
            setAdapter(adapter)
            setOnItemClickListener { _, _, position, _ ->
                // set the protection CodedValue of the item selected
                feature?.attributes?.set("Protection", protectionCodedValues[position].code)
                // enable buffer seekbar
                showBufferSeekbar()
            }
        }
    }

    /**
     *  Retrieve the buffer attribute fields, add the fields
     *  to a ContingentRangeValue, and set the values to a SeekBar
     */
    private fun showBufferSeekbar() {
        // set the bottom sheet view to enable the buffer attribute
        bottomSheetBinding.apply {
            bufferSeekBar.isEnabled = true
            selectedBuffer.text = ""
        }

        // get the contingent value results using the feature and field
        val contingentValueResult = feature?.let {
            featureTable?.getContingentValuesOrNull(it, "BufferSize")
        }

        // get the contingent rang value of the buffer size field group
        val bufferSizeRangeValue = contingentValueResult?.byFieldGroup?.get("BufferSizeFieldGroup")
            ?.get(0) as ContingentRangeValue

        // set the minimum and maximum possible buffer sizes
        val minValue = bufferSizeRangeValue.minValue as Int
        val maxValue = bufferSizeRangeValue.maxValue as Int

        // check if there can be a max value, if not disable SeekBar
        // & set value to attribute size to 0
        if (maxValue > 0) {
            // get SeekBar instance from the binding layout
            bottomSheetBinding.bufferSeekBar.apply {
                // set the min, max and current value of the SeekBar
                valueFrom = minValue.toFloat()
                valueTo = maxValue.toFloat()
                value = valueFrom
                // set the initial attribute and the text to the min of the ContingentRangeValue
                feature?.attributes?.set("BufferSize", value.toInt())
                bottomSheetBinding.selectedBuffer.text = value.toInt().toString()
                // set the change listener to update the attribute value and the displayed value to the SeekBar position
                addOnChangeListener { _, value, _ ->
                    feature?.attributes?.set("BufferSize", value.toInt())
                    bottomSheetBinding.selectedBuffer.text = value.toInt().toString()
                }
            }
        } else {
            // max value is 0, so disable seekbar and update the attribute value accordingly
            bottomSheetBinding.apply {
                bufferSeekBar.isEnabled = false
                selectedBuffer.text = "0"
            }
            feature?.attributes?.set("BufferSize", 0)
        }
    }

    /**
     * Ensure that the selected values are a valid combination.
     * If contingencies are valid, then display [feature] on the [mapPoint]
     */
    private fun validateContingency(mapPoint: Point) {
        // check if all the features have been set
        if (featureTable == null) {
            showError("Input all values to add a feature to the map")
            return
        }

        // validate the feature's contingencies
        val contingencyViolations = feature?.let {
            featureTable?.validateContingencyConstraints(it)
        } ?: return showError("No feature attribute was selected")

        // if there are no contingency violations
        if (contingencyViolations.isEmpty()) {
            // the feature is valid and ready to add to the feature table
            // create a symbol to represent a bird's nest
            val symbol = SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Color.black, 11F)
            // add the graphic to the graphics overlay
            graphicsOverlay.graphics.add(Graphic(mapPoint, symbol))

            // set the geometry of the feature to the map point
            feature?.geometry = mapPoint

            // create the graphic of the feature
            val graphic = feature?.let { createGraphic(it) }
            // add the graphic to the graphics overlay
            graphic?.let { graphicsOverlay.graphics.add(it) }

            // add the feature to the feature table
            lifecycleScope.launch {
                feature?.let { featureTable?.addFeature(it) }
                feature?.load()?.getOrElse {
                    return@launch showError(it.message.toString())
                }
            }
        } else {
            showError("Invalid contingent values: " + (contingencyViolations.size) + " violations found.")
        }

    }

    private fun showError(message: String) {
        Log.e(localClassName, message)
        Snackbar.make(mapView, message, Snackbar.LENGTH_SHORT).show()
    }
}

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