简体   繁体   English

ArcGIS折线文件如何更新属性表中的列

[英]ArcGIS polyline file How to update column in attribute table

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).更新后,我希望“节点列”与“网格代码”列相同,因此我在 ArcGIS 中使用了字段计算器(来自节点 = 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)我的“来自节点”列已更改为 gridcode 但不确定如何更新“至节点”列(这取决于“来自节点”列中的值)

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?我如何在 Python 或 ArcGIS 中做到这一点?

The only way to do this I can see is with Python.我能看到的唯一方法是使用 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 .首先你需要建立一个字典来理解旧节点值和新节点值之间的关系—— old_nodenew_node This can be done with a SearchCursor , matching up the "from node" ( old_node ) to the gridcode ( new_node ).这可以通过SearchCursor完成,将“来自节点”( 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

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).这需要一个UpdateCursor (有点像 Field Calculator,但由于您在 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 (eg feature_class ) before those snippets will work properly.请注意,有很多方法可以“收紧”、改进和缩短此代码,并且在这些代码段正常工作之前需要分配一些变量(例如feature_class )。 As always, I recommend making a backup of your data before messing around in it with somebody else's Python scripts ;)与往常一样,我建议在使用其他人的 Python 脚本之前先备份您的数据;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM