I have three columns
Before update
from node to node gridcode
2 3 8
3 2 9
7 2 5
After update, I want "node column" to be same as "grid code" column, so I used field calculator in ArcGIS (from node = gridcode). My "from node" column has been changed to gridcode but not sure how to update "to node" column (that depends on values in “from node” column)
Below is the final result I want:
from node to node gridcode
8 9 8
9 8 9
5 8 5
So far I have tried "join", "spatial join" but I am not getting the required results. How can I do that in Python or ArcGIS?
The only way to do this I can see is with Python.
First you need to build a dictionary to understand what the relationship is between the old node values and new node values -- old_node
and new_node
. This can be done with a SearchCursor, matching up the "from node" (old_node
) to the gridcode (new_node
).
# instantiate empty dictionary
node_values = {}
# build dictionary of values
with arcpy.da.SearchCursor(feature_class, ["fromnode", "gridcode"]) as cursor:
for row in cursor:
old_node = row[0]
new_node= row[1]
node_values[old_node] = new_node
Then, you need to update all your values. This needs an UpdateCursor (somewhat like Field Calculator, but lets you do more since you're working within Python).
with arcpy.da.UpdateCursor(feature_class, ["fromnode", "tonode", "gridcode"]) as cursor:
for row in cursor:
# set fromnode to gridcode value
row[0] = row[2]
# set tonode to new equivalent node
to_node = row[1]
new_to_node = node_values[to_node]
row[1] = new_to_node
cursor.updateRow(row)
Note that there are a lot of ways this code can be "tightened up", improved, and shortened, and there are variables that need to be assigned (e.g. feature_class
) before those snippets will work properly. As always, I recommend making a backup of your data before messing around in it with somebody else's Python scripts ;)