Problem
GlobalID fields in ArcGIS are robust but too long for compact reports, fieldwork, or external systems requiring shorter, user-friendly unique IDs. GIS professionals often need a scalable solution to derive such IDs without compromising uniqueness.
Solution
This Python script hashes the GlobalID field to generate a 4-character unique ID prefixed with "GID" (e.g., GID1234). It automates the creation of a new field for example (GIS_ID) to store these IDs, ensuring efficient and compact ID generation directly in ArcGIS Pro.
Features
- Compact IDs: 4-character unique IDs derived from GlobalID.
- Customizable: Adjust prefix (GID) or length as needed.
- Universal: Works on file and enterprise geodatabases with edit session support.
- Plug-and-Play: Easy integration into ArcGIS Pro workflows or custom toolboxes.
import arcpy
import hashlib
workspace = r"C:\Path\To\Your\Geodatabase.gdb"
input_fc = "YourFeatureClass"
global_id_field, gis_id_field = "GlobalID", "GIS_ID"
if gis_id_field not in [f.name for f in arcpy.ListFields(input_fc)]:
arcpy.AddField_management(input_fc, gis_id_field, "TEXT", field_length=8)
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
try:
with arcpy.da.UpdateCursor(input_fc, [global_id_field, gis_id_field]) as cursor:
for row in cursor:
if row[0]:
row[1] = f"GID{int(hashlib.md5(row[0].encode()).hexdigest(), 16) % 10000:04d}"
cursor.updateRow(row)
edit.stopEditing(True)
except:
edit.stopEditing(False)