Output field name not showing up in spatial join result

204
1
05-13-2019 07:51 PM
TheWorm
New Contributor

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.

0 Kudos
1 Reply
TheWorm
New Contributor

Heres another example of what I'm talking about.

I am trying to convert a shapefile to a feature class, I also have a geojson file with the field names as I would like them to appear in the output feature class. The `arcpy.FeatureClassToFeatureClass_conversion` functionality accepts a field map, so I tried to build one by looping through a geopandas geodataframe of the geojson file (to set my output field names) and by adding the shapefile to a fieldMappings object, and replacing the field map for each iteration:


    import arcpy
    import geopandas

    file_path = 'damage_results/Culverts/Culverts.shp'
    original_fields = arcpy.ListFields(file_path)
    original_fields = original_fields[2:]
    gdf = geopandas.read_file('damage_results/Culverts/result_culverts.json')
    new_field_names = gdf.columns[:-1].to_list()

    fms = arcpy.FieldMappings()
    fms.addTable(file_path)

    for i,field in enumerate(original_fields):
        print i
        fm = fms.getFieldMap(i)

        out_field = fm.outputField
        out_field.name = new_field_names
        fm.outputField = out_field
        fms.replaceFieldMap(i, fm)

    for fm in fms:
        print fm.outputField.name
arcpy.FeatureClassToFeatureClass_conversion(file_path, 'intermediate_damage.gdb',
'Culverts', field_mapping = fms)

The second `for` loop is to verify that the output field names have been written properly, and it seems from within the script that they are. However when I open the Culverts feature class the field names are unchanged from the shapefile, which is rather infuriating.

Does the output field attribute in the `FieldMap` object actually work? Or is this a bug that ESRI needs to fix?

0 Kudos