Python arcpy检查矢量自相交

30 篇文章 20 订阅

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)

 

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独孤尚亮dugushangliang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值