Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generates a file gdb table containing the paths of photos to be attached to matching points as feature attachments..
#
# @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 = arcpy.management.CreateTable("in_memory", "table1")
# Add fields to temporary table
for k, v in fields.iteritems():
arcpy.management.AddField(tbl, 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
arcpy.management.CopyRows(tbl, "match_table")
arcpy.management.Delete(tbl)
# Attach photos to corresponding point
arcpy.management.AddAttachments(in_dataset=ftr,
in_join_field=fields.keys()[0],
in_match_table="match_table",
out_join_field=fields.keys()[0],
in_match_path_field=fields.keys()[1])
print("Script complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment