Created
February 4, 2015 00:06
-
-
Save JeffJacobson/e4f5b39e13fbba7aaa75 to your computer and use it in GitHub Desktop.
Extract attachments from a file geodatabase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Extracts attachments from a file geodatabase. | |
""" | |
import os, re | |
from os.path import join, exists | |
from arcpy import da | |
def extract_attachments(gdb, attachment_dir="images"): | |
"""Extract attachments from a geodatabase to the filesystem. | |
""" | |
attach_table_name_re = re.compile(r"^\w+(?=__ATTACH$)", re.IGNORECASE) | |
if not exists(attachment_dir): | |
os.mkdir(attachment_dir) | |
for dirpath, dirnames, filenames in da.Walk(gdb, datatype="Table"): | |
for table in filenames: | |
# Skip tables that don't end with "__ATTACH" | |
match = attach_table_name_re.match(table) | |
if not match: | |
continue | |
imgsubdir = join(attachment_dir, match.group(0)) | |
# Create the image subdirectory if it does not already exist | |
if not exists(imgsubdir): | |
os.mkdir(imgsubdir) | |
fields = ["DATA", "ATT_NAME", "REL_OBJECTID", "CONTENT_TYPE", "DATA_SIZE"] | |
table_path = join(dirpath, table) | |
with da.SearchCursor(table_path, fields) as cursor: | |
for row in cursor: | |
memview = row[0] #BLOB fields are returned as memoryview objects. | |
attname = row[1] #This is the filename of the attachment. | |
oid = row[2] # The OBJECTID of the feature associated with the attachment. | |
# Create the path that the image will be written to | |
oiddir = join(imgsubdir, str(oid)) | |
# Create the directory if it does not already exist | |
if not exists(oiddir): | |
os.mkdir(oiddir) | |
# Create the image path. | |
imgpath = join(oiddir, attname) | |
# Write the attachment to the filesystem. | |
open(imgpath, "wb").write(memview.tobytes()) | |
return attach_table_name_re, dirnames, dirpath, filenames | |
if __name__ == '__main__': | |
# Specify path to geodatabase containing images. | |
gdb = "./WildlifeHabConn.gdb" | |
# Specify the directory that will contain the output images. | |
imgdir = "images" | |
attach_table_name_re, dirnames, dirpath, filenames = extract_attachments(gdb, imgdir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment