I am using arcpy
to merge multiple feature classes from a list of feature classes. They all have the same fields. Let's say they all have the fields = ['CO_FIPS', 'MILES', 'FLOOD_ZONE', 'VALID', 'REGION']
. However, I only want to include 'CO_FIPS', 'MILES', and 'FLOOD_ZONE' in the output merged feature class. It seems I need to use field mapping
to limit the fields in my output, but I am not 100% how to do that:
working_dir = r"C:\Projects\MyProj\stream_lines.gdb"
arcpy.env.workspace = working_dir
s_studies_for_merge = ['stream1', 'stream2', 'stream3', ... 'stream10']
fieldMappings = arcpy.FieldMappings()
merged_output = os.path.join(working_dir, "AllStreams")
fieldMappings.addTable('stream1')
for field in fieldMappings.fields:
if field.name not in ['CO_FIPS', 'MILES', 'FLOOD_ZONE']:
fieldMappings.removeFieldMap(fieldMappings.findFieldMapIndex(field.name))
arcpy.Merge_management(s_studies_for_merge, merged_output, fieldMappings)
My confusion is mainly that most examples show only 2 features being merged and that the mapping involves settings that I don't need for certain fields. I only want to exclude the fields I don't want and keep the fields I do. What do I need to do here?
UPDATE
I tried applying the below answer(@geodranic) this way (assuming I needed to iterate and add field mapping for each of the 10 feature classes in my s_studies_for_merge
list):
mappings = arcpy.FieldMappings()
fields = ['REACH_ID', 'STUDY_ID', 'CID', 'FLD_ZONE', 'VALIDATION_STATUS', 'STATUS_TYP', 'MILES', 'STATUS_DATE', 'STUDY_TYPE', 'LINE_TYPE', 'BS_ZONE', 'BS_STDYTYP', 'PRELIM_DATE']
for item in s_studies_for_merge:
print(item)
for field in fields:
print(field)
map = arcpy.FieldMap()
map.addInputField(item, field)
arcpy.Merge_management(s_studies_for_merge, s_studs, mappings)
I got an error that the merge failed because of one of the fields I didn't want included in the merge:
ExecuteError: ERROR 001156: Failed on input OID 1, could not write value 'VIII' to output field FEMA_Region
Failed to execute (Merge)
How can I apply this correctly?
-
You could always just generate a list of the fields on your output, and simply remove the ones you don't want to keep. easy as generating a list of them with list comprehension. Something like fld_lst = [f for f in whateverfc] then whatever fields you dont you want to keep, just use arcpy.DeleteField_management(whatever field or fields)geodranic– geodranic2019年10月30日 12:04:37 +00:00Commented Oct 30, 2019 at 12:04
-
My problem with that approach is that 1) These data sets are huge. Doing an arcpy merge between 10 feature classes with 100k rows and 30 + columns takes a lot of time. By parsing down the columns to just th 5 or 6 that I need, I can speed up the process. 2) When I tried to do a straight merge, I found 1 column (REGIONS), which was a string, was set to 3 char length in some features and 4 in others, which threw an error. So, to overcome that error, I'd still have to do field mapping on the front end during the merge.gwydion93– gwydion932019年10月30日 12:38:57 +00:00Commented Oct 30, 2019 at 12:38
1 Answer 1
Just drop the fields
fields_to_drop = ['junkfield', 'junkfield', 'junkfield', 'junkfield']
arcpy.DeleteField_management(your_featureclass, fields_to_drop)
if you don't know what fields are going to be there, you can generate a list of fieldnames, along with a list of your kept ones
keepers = ['goodstuff', 'goodstuff1', goodstuff2']
badstuff = [f for f in arcpy.ListFields(your_featureclass) if not f in keepers]
now just delete your badstuff list
arcpy.DeleteField_management(your_featureclass, badstuff)
Since you mention you need to drop during merge
keepers = ['CO_FIPS', 'MILES', 'FLOOD_ZONE']
mappings = arcpy.FieldMappings()
for field in keepers:
map = arcpy.FieldMap()
map.addInputField(yourfeatureclass, field)
mappings.addFieldMap(map)
arcpy.Merge_management(s_studies_for_merge, merged_output, mappings)
-
1While this would technically work, I really need to drop the fields during the merge, rather than after. Partly due to the data size and partly due to an slight difference in one of the shared field lengths would require field mapping during the merge anyway.gwydion93– gwydion932019年10月30日 12:48:02 +00:00Commented Oct 30, 2019 at 12:48
-
1I updated the answer, give that a shotgeodranic– geodranic2019年10月30日 13:03:59 +00:00Commented Oct 30, 2019 at 13:03
-
Question: since I am merging 10 feature class, do I need to apply
map.FieldMap()
to each one of those feature classes. In other words, add afor loop
to iterate through each one of the features ins_studies_for_merge' with your
for field in keeps: ....` inside?gwydion93– gwydion932019年10月30日 14:20:26 +00:00Commented Oct 30, 2019 at 14:20 -
The above example is going to apply to your merged output, regardless of how many files it isgeodranic– geodranic2019年10月31日 05:55:33 +00:00Commented Oct 31, 2019 at 5:55
-
In
map.addInputField(yourfeatureclass, field)
, what do I use foryourfeatureclass
? Just one of the fandom featureclasses in mys_studies_for_merge
list? When I iterated through the list and applied it to all 10, the merge completed, but I had 10 sets of the 'keeper' fields.gwydion93– gwydion932019年10月31日 13:12:56 +00:00Commented Oct 31, 2019 at 13:12
Explore related questions
See similar questions with these tags.