Generate a feature class attachment match table in Python

A few months ago, I set up a Collector app for a colleague who went into the field for two weeks. The web map contained 5 feature layers of assets. Once he reached the asset in the field, he would select the corresponding point, update attributes and attach at least one photo. It was basic verification work.

The amount of issues I ran into once he was out in the field was huge. I’m saving all my good stuff for a later series of posts I will write on mobile data capture, but let’s just say Collector has some kinks to work out. On the plus side, I had to write several scripts to sort out the issues, and this was one of them.

# @date 01/07/2015
# @author Cindy Williams
# Generates a file gdb table containing the paths
# of photos to be attached to matching points as feature
# attachments.
# For use as a standalone script.
import arcpy
import os
gdb = r"C:\Some\Arb\Folder\work.gdb"
arcpy.env.workspace = gdb
folder_jpgs = r"C:\Some\Arb\Folder\Photos"
fields = {"ID": 20, "PhotoPath": 125}
ftr = "ftr_point"
# Create temporary table in memory
tbl ="in_memory", "table1")
# Add fields to temporary table
for k, v in fields.iteritems():, k, "TEXT", field_length=v)
# Create an insert cursor on temp table
cursor = arcpy.da.InsertCursor(tbl, fields.keys())
# Write photo paths to table
for root, dirs, jpgs in os.walk(folder_jpgs):
for jpg in jpgs:
cursor.insertRow([os.path.split(root)[1], os.path.join(root, jpg)]
del cursor
# Persist table to disk, "match_table")
# Attach photos to corresponding point,
print("Script complete.")

The photos are stored in separate folders per feature layer, per named feature. For example, the “Mini Substation” folder has a subfolder called “Minisub 1”, which has 5 photos named Photo1.jpg, Photo2.jpg etc. This is why I could not use the Generate Attachment Match Table tool.

In Line 22, I’ve chosen to create the table in memory first, for faster read/write access. In Line 25, I add fields to this new table (in future, I will be doing this using ExtendTable() instead).

In Line 34, I insert a new row into the table for each photo, containing the photo’s root folder as the ID (named feature), and the path to the individual photo. The table is copied to disk in Line 37, and the attachments are added to the feature class in Line 41.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.