Select to view content in your preferred language

Field mappings remove field on a joined layer

1598
5
Jump to solution
04-10-2019 05:03 AM
MKF62
by
Frequent Contributor

Can you remove a field on a joined layer? I am trying to remove some fields before exporting the dataset, but it won't allow me to do so. The fields I am attempting to remove are the joined fields. The only work around I have to this would be to create a selection on the joined layer that selects all the records, remove the join, then export the data. I cannot use Join Field because this should technically be an "invisible" join - I am only joining the tables because I need to filter the data before exporting as the original data are related tables; I don't actually need any of the joined fields in the output.

mgmtJoin = arcpy.AddJoin_management(tractLyr, 'MgmtTractID', MgmtTractAttrbTV, 'MgmtTractID', 'KEEP_COMMON')

fieldmappings = arcpy.FieldMappings()
#This is the target feature class of the join. I joined a table to this feature class.
fieldmappings.addTable(tractFC)
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_OBJECTID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_MgmtTractID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_FocalRefID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_StateID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_VegMgmtPractice"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Herbicide"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_MonthTreated"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_YearTreated"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_ImplementedBy"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_FundedBy"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_FarmBillCode"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Comments"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Area_Acres"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_DateAdded"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Contract_Number"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_CLU_Number"))

arcpy.FeatureClassToFeatureClass_conversion(tractLyr, exportPath, "MgmtTracts", "#", fieldmappings)
arcpy.RemoveJoin_management(tractLyr)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
MKF62
by
Frequent Contributor

Alright, I lied; you can use fieldmappings on layers and not just feature classes. I swear I tried this before and it didn't work, but it's working now, so I must have done something wrong the first time. The problem with my original code posted above is that my tractFC doesn't actually have the joined fields, just the original fields, so when I added the feature class table to the fieldmappings object, the only fields I was trying to remove didn't exist. I changed it to my layer and it started working. Final code:

arcpy.AddJoin_management(tractLyr, 'MgmtTractID', MgmtTractAttrbTV, 'MgmtTractID', 'KEEP_COMMON')

fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(tractLyr)
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_OBJECTID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_MgmtTractID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_FocalRefID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_StateID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_VegMgmtPractice"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Herbicide"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_MonthTreated"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_YearTreated"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_ImplementedBy"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_FundedBy"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_FarmBillCode"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Comments"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Area_Acres"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_DateAdded"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Contract_Number"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_CLU_Number"))

arcpy.FeatureClassToFeatureClass_conversion(tractLyr, exportPath, "MgmtTracts", "#", fieldmappings)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
arcpy.RemoveJoin_management(tractLyr)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

Feature Class to Feature Class—Conversion toolbox | ArcGIS Desktop 

states

To remove fields during processing, delete output fields from the Field Map. This will not affect the input.

Are you saying this isn't working?

0 Kudos
MKF62
by
Frequent Contributor

Correct. The output of the join table currently looks like this (all the fields I'm trying to remove are there, I just cut it off cause it's long):

I am trying to remove all those fields with the MgmtTractAttrb prefix. To do that, you first have to create a fieldmappings object. Then add the table you want to delete things from to that object. Then find and delete the field map (after you add the table to the fieldmappings object, each field in that table is automatically a field map object).

The table must be a feature class or table view, not a feature layer. I have tried using a feature layer and while the script runs, none of the fields get deleted.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Yes, I just didn't see the section where you are deleting the fields in your code

0 Kudos
MKF62
by
Frequent Contributor

fieldmappings.removeFieldMap is supposed to delete the fields unless I am mistaken. Looking at the documentation though, this seems contradictory. Does it change the input features or not?:

The Field Map parameter controls how the input fields in the Input Features will be written to the Output Features.

  • To remove fields during processing, delete output fields from theField Map. This will not affect the input.

The bolded statements completely contradict each other...

For what it's worth, when I do this in the GUI in Desktop, the tool does what it's supposed to do and removes all the fields I want.

0 Kudos
MKF62
by
Frequent Contributor

Alright, I lied; you can use fieldmappings on layers and not just feature classes. I swear I tried this before and it didn't work, but it's working now, so I must have done something wrong the first time. The problem with my original code posted above is that my tractFC doesn't actually have the joined fields, just the original fields, so when I added the feature class table to the fieldmappings object, the only fields I was trying to remove didn't exist. I changed it to my layer and it started working. Final code:

arcpy.AddJoin_management(tractLyr, 'MgmtTractID', MgmtTractAttrbTV, 'MgmtTractID', 'KEEP_COMMON')

fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(tractLyr)
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_OBJECTID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_MgmtTractID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_FocalRefID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_StateID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_VegMgmtPractice"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Herbicide"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_MonthTreated"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_YearTreated"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_ImplementedBy"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_FundedBy"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_FarmBillCode"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Comments"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Area_Acres"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_DateAdded"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_Contract_Number"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("MgmtTractAttrb_CLU_Number"))

arcpy.FeatureClassToFeatureClass_conversion(tractLyr, exportPath, "MgmtTracts", "#", fieldmappings)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
arcpy.RemoveJoin_management(tractLyr)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos