AnsweredAssumed Answered

Update features with multiple values, Spatial Join or Select by Location Or Dictionaries

Question asked by 2CQuiker on Jul 28, 2020
Latest reply on Aug 4, 2020 by 2CQuiker

I have a hard time with field mapping and the only form I seem to understand is the following but it also seems to not work, maybe I doing it wrong but here is what I found. Using a code like below

PIN "PIN" true true false 13 Text 0 0 ,First,#, {0}, PIN,-1,-1;
ACRES "ACRES" true true false 4 Double 0 0  ,First,#, {0}, ACRES,-1,-1;
PARCEL "PARCEL" true true false 50 Text 0 0 ,First,"#",{0}, PARCEL,-1,-1;
PARCELS "PARCELS" true true false 200 Text 0 0 ,Join,",", {1}, PARCEL,-1,-1

arcpy.SpatialJoin_analysis(Par, Par1, "BlahTest", "JOIN_ONE_TO_ONE", "KEEP_ALL",Layers1(Par1), "INTERSECT")


I found that the output had 19 parcels inside the "PARCELS" field and it looked incorrect. So I opened up Arcmap and used select by location with the same spatial selection method "intersect" and selected 25. So I would like to use spatial join with Dictionaries or Select By Location if possible but my problem is how to use the merge rule and delimiter to update the fields. How would I apply the code I am using to use Dictionaries or Select By Location?


Outcome of field PARCELS should be something like the following.

PIN12345,PIN12346,PIN12347, PIN12348, etc.


arcpy.SpatialJoin_analysis(updateFC, Par, sj, "JOIN_ONE_TO_ONE", "KEEP_ALL")

# define the field list from the spatial join
sourceFieldsList = ["TARGET_FID", "Parcel_1"]   

# define the field list to the original Parcels
updateFieldsList = ["OID@", "PARCELS"]

# populate the dictionary from the polygon
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sj, sourceFieldsList)}   

with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:   
    for updateRow in updateRows:   
        keyValue = updateRow[0]   
        if keyValue in valueDict:   
            for n in range (1,len(sourceFieldsList)):     
                updateRow[n] = valueDict[keyValue][n-1] #How do update the field to include multi values from the selection by location?

    del valueDict


targetLayer = "In_memory\tempTarget"
arcpy.MakeFeatureLayer_management(target, targetLayer)

MergetLayer = "In_memory\tempMerge"
with arcpy.da.SearchCursor(Merge2,["SHAPE@", "PARCEL"]) as cursorSearch:
    for row in cursorSearch:      
        arcpy.SelectLayerByLocation_management (targetLayer, "INTERSECT", row[0])      
        with arcpy.da.UpdateCursor(targetLayer,["SHAPE@", "PARCELS"]) as cursorUpdate:
            for row2 in cursorUpdate:
                row2[1] = row[1] #How do update the field to include multi values from the selection by location?