Skip to content

Instantly share code, notes, and snippets.

@danmaps
Created December 14, 2023 20:29
Show Gist options
  • Save danmaps/a8960e294ce567866745dee772956f8c to your computer and use it in GitHub Desktop.
Save danmaps/a8960e294ce567866745dee772956f8c to your computer and use it in GitHub Desktop.
created as part of a proximity analysis to make use of the output of the "generate near table tool". only after this was complete did I realize that a vlookup would do this pretty easily... i think.
projected_fc = "joined"
new_field = "nearby_FLOC"
floc_id_field = "FLOC_ID"
# Add a new field
arcpy.management.AddField(projected_fc, new_field, "TEXT")
# Step 1: Create a dictionary to map OBJECTID to FLOC_ID
floc_id_dict = {}
with arcpy.da.SearchCursor(projected_fc, ["OBJECTID", floc_id_field]) as search_cursor:
for row in search_cursor:
floc_id_dict[row[0]] = row[1]
# Step 2: Update the 'FLOC' field using the dictionary
with arcpy.da.UpdateCursor(projected_fc, [new_field, "NEAR_FID"]) as update_cursor:
for row in update_cursor:
near_fid = row[1]
floc_value = floc_id_dict.get(near_fid, None) # Get the FLOC_ID value based on NEAR_FID
if floc_value is not None:
row[0] = floc_value # Update the FLOC field
update_cursor.updateRow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment