Hello. I'm on arcgis desktop 10.5.1 using python 2.7.
I've got a very simple spatial join in my script but I cant get my head around the field mapping object.
I'd like to join two identically overlapping features (polylines) which have the same schema but different attributes. The only field I care about keeping in the join is an 'ID' string field. I'd like to keep both ID entries as a delimited list in the output layer.
E.g
Input of coincident features from 2 layers.
OBJECTID | ID |
1 | 5 |
89 | 9 |
Desired output
OBJECTID | ID |
1 | 5, 9 |
The field mapping examples give me a headache. Thanks for looking.
Try this script. I just tested it and it should work for you. However just as a word of warning if you are running this on a large dataset this script will probably start to run pretty slow. The reason for this is because I have nested cursors, which isn't the most pythonic thing to do. I tried to load the information from the search cursor into a dictionary however I was having issues comparing the SHAPE objects from the dictionary. If you are running this on a large dataset let me know and I will change the script to the pythonic way so it wont bog down.
Hope this helps:
import arcpy
########################################################################################################################
# Variables
########################################################################################################################
print("\nSetting Variables\n")
layerToAddTo = "Path to layer to add matchind Id's"
layerToPullFrom = "Path to other layer with matching geom"
########################################################################################################################
# This finds the matching geom in both featureclass then updates the ID column in the layerToAddTo with the ID's
# from the matching geom features
########################################################################################################################
print("Finding the matching geom and combining the ID's")
for searchCur in arcpy.da.SearchCursor(layerToPullFrom, ["ID", "SHAPE@"]):
with arcpy.da.UpdateCursor(layerToAddTo, ["SHAPE@", "ID"]) as upCursor:
for updateRow in upCursor:
value1 = updateRow[0]
if value1.equals(searchCur[1]):
updateRow[1] = "{}, {}".format(updateRow[1], searchCur[0])
upCursor.updateRow(updateRow)
Jeremy, thanks very much for your time. I love the idea of comparing the geometries as opposed to a spatial join. With that in mind I can copy your script into my loop easily. Great idea.