|
#coding=utf8 import arcpy import sys def initProgress(hint,num): arcpy.SetProgressor("step", u"正在"+hint,0,num,1) def step(): arcpy.SetProgressorLabel(u"正在进行....") arcpy.SetProgressorPosition() def freeProgress(): arcpy.ResetProgressor() ########## def getCount(inFeature): result = arcpy.GetCount_management(inFeature) count= int(result.getOutput(0)) return count ##### def delsame(jfb_Select): desc = arcpy.Describe(jfb_Select) shapeName = desc.ShapeFieldName arcpy.DeleteIdentical_management(jfb_Select, [shapeName]) def getOIDField(jfb_Select): desc = arcpy.Describe(jfb_Select) OIDField=desc.OIDFieldName return OIDField ####### def AddLayer(mxd,inFeature): df=arcpy.mapping.ListDataFrames(mxd)[0] addLayer = arcpy.mapping.Layer(inFeature) arcpy.mapping.AddLayer(df, addLayer,"TOP") #AUTO_ARRANGE,"BOTTOM",TOP def FieldExists(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return True break return False def getField(TableName,FieldName): desc = arcpy.Describe(TableName) FieldName=FieldName.upper() for field in desc.fields: if field.Name.upper() ==FieldName: return field break return None def getrowbyFID(FieldNames,FID): with arcpy.da.SearchCursor(byFeature, FieldNames,oidFieldNameby+"="+str(FID)) as cursor: for row in cursor: return row def Main(): count=getCount(inFeature) if count <= 0: arcpy.AddMessage(u""+inFeature+"没有数据") return arcpy.Select_analysis(inFeature,outFeature) #ORIG_FID YL_FID1="YL_FID1" if not FieldExists(inFeature,YL_FID1): arcpy.AddField_management(inFeature,YL_FID1 ,"LONG") FidFieldName=getOIDField(outFeature) arcpy.CalculateField_management(inFeature,YL_FID1, '!'+FidFieldName+'!', "YTHON") FieldNames= FieldList.split(";") for FieldName in FieldNames: if not FieldExists(outFeature,FieldName): inField=getField(byFeature,FieldName) if inField: arcpy.AddField_management(outFeature,FieldName ,inField.type,inField.precision ,inField.scale,inField.length,inField.AliasName) num=len(FieldNames) for i in range(num-1,-1,-1):#0需要循环的 FieldName=FieldNames[i] #arcpy.AddMessage(u"FieldName"+FieldName+",i="+str(i)) pField=getField(outFeature,FieldName) if not pField.editable or pField.type=="OID" or pField.type=="Geometry": #arcpy.AddMessage(u"FieldName"+FieldName+",只读,删除") FieldNames.pop(i) FieldNames.append("OID@") YL_FID2="YL_FID2" if not FieldExists(byFeature,YL_FID2): arcpy.AddField_management(byFeature,YL_FID2 ,"LONG") FidFieldName=getOIDField(byFeature) arcpy.CalculateField_management(byFeature,YL_FID2, '!'+FidFieldName+'!', "YTHON") mytemp="in_memory/YL999888" mysort="in_memory/YL999sort" arcpy.AddMessage("TabulateIntersection================") arcpy.TabulateIntersection_analysis(outFeature,YL_FID1,byFeature,mytemp,YL_FID2) arcpy.AddMessage("TabulateIntersection") arcpy.Sort_management(mytemp,mysort,YL_FID1+" ASCENDING;PERCENTAGE DESCENDING") arcpy.AddMessage("Sort_management") arcpy.DeleteIdentical_management(mysort, [YL_FID1]) arcpy.AddMessage("DeleteIdentical") initProgress("update",num) updatecursor=arcpy.da.UpdateCursor(outFeature, FieldNames) scursor=arcpy.da.SearchCursor(mysort, [YL_FID1,YL_FID2,"PERCENTAGE"]) try: srow=scursor.next() Fieldnum=len(FieldNames) k=1 for row in updatecursor: step() FID1=srow[0] FID2=srow[1] Scale=srow[2] #arcpy.AddMessage("========{0},{1},{2}".format(FID1,FID2,Scale)) if Scale>minScale: row2= getrowbyFID(FieldNames,FID2) for i in range(Fieldnum-1): row[i]=row2[i] updatecursor.updateRow(row) else: arcpy.AddMessage(u""+inFeature+"中FID="+str(FID1)+"最大比例"+str(Scale)+",找不到叠加比例大于"+str(minScale)) if k<=num: srow=scursor.next() k=k+1 if FieldExists(byFeature,YL_FID2): arcpy.DeleteField_management(byFeature,YL_FID2) if FieldExists(outFeature,YL_FID1): arcpy.DeleteField_management(outFeature,YL_FID1) if updatecursor: del updatecursor if scursor: del scursor finally: freeProgress() arcpy.env.overwriteOutput = True inFeature = arcpy.GetParameterAsText(0) #输入要素 byFeature = arcpy.GetParameterAsText(1) #依据的要素 FieldList = arcpy.GetParameterAsText(2) #字段列表 outFeature = arcpy.GetParameterAsText(3) #输出要素 minScale=arcpy.GetParameter(4) ##最小比例,如果是负值,取最大的 oidFieldNameby=getOIDField(byFeature) try: Main() except Exception as e: arcpy.AddError(e.message) |
|