Eagle Pipeline GIS Blog

How to Generate GUIDs in ArcMap

Posted by Tracy Thorleifson on Thu, Sep 09, 2010

In the last post we learned a bit about GUIDs, and how and why they are used in the APDM and PODS ESRI Spatial. Now, let's make some!

GUIDs can, of course, be generated using VB Script. But given the gradual deemphasis of VB Script by ESRI, we’ll perform GUID generation in ArcMap using the ninth wonder of the world, Python. For those of you not already in the know, Python is God’s gift to inveterate GIS hackers. My advice: Learn it! Live it! Love it!

Perhaps the coolest thing about Python in ArcMap is that you can use any Python module in the Field Calculator tool. In this case, we’ll rely on a Python module called UUID. UUID stands for Universally Unique ID. You guessed it; UUID = GUID. (“Universally Unique” hints of hubris; I prefer GUID.) The UUID Python module defines the Python UUID object and provides functions that let you generate GUIDs nine ways from Sunday. And the good news is that the UUID module is a standard Python 2.6 module; no additional downloads are necessary.

So let’s get to it. Suppose we have a polyline feature class containing pipeline features; let’s call it Raw Pipeline Polylines. We’ll add two fields to our feature class: LineGUID and StringGUID. LineGUID has a data type of GUID; StringGUID has a data type of String, with Length = 38. The ArcMap table document for our feature class looks something like this:

Pipelines Table

Having both fields present is for illustrative purposes; it turns out the Field Calculator expects GUIDs to be expressed as strings, so we can use the same expression in the Field Calculator to calculate both proper GUIDs and string versions of the same. We’ll start with LineGUID. A right-click on the LineGUID column header in the table document brings up the context menu:

Pipelines Table with Context Menu

Selecting the context menu Field Calculator... option opens the Field Calculator tool, where we enter the following Python code:

Field Calculator with Python Expression

This is the ArcGIS 10 version of the tool; note the Parser control panel at the top. At ArcGIS 10 you can choose whether to use Python or VB Script in the Field Calculator. We, of course, select Python. (Prior to ArcGIS 10 only VB Script is available in the Field Calculator. But never fear, 9.x users, the ArcToolbox Calculate Field tool lets you pick your parser, too. We’ll cover that at the end.) To do the heavy lifting of GUID generation, we define our own little Python function, CalcGUID. The code for this function is entered into the Pre-Logic Script Code text block:

def CalcGUID():
  import uuid
  return '{' + str(uuid.uuid4()).upper() + '}'

Let’s deconstruct this code. The first line defines our function. In Python, definition of functions has the following syntax:

def <your function>(argument1, argument2, argumentn)

In many cases you might choose to pass an existing field (or fields) from your table to the function for processing. In our CalcGUID example we are generating GUIDs from whole cloth, so no arguments are necessary.

The second line of code imports the UUID Python module, which gives us access to the Python UUID object and functions.

The third line of code generates the GUID and returns it as the result of the CalcGUID function. The .uuid4() function is what actually generates the GUID. (.uuid4() is just one of four ways the UUID module can generate GUIDs.) Python returns GUIDs as 16-byte integer values. This does not suit our purposes, so we turn the GUID into a string using the built-in Python str() function. The .upper() string function just makes sure everything is upper case; this is for cosmetics only. Our GUID string lacks the curly brackets ({ }) expected by ArcMap, so we do a little string concatenation to add those. And that’s it.

We use our CalcGUID function in the LineGUID = text block to actually perform the calculation. Pressing the OK button in the Field Calculator tool should render results that look something like this:

Pipelines Table with LineGUID Calculated

We can use the exact same logic to generate GUIDs for our StringGUID field, with results like this:

Pipelines Table with StringGUID Calculated

Those of you who are not yet migrated to ArcGIS 10 can use the ArcToolbox --> Data Management Tools --> Fields --> Calculate Field tool:

Calculate Field Tool with Python Expression

The Expression Type (optional) pull-down let's you select the parser. Either PYTHON or PYTHON_9.3 may be selected; the differences between the two affect mainly shape operations and have no impact here.

Now, go forth and generate GUIDs to your heart's content!

Topics: APDM, PODS Spatial, Pipeline Open Data Standard, Pipeline Data Model, ArcGIS Pipeline Data Model, Python, ArcToolbox