AnsweredAssumed Answered

Output field name not showing up in spatial join result

Question asked by awa5114 on May 13, 2019
Latest reply on May 14, 2019 by awa5114

I am trying to use the `FieldMap` and the `FieldMappings` classes in `arcpy` in the context of a spatial join. I have two line layers as feature classes. These are `Roads` and `Culverts`. Roads has a copy of the `OBJECTID` field called `ID`. I am spatially joining `Roads` to `Culverts`, but I would like to control the name of the joined `ID` field from the road layer. Here's some python code I am using:

 

fm = arcpy.FieldMap()

fms = arcpy.FieldMappings()

fm.addInputField('Road', 'ID')

out_field = fm.outputField

out_field.name = 'ROID'

fm.outputField = out_field

fms.addFieldMap(fm)

 

arcpy.SpatialJoin_analysis(target_features="test.gdb/Culvert", join_features="test.gdb/Road", out_feature_class="test.gdb/join", join_operation="JOIN_ONE_TO_ONE", join_type="KEEP_ALL", field_mapping =fms, match_option="CLOSEST", search_radius="", distance_field_name="")

 

Unfortunately I get a result where the field is called `ID` (just like in the original road layer) and the values are all 0 in this field (which is incorrect). I have a couple of questions: - how do I control the name of the output field using the `FieldMap` and `FieldMappings` classes? - Why are the attributes all 0 and how can I fix that? - Can I use the `OBJECTID` of the road layer directly without having to make a copy called `ID`, and using the `FieldMap` and `FieldMappings` classes THanks.

Outcomes