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.

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

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
)
				
			

At Final Draft Mapping we provide comprehensive courses for automating tasks within ArcGIS Pro and ArcGIS Online with ArcPy and the ArcGIS API for Python. Courses range from beginner to advanced workflows and all paid courses provide extra support where you can ask questions. Automation within ArcGIS is a highly sought after skill, by adding these skills to your arsenal you are placing yourself at the forefront of that demand. 

We appreciate our blog readers, you can get 25% off any (non-sale) course at any time with the code FDMBLOG25

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

Your email address will not be published. Required fields are marked *