Select to view content in your preferred language

Multiple Spatial joins - maintain distance_field_name

175
2
08-22-2024 12:57 PM
CaitlinSjodin
Emerging Contributor

Hi all,

 

I'm putting together a python script that utilizes spatial joins (arcpy.analysis.SpatialJoin()), and I'm running 4 spatial joins one after the other, each using the previously created layer for the next target layer, 3 of the joins require the use of a distance_field_name, but the final join layer only produces the last distance_field_name. 

Is this the expected functionality of the tool? Do I have to create a separate field to move those values into before running the next join?

It's a pretty basic script, but I've attached it here.

 

# Variables established not shown

# all field mapping required
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(san_layer)
fieldmappings.addTable(r"memory\road_buffer")
fieldmappings.addTable(select_building)
fieldmappings.addTable(water_layer)
fieldmappings.addTable(rail_layer)
# fields to keep
keep_field =["COA_ID","DIAMETER","ROADCLASS","FULLNAME","Distance_To_WaterBody","Distance_to_RailRoads","Distance_To_Social_Features","Depth","PROX_TO_REG_SEN_SCORE","DIAMETER_SCORE","ROAD_CLASS_SCORE","SOCIAL_RESOURCES_SCORE","DEPTH_SCORE", "REGULATORY_SCORE","SOCIAL_SCORE","ECONOMIC_SCORE","COF_SCORE","COF_DESCRIPTION","Distance_to_RailRoads","Distance_To_Social_Features","Distance_To_WaterBody"]

for field in fieldmappings.fields:
    if field.name not in keep_field:
        fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex(field.name))
    

# Spatial Join where main and buffer intersect
# Fields required from road layer = ROAD_CLASS, FULLNAME
#Fields required from sanmain = COAID, 
arcpy.analysis.SpatialJoin(san_layer, r"memory\road_buffer", r"memory\RoadJoin", "", "", field_mapping = fieldmappings)

arcpy.analysis.SpatialJoin(r"memory\RoadJoin", select_building, r"memory\BuildingJoin", "", "", fieldmappings, "CLOSEST", "500 Meters", "Distance_To_Social_Features")

arcpy.analysis.SpatialJoin(r"memory\BuildingJoin", water_layer, r"memory\WaterJoin", "", "", fieldmappings, "CLOSEST", "100 Meters", "Distance_To_WaterBody")

arcpy.analysis.SpatialJoin(r"memory\WaterJoin", rail_layer, r"memory\FinalJoinLayer", "", "", fieldmappings, "CLOSEST", "10 Meters", "Distance_to_RailRoads")

 

Everything runs as expected, other than the loss of the distance_field_name field between each spatial join.

 

Thank you for any help!

 

Caitlin

 

 

2 Replies
CaitlinSjodin
Emerging Contributor

I'm not always the brightest crayon in the box. As soon as I posted, I realized that I didn't have the created layers added to the fieldmapping, so it wasn't saving the fields from those tables. If I remove the field mapping aspect, the joins work as expected. I'm not sure how to add the tables after the field mapping has been created, so I'll run the tool without using the field mapping for now.

0 Kudos
MaazaMekuria
Frequent Contributor

Caitlin, there are many ways to add the tables. Ordinarily the newly created tables are by default joined to the original data and the output table should include common fields.  Perhaps the best way to preserve the data is to create an attribute based join with the source feature class through a column join and save this join into a new  feature layer to save permanently .  You can do this every time you run the analysis and also eliminate the duplicate fields as you go along or after you have gone through the whole process.  

 

0 Kudos