【ArcPy】将小面合并到相邻相同类型的面积最大面
# coding=gbk
import arcpy
from arcpy import da as da
import os
def main():
lyr=arcpy.GetParameter(0)
areaFld=arcpy.GetParameterAsText(1)
areaLimit=arcpy.GetParameter(2)
typeFld=arcpy.GetParameterAsText(3)
idFld=arcpy.GetParameterAsText(4)
oIDList=[]
tempOIDList=[]
while True:
desc = arcpy.Describe(lyr)
fcls=desc.dataElement.catalogPath
workspace = os.path.dirname(fcls)
editor=da.Editor(workspace)
editor.startEditing(True,False)
editor.startOperation()
delExpression = u'{0} <= {1}'.format(arcpy.AddFieldDelimiters(lyr, areaFld) ,areaLimit)
with da.UpdateCursor(lyr,['SHAPE@',typeFld,idFld],where_clause=delExpression ) as delCur:
for delRow in delCur:
delGeometry=delRow[0]
schExpression = u"{0} = '{1}' AND {2} <> {3}".format(arcpy.AddFieldDelimiters(lyr, typeFld) ,delRow[1],idFld,delRow[2])
with da.SearchCursor(lyr,['SHAPE@',idFld,areaFld],where_clause=schExpression) as schCur:
updFeatureId=None
maxArea=-1.0
for schRow in schCur:
schGeometry=schRow[0]
if not delGeometry.disjoint (schGeometry):
if schRow[2]>maxArea:
maxArea=schRow[2]
updFeatureId=schRow[1]
if updFeatureId==None:
oIDList.append(delRow[2])
else:
updExpression= u"{0} = {1}".format(arcpy.AddFieldDelimiters(lyr, idFld) ,updFeatureId)
with da.UpdateCursor(lyr,['SHAPE@'],where_clause=updExpression) as updCur:
updRow=updCur.next()
updRow[0]= updRow[0].union(delGeometry)
updCur.updateRow(updRow)
delCur.deleteRow()
editor.stopOperation()
editor.stopEditing(True)
if oIDList==tempOIDList:
break
else:
tempOIDList=oIDList
if __name__=="__main__":
main()