使用python代码实现arcgis两期数据库基于相同形状进行属性对比
【代码】使用python代码实现arcgis两期数据库基于相同形状进行属性对比。
·
# -*- coding: utf-8 -*-
import arcpy
import os
# 设置 GDB 路径
gdb_2023 = r"Z:\JMXX\test2023.gdb"
gdb_2024 = r"Z:\JMXX\test2024.gdb"
# 需要对比的图层和字段
feature_classes = ["ABC", "ABC_1"] # 需要对比的图层列表
compare_fields = ["GD", "DG", "FCODE", "BB"] # 需要对比的字段列表
# 输出目录
output_dir = r"Z:\JMXX"
# 遍历每个图层进行对比
for feature_class in feature_classes:
fc_2023 = os.path.join(gdb_2023, feature_class)
fc_2024 = os.path.join(gdb_2024, feature_class)
# 检查图层是否存在
if not arcpy.Exists(fc_2023) or not arcpy.Exists(fc_2024):
print("警告: 图层 {} 在一个或两个数据库中不存在,跳过...".format(feature_class))
continue
# 获取两个图层的字段
fields_2023 = set([f.name for f in arcpy.ListFields(fc_2023)])
fields_2024 = set([f.name for f in arcpy.ListFields(fc_2024)])
# 确保对比字段存在
valid_fields = ["SHAPE@"] # 确保 SHAPE@ 存在
missing_fields = [] # 记录缺失字段
for field in compare_fields:
if field in fields_2023 and field in fields_2024:
valid_fields.append(field)
else:
missing_fields.append(field)
if missing_fields:
print("警告: 图层 {} 缺少字段: {},这些字段将被忽略...".format(feature_class, ", ".join(missing_fields)))
# 创建存储数据的字典 {geometry_wkt: (Geometry, 字段1_2023, 字段2_2023, ...)}
data_2023 = {}
data_2024 = {}
# 读取 2023 数据
with arcpy.da.SearchCursor(fc_2023, valid_fields) as cursor_2023:
for row in cursor_2023:
geom_wkt = row[0].WKT # 以 WKT 作为唯一标识
data_2023[geom_wkt] = (row[0],) + row[1:] # 存储 (Geometry, GD_2023, NAME_2023, FCODE_2023...)
# 读取 2024 数据
with arcpy.da.SearchCursor(fc_2024, valid_fields) as cursor_2024:
for row in cursor_2024:
geom_wkt = row[0].WKT
data_2024[geom_wkt] = (row[0],) + row[1:] # 存储 (Geometry, GD_2024, NAME_2024, FCODE_2024...)
# 对比数据,找出发生变化的要素
changed_features = []
for geom in data_2024:
if geom in data_2023:
geom_2023, attrs_2023 = data_2023[geom][0], data_2023[geom][1:]
geom_2024, attrs_2024 = data_2024[geom][0], data_2024[geom][1:]
if attrs_2023 != attrs_2024: # 如果属性值不一样
changed_features.append((geom_2024,) + attrs_2023 + attrs_2024) # 存储 (Geometry, GD_2023, NAME_2023, FCODE_2023, GD_2024, NAME_2024, FCODE_2024)
# 如果没有变化,跳过当前图层
if len(changed_features) == 0:
print("图层 {} 没有发现字段变化".format(feature_class))
continue
# 输出 SHP 文件路径
output_shp = os.path.join(output_dir, "{}_changes.shp".format(feature_class))
# 创建 Shapefile
spatial_ref = arcpy.Describe(fc_2024).spatialReference
arcpy.CreateFeatureclass_management(output_dir, os.path.basename(output_shp), "POLYGON", spatial_reference=spatial_ref)
# 添加字段
for field in valid_fields[1:]: # 跳过 SHAPE@
arcpy.AddField_management(output_shp, field + "_2023", "TEXT", field_length=100)
arcpy.AddField_management(output_shp, field + "_2024", "TEXT", field_length=100)
# 写入变化的要素到 Shapefile
insert_fields = ["SHAPE@"] + [f + "_2023" for f in valid_fields[1:]] + [f + "_2024" for f in valid_fields[1:]]
with arcpy.da.InsertCursor(output_shp, insert_fields) as insert_cursor:
for feature in changed_features:
insert_cursor.insertRow(feature)
print("对比完成,图层 {} 发现 {} 条变化数据,结果已保存到: {}".format(feature_class, len(changed_features), output_shp))
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)