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

Sunday, April 27, 2025

๐Ÿ“ Auto-Add Area and Length Fields in a Geodatabase Using ArcPy

 

๐Ÿ“ Auto-Add Area and Length Fields in a Geodatabase Using ArcPy

When managing spatial data in ArcGIS, calculating geometric properties like area or length is a common requirement—especially for analytics, reporting, or quality control.

In this post, you'll learn how to write a Python script using ArcPy that automatically adds:

  • a CALC_AREA field to all Polygon feature classes

  • a CALC_LENGTH field to all Polyline feature classes

All within a given File Geodatabase (.gdb)—whether the feature classes are inside datasets or at the root level.


๐Ÿงพ The Script

python
import arcpy import os # ๐Ÿ“ Set your input File Geodatabase path input_gdb = r"C:\Path\To\Your\DataModel.gdb" # Set the workspace arcpy.env.workspace = input_gdb # ๐Ÿ” List all feature datasets (or include root-level FCS by using [''] if none) datasets = arcpy.ListDatasets(feature_type='feature') or [''] # Loop through all datasets and standalone feature classes for dataset in datasets: dataset_path = os.path.join(input_gdb, dataset) if dataset else input_gdb feature_classes = arcpy.ListFeatureClasses(feature_dataset=dataset) for fc in feature_classes: fc_path = os.path.join(dataset_path, fc) if dataset else os.path.join(input_gdb, fc) # ๐Ÿง  Get geometry type (Polygon or Polyline) desc = arcpy.Describe(fc_path) geom_type = desc.shapeType.upper() # ๐Ÿ—️ Define field to add based on geometry type if geom_type == "POLYGON": field_name, field_type = "CALC_AREA", "DOUBLE" elif geom_type == "POLYLINE": field_name, field_type = "CALC_LENGTH", "DOUBLE" else: continue # Skip Points or unsupported types # ✅ Check if field exists before adding existing_fields = [f.name for f in arcpy.ListFields(fc_path)] if field_name not in existing_fields: arcpy.AddField_management(fc_path, field_name, field_type) print(f"✅ Added field '{field_name}' to '{fc_path}'") else: print(f"โ„น️ Field '{field_name}' already exists in '{fc_path}', skipping.") print("\n๐ŸŽ‰ Geometry field addition complete.")

๐Ÿ› ️ How It Works

  • ListDatasets() gets all feature datasets in your geodatabase.

  • ListFeatureClasses() gets each feature class inside them (or in the root).

  • Describe() helps us detect if a feature class is a Polygon or Polyline.

  • If the appropriate field (CALC_AREA or CALC_LENGTH) is missing, it gets added as a DOUBLE.


๐Ÿ“ฆ Why Use This?

  • You're preparing data for area/length calculations.

  • You want to standardize schema across multiple datasets.

  • You're doing bulk data migration or preparing for a data audit.


✍️ Bonus Tip: Calculate Values

If you'd like to calculate values right after adding the field, insert this below the AddField_management line:

python
if field_name == "CALC_AREA": arcpy.CalculateField_management(fc_path, field_name, "!shape.area!", "PYTHON3") elif field_name == "CALC_LENGTH": arcpy.CalculateField_management(fc_path, field_name, "!shape.length!", "PYTHON3")

No comments:

Post a Comment