๐Ÿ” Extract Field Names Containing 'type' (Integer Fields Without Domain) from GDB Using ArcPy

  ⚙️ How the Script Works ๐Ÿ—‚️ Geodatabase Setup The script starts by pointing to a target File Geodatabase (.gdb) and initializing a CSV ...

Wednesday, May 21, 2025

๐Ÿ“ธ Batch Extraction of Photo Filenames from Fields in Multiple GDBs Using ArcPy

๐Ÿ–ผ️ Batch Filename Extraction for Photo Fields Across Multiple GDBs (ArcPy)

In many GIS workflows, feature classes often store file paths to images or documents—such as photos captured in the field. However, storing the full file path may not always be ideal. Sometimes you only need the filename (e.g., for linking, display, or standardization purposes).

This ArcPy script automates the process of extracting filenames from path strings for specified fields across multiple File Geodatabases (GDBs). It's a powerful way to clean and standardize your photo field data with minimal manual effort.


๐Ÿ”„ How the Script Works

๐Ÿ“‚ Directory Setup

The script starts by defining the folder containing all your GDBs. It automatically loops through each .gdb file inside that folder.

๐Ÿงพ Fields & Expressions

You define a list of fields to update (Photo, Photo1, Photo2, etc.) and a corresponding list of Python expressions that extract just the filename from each path using:

python
!Photo!.split('\\\\')[-1]

This expression splits the full file path by double backslashes (\\) and retrieves the last part (i.e., the filename).

๐Ÿ” GDB Traversal

The script processes:

  • Standalone feature classes

  • Feature classes inside datasets

Each feature class is scanned, and if the field exists, it gets updated using arcpy.management.CalculateField() with the specified Python expression.

๐Ÿ›ก️ Smart Checks

If a field doesn’t exist in a feature class, the script skips it gracefully with a message.


๐Ÿง  The Code

python
import arcpy import os # === USER INPUT === gdb_folder = r'C:\Path\To\Your\GDBs' fields_to_update = ["Photo", "Photo1", "Photo2"] field_values_to_update = [ "!Photo!.split('\\\\')[-1]", "!Photo1!.split('\\\\')[-1]", "!Photo2!.split('\\\\')[-1]" ] for folder in os.listdir(gdb_folder): if folder.endswith(".gdb"): gdb_path = os.path.join(gdb_folder, folder) arcpy.env.workspace = gdb_path # --- 1. Standalone Feature Classes --- feature_classes = arcpy.ListFeatureClasses() for fc in feature_classes: print(f"Working on standalone Feature Class: {fc}") for field_name, update_expression in zip(fields_to_update, field_values_to_update): if arcpy.ListFields(fc, field_name): arcpy.management.CalculateField(fc, field_name, update_expression, "PYTHON3") print(f"✅ Updated {field_name} in {fc}") else: print(f"⚠️ Field {field_name} not found in {fc}, skipping.") # --- 2. Feature Classes in Datasets --- datasets = arcpy.ListDatasets() or [] for ds in datasets: arcpy.env.workspace = os.path.join(gdb_path, ds) dataset_feature_classes = arcpy.ListFeatureClasses() for fc in dataset_feature_classes: print(f"Working on Feature Class: {fc} in Dataset: {ds}") for field_name, update_expression in zip(fields_to_update, field_values_to_update): if arcpy.ListFields(fc, field_name): arcpy.management.CalculateField(fc, field_name, update_expression, "PYTHON3") print(f"✅ Updated {field_name} in {fc}") else: print(f"⚠️ Field {field_name} not found in {fc}, skipping.") print("\n✅ Field update process completed.")

๐Ÿ’ก Use Case Scenarios

  • Removing long or broken directory paths from image/document fields

  • Extracting filenames for linking in web maps or reporting systems

  • Normalizing photo fields across merged or imported datasets

  • Automating repetitive clean-up tasks across dozens of GDBs


✅ Key Benefits

  • Works seamlessly across multiple geodatabases

  • Applies to both standalone and nested feature classes

  • Uses Python 3 expressions, ensuring compatibility with ArcGIS Pro

  • Gracefully skips missing fields to avoid errors

No comments:

Post a Comment