python arcpy練習之面要素重疊拓撲檢查
需求
有多個文件地理數(shù)據(jù)庫(gdb),數(shù)據(jù)庫內(nèi)有多個面要素類圖層,每個圖層不能有自重疊,也不能和其他圖層重疊。所以,需要為每個文件地理數(shù)據(jù)庫(gdb)進行拓撲檢查。
思路
1.在輸出文件夾下新建與輸入文件夾下同名的gdb。
2.在gdb內(nèi)新建要素類數(shù)據(jù)集。
3.在數(shù)據(jù)集內(nèi)創(chuàng)建拓撲。
4.向拓撲中添加要素類。
5.添加拓撲規(guī)則。
6.拓撲驗證。
7.導出拓撲錯誤。

將需要拓撲的gdb放入同一個文件夾下,用python代碼串連起來利用循環(huán)遍歷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" #設(shè)置XY分辨率
arcpy.env.XYTolerance = "0.001 Meters" #設(shè)置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設(shè)為工作區(qū)
arcpy.env.workspace = workspace
fc_list = arcpy.ListFeatureClasses() #列出gdb內(nèi)的要素類圖層
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 + " 內(nèi)新建要素數(shù)據(jù)集...")
dataset_name = "dataset"
dataset_path = os.path.join(result_gdb,dataset_name)
# 引用包含要應用的空間參考的要素類或要素數(shù)據(jù)集,
# 將in_fc_path_list[0]的坐標作為要素數(shù)據(jù)集的坐標,所以gdb內(nèi)的所有要素類應該是統(tǒng)一坐標
arcpy.CreateFeatureDataset_management(result_gdb, dataset_name, in_fc_path_list[0])
#導入要素類到數(shù)據(jù)集
arcpy.AddMessage(" 向 " + dataset_path + " 導入要素類圖層...")
arcpy.FeatureClassToGeodatabase_conversion(in_fc_path_list,dataset_path)
#在dataset數(shù)據(jù)內(nèi)創(chuàng)建拓撲
arcpy.AddMessage(" 創(chuàng)建拓撲...")
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 = [] #數(shù)據(jù)集中的要素類絕對路徑列表
for ifc_name in fc_list:
# 拼接數(shù)據(jù)集中的要素類絕對路徑
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(" 添加拓撲規(guī)則...")
for i in range(len(dataset_fc_path_lsit)):
fc_path1 = dataset_fc_path_lsit[i]
#規(guī)則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]
# 規(guī)則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)
效果圖



總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Python import用法以及與from...import的區(qū)別
這篇文章主要介紹了Python import用法以及與from...import的區(qū)別,本文簡潔明了,很容易看懂,需要的朋友可以參考下2015-05-05
Python計時相關(guān)操作詳解【time,datetime】
這篇文章主要介紹了Python計時相關(guān)操作,涉及time,datetime模塊的使用技巧,包括時間戳、時間差、日期格式等操作方法,需要的朋友可以參考下2017-05-05

