I am transforming one feature class into another (currently empty) feature class.
The input feature class and output feature class share the same fields, but the output feature class has some additional fields. I'm aware I can do this using a Data Acess module InsertCursor, but I'm trying to learn how to use the FieldMappings module and got stuck here.
staging = 'C:/Path/To/Input.gdb'
gdb = 'C:/Path/To/Output.gdb'
input_fc = os.path.join(staging, 'Hazard')
output_fc = os.path.join(gdb, 'Hazard')
# Get a list of common fields
common_fields = [f for f in input_fields if f.name in [g.name for g in output_fields] and not f.required]
# Get a list of fields found in the output, but not in the input
uncommon_fields = [f for f in output_fields if not f.name in [g.name for g in input_fields] and not f.required]
# Get a list of fields found in the input, but not in the output
unmapped_fields = [f for f in input_fields if f.name not in [g.name for g in output_fields] and not f.required]
print [f.name for f in common_fields]
print '*' * 20
print [f.name for f in uncommon_fields]
print '*' * 20
print [f.name for f in unmapped_fields]
This returns
[u'hazard_type', u'inspection_staff', u'comments', u'inspection_date', u'pp_survey_cycle', u'pp_timestamp', u'record_uid', u'record_timestamp', u'flagged', u'reason_flagged', u'marked_for_deletion', u'data_quality_assured', u'data_qa_staff', u'data_qa_comments', u'data_qa_timestamp', u'gdb_timestamp']
********************
[u'pp_azu', u'pp_city', u'pp_state', u'pp_zip', u'pp_survey_group']
********************
[]
What I want to happen is for fields in common_fields
to get mapped over to output_fc
and fields in uncommon_fields
to remain Null.
Here's the code I'm using, which runs without error but doesn't append anything (grr ESRI...). Do I have to specifically remove fields from the FieldMappings object? How do I do that?
# Specify the mappings using a dictionary. We're just mapping fields that have the same name to one another here.
custom_mappings = {}
for f in input_fields:
if not f.required:
custom_mappings[f] = next((g for g in output_fields if g.name == f.name), None)
# Create field mapping object
fms = arcpy.FieldMappings()
# Specify the field map for each field and add it to the FieldMappings object
for field in input_fields:
if not field.required:
fm = arcpy.FieldMap()
fm.addInputField(input_fc, field.name)
fm.outputField = custom_mappings[field]
fms.addFieldMap(fm)
# Append to the output_fc using the field mappings
arcpy.Append_management(input_fc, output_fc, 'NO_TEST', fms)