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

Monday, April 28, 2025

🧩 Add Standard Metadata Fields to All Feature Classes in Multiple Geodatabases Using ArcPy

 

🧩 Add Standard Metadata Fields to All Feature Classes in Multiple Geodatabases Using ArcPy

When working on large GIS projects—especially in urban planning, infrastructure, or government contexts—it's essential to maintain consistent metadata across all your datasets.

This script automates the process of adding predefined fields to every feature class in multiple file geodatabases (.gdb) inside a folder. It's perfect for enforcing a standard schema and ensuring that downstream users and systems have the metadata they need.


🎯 What the Script Does

  • Scans a folder for all .gdb files (file geodatabases).

  • For each .gdb:

    • Adds a set of user-defined fields to all standalone feature classes.

    • Adds the same fields to all feature classes inside datasets.

  • Skips adding a field if it already exists.


πŸ“œ The Python Script (ArcPy)

python
import arcpy import os # πŸ“ Folder containing your .gdb files gdb_folder = r'C:\Path\To\Your\GeodatabaseFolder' # 🧾 Fields to be added to each feature class fields_to_add = [ ("OriginalName", "TEXT", 255), ("OriginalPath", "TEXT", 1000), ("DIA_ID", "TEXT", 255), ("DIA_Date", "Date", 255) ] # 🧰 Function to add fields to a single feature class def add_fields_to_fc(feature_class): for field_name, field_type, field_length in fields_to_add: fields = arcpy.ListFields(feature_class, field_name) if not 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}' of type {field_type} to {feature_class}") else: print(f"β„Ή️ Field '{field_name}' already exists in {feature_class}") # πŸ” Loop through all geodatabases in the folder for folder in os.listdir(gdb_folder): if folder.endswith(".gdb"): gdb_path = os.path.join(gdb_folder, folder) arcpy.env.workspace = gdb_path print(f"\nπŸ“¦ Processing Geodatabase: {gdb_path}") # ➕ Standalone Feature Classes feature_classes = arcpy.ListFeatureClasses() for fc in feature_classes: print(f"πŸ”Ή Standalone FC: {fc}") add_fields_to_fc(fc) # πŸ“ Feature Datasets datasets = arcpy.ListDatasets(feature_type='Feature') for dataset in datasets: dataset_fcs = arcpy.ListFeatureClasses(feature_dataset=dataset) for fc in dataset_fcs: print(f"πŸ”Έ FC in Dataset '{dataset}': {fc}") add_fields_to_fc(fc) print("\nπŸŽ‰ Metadata field addition complete.")

🧠 Use Cases

  • Standardizing data before submission to a national GIS database.

  • Adding internal tracking fields to all project layers.

  • Preparing spatial data for external collaboration or audit.


πŸ”§ Customizing the Script

You can change or expand the fields_to_add list as needed:

python
fields_to_add = [ ("NewField", "TEXT", 50), ("ImportDate", "DATE", None), ("Status", "TEXT", 25) ]

πŸ’‘ Tips

  • Want to calculate values too? Use arcpy.CalculateField_management() after adding the field.

  • Running in ArcGIS Pro? Make sure your Python environment is set to arcgispro-py3.

  • For batch runs, wrap this into a .bat file and run on schedule using Task Scheduler.

No comments:

Post a Comment