工具箱相关
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SZdw1129_syb.py 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. import arcpy
  2. import math
  3. def SZpoly(zdpath1,zdpath2,tbpath1,tbpath2,tbpath3,outpath,):
  4. tb1 = -1
  5. arcpy.MakeFeatureLayer_management(zdpath1, 'ZD')
  6. #获取坐标系
  7. sr = arcpy.Describe('ZD').spatialReference
  8. try:
  9. arcpy.MakeFeatureLayer_management(tbpath1, 'TB')
  10. tb1 = 1
  11. except:
  12. arcpy.AddMessage('无图斑1shp可加载')
  13. zd2 = -1
  14. try:
  15. arcpy.MakeFeatureLayer_management(zdpath2, 'ZD2')
  16. zd2 = 1
  17. except:
  18. arcpy.AddMessage('无宗地2shp可加载')
  19. tb2 = -1
  20. try:
  21. arcpy.MakeFeatureLayer_management(tbpath2, 'TB2')
  22. tb2 = 1
  23. except:
  24. arcpy.AddMessage('无图斑2shp可加载')
  25. tb3 = -1
  26. try:
  27. arcpy.MakeFeatureLayer_management(tbpath3, 'TB3')
  28. tb3 = 1
  29. except:
  30. arcpy.AddMessage('无图斑3shp可加载')
  31. #添加字段
  32. try:
  33. arcpy.AddField_management('ZD', '四至北', 'TEXT')
  34. except:
  35. arcpy.AddMessage('四至北字段已存在')
  36. try:
  37. arcpy.AddField_management('ZD', '四至南', 'TEXT')
  38. except:
  39. arcpy.AddMessage('四至南字段已存在')
  40. try:
  41. arcpy.AddField_management('ZD', '四至西', 'TEXT')
  42. except:
  43. arcpy.AddMessage('四至西字段已存在')
  44. try:
  45. arcpy.AddField_management('ZD', '四至东', 'TEXT')
  46. except:
  47. arcpy.AddMessage('四至东字段已存在')
  48. #先全部把包络矩形做出来
  49. outshp = outpath + '\\minipolygon.shp'
  50. #效果不理想的话可以用面积
  51. try:
  52. arcpy.MinimumBoundingGeometry_management(zdpath1,outshp,"RECTANGLE_BY_AREA", "NONE")
  53. except:
  54. arcpy.Delete_management(outshp)
  55. arcpy.MinimumBoundingGeometry_management(zdpath1,outshp,"RECTANGLE_BY_AREA", "NONE")
  56. #先读取方向存成list(shape,宗地编号,北的方向)
  57. llist = []
  58. arcpy.MakeFeatureLayer_management(outshp, 'out1')
  59. with arcpy.da.UpdateCursor('out1', ['宗地编号','SHAPE@']) as cursorZ:
  60. for curZ in cursorZ:
  61. #把四至边的位置确定下来,并存下来
  62. shp0 = curZ[1]
  63. fxlist = []
  64. fxlist = pdszx(shp0,sr)
  65. sllist = []
  66. sllist.append(curZ[0])
  67. sllist.append(fxlist)
  68. llist.append(sllist)
  69. with arcpy.da.UpdateCursor('ZD', ['宗地编号','权利人','四至北','四至东','四至南','四至西','SHAPE@']) as cursorA:
  70. for curA in cursorA:
  71. zdbh = curA[0]
  72. ii = 0
  73. fxlist = []
  74. while ii < len(llist):
  75. if zdbh == llist[ii][0]:
  76. fxlist = llist[ii][1]
  77. break
  78. else:
  79. ii = ii + 1
  80. szlist = []
  81. szlist.append(curA[2])
  82. szlist.append(curA[3])
  83. szlist.append(curA[4])
  84. szlist.append(curA[5])
  85. szlist1 = zdszsf('ZD',szlist,fxlist,zdbh)
  86. szlist = szlist1
  87. if zd2 == 1:
  88. szlist1 = zdszsf('ZD2',szlist,fxlist,zdbh)
  89. szlist = szlist1
  90. if szlist1[0] !='' and szlist1[1] !='' and szlist1[2] !='' and szlist1[3] !='':
  91. pass
  92. else:
  93. #图斑
  94. if tb1 == 1:
  95. szlist1 = tbszsf('TB',szlist,fxlist,shp0)
  96. szlist = szlist1
  97. if tb2 == 1:
  98. szlist1 = tbszsf('TB2',szlist,fxlist,shp0)
  99. szlist = szlist1
  100. if tb3 == 1:
  101. szlist1 = tbszsf('TB3',szlist,fxlist,shp0)
  102. szlist = szlist1
  103. curA[2] = szlist[0]
  104. curA[3] = szlist[1]
  105. curA[4] = szlist[2]
  106. curA[5] = szlist[3]
  107. cursorA.updateRow (curA)
  108. arcpy.AddMessage(zdbh + ' finish')
  109. #约束下,至少占比5/1(这个可以调)
  110. def zdszsf(layer1,szlist,fxlist,zdbh):
  111. #然后挨个做相交
  112. arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',fxlist[0])
  113. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  114. with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人']) as cursorB:
  115. for curB in cursorB:
  116. if count0 != 0 and curB[0] != zdbh:
  117. str1 = curB[1] + '(' + curB[0] + ')'
  118. if szlist[0] == '':
  119. szlist[0] = str1
  120. break
  121. else:
  122. pass
  123. arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[0])
  124. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  125. with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人','SHAPE@']) as cursorC:
  126. for curC in cursorC:
  127. if count0 != 0 and curC[0] != zdbh:
  128. #先尝试相交
  129. shp0 = fxlist[0].buffer(1)
  130. shp1 = curC[2]
  131. line1 = shp0.intersect(shp1, 4)
  132. len1 = (shp0.area)/5
  133. if line1.area < len1:
  134. pass
  135. else:
  136. str1 = curC[1] + '(' + curC[0] + ')'
  137. if szlist[0] == '':
  138. szlist[0] = str1
  139. break
  140. else:
  141. pass
  142. arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',fxlist[1])
  143. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  144. with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人']) as cursorD:
  145. for curD in cursorD:
  146. if count0 != 0 and curD[0] != zdbh:
  147. str1 = curD[1] + '(' + curD[0] + ')'
  148. if szlist[1] == '':
  149. szlist[1] = str1
  150. break
  151. else:
  152. pass
  153. arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[1])
  154. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  155. with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人','SHAPE@']) as cursorE:
  156. for curE in cursorE:
  157. if count0 != 0 and curE[0] != zdbh:
  158. shp0 = fxlist[1].buffer(1)
  159. shp1 = curE[2]
  160. line1 = shp0.intersect (shp1, 4)
  161. len1 = (shp0.area)/5
  162. if line1.area < len1:
  163. pass
  164. else:
  165. str1 = curE[1] + '(' + curE[0] + ')'
  166. if szlist[1] == '':
  167. szlist[1] = str1
  168. break
  169. else:
  170. pass
  171. arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',fxlist[2])
  172. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  173. with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人']) as cursorF:
  174. for curF in cursorF:
  175. if count0 != 0 and curF[0] != zdbh:
  176. str1 = curF[1] + '(' + curF[0] + ')'
  177. if szlist[2] == '':
  178. szlist[2] = str1
  179. break
  180. else:
  181. pass
  182. arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[2])
  183. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  184. with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人','SHAPE@']) as cursorG:
  185. for curG in cursorG:
  186. if count0 != 0 and curG[0] != zdbh:
  187. shp0 = fxlist[2].buffer(1)
  188. shp1 = curG[2]
  189. line1 = shp0.intersect (shp1, 4)
  190. len1 = (shp0.area)/5
  191. if line1.area < len1:
  192. pass
  193. else:
  194. str1 = curG[1] + '(' + curG[0] + ')'
  195. if szlist[2] == '':
  196. szlist[2] = str1
  197. break
  198. else:
  199. pass
  200. arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',fxlist[3])
  201. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  202. with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人']) as cursorH:
  203. for curH in cursorH:
  204. if count0 != 0 and curH[0] != zdbh:
  205. str1 = curH[1] + '(' + curH[0] + ')'
  206. if szlist[3] == '':
  207. szlist[3] = str1
  208. break
  209. else:
  210. pass
  211. arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[3])
  212. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  213. with arcpy.da.SearchCursor(layer1, ['宗地编号','权利人','SHAPE@']) as cursorI:
  214. for curI in cursorI:
  215. if count0 != 0 and curI[0] != zdbh:
  216. shp0 = fxlist[3].buffer(1)
  217. shp1 = curI[2]
  218. line1 = shp0.intersect (shp1, 4)
  219. len1 = (shp0.area)/5
  220. if line1.area < len1:
  221. pass
  222. else:
  223. str1 = curI[1] + '(' + curI[0] + ')'
  224. if szlist[3] == '':
  225. szlist[3] = str1
  226. break
  227. else:
  228. pass
  229. return szlist
  230. def tbszsf(layer1,szlist,fxlist,shp0):
  231. #然后挨个做相交
  232. arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',shp0)
  233. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  234. with arcpy.da.SearchCursor(layer1, ['DLMC']) as cursorB:
  235. for curB in cursorB:
  236. if count0 != 0:
  237. str1 = curB[0]
  238. if szlist[0] == '':
  239. szlist[0] = str1
  240. break
  241. else:
  242. pass
  243. arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[0])
  244. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  245. list1 = []
  246. with arcpy.da.SearchCursor(layer1, ['DLMC','SHAPE@']) as cursorC:
  247. for curC in cursorC:
  248. if count0 != 0:
  249. try:
  250. shp2 = fxlist[0].buffer(1)
  251. shp1 = curC[1]
  252. line1 = shp2.intersect(shp1, 4)
  253. len1 = (shp2.area)/5
  254. if line1.area < len1:
  255. pass
  256. else:
  257. str1 = curC[0]
  258. if szlist[0] == '':
  259. szlist[0] = str1
  260. break
  261. else:
  262. pass
  263. except:
  264. pass
  265. arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',shp0)
  266. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  267. with arcpy.da.SearchCursor(layer1, ['DLMC']) as cursorD:
  268. for curD in cursorD:
  269. if count0 != 0:
  270. str1 = curD[0]
  271. if szlist[1] == '':
  272. szlist[1] = str1
  273. break
  274. else:
  275. pass
  276. arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[1])
  277. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  278. list1 = []
  279. with arcpy.da.SearchCursor(layer1, ['DLMC','SHAPE@']) as cursorE:
  280. for curE in cursorE:
  281. if count0 != 0:
  282. try:
  283. shp2 = fxlist[1].buffer(1)
  284. shp1 = curE[1]
  285. line1 = shp2.intersect(shp1, 4)
  286. len1 = (shp2.area)/5
  287. if line1.area < len1:
  288. pass
  289. else:
  290. str1 = curE[0]
  291. if szlist[1] == '':
  292. szlist[1] = str1
  293. break
  294. else:
  295. pass
  296. except:
  297. pass
  298. arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',shp0)
  299. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  300. with arcpy.da.SearchCursor(layer1, ['DLMC']) as cursorF:
  301. for curF in cursorF:
  302. if count0 != 0:
  303. str1 = curF[0]
  304. if szlist[2] == '':
  305. szlist[2] = str1
  306. break
  307. else:
  308. pass
  309. arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[2])
  310. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  311. list1 = []
  312. with arcpy.da.SearchCursor(layer1, ['DLMC','SHAPE@']) as cursorG:
  313. for curG in cursorG:
  314. if count0 != 0:
  315. try:
  316. shp2 = fxlist[2].buffer(1)
  317. shp1 = curG[1]
  318. line1 = shp2.intersect(shp1, 4)
  319. len1 = (shp2.area)/5
  320. if line1.area < len1:
  321. pass
  322. else:
  323. str1 = curG[0]
  324. if szlist[2] == '':
  325. szlist[2] = str1
  326. break
  327. else:
  328. pass
  329. except:
  330. pass
  331. arcpy.SelectLayerByLocation_management(layer1,'CONTAINS',shp0)
  332. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  333. with arcpy.da.SearchCursor(layer1, ['DLMC']) as cursorH:
  334. for curH in cursorH:
  335. if count0 != 0:
  336. str1 = curH[0]
  337. if szlist[3] == '':
  338. szlist[3] = str1
  339. break
  340. else:
  341. pass
  342. arcpy.SelectLayerByLocation_management(layer1,'INTERSECT',fxlist[3])
  343. count0 = int(arcpy.GetCount_management(layer1).getOutput(0))
  344. list1 = []
  345. with arcpy.da.SearchCursor(layer1, ['DLMC','SHAPE@']) as cursorI:
  346. for curI in cursorI:
  347. if count0 != 0:
  348. try:
  349. shp2 = fxlist[3].buffer(1)
  350. shp1 = curI[1]
  351. line1 = shp2.intersect(shp1, 4)
  352. len1 = (shp2.area)/5
  353. if line1.area < len1:
  354. pass
  355. else:
  356. str1 = curI[0]
  357. if szlist[3] == '':
  358. szlist[3] = str1
  359. break
  360. else:
  361. pass
  362. except:
  363. pass
  364. return szlist
  365. def pdszx(shp1,sr):
  366. zd1 = shp1.centroid
  367. zdx = zd1.X
  368. zdy = zd1.Y
  369. points = shp1.getPart()
  370. pt = points[0]
  371. ii = 0
  372. bfx = []
  373. dfx = []
  374. nfx = []
  375. xfx = []
  376. while ii < 4:
  377. pt0 = pt[ii]
  378. pty = pt0.Y
  379. if pty > zdy:
  380. bfx.append(pt0)
  381. else:
  382. nfx.append(pt0)
  383. ii = ii + 1
  384. #暂时没想到会有三个点的可能性
  385. if len(bfx) == 2:
  386. # if bfx[0].X > bfx[1].X:
  387. if bfx[0].X > zdx and bfx[1].X < zdx:
  388. dfx.append(bfx[0])
  389. xfx.append(bfx[1])
  390. elif bfx[0].X < zdx and bfx[1].X > zdx:
  391. dfx.append(bfx[1])
  392. xfx.append(bfx[0])
  393. elif bfx[0].X > bfx[1].X and bfx[1].X > zdx:
  394. dfx.append(bfx[0])
  395. dfx.append(bfx[1])
  396. nfx.append(bfx[0])
  397. bfx.remove(bfx[0])
  398. elif bfx[0].X < bfx[1].X and bfx[0].X > zdx:
  399. dfx.append(bfx[0])
  400. dfx.append(bfx[1])
  401. nfx.append(bfx[1])
  402. bfx.remove(bfx[1])
  403. elif bfx[0].X > bfx[1].X and bfx[0].X < zdx:
  404. xfx.append(bfx[0])
  405. xfx.append(bfx[1])
  406. nfx.append(bfx[1])
  407. bfx.remove(bfx[1])
  408. elif bfx[0].X < bfx[1].X and bfx[1].X < zdx:
  409. xfx.append(bfx[0])
  410. xfx.append(bfx[1])
  411. nfx.append(bfx[0])
  412. bfx.remove(bfx[0])
  413. if nfx[0].X > zdx and zdx > nfx[1].X:
  414. dfx.append(nfx[0])
  415. xfx.append(nfx[1])
  416. elif nfx[0].X < zdx and zdx < nfx[1].X:
  417. dfx.append(nfx[1])
  418. xfx.append(nfx[0])
  419. elif nfx[0].X > nfx[1].X and nfx[1].X > zdx:
  420. dfx.append(nfx[0])
  421. dfx.append(nfx[1])
  422. bfx.append(nfx[0])
  423. nfx.remove(nfx[0])
  424. elif nfx[0].X < nfx[1].X and nfx[0].X > zdx:
  425. dfx.append(nfx[0])
  426. dfx.append(nfx[1])
  427. bfx.append(nfx[1])
  428. nfx.remove(nfx[1])
  429. elif nfx[0].X > nfx[1].X and nfx[0].X < zdx:
  430. xfx.append(nfx[0])
  431. xfx.append(nfx[1])
  432. bfx.append(nfx[1])
  433. nfx.remove(nfx[1])
  434. elif nfx[0].X < nfx[1].X and nfx[1].X < zdx:
  435. xfx.append(nfx[0])
  436. xfx.append(nfx[1])
  437. bfx.append(nfx[0])
  438. nfx.remove(nfx[0])
  439. elif len(bfx) == 1:
  440. #也就是必有两个相同y值
  441. if round(nfx[0].Y,2) == round(nfx[1].Y,2):
  442. if nfx[0].X > nfx[1].X:
  443. bfx.append(nfx[0])
  444. nfx.remove(nfx[0])
  445. dfx.append(bfx[1])
  446. dfx.append(nfx[1])
  447. xfx.append(bfx[0])
  448. xfx.append(nfx[0])
  449. else:
  450. bfx.append(nfx[1])
  451. nfx.remove(nfx[1])
  452. dfx.append(bfx[1])
  453. dfx.append(nfx[1])
  454. xfx.append(bfx[0])
  455. xfx.append(nfx[0])
  456. elif round(nfx[0].Y,2) == round(nfx[2].Y,2):
  457. if nfx[0].X > nfx[2].X:
  458. bfx.append(nfx[0])
  459. nfx.remove(nfx[0])
  460. dfx.append(bfx[1])
  461. dfx.append(nfx[0])
  462. xfx.append(bfx[0])
  463. xfx.append(nfx[1])
  464. else:
  465. bfx.append(nfx[2])
  466. nfx.remove(nfx[2])
  467. dfx.append(bfx[1])
  468. dfx.append(nfx[1])
  469. xfx.append(bfx[0])
  470. xfx.append(nfx[0])
  471. elif round(nfx[1].Y,2) == round(nfx[2].Y,2):
  472. if nfx[1].X > nfx[2].X:
  473. bfx.append(nfx[1])
  474. nfx.remove(nfx[1])
  475. dfx.append(bfx[1])
  476. dfx.append(nfx[0])
  477. xfx.append(bfx[0])
  478. xfx.append(nfx[1])
  479. else:
  480. bfx.append(nfx[2])
  481. nfx.remove(nfx[2])
  482. dfx.append(bfx[1])
  483. dfx.append(nfx[0])
  484. xfx.append(bfx[0])
  485. xfx.append(nfx[1])
  486. #把点变成线,然后根据BDNX的顺序存储
  487. line1 = arcpy.Polyline(arcpy.Array(bfx),sr)
  488. line2 = arcpy.Polyline(arcpy.Array(dfx),sr)
  489. line3 = arcpy.Polyline(arcpy.Array(nfx),sr)
  490. line4 = arcpy.Polyline(arcpy.Array(xfx),sr)
  491. llist = []
  492. llist.append(line1)
  493. llist.append(line2)
  494. llist.append(line3)
  495. llist.append(line4)
  496. return llist
  497. def takefir(elem):
  498. return elem[0]
  499. if __name__ == '__main__':
  500. try:
  501. #根据需求设计4个选项,宗地1,宗地2,外业补充,731,待定
  502. 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')
  503. except arcpy.ExecuteError:
  504. arcpy.AddMessage(arcpy.GetMessages())