import arcpy import math def SZpoly(zdpath1,zdpath2,tbpath1,tbpath2,tbpath3,outpath,): tb1 = -1 arcpy.MakeFeatureLayer_management(zdpath1, 'ZD') #获取坐标系 sr = arcpy.Describe('ZD').spatialReference try: arcpy.MakeFeatureLayer_management(tbpath1, 'TB') tb1 = 1 except: arcpy.AddMessage('无图斑1shp可加载') zd2 = -1 try: arcpy.MakeFeatureLayer_management(zdpath2, 'ZD2') zd2 = 1 except: arcpy.AddMessage('无宗地2shp可加载') tb2 = -1 try: arcpy.MakeFeatureLayer_management(tbpath2, 'TB2') tb2 = 1 except: arcpy.AddMessage('无图斑2shp可加载') tb3 = -1 try: arcpy.MakeFeatureLayer_management(tbpath3, 'TB3') tb3 = 1 except: arcpy.AddMessage('无图斑3shp可加载') #添加字段 try: arcpy.AddField_management('ZD', '四至北', 'TEXT') except: arcpy.AddMessage('四至北字段已存在') try: arcpy.AddField_management('ZD', '四至南', 'TEXT') except: arcpy.AddMessage('四至南字段已存在') try: arcpy.AddField_management('ZD', '四至西', 'TEXT') except: arcpy.AddMessage('四至西字段已存在') try: arcpy.AddField_management('ZD', '四至东', 'TEXT') except: arcpy.AddMessage('四至东字段已存在') #先全部把包络矩形做出来 outshp = outpath + '\\minipolygon.shp' #效果不理想的话可以用面积 try: arcpy.MinimumBoundingGeometry_management(zdpath1,outshp,"RECTANGLE_BY_AREA", "NONE") except: arcpy.Delete_management(outshp) arcpy.MinimumBoundingGeometry_management(zdpath1,outshp,"RECTANGLE_BY_AREA", "NONE") #先读取方向存成list(shape,宗地编号,北的方向) llist = [] arcpy.MakeFeatureLayer_management(outshp, 'out1') with arcpy.da.UpdateCursor('out1', ['宗地编号','SHAPE@']) as cursorZ: for curZ in cursorZ: #把四至边的位置确定下来,并存下来 shp0 = curZ[1] fxlist = [] fxlist = pdszx(shp0,sr) sllist = [] sllist.append(curZ[0]) sllist.append(fxlist) llist.append(sllist) with arcpy.da.UpdateCursor('ZD', ['宗地编号','权利人','四至北','四至东','四至南','四至西','SHAPE@']) as cursorA: for curA in cursorA: zdbh = curA[0] ii = 0 fxlist = [] while ii < len(llist): if zdbh == llist[ii][0]: fxlist = llist[ii][1] break else: ii = ii + 1 szlist = [] szlist.append(curA[2]) szlist.append(curA[3]) szlist.append(curA[4]) szlist.append(curA[5]) szlist1 = zdszsf('ZD',szlist,fxlist,zdbh) szlist = szlist1 if zd2 == 1: szlist1 = zdszsf('ZD2',szlist,fxlist,zdbh) szlist = szlist1 if szlist1[0] !='' and szlist1[1] !='' and szlist1[2] !='' and szlist1[3] !='': pass else: #图斑 if tb1 == 1: szlist1 = tbszsf('TB',szlist,fxlist,shp0) szlist = szlist1 if tb2 == 1: szlist1 = tbszsf('TB2',szlist,fxlist,shp0) szlist = szlist1 if tb3 == 1: szlist1 = tbszsf('TB3',szlist,fxlist,shp0) szlist = szlist1 curA[2] = szlist[0] curA[3] = szlist[1] curA[4] = szlist[2] curA[5] = szlist[3] cursorA.updateRow (curA) arcpy.AddMessage(zdbh + ' finish') #约束下,至少占比5/1(这个可以调) def zdszsf(layer1,szlist,fxlist,zdbh): #然后挨个做相交 arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',fxlist[0]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人']) as cursorB: for curB in cursorB: if count0 != 0 and curB[0] != zdbh: str1 = curB[1] + '(' + curB[0] + ')' if szlist[0] == '': szlist[0] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[0]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人','SHAPE@']) as cursorC: for curC in cursorC: if count0 != 0 and curC[0] != zdbh: #先尝试相交 shp0 = fxlist[0].buffer(1) shp1 = curC[2] line1 = shp0.intersect(shp1, 4) len1 = (shp0.area)/5 if line1.area < len1: pass else: str1 = curC[1] + '(' + curC[0] + ')' if szlist[0] == '': szlist[0] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',fxlist[1]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人']) as cursorD: for curD in cursorD: if count0 != 0 and curD[0] != zdbh: str1 = curD[1] + '(' + curD[0] + ')' if szlist[1] == '': szlist[1] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[1]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人','SHAPE@']) as cursorE: for curE in cursorE: if count0 != 0 and curE[0] != zdbh: shp0 = fxlist[1].buffer(1) shp1 = curE[2] line1 = shp0.intersect (shp1, 4) len1 = (shp0.area)/5 if line1.area < len1: pass else: str1 = curE[1] + '(' + curE[0] + ')' if szlist[1] == '': szlist[1] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',fxlist[2]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人']) as cursorF: for curF in cursorF: if count0 != 0 and curF[0] != zdbh: str1 = curF[1] + '(' + curF[0] + ')' if szlist[2] == '': szlist[2] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[2]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人','SHAPE@']) as cursorG: for curG in cursorG: if count0 != 0 and curG[0] != zdbh: shp0 = fxlist[2].buffer(1) shp1 = curG[2] line1 = shp0.intersect (shp1, 4) len1 = (shp0.area)/5 if line1.area < len1: pass else: str1 = curG[1] + '(' + curG[0] + ')' if szlist[2] == '': szlist[2] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',fxlist[3]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人']) as cursorH: for curH in cursorH: if count0 != 0 and curH[0] != zdbh: str1 = curH[1] + '(' + curH[0] + ')' if szlist[3] == '': szlist[3] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[3]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人','SHAPE@']) as cursorI: for curI in cursorI: if count0 != 0 and curI[0] != zdbh: shp0 = fxlist[3].buffer(1) shp1 = curI[2] line1 = shp0.intersect (shp1, 4) len1 = (shp0.area)/5 if line1.area < len1: pass else: str1 = curI[1] + '(' + curI[0] + ')' if szlist[3] == '': szlist[3] = str1 break else: pass return szlist def tbszsf(layer1,szlist,fxlist,shp0): #然后挨个做相交 arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',shp0) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['DLMC']) as cursorB: for curB in cursorB: if count0 != 0: str1 = curB[0] if szlist[0] == '': szlist[0] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[0]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) list1 = [] with arcpy.da.SearchCursor(layer1, ['DLMC','SHAPE@']) as cursorC: for curC in cursorC: if count0 != 0: try: shp2 = fxlist[0].buffer(1) shp1 = curC[1] line1 = shp2.intersect(shp1, 4) len1 = (shp2.area)/5 if line1.area < len1: pass else: str1 = curC[0] if szlist[0] == '': szlist[0] = str1 break else: pass except: pass arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',shp0) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['DLMC']) as cursorD: for curD in cursorD: if count0 != 0: str1 = curD[0] if szlist[1] == '': szlist[1] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[1]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) list1 = [] with arcpy.da.SearchCursor(layer1, ['DLMC','SHAPE@']) as cursorE: for curE in cursorE: if count0 != 0: try: shp2 = fxlist[1].buffer(1) shp1 = curE[1] line1 = shp2.intersect(shp1, 4) len1 = (shp2.area)/5 if line1.area < len1: pass else: str1 = curE[0] if szlist[1] == '': szlist[1] = str1 break else: pass except: pass arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',shp0) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['DLMC']) as cursorF: for curF in cursorF: if count0 != 0: str1 = curF[0] if szlist[2] == '': szlist[2] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[2]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) list1 = [] with arcpy.da.SearchCursor(layer1, ['DLMC','SHAPE@']) as cursorG: for curG in cursorG: if count0 != 0: try: shp2 = fxlist[2].buffer(1) shp1 = curG[1] line1 = shp2.intersect(shp1, 4) len1 = (shp2.area)/5 if line1.area < len1: pass else: str1 = curG[0] if szlist[2] == '': szlist[2] = str1 break else: pass except: pass arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',shp0) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) with arcpy.da.SearchCursor(layer1, ['DLMC']) as cursorH: for curH in cursorH: if count0 != 0: str1 = curH[0] if szlist[3] == '': szlist[3] = str1 break else: pass arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[3]) count0 = int(arcpy.GetCount_management(layer1).getOutput(0)) list1 = [] with arcpy.da.SearchCursor(layer1, ['DLMC','SHAPE@']) as cursorI: for curI in cursorI: if count0 != 0: try: shp2 = fxlist[3].buffer(1) shp1 = curI[1] line1 = shp2.intersect(shp1, 4) len1 = (shp2.area)/5 if line1.area < len1: pass else: str1 = curI[0] if szlist[3] == '': szlist[3] = str1 break else: pass except: pass return szlist def pdszx(shp1,sr): zd1 = shp1.centroid zdx = zd1.X zdy = zd1.Y points = shp1.getPart() pt = points[0] ii = 0 bfx = [] dfx = [] nfx = [] xfx = [] while ii < 4: pt0 = pt[ii] pty = pt0.Y if pty > zdy: bfx.append(pt0) else: nfx.append(pt0) ii = ii + 1 #暂时没想到会有三个点的可能性 if len(bfx) == 2: # if bfx[0].X > bfx[1].X: if bfx[0].X > zdx and bfx[1].X < zdx: dfx.append(bfx[0]) xfx.append(bfx[1]) elif bfx[0].X < zdx and bfx[1].X > zdx: dfx.append(bfx[1]) xfx.append(bfx[0]) elif bfx[0].X > bfx[1].X and bfx[1].X > zdx: dfx.append(bfx[0]) dfx.append(bfx[1]) nfx.append(bfx[0]) bfx.remove(bfx[0]) elif bfx[0].X < bfx[1].X and bfx[0].X > zdx: dfx.append(bfx[0]) dfx.append(bfx[1]) nfx.append(bfx[1]) bfx.remove(bfx[1]) elif bfx[0].X > bfx[1].X and bfx[0].X < zdx: xfx.append(bfx[0]) xfx.append(bfx[1]) nfx.append(bfx[1]) bfx.remove(bfx[1]) elif bfx[0].X < bfx[1].X and bfx[1].X < zdx: xfx.append(bfx[0]) xfx.append(bfx[1]) nfx.append(bfx[0]) bfx.remove(bfx[0]) if nfx[0].X > zdx and zdx > nfx[1].X: dfx.append(nfx[0]) xfx.append(nfx[1]) elif nfx[0].X < zdx and zdx < nfx[1].X: dfx.append(nfx[1]) xfx.append(nfx[0]) elif nfx[0].X > nfx[1].X and nfx[1].X > zdx: dfx.append(nfx[0]) dfx.append(nfx[1]) bfx.append(nfx[0]) nfx.remove(nfx[0]) elif nfx[0].X < nfx[1].X and nfx[0].X > zdx: dfx.append(nfx[0]) dfx.append(nfx[1]) bfx.append(nfx[1]) nfx.remove(nfx[1]) elif nfx[0].X > nfx[1].X and nfx[0].X < zdx: xfx.append(nfx[0]) xfx.append(nfx[1]) bfx.append(nfx[1]) nfx.remove(nfx[1]) elif nfx[0].X < nfx[1].X and nfx[1].X < zdx: xfx.append(nfx[0]) xfx.append(nfx[1]) bfx.append(nfx[0]) nfx.remove(nfx[0]) elif len(bfx) == 1: #也就是必有两个相同y值 if round(nfx[0].Y,2) == round(nfx[1].Y,2): if nfx[0].X > nfx[1].X: bfx.append(nfx[0]) nfx.remove(nfx[0]) dfx.append(bfx[1]) dfx.append(nfx[1]) xfx.append(bfx[0]) xfx.append(nfx[0]) else: bfx.append(nfx[1]) nfx.remove(nfx[1]) dfx.append(bfx[1]) dfx.append(nfx[1]) xfx.append(bfx[0]) xfx.append(nfx[0]) elif round(nfx[0].Y,2) == round(nfx[2].Y,2): if nfx[0].X > nfx[2].X: bfx.append(nfx[0]) nfx.remove(nfx[0]) dfx.append(bfx[1]) dfx.append(nfx[0]) xfx.append(bfx[0]) xfx.append(nfx[1]) else: bfx.append(nfx[2]) nfx.remove(nfx[2]) dfx.append(bfx[1]) dfx.append(nfx[1]) xfx.append(bfx[0]) xfx.append(nfx[0]) elif round(nfx[1].Y,2) == round(nfx[2].Y,2): if nfx[1].X > nfx[2].X: bfx.append(nfx[1]) nfx.remove(nfx[1]) dfx.append(bfx[1]) dfx.append(nfx[0]) xfx.append(bfx[0]) xfx.append(nfx[1]) else: bfx.append(nfx[2]) nfx.remove(nfx[2]) dfx.append(bfx[1]) dfx.append(nfx[0]) xfx.append(bfx[0]) xfx.append(nfx[1]) #把点变成线,然后根据BDNX的顺序存储 line1 = arcpy.Polyline(arcpy.Array(bfx),sr) line2 = arcpy.Polyline(arcpy.Array(dfx),sr) line3 = arcpy.Polyline(arcpy.Array(nfx),sr) line4 = arcpy.Polyline(arcpy.Array(xfx),sr) llist = [] llist.append(line1) llist.append(line2) llist.append(line3) llist.append(line4) return llist def takefir(elem): return elem[0] if __name__ == '__main__': try: #根据需求设计4个选项,宗地1,宗地2,外业补充,731,待定 SZpoly(r'D:\2work_now\20221026\test\testSZ.shp','',r'D:\2work_now\20221026\test\BDDW.shp',r'D:\2work_now\20221026\test\BGTB.shp','',r'D:\2work_now\20221026\test') except arcpy.ExecuteError: arcpy.AddMessage(arcpy.GetMessages())