ArcGIS 折线文件如何更新属性中的列 table
ArcGIS polyline file How to update column in attribute table
我有三列
更新前
from node to node gridcode
2 3 8
3 2 9
7 2 5
更新后,我希望 "node column" 与 "grid code" 列相同,所以我在 ArcGIS 中使用了字段计算器(来自 node = gridcode)。我的 "from node" 列已更改为 gridcode 但不确定如何更新 "to node" 列(这取决于“从节点”列中的值)
下面是我想要的最终结果:
from node to node gridcode
8 9 8
9 8 9
5 8 5
到目前为止,我已经尝试了 "join"、"spatial join",但我没有得到所需的结果。我如何在 Python 或 ArcGIS 中做到这一点?
我能看到的唯一方法是使用 Python。
首先你需要建立一个字典来理解旧节点值和新节点值之间的关系——old_node
和new_node
。这可以通过 SearchCursor 来完成,将 "from node" (old_node
) 匹配到网格代码 (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
然后,您需要更新所有值。这需要一个 UpdateCursor(有点像字段计算器,但由于您在 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)
请注意,有很多方法可以 "tightened up"、改进和缩短此代码,并且在这些代码段起作用之前需要分配一些变量(例如 feature_class
)适当地。一如既往,我建议在使用其他人的 Python 脚本之前备份您的数据 ;)
我有三列
更新前
from node to node gridcode
2 3 8
3 2 9
7 2 5
更新后,我希望 "node column" 与 "grid code" 列相同,所以我在 ArcGIS 中使用了字段计算器(来自 node = gridcode)。我的 "from node" 列已更改为 gridcode 但不确定如何更新 "to node" 列(这取决于“从节点”列中的值)
下面是我想要的最终结果:
from node to node gridcode
8 9 8
9 8 9
5 8 5
到目前为止,我已经尝试了 "join"、"spatial join",但我没有得到所需的结果。我如何在 Python 或 ArcGIS 中做到这一点?
我能看到的唯一方法是使用 Python。
首先你需要建立一个字典来理解旧节点值和新节点值之间的关系——old_node
和new_node
。这可以通过 SearchCursor 来完成,将 "from node" (old_node
) 匹配到网格代码 (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
然后,您需要更新所有值。这需要一个 UpdateCursor(有点像字段计算器,但由于您在 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)
请注意,有很多方法可以 "tightened up"、改进和缩短此代码,并且在这些代码段起作用之前需要分配一些变量(例如 feature_class
)适当地。一如既往,我建议在使用其他人的 Python 脚本之前备份您的数据 ;)