Python arcpy检查矢量自相交

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

相交的基本思路如下:

Python arcpy检查矢量自相交_第1张图片

检查相交的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)

 

检查相交、重复、重叠

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

Python arcpy检查矢量自相交_第2张图片

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)

 

你可能感兴趣的:(Python,遥感)