使用arcpy对两个要素进行更新操作

需求1:

想在两个要素之间做更新,目标要素需要更新源要素的内容过来,包括几何和属性信息,他们的字段基本一致。

数据:

案例数据:

链接:https://pan.baidu.com/s/1xAcOTuLXvtdPiiSFGPDWdQ 
提取码:lh2i 

 脚本:

import arcpy

arcpy.env.workspace = r"E:\testing\Beijing4490_v10_0.gdb"
edit = arcpy.da.Editor(r"E:\testing\Beijing4490_v10_0.gdb")

# Edit session is started without an undo/redo stack for versioned data
#  (for second argument, use False for unversioned data)
edit.startEditing(False, True)
edit.startOperation()
#da.SearchCursor字段不能使用*,必须指定Shape@,之前可以使用ListFields列出所有字段,使用*会导致后面的shape无法赋值
cursor = arcpy.da.SearchCursor("source",["SHAPE@","name"],where_clause="OBJECTID = 4")

for row in cursor:

    shape = row[0]
    # index = cursor.fields.index("GBCODE")
    name = row[1]
    print name

# Insert a row into the table.
cursor1 = arcpy.da.UpdateCursor("destination",["SHAPE@","name"],where_clause="OBJECTID = 4")
for row1 in cursor1:
    row1[0]=shape
    row1[1]=name
    cursor1.updateRow(row1)

print("success")

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)
del cursor1
del cursor

效果图:

destination图层的OBJECTID为4要素的几何形状和属性信息(NAME99字段)被更新。

 

 需求2:将source图层的图形和属性插入给destination图层

由于da.InsertCursor无法修改参与了几何网络的要素类,是一个已知bug,只能使用arcpy.InsertCursor

import  arcpy

arcpy.env.workspace = r"E:\testing\Beijing4490_v10_0.gdb"
edit = arcpy.da.Editor(r"E:\testing\Beijing4490_v10_0.gdb")

edit.startEditing(False, True)
edit.startOperation()
cursor = arcpy.da.SearchCursor("destination",["SHAPE@","ADCODE99"],where_clause="OBJECTID = 12")

for row in cursor:

    shape = row[0]
    # index = cursor.fields.index("ADCODE99")
    length = row[1]

cursor1 = arcpy.InsertCursor("source")
row1 = cursor1.newRow()
row1.setValue("Shape", shape)
row1.setValue("ADCODE99", length)
cursor1.insertRow(row1)

print("success")

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)
del cursor1
del cursor

 需求3:将空值赋值给有默认值的要素。

测试环境:

       ①要素类在要素数据集中

       ②该要素类参与了几何网络

       ③该要素类所在的要素数据集注册了版本

       ④所用数据库为企业级地理数据库Oracle

       ⑤其他条件:属性域,允许为空,有默认值

遇到的问题:更新一个应用了属性域有默认值(默认值不为空)但可为空的字段时发现了一个问题,当源要素该字段为空,目标要素该字段不为空,通过da.UpdateCursor无法将源要素该字段空值赋给目标要素,最终得到的结果是默认值而非空值。

经过研究,发现使用arcpy.da下没有方法能够将空值(数值型/字符串型等)传递给另一个字段。
这被认定是一个bug,见链接:https://support.esri.com/zh-cn/bugs/nimbus/QlVHLTAwMDA5MTMxNA==
这个bug目前正在新的产品计划之中,如果修复的话,也会在上述链接中进行状态更新。

绕行方案是使用之前老的update cursor,里面有setNull()方法,可以将数值型等字段的属性值设置为null。

import arcpy

arcpy.env.workspace = r"C:\Users\admin\AppData\Roaming\ESRI\Desktop10.7\ArcCatalog\ORCLUSER1.sde"
edit = arcpy.da.Editor( r"C:\Users\admin\AppData\Roaming\ESRI\Desktop10.7\ArcCatalog\ORCLUSER1.sde")

# Edit session is started without an undo/redo stack for versioned data
#  (for second argument, use False for unversioned data)
edit.startEditing(False, True)
edit.startOperation()

cursor = arcpy.SearchCursor("source2","OBJECTID = 4","",["SHAPE@","test"])

for row in cursor:
    shape = row[0]
    # index = cursor.fields.index("GBCODE")
    test = row[1]
    print test

# Insert a row into the table.
cursor1 = arcpy.UpdateCursor("destination2","OBJECTID = 4","",["SHAPE@","test"])
for row1 in cursor1:
    row1[0]=shape
    if test is None:
        row1[1].setNull(test)
        print row1[1]
    else:
        row1[1] = test

    cursor1.updateRow(row1)

print("success")

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)
del cursor1
del cursor

参考资料:

 https://gis.stackexchange.com/questions/183932/insert-or-update-cursor-setting-value-to-none-not-honored-when-field-has-a-def

https://blog.csdn.net/baoqian1993/article/details/71157714 创建几何网络

https://www.cnblogs.com/aoldman/archive/2013/05/02/3054729.html

https://blog.csdn.net/sophiasy/article/details/38088429

https://blog.csdn.net/kone0611/article/details/50259599

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用arcpy中的工具和函数来检查两个线要素的属性和图形是否有变化。下面是一个简单的示例代码,可以帮助您完成此任务: ```python import arcpy # 设置工作空间 arcpy.env.workspace = "YourWorkspace" # 输入要素类路径 fc1 = "Path_to_first_feature_class" fc2 = "Path_to_second_feature_class" # 创建游标以遍历要素 cursor1 = arcpy.da.SearchCursor(fc1, ["SHAPE@", "Field1", "Field2"]) # 替换"Field1"和"Field2"为您要比较的属性字段 cursor2 = arcpy.da.SearchCursor(fc2, ["SHAPE@", "Field1", "Field2"]) # 遍历要素进行比较 for row1, row2 in zip(cursor1, cursor2): shape1, field1_1, field1_2 = row1 shape2, field2_1, field2_2 = row2 # 检查属性是否有变化 if field1_1 != field2_1 or field1_2 != field2_2: print("属性有变化") # 检查图形是否有变化 if not shape1.equals(shape2): print("图形有变化") # 释放游标对象 del cursor1, cursor2 ``` 请替换代码中的以下部分: - "YourWorkspace":您的工作空间路径,可以是文件地理数据库或文件夹。 - "Path_to_first_feature_class"和"Path_to_second_feature_class":您要比较的两个要素类的路径。 - "Field1"和"Field2":您要比较的属性字段的名称。 这段代码将遍历两个要素类中的要素,并逐个比较它们的属性和图形。如果属性或图形有变化,将会输出相应的信息。注意,此示例假设两个要素类中的要素顺序、数量和属性字段完全一致。如果要素顺序不一致或要素数量不同,可能需要进行更复杂的比较逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值