HOW TO
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.
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.
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"<pathToFc1>"
fc2 = r"<pathToFc2>"
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.
Article ID: 000033164
Get help from ArcGIS experts
Download the Esri Support App