123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- 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())
-
-
-
-
-
-
-
|