replaceFieldMap() appears broken in arcpy

2245
6
07-17-2011 11:09 PM
SusanJones
Occasional Contributor II
ArcPy People:

The scenario is a simple one. I want to join a table view to a feature layer, clean up the output fields so the field name does not contain the table name, and export the contents to a file geodatabase using either FeatureClassToFeatureClass_management or TableToTable_management.

The python/arcpy script crashes out when it encounters either of these functions with the fieldMappings object as a parameter.

I can remove unwanted fields from the fieldMappings using removeFieldMap(). But when I replace a fieldMap in the fieldMappings with a newly formatted field name, the replaceFieldMap() command causes the script to fail.

We can easily rename output fields using the Geoprocessing Tools out of ArcCatalog and Model Builder. The problem is with arcpy and the replaceFieldMap().

My workaround thus far is to use layerfiles with a pre canned set of output field names.

It would be nice to have something a little more elegant.

(I am not sure if ESRI have raised this as a bug, local support here will try and recreate the issue using their own data and scripts)

Susan Jones
GIS Consultant
Spatial Logic Ltd
sjones@spatiallogic.co.nz
http://www.spatiallogic.co.nz
Tags (2)
0 Kudos
6 Replies
SusanJones
Occasional Contributor II
arcpy.env.qualifiedFieldNames = 0 wil suppress the table name in the field name.

Still no way of being to specify output field alias after a join.
0 Kudos
JoelCalhoun
New Contributor III
Susan,

This might not be exactly what you are looking for but I wrote a little python script for joining a table to a feature class.  The output preserves the input table attribute names instead of the table name being appended to each attribute name or the sequential _1, _2, _3, etc. being added using the standard ESRI join.

In this script I perserve the original field names from the input table.  I don't do anything with aliases.

In the script tool interface I have added an option to either preserve all table attributes in the output, regardless if they match an attribute name in the input GIS dataset, or to drop the table fields that match the input GIS dataset. 

In the case where you choose to keep all, the table field names that match existing input GIS dataset field names will have an _tab added to the attribute name to distinguish it from the existing input GIS dataset field names, this may result in the output table field names being truncated.

I have also included the standard option to keep all records or to just include the records that match.

Currently the script only supports input table types of dbf and excel.
Input GIS datasets supported are .shp, file geodatabase, and SDE feature classes.
Output is determined by the input GIS dataset type. 
Input .shp and SDE feature classes output as .shp and input file geodatabases output as file geodatabase.

The attached .zip file includes the python script and a toolbox with the associated script tool.

If you have any questions just reply to this thread.


Thanks,



Joel
0 Kudos
SusanJones
Occasional Contributor II
dear Joel

I think you have just answered my problem. I will take a look at the field mappings in the morning when I am more refreshed.

Earlier I came across the nimbus bug, after spending quite a few days wrestling with local support.

man, You are good.

susan
0 Kudos
SusanJones
Occasional Contributor II
Hi Joel and others,

A very big thank you to this. Working with text strings is a little (lot) scary but atleast it solves the problem of the replaceFieldMap()

Here is an example of what works, to solving NIM047605. (indentation is a bit out so I will also upload the python script separately).

---start
#the Way TO Get Around Field Mapping From NIM047605
fieldMappings = arcpy.FieldMappings()
fieldMappings.addTable("TableView")

#use exportToString() To Dump Contents Into An ASCII File
strFieldMaps = fieldMappings.exportToString()

#each FieldMap Is Separated By A semi colon
lstFieldMaps = string.strip(strFieldMaps,";")

#loop Through The FieldMaps
newfm = ""
for fm in lstFieldMaps:
   #get The AliasName, The First Occurance In lstFm
   lstFm = string.strip(fm, " ")
   aliasName = lstFm[0]
   newAliasName = aliasName + "_Alias"
   #assign The Alias
   fm = string.replace(fm,aliasName + " ",newAliasName + " ")
   #add To The New FieldMap
   newfm = newfm + ";"

#make The New FieldMap
fieldMappings.removeAll()
fieldMappings.loadFromString(newfm)

#convert To Table WIth The New FieldMappings
arcpy.TableToTable_conversion("TableView", "c:\\Temp\\new.gdb","newTable","",fieldMappings)

---end

Susan
0 Kudos
JoelCalhoun
New Contributor III
Yes, I agree, exporting the field map to a string, parsing it, and passing it back in is a little scary but at least it works. 🙂


Anyway, I'm glad I could help.



Joel
0 Kudos
SusanJones
Occasional Contributor II
Dear All:

Attached is the python script with all it's formatting to make it easier to follow and demonstrate.
Do a search on "NIM04" to get to the fieldMappings part in the arcpy script. .

Susan


the replaceFieldMap() Bug is located here in this piece of code
--begin
arcpy.AddMessage("***drop Existing Fields and Fields Not in the List")
outFieldList = getOutFieldList(application)
fieldMappings = arcpy.FieldMappings()
fieldMappings.addTable("TableView")

index = 0
while index < fieldMappings.fieldCount:

#get Field Map, fm
fm = fieldMappings.getFieldMap(index)
fld = fm.outputField
fldName = checkField(string.upper(fld.name))
fld.name = fldName
fm.outputField = fld
fieldMappings.replaceFieldMap(index,fm)
arcpy.AddMessage("***" + fld.name)

#next Field Map
index += 1

test = fieldMappings.exportToString()
lstTest = string.split(test,";")
for ts in lstTest:
arcpy.AddMessage(ts)

#tableToTable Fails
arcpy.TableToTable_conversion("TableView",outWsp,application,"",fieldMappings)
-- end
0 Kudos