Sequentially Increment Numbered Values in ArcGIS Online with the ArcGIS API for Python

Table of Contents

Introduction

The ArcGIS API for Python is a powerful Python library that allows users to interact with and automate tasks in ArcGIS Online (or Portal). The API is excellent for programmatically creating, maintaining, and updating components of ArcGIS Online including data and attribute updates. In this post we will focus on updating a text field that should contain unique IDs and increment the number component of the ID by one for each record in the table.

Automate ArcGIS Online Feature Service Workflows with the ArcGIS API for Python | A Complete Guide from Beginner to Advanced | Full Course

Free on YouTube! A set of videos detailing ArcGIS Online Feature Services workflows using the ArcGIS API for Python. We will do a deep dive into properties and methods available, along with custom workflows for automating with Feature Services and the ArcGIS API for Python.

arcgis modules

The API provides access to your organisations ArcGIS Online via the GIS class in the gis module. This GIS class is the gateway to ArcGIS Online.

				
					## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS


				
			

Accessing ArcGIS Online

Our first port of call is to access your ArcGIS Online via the GIS class. There are a handful of ways to achieve access, if you are logged into your ArcGIS Online in ArcGIS Pro you can simply use "home", otherwise, another common way is to provide the ArcGIS Online URL, followed by your username and password.
				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

User Inputs

For the user inputs we require the Item ID for the Feature Service that contains the Feature Layer that we want to update the IDs for; the name of the Feature Layer within the Feature Service to apply the update to; the name of the field within the Feature Layer to apply the update to; an optional text prefix (eg. ID-); and the length of the numeric part, for example entering 3 would return 001, 002, 003 etc.

				
					## the Item ID for the Feature Service that contains teh Feature Layer
fs_item_id = "FS_ITEM_ID"

## the name of the Featurer Layer in the Feature Service
lyr_name =  "LAYER_NAME"

## the name of the field to apply the update to
fld_name = "FIELD_NAME"

## prefix before each code
id_prefix = ""

## how many digits after the id_prefix
zero_padding = 4
				
			

Required Objects

We have two required objects, the Feature Service as an Item object, and the Feature Layer as a FeatureLayer object.

				
					## Feature Service as an Item object
item = agol.content.get(fs_item_id)

## Get the FeatureLayer object representing the layer to update
fl = [lyr for lyr in item.layers if lyr.properties.name == lyr_name][0]
				
			

Get the FeatureSet

We use the query() method for the FeatureLayer object to return a FeatureSet that contains OIDs only. A dictionary is returned that contains key of “objectIds” and a value which is a list of all OIDs.

				
					fs = fl.query(
    where = "1=1",
    return_ids_only = True,
    return_geometry = False
)
				
			

Create the List of Dictionaries

We use this list along with dictionary comprehension to create a list of dictionaries where we have two keys, the OID field name with value of the OID, and the name of the field to update with a value of the corresponding generated ID. 

				
					## the oid field of the FeatureLayer
oid_fld = fl.properties.uniqueIdField["name"]

## create a list if dictionaries each dictionary represents similar to below.
## {'attributes': {'FID': 21, 'new_id': 'PREFIX0021'}}
updates = [
    {"attributes": {oid_fld: oid, fld_name: f"{id_prefix}{str(oid).zfill(zero_padding)}"}}
    for oid in fs["objectIds"]
]
				
			

Apply the Updates to the Feature Layer

We use the edit_featurs() method for a FeatureLayer object to apply the updates. 

Note: If you have over 2000 records, you might need to break the edit_feature() updates into chunks of 2000 records to avoid errors.

				
					fl.edit_features(
    updates=updates
)
				
			

If you’ve found these blogs helpful and would like to support the project, please consider making a donation. All learning material is provided free of charge to help GIS professionals, students, and developers learn ArcPy, the ArcGIS API for Python, ArcGIS Pro, and ArcGIS Online. Donations help cover website hosting, software licensing, domain costs, and the time involved in creating and maintaining tutorials, courses, and learning resources. Your support helps keep this content freely available to everyone and allows new courses and materials to be developed for the GIS community. If the content has helped you solve a problem, learn a new skill, or advance your career, please consider supporting the project with a donation. Every contribution, no matter the size, is greatly appreciated.

All the code in one place

You can find the entire code workflow below with links to important components in the documentation that were used.

				
					from arcgis.gis import GIS

################################################################################
## API Reference Links:
##  https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#gis
##  https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ContentManager.get
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#featurelayer
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayer.query
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayer.edit_features
##
## API Version: 2.1.0.2, 2.2.0.1, 2.3.0
##
################################################################################

################################################################################
## Access ArcGIS Online ########################################################

agol = GIS("home")

################################################################################
## User Inputs #################################################################

## the Item ID for the Feature Service that contains teh Feature Layer
fs_item_id = "FS_ITEM_ID"

## the name of the Featurer Layer in the Feature Service
lyr_name =  "LAYER_NAME"

## the name of the field to apply the update to
fld_name = "FIELD_NAME"

## prefix before each code
id_prefix = ""

## how many digits after the id_prefix
zero_padding = 4

################################################################################
## Required Objects ############################################################

## Feature Service as an Item object
item = agol.content.get(fs_item_id)

## Get the FeatureLayer object representing the layer to update
fl = [lyr for lyr in item.layers if lyr.properties.name == lyr_name][0]

################################################################################
## Get FeatureSet containing the OIDs ##########################################

fs = fl.query(
    where = "1=1",
    return_ids_only = True,
    return_geometry = False
)

################################################################################
## Create the update list of dictionaries ######################################

## the oid field of the FeatureLayer
oid_fld = fl.properties.uniqueIdField["name"]

## create a list if dictionaries each dictionary represents similar to below.
## {'attributes': {'FID': 21, 'new_id': 'PREFIX0021'}}
updates = [
    {"attributes": {oid_fld: oid, fld_name: f"{id_prefix}{str(oid).zfill(zero_padding)}"}}
    for oid in fs["objectIds"]
]

################################################################################
## Apply the updates to the FeatureLayer #######################################

fl.edit_features(
    updates=updates
)

################################################################################
				
			

Leave a Comment