(三)arcpy开发&利用arcpy实现接边处理(arcgis要素创建、更新、图层选择)

58 篇文章 19 订阅
37 篇文章 17 订阅

首先小伙伴们,可以看一下我们的学习视频

https://edu.csdn.net/course/detail/25535

之前一个项目中有关于接边方面内容,即在两个相邻的行政区域内出现面数据有相邻的部分,现在需要将相邻部分两个面的ID互换。具体的数据如下图所示:

那么如何来解决这个问题呢,首先在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

当然在中间遇到了不少的问题,首先来看一下这个,显然是使用游标选择的时候,字段值没有造成的错误。

RuntimeError: A column was specified that does not exist.


                                                                              更多内容,请关注公众号

                                                                    

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yGIS

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

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

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

打赏作者

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

抵扣说明:

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

余额充值