python arcpy練習之面要素重疊拓撲檢查

需求

有多個文件地理數據庫(gdb),數據庫內有多個面要素類圖層,每個圖層不能有自重疊,也不能和其他圖層重疊。所以,需要為每個文件地理數據庫(gdb)進行拓撲檢查。

思路

1.在輸出文件夾下新建與輸入文件夾下同名的gdb。

2.在gdb內新建要素類數據集。

3.在數據集內創建拓撲。

4.向拓撲中添加要素類。

5.添加拓撲規則。

6.拓撲驗證。

7.導出拓撲錯誤。

將需要拓撲的gdb放入同一個文件夾下,用python代碼串連起來利用循環遍歷gdb便可以進行批量拓撲瞭。

代碼

# -*- coding: gbk -*-
import os
import arcpy
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
arcpy.AddMessage(sys.getdefaultencoding())
arcpy.env.XYResolution = "0.0001 Meters" #設置XY分辨率
arcpy.env.XYTolerance = "0.001 Meters"   #設置XY容差
def checkTopology(in_path, out_path):
    cout = 0
    fail = 0
    warning = 0
    fail_list = []
    warning_list = []
    arcpy.env.workspace = in_path
    workspaces = arcpy.ListWorkspaces("*", "ALL")
    for workspace in workspaces:
        arcpy.AddMessage("=" * 60)
        cout += 1
        arcpy.AddMessage(" (" + str(cout) + ") " + os.path.basename(workspace))
        arcpy.AddMessage("  ")
        try:
            # 將每個gdb設為工作區
            arcpy.env.workspace = workspace
            fc_list = arcpy.ListFeatureClasses() #列出gdb內的要素類圖層
            in_fc_path_list = []
            for fc in fc_list:
                in_fc_path_list.append(os.path.join(workspace,fc))
            # 在輸出路徑out_path下新建gdb
            gdb_name1 = os.path.basename(workspace)
            result_gdb = os.path.join(out_path, gdb_name1)
            if not arcpy.Exists(result_gdb):
                arcpy.AddMessage("    新建 " + result_gdb + " ...")
                arcpy.CreateFileGDB_management(out_path, gdb_name1)
            else:
                arcpy.AddWarning(result_gdb + " 已存在!")
                warning_list.append(result_gdb)
                warning += 1
                continue
            arcpy.AddMessage("    在 " + result_gdb + " 內新建要素數據集...")
            dataset_name = "dataset"
            dataset_path = os.path.join(result_gdb,dataset_name)
            # 引用包含要應用的空間參考的要素類或要素數據集,
            # 將in_fc_path_list[0]的坐標作為要素數據集的坐標,所以gdb內的所有要素類應該是統一坐標
            arcpy.CreateFeatureDataset_management(result_gdb, dataset_name, in_fc_path_list[0])
            #導入要素類到數據集
            arcpy.AddMessage("    向 " + dataset_path + " 導入要素類圖層...")
            arcpy.FeatureClassToGeodatabase_conversion(in_fc_path_list,dataset_path)
            #在dataset數據內創建拓撲
            arcpy.AddMessage("    創建拓撲...")
            topology_name = "topology"
            topology_path = os.path.join(dataset_path, topology_name)
            arcpy.CreateTopology_management(dataset_path, topology_name)
            # 將導入dataset的要素類添加到拓撲中
            arcpy.AddMessage("    向拓撲中添加要素類...")
            dataset_fc_path_lsit = []  #數據集中的要素類絕對路徑列表
            for ifc_name in fc_list:
                # 拼接數據集中的要素類絕對路徑
                dataset_fc_path = os.path.join(dataset_path,ifc_name)
                dataset_fc_path_lsit.append(dataset_fc_path)
                arcpy.AddFeatureClassToTopology_management(topology_path, dataset_fc_path, "1", "1")
            arcpy.AddMessage("    添加拓撲規則...")
            for i in range(len(dataset_fc_path_lsit)):
                fc_path1 = dataset_fc_path_lsit[i]
                #規則1:不能重疊
                arcpy.AddRuleToTopology_management(topology_path, "Must Not Overlap (Area)", fc_path1, "", "", "")
                for j in range(i + 1, len(dataset_fc_path_lsit)):
                    fc_path2 = dataset_fc_path_lsit[j]
                    # 規則2:不能重疊與其他要素類重疊
                    arcpy.AddRuleToTopology_management(topology_path, "Must Not Overlap With (Area-Area)", fc_path1, "", fc_path2, "")
            arcpy.AddMessage("    拓撲驗證...")
            arcpy.ValidateTopology_management(topology_path, "Full_Extent")
            arcpy.AddMessage("    導出拓撲錯誤...")
            arcpy.ExportTopologyErrors_management(topology_path, dataset_path, "topoError")
        except Exception as e:
            arcpy.AddError(e.message)
            arcpy.AddError("    " + os.path.basename(workspace) + " 失敗!")
            fail_list.append(os.path.basename(workspace))
            fail += 1
    arcpy.AddMessage('+' * 60)
    arcpy.AddMessage(u"  成功:" + str(cout - fail - warning) + u" 個!")
    if warning > 0:
        arcpy.AddWarning(u"  警告:" + str(warning) + u" 個! 如下:")
        arcpy.AddWarning("####" + '*' * 20)
        for fff in warning_list:
            arcpy.AddWarning("  " + fff)
        arcpy.AddWarning("####" + '*' * 20)
    if fail > 0:
        arcpy.AddError(u"  失敗:" + str(fail) + u" 個! 如下:")
        arcpy.AddError("####" + '*' * 20)
        for ff in fail_list:
            arcpy.AddError("  " + ff)
        arcpy.AddError("####" + '*' * 20)
    arcpy.AddMessage('+' * 60)
if __name__ == "__main__":
    in_path_globle = arcpy.GetParameterAsText(0)
    out_path_globe = arcpy.GetParameterAsText(1)
    checkTopology(in_path_globle,out_path_globe)

效果圖

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!

推薦閱讀: