Select to view content in your preferred language

Spatial Join & Dictionary transfer

1228
10
07-27-2022 08:58 AM
2Quiker
Occasional Contributor II

I have a two layers, I need to transfer a field attribute to the other layer of the selected feature.

The EMS layer has a filed "SiteCity" and so does the "Ass_Parcels" layer.

I need to transfer the Ass_Parcels SiteCity attribute to the EMS's SiteCity field.

I have the following but I am not able to get it to work.

I don't get an error, but the EMs's SiteCity field doesn't get populated.

pointLayer = 'EMS'
parcel = 'Ass_Parcels'

ptCount = int(arcpy.GetCount_management(pointLayer).getOutput(0))

dsc = arcpy.Describe(pointLayer)     
selection_set = dsc.FIDSet             
if len(selection_set) == 0:
    print "There are no features selected"

elif ptCount >= 1:
elif ptCount >= 1:

    arcpy.SpatialJoin_analysis(pointLayer, parcel, sjpoints)
    search_feats ={f[0]:(f[1:]) for f in arcpy.da.SearchCursor(sjpoints,'SiteCity_1')}
    print(search_feats)

    with arcpy.da.UpdateCursor(pointLayer,["SHAPE@","SiteCity"]) as upd_cur:
        for upd_row in upd_cur:
            try:
                if search_feats[upd_row[0]][1] == "Sea":              
                    upd_row[1] == "Seattle"
                elif search_feats[upd_row[0]][1] == "Spo":
                    upd_row[1] == "Spokane"
                elif search_feats[upd_row[0]][1] == "Tac":
                    upd_row[1] == "Tacoma"
                upd_cur.updateRow(upd_row)
            except:
               continue

 

 

0 Kudos
10 Replies
2Quiker
Occasional Contributor II

apologizes for the delay response, I got busy with something else.

The print(search_feats), prints {u'Ros': ()}

I have attached the data I am working with, if it helps.

I changed the parcel data to City limits to help with the size of the up loaded file.

I need to update the pointLayer = EMS points2 SiteCity field with the

Parcels =  CityLimits1 SiteCity field

 

 

 

pointLayer = 'EMS_Points2'
city = 'CityLimits1'
sjpoints = "In_memory\sjpoints"

ptCount = int(arcpy.GetCount_management(pointLayer).getOutput(0))

dsc = arcpy.Describe(pointLayer)     
selection_set = dsc.FIDSet             
if len(selection_set) == 0:
    print "There are no features selected"

elif ptCount >= 1:
    #Run the Spatial Join tool, using the defaults for the join operation and join type
    arcpy.SpatialJoin_analysis(pointLayer, city, sjpoints)

    search_feats ={f[0]:(f[0]) for f in arcpy.da.SearchCursor(sjpoints,'SiteCity_1')}
    print(search_feats)

##    with arcpy.da.UpdateCursor(pointLayer,["SHAPE@","SiteCity"]) as upd_cur:
##        for upd_row in upd_cur:
##            try:
##                if search_feats[upd_row[0]][1] == "Sea":              
##                    upd_row[1] = "Seattle"
##                elif search_feats[upd_row[0]][1] == "Spo":
##                    upd_row[1] = "Spokane"
##                elif search_feats[upd_row[0]][1] == "Tac":
##                    upd_row[1] = "Tacoma"
##                upd_cur.updateRow(upd_row)
##            except:
##               continue

    place_dict = {"Sea":"Seattle", "Spo":"Spokane", "Ros":"Roslyn"}

    with arcpy.da.UpdateCursor(pointLayer,["SHAPE@","SiteCity"]) as upd_cur:
        for upd_row in upd_cur:
            try:
                upd_row[1] = place_dict.get(search_feats[upd_row[0]][1], upd_row[1])
                upd_cur.updateRow(upd_row)
            except:
               continue

 

 

0 Kudos