【基于arcpy实现接边处理】

用SelectLayerByLocation_management对图层进行选择,该函数需要传入选择图层,被选择图层,以及选择的方式,是否相邻之类的。

关于该函数的使用,大家可以查阅相关的资料,这里就不一一说明。

首先在编写这个程序时,需要对一个区域所有的面进行遍历,然后再与另外区域的所有面进行选择。因此,这中间涉及到面遍历后创建新元素,然后将新元素放到选择函数中,之后还需要删除该图层。这中间比较浪费时间,特别是在创建元素和删除元素的时候,以及进行选择操作时。经过比较后找到相应的id然后更新原来的传入的数据。具体的实现看一下源代码。
 

# coding:utf-8
import arcpy
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
 
 
 
 
#交换id
def switchID(selectLayer,selectID,selectedLayer,selectedID):
    #处理选择图层
    with  arcpy.da.UpdateCursor(selectLayer,["ID","JBX"])  as cursor:
        for row in cursor:
            tmpSelectID=row[0].encode("utf-8")
            if tmpSelectID==selectID:
                row[1]=selectedID
                cursor.updateRow(row)
                break
 
        del  cursor
    #处理被选择图层
    with  arcpy.da.UpdateCursor(selectedLayer,["ID","JBX"])  as cursor:
        for row in cursor:
            tmpSelectID=row[0].encode("utf-8")
            if tmpSelectID==selectedID:
                row[1]=selectID
                cursor.updateRow(row)
                break
 
        del  cursor
 
def delShpFile(root):
     extends=[".cpg",".dbf",".prj",".sbn",".sbx",".shp",".shp.xml",".shx"]
     files=[]
     for extend in extends:
        tmpFile=root+extend
        if os.path.isfile(tmpFile):
            files.append(tmpFile)
     for file in files:
         os.remove(file)
 
selectLayer="C:\Users\Desktop\STProject\statistics\测试数据\xx1遥感统计数据库.gdb\STM"
selectedLayer="C:\Users\Desktop\STProject\statistics\测试数据\xx2遥感统计数据库.gdb\STM"
 
arcpy.MakeFeatureLayer_management(selectLayer,"STM")
 
arcpy.MakeFeatureLayer_management(selectedLayer,"STM2")
 
selection=arcpy.SelectLayerByLocation_management("STM",
                                                 "BOUNDARY_TOUCHES",
                                                 selectedLayer,"","NEW_SELECTION")
 
selection2=arcpy.SelectLayerByLocation_management("STM2",
                                                 "BOUNDARY_TOUCHES",
                                                 selectLayer,"","NEW_SELECTION")
 
cnt=arcpy.GetCount_management(selection);
 
rootPath="C:\Users\qrb_PC\Desktop\mestShp"
 
 
 
 
# with arcpy.da.SearchCursor(selectLayer, ["SHAPE@",'ID']) as cursor:
#      for row in cursor:
#         geometry = row[0]
#         arcpy.MakeFeatureLayer_management(geometry,"TmpSTM")
#         itemSelect=arcpy.SelectLayerByLocation_management("TmpSTM",selectedLayer,"","NEW_SELECTION")
#         tmpCNT = arcpy.GetCount_management(itemSelect);
#      del cursor
 
 
selectResultPath=rootPath+"\Metd.shp"
arcpy.CopyFeatures_management(selection, selectResultPath)
 
selectedResultPath=rootPath+"\Metd2.shp"
 
finalResPath=rootPath+"\Final.shp"
 
cnt2=arcpy.GetCount_management(selection2)
arcpy.CopyFeatures_management(selection2, selectedResultPath)
 
# 遍历选择面
with arcpy.da.SearchCursor(selectResultPath, ["SHAPE@",'ID']) as selectCursor:
    for selectRow in selectCursor:
        out_name = "Select"+selectRow[1] + '.shp'
        tmpSelectOutName="Select"+selectRow[1];
        selectID=selectRow[1].encode("utf-8")
        arcpy.FeatureClassToFeatureClass_conversion(selectRow[0], rootPath, out_name)
        # 遍历被选择面
        with arcpy.da.SearchCursor(selectedResultPath, ["SHAPE@",'ID']) as selectedCursor:
            for selectedRow in selectedCursor:
                out_name2 = "SelectED" + selectedRow[1] + '.shp'
                tmpSelectedOutName = "SelectED" + selectedRow[1]
                creatName="RSelectED" + selectedRow[1]
 
                creatName.encode("utf-8")
                arcpy.FeatureClassToFeatureClass_conversion(selectedRow[0], rootPath, out_name2)
 
                tmpResultPath=rootPath +"\\"+ out_name
                tmpResultPath2 = rootPath + "\\" + out_name2
                #创建临时图层
                arcpy.MakeFeatureLayer_management(tmpResultPath, creatName)
                resSelection = arcpy.SelectLayerByLocation_management(creatName,"BOUNDARY_TOUCHES",tmpResultPath2)
                fCNT = int(arcpy.GetCount_management(resSelection).getOutput(0))
 
                selectedID=selectedRow[1].encode("utf-8")
                mypath=rootPath+"\\"+tmpSelectedOutName
                if fCNT==1:
                    print "已经找到"
                    switchID(selectLayer,selectID,selectedLayer,selectedID)
                    delShpFile(rootPath+"\\"+tmpSelectedOutName)
                else:
                    delShpFile(rootPath+"\\"+tmpSelectedOutName)
                #删除临时图层
                arcpy.Delete_management(creatName)
            del selectedCursor
        delShpFile(rootPath + "\\" + tmpSelectOutName)
    del selectCursor
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值