Python arcpy检查矢量自相交

您所在的位置:网站首页 arcgis怎么检查图形自相交 Python arcpy检查矢量自相交

Python arcpy检查矢量自相交

2024-07-02 00:15| 来源: 网络整理| 查看: 265

arcpy检查面矢量自相交或异相交的问题。 

相交的基本思路如下:

检查相交的Python脚本如下,需在ArcGIS 10以上版本中运行 ,最后生成__WT.shp的矢量,即为问题矢量:

import arcpy #打开覆盖写入 arcpy.env.overwriteOutput=True A="E:\\zxj\\hh.shp" fold="E:\\zxj\\" index=A.rfind("\\") B=fold+A[index:-4]+"B.shp" C=fold+A[index:-4]+"C.shp" D=fold+A[index:-4]+"D.shp" E=fold+A[index:-4]+"E.shp" G=fold+A[index:-4]+"G.shp" #问题区域 prob=fold+A[index:-4]+"__WT.shp" #关闭结果加入图层 arcpy.env.addOutputsToMap=False arcpy.FeatureToLine_management(A,B) arcpy.FeatureToPolygon_management(B,C) arcpy.Intersect_analysis([C,A],G) arcpy.FeatureToPoint_management(G, D) arcpy.FeatureToPoint_management(A, E) #打开结果加入图层 arcpy.env.addOutputsToMap=True arcpy.Erase_analysis(E,D,prob)

 

检查相交、重复、重叠

效果如下。重复只会标记一次,相交会标记两次,重叠标记一次。重叠指一个要素在另一个要素内部

import arcpy import os #检查相交或重复的问题 A = r"D:\data\test.shp" fold = r'D:\data\bb' #打开覆盖写入 arcpy.env.overwriteOutput=True index=A.rfind("\\") B=fold+A[index:-4]+"B.shp" C=fold+A[index:-4]+"C.shp" D=fold+A[index:-4]+"D.shp" E=fold+A[index:-4]+"E.shp" G=fold+A[index:-4]+"G.shp" if not os.path.exists(fold): os.makedirs(fold) #问题区域 相交的问题 prob=fold+A[index:-4]+"H.shp" prob2=fold+A[index:-4]+"__WT.shp" #关闭结果加入图层 arcpy.env.addOutputsToMap=False arcpy.FeatureToLine_management(A,B) arcpy.FeatureToPolygon_management(B,C) arcpy.Intersect_analysis([C,A],G) arcpy.FeatureToPoint_management(G, D) arcpy.FeatureToPoint_management(A, E) arcpy.Erase_analysis(E,D,prob) #添加字段 问题类型 arcpy.AddField_management(prob, "problem", "TEXT","","", 12) with arcpy.da.UpdateCursor(prob, "problem") as cursor: for row in cursor: row[0] = "相交" cursor.updateRow(row) del cursor #检查重复的问题,根据坐标是否一致判断 #E = r'D:\data\fumz\test\testE.shp' sets = set() sets2 = set() arcpy.AddField_management(E, "problem", "TEXT","","", 12) fields = ['SHAPE@WKT','problem','SHAPE@X','SHAPE@Y'] with arcpy.da.UpdateCursor(E, fields) as cursor: for row in cursor: id = row[0] id2 = '%s,%s'%(row[2],row[3]) if id in sets: row[1] = '重复' cursor.updateRow(row) elif id2 in sets2: row[1] = '疑似相交' cursor.updateRow(row) else: cursor.deleteRow() #cursor.updateRow(row) sets.add(id) sets2.add(id2) del cursor #打开结果加入图层 arcpy.env.addOutputsToMap=True #合并图层 arcpy.Merge_management([E, prob], prob2) #arcpy.env.addOutputsToMap=False delete = False #删 if delete: arcpy.Delete_management(B) arcpy.Delete_management(C) arcpy.Delete_management(D) arcpy.Delete_management(E) arcpy.Delete_management(G) arcpy.Delete_management(prob) 批处理版 # -*- coding: utf-8 -*- import arcpy import os import sys reload(sys) sys.setdefaultencoding('utf-8') #检查相交或重复的问题 shpFolder = r"D:\data\a" #shp文件夹 fold = r'D:\data\a\bc' #输出文件夹 #---------------------分割线------------------------------- files = [shpFolder+"\\"+i for i in os.listdir(shpFolder) if i.endswith('.shp')] for A in files: #打开覆盖写入 A = A.decode('gbk') arcpy.env.overwriteOutput=True index=A.rfind("\\") B=fold+A[index:-4]+"B.shp" C=fold+A[index:-4]+"C.shp" D=fold+A[index:-4]+"D.shp" E=fold+A[index:-4]+"E.shp" G=fold+A[index:-4]+"G.shp" if not os.path.exists(fold): os.makedirs(fold) #问题区域 相交的问题 prob=fold+A[index:-4]+"H.shp" prob2=fold+A[index:-4]+"__WT.shp" #关闭结果加入图层 arcpy.env.addOutputsToMap=False arcpy.FeatureToLine_management(A,B) arcpy.FeatureToPolygon_management(B,C) arcpy.Intersect_analysis([C,A],G) arcpy.FeatureToPoint_management(G, D) arcpy.FeatureToPoint_management(A, E) arcpy.Erase_analysis(E,D,prob) #添加字段 问题类型 arcpy.AddField_management(prob, "problem", "TEXT","","", 12) with arcpy.da.UpdateCursor(prob, "problem") as cursor: for row in cursor: row[0] = "相交" cursor.updateRow(row) del cursor #检查重复的问题,根据坐标是否一致判断 #E = r'D:\data\fumz\test\testE.shp' sets = set() sets2 = set() arcpy.AddField_management(E, "problem", "TEXT","","", 12) fields = ['SHAPE@WKT','problem','SHAPE@X','SHAPE@Y'] with arcpy.da.UpdateCursor(E, fields) as cursor: for row in cursor: id = row[0] id2 = '%s,%s'%(row[2],row[3]) if id in sets: row[1] = '重复' cursor.updateRow(row) elif id2 in sets2: row[1] = '疑似相交' cursor.updateRow(row) else: cursor.deleteRow() #cursor.updateRow(row) sets.add(id) sets2.add(id2) del cursor #打开结果加入图层 arcpy.env.addOutputsToMap=True #合并图层 arcpy.Merge_management([E, prob], prob2) #arcpy.env.addOutputsToMap=False delete = True #删 if delete: arcpy.Delete_management(B) arcpy.Delete_management(C) arcpy.Delete_management(D) arcpy.Delete_management(E) arcpy.Delete_management(G) arcpy.Delete_management(prob)

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3