利用arcpy實現接邊處理(arcgis要素建立、更新、圖層選擇)
阿新 • • 發佈:2018-12-16
之前一個專案中有關於接邊方面內容,即在兩個相鄰的行政區域內出現面數據有相鄰的部分,現在需要將相鄰部分兩個面的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, [" [email protected]",'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, [" [email protected]",'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, ["[email protected]",'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.
更多內容,請關注公眾號