HOW TO

List feature class comparisons in ArcGIS Pro using ArcPy

Last Published: August 8, 2024

Summary

In ArcGIS Pro, comparing the differences between two feature classes and returning the differences in the field structure, such as listing missing fields, ensures data quality, accuracy, and reliability for better decision-making and analysis. This article describes the workflow to compare two feature classes in ArcGIS Pro using ArcPy.

Procedure

Note: 
This workflow requires a full script to run in the ArcGIS Pro Python window. The indents must be retained as portrayed in the code block.
  1. Open a project in ArcGIS Pro.
  2. Open the Python window. Refer to ArcGIS Pro: Python window for more information.
  3. Run the following script:
    1. Retrieve and store the field names and types of the two datasets for comparison.
import arcpy

def compare_feature_classes(fc1, fc2):
    def get_fields_info(fc):
        fields = arcpy.ListFields(fc)
        fields_info = {field.name: field.type for field in fields}
        return fields_info
  1. Fetch and store information about the fields of the two different feature classes.
    fields_info1 = get_fields_info(fc1)
    fields_info2 = get_fields_info(fc2)
  1. Create a list for the comparison of the feature classes.
    missing_in_fc2 = []
    missing_in_fc1 = []
    different_types = []
  1. Check the missing fields in both feature classes.
    for field_name, field_type in fields_info1.items():
        if field_name not in fields_info2:
            missing_in_fc2.append(field_name)
        elif fields_info2[field_name] != field_type:
          different_types.append((field_name, field_type, fields_info2[field_name]))

    for field_name in fields_info2.keys():
        if field_name not in fields_info1:
            missing_in_fc1.append(field_name)
  1. Return the results.
    return {
        "missing_in_fc2": missing_in_fc2,
        "missing_in_fc1": missing_in_fc1,
        "different_types": different_types
    }
  1. Specify the file path of both feature classes. Replace <pathToFc1> and <pathToFc2> with the path of the respective feature classes.
fc1 = r"<pathToFc1>"
fc2 = r"<pathToFc2>"
  1. Compare the feature classes and print the results.
differences = compare_feature_classes(fc1, fc2)

print("Fields missing in fc2:", differences["missing_in_fc2"])
print("Fields missing in fc1:", differences["missing_in_fc1"])
print("Fields with different types:", differences["different_types"])

The code block below demonstrates the full script.

import arcpy

def compare_feature_classes(fc1, fc2):
    def get_fields_info(fc):
        fields = arcpy.ListFields(fc)
        fields_info = {field.name: field.type for field in fields}
        return fields_info

    fields_info1 = get_fields_info(fc1)
    fields_info2 = get_fields_info(fc2)

    missing_in_fc2 = []
    missing_in_fc1 = []
    different_types = []

    for field_name, field_type in fields_info1.items():
        if field_name not in fields_info2:
            missing_in_fc2.append(field_name)
        elif fields_info2[field_name] != field_type:
          different_types.append((field_name, field_type, fields_info2[field_name]))

    for field_name in fields_info2.keys():
        if field_name not in fields_info1:
            missing_in_fc1.append(field_name)

    return {
        "missing_in_fc2": missing_in_fc2,
        "missing_in_fc1": missing_in_fc1,
        "different_types": different_types
    }

fc1 = r"C:\Training\ProxAnalysisIntro\ProxAnalysisIntro\GymAnalysis\GymAnalysis.gdb\GymLocations"
fc2 = r"C:\Training\ProxAnalysisIntro\ProxAnalysisIntro\GymAnalysis\GymAnalysis.gdb\Members"

differences = compare_feature_classes(fc1, fc2)

print("Fields missing in fc2:", differences["missing_in_fc2"])
print("Fields missing in fc1:", differences["missing_in_fc1"])
print("Fields with different types:", differences["different_types"])

The image below shows the comparison of the missing fields between the two feature classes printed in the Python window.

The comparison of the missing fields between the two feature classes printed in the Python window

Article ID: 000033164

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 3
  • ArcGIS Pro 3 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options