from asyncore import write import arcpy def TFtoTB(tbshp,tfshp,outfilepath): outpath = outfilepath + '\\TF.txt' #加载 arcpy.MakeFeatureLayer_management(tbshp, 'TB') arcpy.MakeFeatureLayer_management(tfshp, 'TF') with open(outpath,'w') as f: with arcpy.da.SearchCursor('TB',['SHAPE@','宗地编码']) as cursorA: for curA in cursorA: list1 = [] shape1 = curA[0] zdh = curA[1] #相交,包含,被包含 arcpy.SelectLayerByLocation_management('TF',"INTERSECT",shape1) ma1 = int(arcpy.GetCount_management('TF').getOutput(0)) if ma1 != 0: with arcpy.da.SearchCursor('TF',['TFH']) as cursorB: for curB in cursorB: tfh1 = str(curB[0]) list1.append(tfh1) arcpy.SelectLayerByLocation_management('TF',"WITHIN",shape1) ma2 = int(arcpy.GetCount_management('TF').getOutput(0)) if ma2 != 0: with arcpy.da.SearchCursor('TF',['TFH']) as cursorC: for curC in cursorC: tfh2 = str(curC[0]) list1.append(tfh2) arcpy.SelectLayerByLocation_management('TF',"COMPLETELY_CONTAINS",shape1) ma3 = int(arcpy.GetCount_management('TF').getOutput(0)) if ma3 != 0: with arcpy.da.SearchCursor('TF',['TFH']) as cursorD: for curD in cursorD: tfh3 = str(curD[0]) list1.append(tfh3) if list1 == []: pass else: f.write(zdh) f.write(':') ii = 0 list2 = [] while ii < len(list1): if list1[ii] in list2: pass else: if ii == 0: f.write(list1[ii]) list2.append(list1[ii]) else: f.write(',') f.write(list1[ii]) list2.append(list1[ii]) ii = ii + 1 f.write('\n') f.flush() print('ok') arcpy.AddMessage ('ok') if __name__ == '__main__': try: #输入shp,图幅shp,输出文件夹 TFtoTB(arcpy.GetParameterAsText(0),arcpy.GetParameterAsText(1),arcpy.GetParameterAsText(2)) except arcpy.ExecuteError: arcpy.AddMessage (arcpy.GetMessages())