Skip to content
View on GitHubSample viewer app

Create, update, and delete features to manage a feature layer.

Screenshot of manage features

Use case

An end-user performing a survey may want to manage features on the map in various ways during the course of their work.

How to use the sample

Pick an operation, then tap on the map to perform the operation at that location. Available feature management operations include: "Create feature", "Delete feature", "Update attribute", and "Update geometry".

How it works

  1. Create a ServiceGeodatabase from a URL.
  2. Get a ServiceFeatureTable from the ServiceGeodatabase.
  3. Create a FeatureLayer derived from the ServiceFeatureTable instance.
  4. Apply the feature management operation upon tapping the map.
    • Create features: create a Feature with attributes and a location using the ServiceFeatureTable.
    • Delete features: delete the selected Feature from the FeatureTable.
    • Update attribute: update the attribute of the selected Feature.
    • Update geometry: update the geometry of the selected Feature.
  5. Update the FeatureTable locally.
  6. Update the ServiceGeodatabase of the ServiceFeatureTable by calling applyEdits(). This pushes the changes to the server.

Relevant API

  • Feature
  • FeatureEditResult
  • FeatureLayer
  • ServiceFeatureTable
  • ServiceGeodatabase

Additional information

When editing feature tables that are subject to database behavior (operations on one table affecting another table), it's recommended to call these methods (apply or undo edits) on the ServiceGeodatabase object rather than on the ServiceFeatureTable object. Using the ServiceGeodatabase object to call these operations will prevent possible data inconsistencies and ensure transactional integrity so that all changes can be committed or rolled back.

Tags

amend, attribute, create, delete, deletion, details, edit, editing, feature, feature layer, feature table, geodatabase, information, moving, online service, service, update, updating, value

Sample Code

ManageFeaturesViewModel.ktManageFeaturesViewModel.ktMainActivity.ktManageFeaturesScreen.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 /* 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  *  * 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.managefeatures.components  import android.app.Application import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.unit.dp import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.viewModelScope import com.arcgismaps.LoadStatus import com.arcgismaps.data.ArcGISFeature import com.arcgismaps.data.CodedValueDomain import com.arcgismaps.data.ServiceFeatureTable import com.arcgismaps.data.ServiceGeodatabase import com.arcgismaps.geometry.GeometryEngine import com.arcgismaps.geometry.Point import com.arcgismaps.geometry.SpatialReference import com.arcgismaps.mapping.ArcGISMap import com.arcgismaps.mapping.BasemapStyle import com.arcgismaps.mapping.Viewpoint import com.arcgismaps.mapping.layers.FeatureLayer import com.arcgismaps.mapping.view.ScreenCoordinate import com.arcgismaps.mapping.view.SingleTapConfirmedEvent import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel import kotlinx.coroutines.launch   class ManageFeaturesViewModel(application: Application) : AndroidViewModel(application) {   val mapViewProxy = MapViewProxy()   // Hold a reference to the feature table.  private var damageFeatureTable: ServiceFeatureTable? = null   // Hold a reference to the feature layer.  private var damageLayer: FeatureLayer? = null   // Hold a reference to the selected feature.  var selectedFeature: ArcGISFeature? by mutableStateOf(null)   // The current feature operation to perform.  var currentFeatureOperation by mutableStateOf(FeatureOperationType.CREATE)   // The list of damage types to update the feature attribute.  var damageTypeList: List<String> = mutableListOf()   var currentDamageType by mutableStateOf("")   // Create the map with streets basemap.  val arcGISMap = ArcGISMap(BasemapStyle.ArcGISStreets).apply {  // Zoom to the United States.  initialViewpoint = Viewpoint(  Point(x = -10800000.0, y = 4500000.0, spatialReference = SpatialReference.webMercator()), scale = 3e7  )  }   // Create a snackbar message to display the result of feature operations.  var snackBarMessage: String by mutableStateOf("")   // Create a message dialog view model for handling error messages  val messageDialogVM = MessageDialogViewModel()   init {  viewModelScope.launch {  // Create a service geodatabase from the feature service.  val serviceGeodatabase =  ServiceGeodatabase("https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0")  serviceGeodatabase.load().onSuccess {  // Get the feature table from the service geodatabase referencing the Damage Assessment feature service.  serviceGeodatabase.getTable(0)?.let { serviceFeatureTable ->  // Load the feature table to get the coded value domain for the attribute field.  serviceFeatureTable.load().onSuccess {  // Hold a reference to the feature table.  damageFeatureTable = serviceFeatureTable  // Get the field from the feature table that will be updated.  val typeDamageField = serviceFeatureTable.fields.first { it.name == "typdamage" }  // Get the coded value domain for the field.  val attributeDomain = typeDamageField.domain as CodedValueDomain  // Add the damage types to the list.  attributeDomain.codedValues.forEach {  damageTypeList += it.name  }  // Create a feature layer to visualize the features in the table.  FeatureLayer.createWithFeatureTable(serviceFeatureTable).let { featureLayer ->  // Hold a reference to the feature layer.  damageLayer = featureLayer  // Add it to the map.  arcGISMap.operationalLayers.add(featureLayer)  // Load the map.  arcGISMap.load().onFailure { error ->  messageDialogVM.showMessageDialog(  "Failed to load map", error.message.toString()  )  }  }  }.onFailure { error ->  messageDialogVM.showMessageDialog(  "Failed to load feature table", error.message.toString()  )  }  }  }.onFailure { error ->  // Show the message dialog and pass the error message to be displayed in the dialog.  messageDialogVM.showMessageDialog(  "Failed to load service geodatabase", error.message.toString()  )  }  }  }   /**  * Directs the behaviour of tap's on the map view.  */  fun onTap(singleTapConfirmedEvent: SingleTapConfirmedEvent) {  if (damageLayer?.loadStatus?.value != LoadStatus.Loaded) {  snackBarMessage = "Layer not loaded!"  return  }  when (currentFeatureOperation) {  FeatureOperationType.CREATE -> createFeatureAt(singleTapConfirmedEvent.screenCoordinate)  FeatureOperationType.DELETE -> deleteFeatureAt(singleTapConfirmedEvent.screenCoordinate)  FeatureOperationType.UPDATE_ATTRIBUTE -> selectFeatureForAttributeEditAt(singleTapConfirmedEvent.screenCoordinate)  FeatureOperationType.UPDATE_GEOMETRY -> updateFeatureGeometryAt(singleTapConfirmedEvent.screenCoordinate)  }  }   /**  * Set the current feature operation to perform based on the selected index from the dropdown. Also, reset feature  * selection.  */  fun onFeatureOperationSelected(index: Int) {  currentFeatureOperation = FeatureOperationType.entries[index]  // Reset the selected feature when the operation changes.  damageLayer?.clearSelection()  selectedFeature = null  }   /**  * Create a new feature at the tapped location with some default attributes  */  private fun createFeatureAt(screenCoordinate: ScreenCoordinate) {  // Create the feature.  val feature = damageFeatureTable?.createFeature()?.apply {  // Get the normalized geometry for the tapped location and use it as the feature's geometry.  mapViewProxy.screenToLocationOrNull(screenCoordinate)?.let { mapPoint ->  geometry = GeometryEngine.normalizeCentralMeridian(mapPoint)  // Set feature attributes.  attributes["typdamage"] = "Minor"  attributes["primcause"] = "Earthquake"  }  }  feature?.let {  viewModelScope.launch {  // Add the feature to the table.  damageFeatureTable?.addFeature(it)  // Apply the edits to the service on the service geodatabase.  damageFeatureTable?.serviceGeodatabase?.applyEdits()  // Update the feature to get the updated objectid - a temporary ID is used before the feature is added.  it.refresh()  // Confirm feature addition.  snackBarMessage = "Created feature ${it.attributes["objectid"]}"  }  }  }   /**  * Selects a feature at the tapped location in preparation for deletion.  */  private fun deleteFeatureAt(screenCoordinate: ScreenCoordinate) {  damageLayer?.let { damageLayer ->  // Clear any existing selection.  damageLayer.clearSelection()  selectedFeature = null  viewModelScope.launch {  // Determine if a user tapped on a feature.  mapViewProxy.identify(damageLayer, screenCoordinate, 10.dp).onSuccess { identifyResult ->  selectedFeature = (identifyResult.geoElements.firstOrNull() as? ArcGISFeature)?.also {  damageLayer.selectFeature(it)  }  }  }  }  }   /**  * Delete the selected feature from the feature table and service geodatabase.  */  fun deleteSelectedFeature() {  selectedFeature?.let {  // Get the feature's object id.  val featureId = it.attributes["objectid"] as Long  viewModelScope.launch {  // Delete the feature from the feature table.  damageFeatureTable?.deleteFeature(it)?.onSuccess {  snackBarMessage = "Deleted feature $featureId"  // Apply the edits to the service geodatabase.  damageFeatureTable?.serviceGeodatabase?.applyEdits()  selectedFeature = null  }?.onFailure {  snackBarMessage = "Failed to delete feature $featureId"  }  }  }  }   /**  * Selects a feature at the tapped location in preparation for attribute editing.  */  private fun selectFeatureForAttributeEditAt(screenCoordinate: ScreenCoordinate) {  damageLayer?.let { damageLayer ->  // Clear any existing selection.  damageLayer.clearSelection()  selectedFeature = null  viewModelScope.launch {  // Determine if a user tapped on a feature.  mapViewProxy.identify(damageLayer, screenCoordinate, 10.dp).onSuccess { identifyResult ->  // Get the identified feature.  val identifiedFeature = identifyResult.geoElements.firstOrNull() as? ArcGISFeature  identifiedFeature?.let {  val currentAttributeValue = it.attributes["typdamage"] as String  currentDamageType = currentAttributeValue  selectedFeature = it.also {  damageLayer.selectFeature(it)  }  } ?: run {  // Reset damage type if no feature identified.  currentDamageType = ""  }  }  }  }  }   /**  * Update the attribute value of the selected feature to the new value from the new damage type.  */  fun onDamageTypeSelected(index: Int) {  // Get the new value.  currentDamageType = damageTypeList[index]  selectedFeature?.let { selectedFeature ->  viewModelScope.launch {  // Load the feature.  selectedFeature.load().onSuccess {  // Update the attribute value.  selectedFeature.attributes["typdamage"] = currentDamageType  // Update the table.  damageFeatureTable?.updateFeature(selectedFeature)  // Update the service on the service geodatabase.  damageFeatureTable?.serviceGeodatabase?.applyEdits()?.onSuccess {  snackBarMessage =  "Updated feature ${selectedFeature.attributes["objectid"]} to $currentDamageType"  }  }  }  }  }   /**  * Select a feature, if none is selected. If a feature is selected, update its geometry to the tapped location.  */  private fun updateFeatureGeometryAt(screenCoordinate: ScreenCoordinate) {   damageLayer?.let { damageLayer ->  when (selectedFeature) {  // When no feature is selected.  null -> {  viewModelScope.launch {  // Determine if a user tapped on a feature.  mapViewProxy.identify(damageLayer, screenCoordinate, 10.dp).onSuccess { identifyResult ->  // Get the identified feature.  val identifiedFeature = identifyResult.geoElements.firstOrNull() as? ArcGISFeature  identifiedFeature?.let {  selectedFeature = it.also {  damageLayer.selectFeature(it)  }  }  }  }  }  // When a feature is selected, update its geometry to the tapped location.  else -> {  mapViewProxy.screenToLocationOrNull(screenCoordinate)?.let { mapPoint ->  // Normalize the point - needed when the tapped location is over the international date line.  val destinationPoint = GeometryEngine.normalizeCentralMeridian(mapPoint)  viewModelScope.launch {  selectedFeature?.let { selectedFeature ->  // Load the feature.  selectedFeature.load().onSuccess {  // Update the geometry of the selected feature.  selectedFeature.geometry = destinationPoint  // Apply the edit to the feature table.  damageFeatureTable?.updateFeature(selectedFeature)  // Push the update to the service with the service geodatabase.  damageFeatureTable?.serviceGeodatabase?.applyEdits()?.onSuccess {  snackBarMessage = "Moved feature ${selectedFeature.attributes["objectid"]}"  }?.onFailure {  snackBarMessage =  "Failed to move feature ${selectedFeature.attributes["objectid"]}"  }  }  }  }  }  }  }  }  } }  enum class FeatureOperationType(val operationName: String, val instruction: String) {  CREATE("Create feature", "Tap on the map to create a new feature."),  DELETE("Delete feature", "Select an existing feature to delete it."),  UPDATE_ATTRIBUTE("Update attribute", "Select an existing feature to edit its attribute."),  UPDATE_GEOMETRY("Update geometry", "Select an existing feature and tap the map to move it to a new position.") }

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