工具箱相关
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JZDpoint20230110_1.py 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. import arcpy
  2. import math
  3. def JZpointmake(inpath, outfileshp):
  4. # region [读取shp]
  5. shpstr = 'JZD1.shp'
  6. outpath = outfileshp + '\\' + 'JZD1.shp'
  7. try:
  8. arcpy.CreateFeatureclass_management(outfileshp, shpstr, 'POINT')
  9. except:
  10. arcpy.Delete_management(outpath)
  11. arcpy.CreateFeatureclass_management(outfileshp, shpstr, 'POINT')
  12. # 加载
  13. arcpy.MakeFeatureLayer_management(inpath, 'LB')
  14. arcpy.MakeFeatureLayer_management(outpath, 'JZD')
  15. sr = arcpy.Describe('LB').spatialReference
  16. # 添加一群字段
  17. arcpy.AddField_management('JZD', '标识码', 'LONG')
  18. arcpy.AddField_management('JZD', 'YSDM', 'TEXT')
  19. arcpy.AddField_management('JZD', 'JZDH', 'TEXT')
  20. arcpy.AddField_management('JZD', 'JBLX', 'TEXT')
  21. arcpy.AddField_management('JZD', 'JZDLX', 'TEXT')
  22. arcpy.AddField_management('JZD', 'ZDBH', 'TEXT')
  23. arcpy.AddField_management('JZD', '顺序号', 'LONG')
  24. arcpy.AddField_management('JZD', 'flag', 'LONG')
  25. #float按理说应该符合的
  26. bsm = 0
  27. field1 = ['SHAPE@', '标识码', 'YSDM', 'JZDH', 'JBLX',
  28. 'JZDLX', 'ZDBH', '顺序号','flag']
  29. # endregion
  30. with arcpy.da.InsertCursor('JZD', field1) as cursorZ:
  31. with arcpy.da.SearchCursor('LB', ['SHAPE@', '宗地编号']) as cursorA:
  32. for curA in cursorA:
  33. # region [读折点]
  34. poly1 = curA[0]
  35. zdbh = curA[1]
  36. if zdbh == None or zdbh == '' or zdbh == ' ':
  37. pass
  38. else:
  39. # 获取折点
  40. points = poly1.getPart()
  41. pt = points[0]
  42. mm = 0
  43. linelist = []
  44. indexp = 0
  45. #存的都是点
  46. while mm < pt.count:
  47. jzd = pt[mm]
  48. try:
  49. xx = round(jzd.X,4)
  50. yy = round(jzd.Y,4)
  51. if mm == 0 or mm == indexp:
  52. xx0 = xx
  53. yy0 = yy
  54. slist = []
  55. slist.append(pt[mm])
  56. else:
  57. numdis0 = xx - xx0
  58. numdis1 = yy - yy0
  59. numdis = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1))
  60. if xx == xx0 and yy == yy0:
  61. #一个的结束
  62. indexp = mm + 1
  63. linelist.append(slist)
  64. if numdis < 0.1:
  65. pass
  66. else:
  67. slist.append(pt[mm])
  68. mm = mm + 1
  69. except:
  70. if mm == indexp:
  71. indexp = mm + 1
  72. mm = mm + 1
  73. llen = len(linelist)
  74. #判断下如果多环,排个序
  75. linelist1 = pdlen(linelist)
  76. linelist = linelist1
  77. # endregion
  78. #甭管有没有环,都是第一个来处理
  79. ln1 = linelist[0]
  80. list1 = []
  81. nn1 = 0
  82. while nn1 < len(ln1):
  83. if nn1 == len(ln1) - 1:
  84. fpx = round(ln1[nn1].X,4)
  85. fpy = round(ln1[nn1].Y,4)
  86. lpx = round(ln1[0].X,4)
  87. lpy = round(ln1[0].Y,4)
  88. fpoint = arcpy.Point(fpx, fpy)
  89. lpoint = arcpy.Point(lpx, lpy)
  90. else:
  91. fpx = round(ln1[nn1].X,4)
  92. fpy = round(ln1[nn1].Y,4)
  93. lpx = round(ln1[nn1 + 1].X,4)
  94. lpy = round(ln1[nn1 + 1].Y,4)
  95. fpoint = arcpy.Point(fpx, fpy)
  96. lpoint = arcpy.Point(lpx, lpy)
  97. new_array = arcpy.Array()
  98. new_array.append(fpoint)
  99. new_array.append(lpoint)
  100. ln2 = arcpy.Polyline(new_array,sr)
  101. #先把起始点加进去
  102. bl1 = addpoint(list1,fpoint)
  103. if bl1 == 1:
  104. list1.append(fpoint)
  105. # list1.append(lpoint)
  106. #插入点
  107. arcpy.SelectLayerByLocation_management('LB','INTERSECT',ln2)
  108. count0 = int(arcpy.GetCount_management('LB').getOutput(0))
  109. if count0 < 2:
  110. bl4 = addpoint(list1,lpoint)
  111. if bl4 == 1:
  112. list1.append(lpoint)
  113. nn1 = nn1 + 1
  114. else:
  115. #对于同一个线段来说,把所有点的位置准确点
  116. listp1 = []
  117. with arcpy.da.SearchCursor('LB', ['SHAPE@', '宗地编号']) as cursorB:
  118. for curB in cursorB:
  119. zdbh1 = curB[1]
  120. #排除自己
  121. if zdbh1 == zdbh:
  122. pass
  123. else:
  124. try:
  125. shpB = curB[0]
  126. lineB = shpB.boundary ()
  127. rln = lineB.intersect (ln2, 2)
  128. fpoint1 = rln.firstPoint
  129. lpoint1 = rln.lastPoint
  130. fx = round(fpoint1.X,4)
  131. fy = round(fpoint1.Y,4)
  132. lx = round(lpoint1.X,4)
  133. ly = round(lpoint1.Y,4)
  134. #判断这两个点是否都在里面
  135. fp = arcpy.Point(fx, fy)
  136. lp = arcpy.Point(lx, ly)
  137. list2 = []
  138. bl1 = addpoint(list1,fp)
  139. if bl1 == 1:
  140. #判断与起点的距离
  141. dis1 = math.sqrt(((fpx - fx)*(fpx - fx)) + ((fpy-fy)*(fpy-fy)))
  142. slist2 = []
  143. slist2.append(dis1)
  144. slist2.append(fp)
  145. list2.append(slist2)
  146. bl1 = addpoint(list1,lp)
  147. if bl1 == 1:
  148. dis1 = math.sqrt(((fpx - lx)*(fpx - lx)) + ((fpy-ly)*(fpy-ly)))
  149. slist2 = []
  150. slist2.append(dis1)
  151. slist2.append(lp)
  152. list2.append(slist2)
  153. if len(list2) == 1:
  154. listp1.append(list2[0][1])
  155. elif len(list2) == 2:
  156. #对2排序
  157. if list2[0][0] < list2[1][0]:
  158. listp1.append(list2[0][1])
  159. listp1.append(list2[1][1])
  160. else:
  161. listp1.append(list2[1][1])
  162. listp1.append(list2[0][1])
  163. except:
  164. pass
  165. #得到所有的点,进行一个与起点的排序
  166. nn2 = 0
  167. listp2 =[]
  168. while nn2 < len(listp1):
  169. num0 = fpx - listp1[nn2].X
  170. num1 = fpy - listp1[nn2].Y
  171. dis2 = math.sqrt((num0*num0)+(num1*num1))
  172. slistp2 = []
  173. slistp2.append(dis2)
  174. slistp2.append(listp1[nn2])
  175. listp2.append(slistp2)
  176. nn2 = nn2 + 1
  177. listp2.sort()
  178. nn3 = 0
  179. bl3 = 0
  180. while nn3 < len(listp2):
  181. #这里要看下要不要除重
  182. bl3 = addpoint(list1,listp2[nn3][1])
  183. if bl3 == 1:
  184. list1.append(listp2[nn3][1])
  185. nn3 = nn3 + 1
  186. bl4 = addpoint(list1,lpoint)
  187. if bl4 == 1:
  188. list1.append(lpoint)
  189. nn1 = nn1 + 1
  190. if len(linelist) == 1:
  191. listF = linezd(list1,poly1)
  192. #直接写
  193. ii = 0
  194. zdh = 0
  195. while ii < len(listF):
  196. if ii == 0:
  197. zdh = zdh + 1
  198. bsm = bsm + 1
  199. # shape
  200. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  201. # 要素代码
  202. ysdm = '6002010300'
  203. # 顺序号
  204. sxh = ii + 1
  205. # 界址点号
  206. jzdh = 'J' + str(zdh)
  207. # 宗地代码
  208. zddm = curA[1]
  209. rlist = []
  210. rlist.append(point1)
  211. rlist.append(bsm)
  212. rlist.append(ysdm)
  213. rlist.append(jzdh)
  214. rlist.append('')
  215. rlist.append('')
  216. rlist.append(zddm)
  217. rlist.append(zdh)
  218. rlist.append(1)
  219. cursorZ.insertRow(rlist)
  220. else:
  221. numdis0 = listF[ii][0] - listF[ii - 1][0]
  222. numdis1 = listF[ii][1] - listF[ii - 1][1]
  223. numdis2 = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1))
  224. if numdis2 < 0.1:
  225. pass
  226. else:
  227. zdh = zdh + 1
  228. bsm = bsm + 1
  229. # shape
  230. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  231. # 要素代码
  232. ysdm = '6002010300'
  233. # 顺序号
  234. sxh = ii + 1
  235. # 界址点号
  236. jzdh = 'J' + str(zdh)
  237. # 宗地代码
  238. zddm = curA[1]
  239. rlist = []
  240. rlist.append(point1)
  241. rlist.append(bsm)
  242. rlist.append(ysdm)
  243. rlist.append(jzdh)
  244. rlist.append('')
  245. rlist.append('')
  246. rlist.append(zddm)
  247. rlist.append(zdh)
  248. rlist.append(1)
  249. cursorZ.insertRow(rlist)
  250. ii = ii + 1
  251. else:
  252. mm0 = 0
  253. zdh = 0
  254. while mm0 < len(linelist):
  255. if mm0 == 0:
  256. listF = linezd(list1,poly1)
  257. ii = 0
  258. while ii < len(listF):
  259. if ii == 0:
  260. zdh = zdh + 1
  261. bsm = bsm + 1
  262. # shape
  263. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  264. # 要素代码
  265. ysdm = '6002010300'
  266. # 顺序号
  267. sxh = ii + 1
  268. # 界址点号
  269. jzdh = 'J' + str(zdh)
  270. # 宗地代码
  271. zddm = curA[1]
  272. rlist = []
  273. rlist.append(point1)
  274. rlist.append(bsm)
  275. rlist.append(ysdm)
  276. rlist.append(jzdh)
  277. rlist.append('')
  278. rlist.append('')
  279. rlist.append(zddm)
  280. rlist.append(zdh)
  281. rlist.append(mm0+1)
  282. cursorZ.insertRow(rlist)
  283. else:
  284. numdis0 = listF[ii][0] - listF[ii - 1][0]
  285. numdis1 = listF[ii][1] - listF[ii - 1][1]
  286. numdis2 = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1))
  287. if numdis2 < 0.1:
  288. pass
  289. else:
  290. zdh = zdh + 1
  291. bsm = bsm + 1
  292. # shape
  293. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  294. # 要素代码
  295. ysdm = '6002010300'
  296. # 顺序号
  297. sxh = ii + 1
  298. # 界址点号
  299. jzdh = 'J' + str(zdh)
  300. # 宗地代码
  301. zddm = curA[1]
  302. rlist = []
  303. rlist.append(point1)
  304. rlist.append(bsm)
  305. rlist.append(ysdm)
  306. rlist.append(jzdh)
  307. rlist.append('')
  308. rlist.append('')
  309. rlist.append(zddm)
  310. rlist.append(zdh)
  311. rlist.append(mm0+1)
  312. cursorZ.insertRow(rlist)
  313. ii = ii + 1
  314. else:
  315. listF = linezd(linelist[mm0],poly1)
  316. ii = len(listF) - 1
  317. while ii >= 0:
  318. if ii == len(listF) - 1:
  319. zdh = zdh + 1
  320. bsm = bsm + 1
  321. # shape
  322. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  323. # 要素代码
  324. ysdm = '6002010300'
  325. # 顺序号
  326. sxh = ii + 1
  327. # 界址点号
  328. jzdh = 'J' + str(zdh)
  329. # 宗地代码
  330. zddm = curA[1]
  331. rlist = []
  332. rlist.append(point1)
  333. rlist.append(bsm)
  334. rlist.append(ysdm)
  335. rlist.append(jzdh)
  336. rlist.append('')
  337. rlist.append('')
  338. rlist.append(zddm)
  339. rlist.append(zdh)
  340. rlist.append(mm0+1)
  341. cursorZ.insertRow(rlist)
  342. else:
  343. numdis0 = listF[ii][0] - listF[ii - 1][0]
  344. numdis1 = listF[ii][1] - listF[ii - 1][1]
  345. numdis2 = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1))
  346. if numdis2 < 0.1:
  347. pass
  348. else:
  349. zdh = zdh + 1
  350. bsm = bsm + 1
  351. # shape
  352. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  353. # 要素代码
  354. ysdm = '6002010300'
  355. # 顺序号
  356. sxh = ii + 1
  357. # 界址点号
  358. jzdh = 'J' + str(zdh)
  359. # 宗地代码
  360. zddm = curA[1]
  361. rlist = []
  362. rlist.append(point1)
  363. rlist.append(bsm)
  364. rlist.append(ysdm)
  365. rlist.append(jzdh)
  366. rlist.append('')
  367. rlist.append('')
  368. rlist.append(zddm)
  369. rlist.append(zdh)
  370. rlist.append(mm0+1)
  371. cursorZ.insertRow(rlist)
  372. ii = ii - 1
  373. mm0 = mm0 + 1
  374. arcpy.AddMessage(zdbh + ' FINISH')
  375. def pdlen(list1):
  376. ii = 0
  377. list2 = []
  378. while ii < len(list1):
  379. pts = list1[ii]
  380. length1 = 0
  381. mm = 0
  382. while mm < len(pts):
  383. if mm == len(pts) - 1:
  384. pt1 = pts[mm]
  385. pt2 = pts[0]
  386. dis1 = math.sqrt(((pt1.X-pt2.X)*(pt1.X-pt2.X))+((pt1.Y-pt2.Y)*(pt1.Y-pt2.Y)))
  387. length1 = length1 + dis1
  388. mm = mm + 1
  389. else:
  390. pt1 = pts[mm]
  391. pt2 = pts[mm + 1]
  392. dis1 = math.sqrt(((pt1.X-pt2.X)*(pt1.X-pt2.X))+((pt1.Y-pt2.Y)*(pt1.Y-pt2.Y)))
  393. length1 = length1 + dis1
  394. mm = mm + 1
  395. slist2 = []
  396. slist2.append(length1)
  397. slist2.append(pts)
  398. list2.append(slist2)
  399. ii = ii + 1
  400. list2.sort(reverse=True)
  401. nn = 0
  402. list3 = []
  403. while nn < len(list2):
  404. list3.append(list2[nn][1])
  405. nn = nn + 1
  406. return list3
  407. def addpoint(list1,pt):
  408. ptx = round(pt.X,4)
  409. pty = round(pt.Y,4)
  410. ii = 0
  411. bl = 1
  412. while ii < len(list1):
  413. if ptx == round(list1[ii].X,4) and pty == round(list1[ii].Y,4):
  414. bl = -1
  415. ii = ii + 1
  416. return bl
  417. def linezd(line1,poly):
  418. # 获取折点
  419. mm = 0
  420. pp = 0
  421. list1 = []
  422. while mm < len(line1):
  423. plist = []
  424. jzd = line1[mm]
  425. try:
  426. xx = jzd.X
  427. yy = jzd.Y
  428. plist.append(xx)
  429. plist.append(yy)
  430. # 初始顺序
  431. plist.append(pp)
  432. list1.append(plist)
  433. pp = pp + 1
  434. mm = mm + 1
  435. except:
  436. mm = mm + 1
  437. # 对折点排序(算法)
  438. if len(list1) < 3:
  439. listF = []
  440. else:
  441. # listF = zdsort(list1, zx)
  442. listF = jzdsort(list1,poly)
  443. return listF
  444. def jzdsort(list0,poly):
  445. m = 0
  446. n = 0
  447. northwest = []
  448. northwest.append(poly.extent.XMin)
  449. northwest.append(poly.extent.YMax)
  450. minDis = (northwest[1] - list0[0][1])*(northwest[1] - list0[0][1]) + (northwest[0] - list0[0][0])*(northwest[0] - list0[0][0])
  451. maxStdangle = cal_stdangle(northwest,list0[0])
  452. while m < len(list0):
  453. Dis = (northwest[1] - list0[m][1])*(northwest[1] - list0[m][1]) + (northwest[0] - list0[m][0])*(northwest[0] - list0[m][0])
  454. if Dis < minDis:
  455. minDis = Dis
  456. maxStdangle = cal_stdangle(northwest,list0[m])
  457. n = m
  458. if abs(Dis-minDis) < 0.000001:
  459. stdangle = cal_stdangle(northwest,list0[m])
  460. if stdangle > maxStdangle:
  461. maxStdangle = stdangle
  462. n = m
  463. m = m + 1
  464. i = 0
  465. while i < len(list0):
  466. if n == 0:
  467. vectangleN = cal_stdangle(list0[0], list0[1])
  468. vectangleNl = cal_stdangle(list0[len(list0)-1], list0[0])
  469. if abs(vectangleN-vectangleNl) < 3:
  470. n = len(list0)-1
  471. else:
  472. if n == len(list0)-1:
  473. vectangleN = cal_stdangle(list0[n], list0[0])
  474. else:
  475. vectangleN = cal_stdangle(list0[n], list0[n+1])
  476. vectangleNl = cal_stdangle(list0[n-1], list0[n])
  477. if abs(vectangleN-vectangleNl) < 3:
  478. n = n-1
  479. i = i + 1
  480. list4 = []
  481. a = n
  482. num = 0
  483. while a < len(list0):
  484. slist4 = []
  485. num = num + 1
  486. slist4.append(list0[a][0])
  487. slist4.append(list0[a][1])
  488. slist4.append(num)
  489. list4.append(slist4)
  490. a = a + 1
  491. a = 0
  492. while a < n:
  493. slist4 = []
  494. num = num + 1
  495. slist4.append(list0[a][0])
  496. slist4.append(list0[a][1])
  497. slist4.append(num)
  498. list4.append(slist4)
  499. a = a + 1
  500. return list4
  501. def cal_stdangle(lastp,p):
  502. stdangle=0
  503. dy= p[1]-lastp[1]
  504. dx= p[0]-lastp[0]
  505. if dx==0 and dy>0:
  506. stdangle = 0
  507. if dx==0 and dy<0:
  508. stdangle = 180
  509. if dy==0 and dx>0:
  510. stdangle = 90
  511. if dy==0 and dx<0:
  512. stdangle = 270
  513. if dx>0 and dy>0:
  514. stdangle = math.atan(dx/dy)*180/math.pi
  515. elif dx<0 and dy>0:
  516. stdangle = 360 + math.atan(dx/dy)*180/math.pi
  517. elif dx<0 and dy<0:
  518. stdangle = 180 + math.atan(dx/dy)*180/math.pi
  519. elif dx>0 and dy<0:
  520. stdangle = 180 + math.atan(dx/dy)*180/math.pi
  521. return stdangle
  522. def calcangle(lastp,p):
  523. angle=0
  524. stdangle = cal_stdangle(lastp,p)
  525. if (stdangle < 45 and stdangle >= 0) or (stdangle <= 360 and stdangle >= 315):
  526. angle = 270
  527. if (stdangle < 135 and stdangle >= 45):
  528. angle = 0
  529. if (stdangle < 225 and stdangle >= 135):
  530. angle = 90
  531. if (stdangle < 315 and stdangle >= 225):
  532. angle = 180
  533. return angle
  534. if __name__ == '__main__':
  535. try:
  536. # 输入shp,输出界址点文件夹
  537. JZpointmake(arcpy.GetParameterAsText(0),arcpy.GetParameterAsText(1))
  538. except arcpy.ExecuteError:
  539. arcpy.AddMessage(arcpy.GetMessages())