I'm scripting a spatial join in ArcGIS with Python. I only want the resultant feature class to have a single field containing one attribute from the join features. In general, many join features overlap a single target feature and I want to preserve the attributes from all of them. I'm joining one-to-one, with the 'join' merge rule and a comma delimiter.
The problem arises when the new field exceeds 50 characters (the default) and the spatial join fails. I need to update the field's 'length' attribute, but I can't - if I assign a new value to it, nothing changes.
Here's a snippet that generates the field mapping object that I'll pass to spatial join:
# Generate a field mappings object and add the two join layers
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(outPolys)
fieldmappings.addTable(overlapping_SAs)
# facID_str is the field that we want
# Set its merge rule to 'join' and add a comma delimiter
# Get all field names from the join features
fnames = arcpy.ListFields(overlapping_SAs)
for field in fnames:
if field.name == "facID_str":
fidx = fieldmappings.findFieldMapIndex(field.name)
fmap = fieldmappings.getFieldMap(fidx)
# Set the merge rule to sum and update the object
fmap.mergeRule = "join"
fmap.joinDelimiter = ","
fieldmappings.replaceFieldMap(fidx, fmap)
# Delete all other fields
else:
fidx = fieldmappings.findFieldMapIndex(field.name)
if fidx > 0:
fieldmappings.removeFieldMap(fidx)
I can see the field I want by calling fieldmappings.fields[1].length but if I try to set the length to something other than 50, nothing happens. For example:
>>> fieldmappings.fields[1].length
50
>>> fieldmappings.fields[1].length = 500
>>> fieldmappings.fields[1].length
50
The eventual call to spatial join looks like this: arcpy.SpatialJoin_analysis(outPolys, overlapping_SAs, outJoin, "JOIN_ONE_TO_ONE", "KEEP_COMMON", fieldmappings, "WITHIN")
How do I set length to a value more appropriate for my application?