0

I am trying to merge 10 stream feature classes together using arcpy. But I need to limit the number of fields in the output (all feature classes share the same fields).

working_dir = r"C:\Projects\MyProj\stream_lines.gdb"
arcpy.env.workspace = working_dir
all_regions_name = "CNMS_AllRegions_{}.gdb".format(working_dir_name)
all_regions_gdb = os.path.join(working_dir, all_regions_name)

s_studies_for_merge = ['stream1', 'stream2', 'stream3', ... 'stream10']
merged_output = os.path.join(working_dir, "AllStreams")

fields = ['REACH_ID', 'STUDY_ID', 'CID', 'FLD_ZONE', 'VALIDATION_STATUS', 
          'STATUS_TYP', 'MILES', 'STATUS_DATE', 'STUDY_TYPE', 'LINE_TYPE',
          'BS_ZONE', 'BS_STDYTYP']
cnms_inventory = os.path.join(all_regions_gdb, "CNMS_Inventory")
s_studs = os.path.join(cnms_inventory, "S_Studies_Ln")

To do this I am trying to apply field mapping during the merge (I specifically need to limit the fields during the merge, not after). I am having difficulty getting only the fields I want mapped and excluding those that do not belong.

fieldMappings = arcpy.FieldMappings()
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)

When I run the merge above, it is still trying to merge a field that I don't want. What am I doing incorrectly here?

  • 2
    You haven't added any of the FieldMap objects to the fieldMappings object. You use a mappings variable in one spot, but define fieldMappings earlier. – user2856 Oct 30 '19 at 19:29
1

Fieldmap() and Fieldmappings() are hard to program.

What I do is run the merge tool interactively including setting the fields to merge and suppress. When this works properly, copyAsSnippet in the results pane and paste it into your script.

The fieldmappings() will be serialised into very long strings. This doesn't matter, but you can tidy it up by using treble quotes and breaking the string at semicolons. You can replace the input hardcoded paths with variables and if you set a workspace first you can leave out all the paths too.

  • Lol, I tried this repeatedly and ArcMap kept crashing. I was able to copy the long string in the Interactive Run window. I'll see if I can clean it up and use it. – gwydion93 Oct 31 '19 at 13:55
  • It is probably because the cmd buffer size is being exceeded. The data sources must have too many fields. CopyAsSnippet is effectively a command line version in text. Have you considered using layers? By using layers you can suppress fields before you merge. You use a FieldInfo() object which is easier to use than a fieldmapping, except that renaming doesn't rename the alias, so just limit it to VISIBLE and HIDDEN (not INVISIBLE!) – KimO Oct 31 '19 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.