Extracting Attachments from a Geodatabase File Using Python
Introduction File Geodatabases (GDB) are commonly used in GIS for storing spatial and non-spatial data. When working with Survey123 or other Esri tools, attachments such as images and documents are stored within an attachment table. This article provides a Python script to extract these attachments efficiently.
Prerequisites
ArcGIS Pro or ArcMap with ArcPy installed.
A File Geodatabase (GDB) containing attachment tables.
Python 3.x configured with ArcGIS.
Understanding Attachment Tables Attachment tables in a GDB store binary data (files) and are linked to feature classes. The standard fields in these tables include:
REL_OBJECTID(orATTACHMENTID or rel_globalid): Links the attachment to a feature.ATT_NAME(orNAME): Stores the file name.DATA: Stores the binary file data.
Steps to Export and Extract Attachments from a File Geodatabase
Step 1: Exporting a GDB from Survey123
Open Survey123 and navigate to the survey with attachments.
Click on the 'Data' tab and select 'Download'.
Choose 'File Geodatabase' (GDB) as the export format.
Save the exported GDB to your system.
Step 2: Identifying the Attachment Table and Relevant Fields
Open ArcGIS Pro and add your GDB to a new project.
In the Catalog pane, navigate to your GDB and look for tables ending in
__ATTACH.Open the attribute table and check for key fields:
REL_OBJECTID(orATTACHMENTID) – Links the attachment to the feature.ATT_NAME(orNAME) – Stores the filename.DATA– Contains the binary file data.
Open QGIS and add your GDB to a LAYER > ADD LAYER > ADDD VECTOR LAYER>SOURCE TYPE = DIRECTORY > TYPE OpenFileGDB and select GDB folder click Add Layer.
In the Layers pane, navigate to your GDB and look for tables ending in
__ATTACH.Open the attribute table and check for key fields:
REL_OBJECTID(orATTACHMENTID) – Links the attachment to the feature.ATT_NAME(orNAME) – Stores the filename.DATA– Contains the binary file data.
Step 3: Running the Python Script in ArcGIS Pro
Open ArcGIS Pro.
Go to Geoprocessing Pane > Python Window.
Copy and paste the following Python script and press Enter.
Python Script to Extract Attachments
Step 4: Running the Script
Update the
gdb_pathwith the location of your File Geodatabase.Replace
attachment_tablewith the correct attachment table name.Set
output_folderto the directory where extracted files should be saved.Run the script in the Python window inside ArcGIS Pro.
Python Script to Extract Attachments Below is the Python script that automates the extraction process:
Python Script
-------------------------------------------Start-----------------------------------------------------
import arcpy
import os
def extract_attachments(gdb_path, attachment_table, output_folder):
"""
Extracts attachments from a File Geodatabase and saves them to an output folder.
:param gdb_path: Path to the File Geodatabase (.gdb)
:param attachment_table: Name of the attachment table (typically ends with _ATTACH)
:param output_folder: Directory to save extracted attachments
"""
if not os.path.exists(output_folder):
os.makedirs(output_folder)
attachment_table_path = os.path.join(gdb_path, attachment_table)
with arcpy.da.SearchCursor(attachment_table_path, ["rel_globalid", "att_name", "data"]) as cursor:
for row in cursor:
object_id, file_name, file_data = row
# Ensure file_name is valid
if not file_name:
file_name = f"attachment_{object_id}.bin"
# Define output file path
file_path = os.path.join(output_folder, file_name)
# Write binary data to file
with open(file_path, "wb") as file:
file.write(file_data)
print(f"Extracted: {file_name}")
if __name__ == "__main__":
# Use raw strings (r"") or double backslashes for Windows paths
gdb_path = r"C:\Users\DELL\Downloads\afish\S123_dccb77baa342419ebd5222941a4c5232_FGDB\_ags_data0675F4D84C6149F6B26B7466C77B1A69.gdb"
attachment_table = r"C:\Users\DELL\Downloads\afish\S123_dccb77baa342419ebd5222941a4c5232_FGDB\_ags_data0675F4D84C6149F6B26B7466C77B1A69.gdb\service_96d10997b3a64cb889af8a1ab74f2298_survey__ATTACH"
output_folder = r"E:\naba\gdbattch"
extract_attachments(gdb_path, attachment_table, output_folder)
------------------------------------END-------------------------------------------------







Comments
Post a Comment