import arcpy import math def LZDpoly(zdshp, jzdshp, outpath): # 先把总点数搞出来,zdbh+总点数 txtpath1 = outpath + '\\LZD.txt' arcpy.MakeFeatureLayer_management(jzdshp, 'JZD') arcpy.MakeFeatureLayer_management(zdshp, 'ZD') sr = arcpy.Describe('ZD').spatialReference cc = 1 # 在外面写个字典 zd = {'公路用地': 'GLYD', '特殊用地': 'TSYD', '公用设施用地': 'GYSS', '交通服务场站用地': 'JTFW', '沟渠': 'GQ', '灌木林地': 'GMLD', '其他林地': 'QTLD', '乔木林地': 'QMLD', '竹林地': 'ZLD', '果园': 'GY', '旱地': 'HD', '河流水面': 'HLSM', '坑塘水面': 'KTSM', '养殖坑塘': 'YZKT', '农村道路': 'NCDL', '农村宅基地': 'NCZJD', '其他草地': 'QTCD', '其他园地': 'QTYD', '设施农用地': 'SSNYD', '水田': 'ST'} # with open(txtpath1.decode('utf-8'), 'w') as line: with arcpy.da.SearchCursor('ZD', ['SHAPE@', '宗地编码']) as cursorA: for curA in cursorA: # 先找到对应宗地 shpA = curA[0] # 所有点存个list,方便删(list0,listz0) zdbh = curA[1] if zdbh == '' or zdbh == ' ' or zdbh == None: pass else: # if cc == 1: # cc = -1 # else: # line.write('\n') # line.write(zdbh + ':') list1 = [] ii = 0 with arcpy.da.SearchCursor('JZD', ['SHAPE@', 'JZDH','ZDBH']) as cursorB: for curB in cursorB: # 前一个和当前的相交 if zdbh == curB[2]: if ii == 0: fp = curB[0].firstPoint fpx = fp.X fpy = fp.Y fpoint = arcpy.Point(fpx, fpy) firstpoint = arcpy.Point(fpx, fpy) else: lp = curB[0].firstPoint lpx = lp.X lpy = lp.Y lpoint = arcpy.Point(lpx, lpy) new_array = arcpy.Array() new_array.append(fpoint) new_array.append(lpoint) ln2 = arcpy.Polyline(new_array, sr) arcpy.SelectLayerByLocation_management( 'ZD', 'INTERSECT', ln2) with arcpy.da.SearchCursor('ZD', ['SHAPE@', '宗地编号', 'DLMC', '权利人']) as cursorC: for curC in cursorC: # 排除自己 str1 = curC[1] shpC = curC[0] if str1 == zdbh: pass else: # 排除只交一个点的情况 bl1 = fpoint.disjoint(shpC) bl2 = lpoint.disjoint(shpC) if bl1 == False and bl2 == False: if str1 == None or str1 == '' or str1 == ' ': str1 = curC[2] str2 = zd[str1] slist1 = [] slist1.append(ii) slist1.append(ii+1) slist1.append(str2) list1.append(slist1) else: str1 = curC[3] + \ '(' + curC[1]+')' slist1 = [] slist1.append(ii) slist1.append(ii+1) slist1.append(str1) list1.append(slist1) fp = curB[0].firstPoint fpx = fp.X fpy = fp.Y fpoint = arcpy.Point(fpx, fpy) ii = ii + 1 new_array = arcpy.Array() new_array.append(fpoint) new_array.append(firstpoint) ln2 = arcpy.Polyline(new_array, sr) arcpy.SelectLayerByLocation_management( 'ZD', 'INTERSECT', ln2) with arcpy.da.SearchCursor('ZD', ['SHAPE@', '宗地编号', 'DLMC', '权利人']) as cursorC: for curC in cursorC: # 排除自己 str1 = curC[1] shpC = curC[0] if str1 == zdbh: pass else: # 排除只交一个点的情况 bl1 = fpoint.disjoint(shpC) bl2 = lpoint.disjoint(shpC) if bl1 == False and bl2 == False: if str1 == None or str1 == '' or str1 == ' ': str1 = curC[2] slist1 = [] slist1.append(ii) slist1.append(1) slist1.append(str1) list1.append(slist1) else: str1 = curC[3] + '(' + curC[1]+')' slist1 = [] slist1.append(ii) slist1.append(1) slist1.append(str1) list1.append(slist1) # 然后把相同的合在一起,书写在txt上 mm = 0 qsd = '' zd = '' nr = '' while mm < len(list1): if mm == len(list1) - 1: if nr == list1[mm][2]: if 'JE' in nr: str1 = nr.split('(',-1)[-1] nr = str1.replace(')','') else: try: nr = str1 except: nr = 'QT' zd = 'J1' # line.write(nr + ',') # line.write(qsd + ',') # line.write(zd + ';') else: if 'JE' in nr: str1 = nr.split('(',-1)[-1] nr = str1.replace(')','') else: try: nr = str1 except: nr = 'QT' # line.write(nr + ',') # line.write(qsd + ',') # line.write(zd + ';') qsd = 'J' + str(list1[mm][0]) nr = list1[mm][2] zd = 'J1' if 'JE' in nr: str1 = nr.split('(',-1)[-1] nr = str1.replace(')','') else: try: nr = str1 except: nr = 'QT' # line.write(nr + ',') # line.write(qsd + ',') # line.write(zd + ';') elif mm == 0: nr = list1[mm][2] qsd = 'J1' else: if nr == list1[mm][2]: pass else: zd = 'J' + str(list1[mm][0]) if 'JE' in nr: str1 = nr.split('(',-1)[-1] nr = str1.replace(')','') else: try: nr = str1 except: nr = 'QT' # line.write(nr + ',') # line.write(qsd + ',') # line.write(zd + ';') qsd = 'J' + str(list1[mm][0]) nr = list1[mm][2] mm = mm + 1 arcpy.AddMessage(zdbh + ' finish') if __name__ == '__main__': try: # 输入宗地shp,输入界址点,界址点总数,底图all,输出文件夹 LZDpoly(r'D:\2work_now\20221026\test\testLZD1.shp', r'D:\2work_now\20221026\JZD\JZD.shp', r'D:\2work_now\20221026\test') except arcpy.ExecuteError: arcpy.AddMessage(arcpy.GetMessages())