๐Ÿ” 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 ...

Friday, April 25, 2025

๐Ÿ” Automating Field Addition in File Geodatabases using ArcPy

 

๐Ÿ” Automating Field Addition in File Geodatabases using ArcPy

Managing large GIS data repositories often requires performing the same operation across multiple datasets and feature classes. Whether you're maintaining metadata, tracking source information, or preparing datasets for analysis, automating repetitive tasks can save a lot of time.

In this post, I’ll show you a Python script using ArcPy that automatically adds a predefined set of fields to all feature classes in all File Geodatabases (.gdb) within a given folder. This is especially useful when you're dealing with standardized data models or working on large-scale projects with multiple data sources.


What This Script Does

  • Searches for all .gdb (File Geodatabases) in a specified folder.

  • For each geodatabase:

    • Adds fields to standalone feature classes.

    • Adds fields to feature classes inside feature datasets.

  • Checks if each field already exists before trying to add it.

  • Handles both TEXT and DATE field types.


๐Ÿง  How to Use It

You’ll need:

  • ArcGIS Pro or ArcMap with ArcPy available.

  • A folder path where your .gdb files are stored.

Update the gdb_folder and fields_to_add as needed.


๐Ÿงพ The Script

python
import arcpy import os # ๐Ÿ“ Set the directory where your .gdb files are stored gdb_folder = r'C:\Path\To\Your\Geodatabases' # ๐Ÿ“ Define the fields to be added: (field name, type, optional length) fields_to_add = [ ("OriginalName", "TEXT", 255), ("OriginalPath", "TEXT", 1000), ("DIA_ID", "TEXT", 255), ("DIA_Date", "DATE", None) ] # ๐Ÿš€ Function to add fields to a given feature class def add_fields_to_feature_class(feature_class): for field_name, field_type, field_length in fields_to_add: try: existing_fields = arcpy.ListFields(feature_class, field_name) if not existing_fields: if field_length and field_type.upper() == "TEXT": arcpy.AddField_management(feature_class, field_name, field_type, field_length=field_length) else: arcpy.AddField_management(feature_class, field_name, field_type) print(f"✅ Added field '{field_name}' to {feature_class}") else: print(f"โ„น️ Field '{field_name}' already exists in {feature_class}") except Exception as e: print(f"❌ Error adding field '{field_name}' to {feature_class}: {e}") # ๐Ÿ”„ Loop through each .gdb in the folder for item in os.listdir(gdb_folder): if item.endswith(".gdb"): gdb_path = os.path.join(gdb_folder, item) arcpy.env.workspace = gdb_path print(f"\n๐Ÿ“‚ Processing Geodatabase: {gdb_path}") # Process standalone feature classes fcs = arcpy.ListFeatureClasses() if fcs: for fc in fcs: print(f"๐Ÿ”น Feature Class: {fc}") add_fields_to_feature_class(fc) else: print("⚠️ No standalone feature classes found.") # Process feature datasets datasets = arcpy.ListDatasets(feature_type='Feature') if datasets: for ds in datasets: print(f"\n๐Ÿ“ Dataset: {ds}") ds_fcs = arcpy.ListFeatureClasses(feature_dataset=ds) if ds_fcs: for fc in ds_fcs: print(f"๐Ÿ”ธ Feature Class in Dataset: {fc}") add_fields_to_feature_class(fc) else: print(f"⚠️ No feature classes in dataset '{ds}'") else: print("⚠️ No feature datasets found.") print("\n๐ŸŽ‰ Field addition completed for all geodatabases.")

No comments:

Post a Comment