工具箱相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exportZDT_Batch.py 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. # -*- coding: utf-8 -*-
  2. from cmath import e
  3. import os
  4. from re import L
  5. import arcpy
  6. import pythonaddins
  7. import math
  8. from multiprocessing import Process
  9. import random
  10. import fnmatch
  11. import time
  12. def TFtoTB(shape, elem):
  13. list1 = []
  14. # 相交,包含,被包含
  15. arcpy.SelectLayerByLocation_management('TF', "INTERSECT", shape)
  16. ma1 = int(arcpy.GetCount_management('TF').getOutput(0))
  17. if ma1 != 0:
  18. with arcpy.da.SearchCursor('TF', ['TFH1']) as cursorB:
  19. for curB in cursorB:
  20. tfh1 = str(curB[0])
  21. if list1.__contains__(tfh1):
  22. pass
  23. else:
  24. list1.append(tfh1)
  25. arcpy.SelectLayerByLocation_management('TF', "WITHIN", shape)
  26. ma2 = int(arcpy.GetCount_management('TF').getOutput(0))
  27. if ma2 != 0:
  28. with arcpy.da.SearchCursor('TF', ['TFH1']) as cursorC:
  29. for curC in cursorC:
  30. tfh2 = str(curC[0])
  31. if list1.__contains__(tfh2):
  32. pass
  33. else:
  34. list1.append(tfh2)
  35. arcpy.SelectLayerByLocation_management('TF', "COMPLETELY_CONTAINS", shape)
  36. ma3 = int(arcpy.GetCount_management('TF').getOutput(0))
  37. if ma3 != 0:
  38. with arcpy.da.SearchCursor('TF', ['TFH1']) as cursorD:
  39. for curD in cursorD:
  40. tfh3 = str(curD[0])
  41. if list1.__contains__(tfh3):
  42. pass
  43. else:
  44. list1.append(tfh3)
  45. if list1 == []:
  46. pass
  47. else:
  48. ii = 0
  49. while ii < len(list1):
  50. # tfh.append(list1[ii])
  51. elem.text = elem.text + list1[ii]
  52. if ii == (len(list1) - 1):
  53. pass
  54. elif ( ii + 1 ) % 2 == 0:
  55. elem.text = elem.text + '\n'+ ' '
  56. else:
  57. elem.text = elem.text + ','
  58. ii = ii + 1
  59. def JZGenerate(outfileshp, zdbh):
  60. # def JZGenerate(outfileshp):
  61. shpstr = 'JZD.shp'
  62. shpstr1 = 'JZX.shp'
  63. outpath = outfileshp + '\\' + shpstr
  64. outpath1 = outfileshp + '\\' + shpstr1
  65. try:
  66. arcpy.CreateFeatureclass_management(outfileshp, shpstr, 'POINT', spatial_reference=4523) #"4507"
  67. except:
  68. arcpy.Delete_management(outpath)
  69. arcpy.CreateFeatureclass_management(outfileshp, shpstr, 'POINT', spatial_reference=4523)
  70. try:
  71. arcpy.CreateFeatureclass_management(outfileshp, shpstr1, 'POLYLINE', spatial_reference=4523)
  72. except:
  73. arcpy.Delete_management(outpath1)
  74. arcpy.CreateFeatureclass_management(outfileshp, shpstr1, 'POLYLINE', spatial_reference=4523)
  75. sr = arcpy.Describe(LayerObject.currentLayer).spatialReference
  76. # 加载
  77. lyrjzd = arcpy.mapping.Layer(outpath)
  78. arcpy.mapping.AddLayer(df, lyrjzd)
  79. lyrjzx = arcpy.mapping.Layer(outpath1)
  80. arcpy.mapping.AddLayer(df, lyrjzx)
  81. # 添加一群字段
  82. arcpy.AddField_management('JZD', '标识码', 'LONG')
  83. arcpy.AddField_management('JZD', 'YSDM', 'TEXT')
  84. arcpy.AddField_management('JZD', 'JZDH', 'TEXT')
  85. arcpy.AddField_management('JZD', 'JBLX', 'TEXT')
  86. arcpy.AddField_management('JZD', 'JZDLX', 'TEXT')
  87. arcpy.AddField_management('JZD', 'ZDBH', 'TEXT')
  88. arcpy.AddField_management('JZD', '顺序号', 'LONG')
  89. arcpy.AddField_management('JZD', 'flag', 'LONG')
  90. arcpy.AddField_management('JZD', 'ANGLE', 'LONG')
  91. #float按理说应该符合的
  92. bsm = 0
  93. field1 = ['SHAPE@', '标识码', 'YSDM', 'JZDH', 'JBLX',
  94. 'JZDLX', 'ZDBH', '顺序号','flag','ANGLE']
  95. # endregion
  96. with arcpy.da.InsertCursor('JZD', field1) as cursorZ:
  97. with arcpy.da.SearchCursor(LayerObject.currentLayer, ['SHAPE@', '宗地编号']) as cursorA:
  98. for curA in cursorA:
  99. # region [读折点]
  100. poly1 = curA[0]
  101. zdbh = curA[1]
  102. if zdbh == None or zdbh == '' or zdbh == ' ':
  103. pass
  104. else:
  105. # 获取折点
  106. points = poly1.getPart()
  107. pt = points[0]
  108. mm = 0
  109. linelist = []
  110. indexp = 0
  111. #存的都是点
  112. while mm < pt.count:
  113. jzd = pt[mm]
  114. try:
  115. xx = round(jzd.X,4)
  116. yy = round(jzd.Y,4)
  117. if mm == 0 or mm == indexp:
  118. xx0 = xx
  119. yy0 = yy
  120. slist = []
  121. slist.append(pt[mm])
  122. else:
  123. numdis0 = xx - xx0
  124. numdis1 = yy - yy0
  125. numdis = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1))
  126. if xx == xx0 and yy == yy0:
  127. #一个的结束
  128. indexp = mm + 1
  129. linelist.append(slist)
  130. if numdis < 0.1:
  131. pass
  132. else:
  133. slist.append(pt[mm])
  134. mm = mm + 1
  135. except:
  136. if mm == indexp:
  137. indexp = mm + 1
  138. mm = mm + 1
  139. llen = len(linelist)
  140. #判断下如果多环,排个序
  141. linelist1 = pdlen(linelist)
  142. linelist = linelist1
  143. # endregion
  144. #甭管有没有环,都是第一个来处理
  145. ln1 = linelist[0]
  146. list1 = []
  147. nn1 = 0
  148. while nn1 < len(ln1):
  149. if nn1 == len(ln1) - 1:
  150. fpx = round(ln1[nn1].X,4)
  151. fpy = round(ln1[nn1].Y,4)
  152. lpx = round(ln1[0].X,4)
  153. lpy = round(ln1[0].Y,4)
  154. fpoint = arcpy.Point(fpx, fpy)
  155. lpoint = arcpy.Point(lpx, lpy)
  156. else:
  157. fpx = round(ln1[nn1].X,4)
  158. fpy = round(ln1[nn1].Y,4)
  159. lpx = round(ln1[nn1 + 1].X,4)
  160. lpy = round(ln1[nn1 + 1].Y,4)
  161. fpoint = arcpy.Point(fpx, fpy)
  162. lpoint = arcpy.Point(lpx, lpy)
  163. new_array = arcpy.Array()
  164. new_array.append(fpoint)
  165. new_array.append(lpoint)
  166. ln2 = arcpy.Polyline(new_array,sr)
  167. #先把起始点加进去
  168. bl1 = addpoint(list1,fpoint)
  169. if bl1 == 1:
  170. list1.append(fpoint)
  171. # list1.append(lpoint)
  172. #插入点
  173. arcpy.SelectLayerByLocation_management(LayerObject.currentLayer,'INTERSECT',ln2)
  174. count0 = int(arcpy.GetCount_management(LayerObject.currentLayer).getOutput(0))
  175. if count0 < 2:
  176. bl4 = addpoint(list1,lpoint)
  177. if bl4 == 1:
  178. list1.append(lpoint)
  179. nn1 = nn1 + 1
  180. else:
  181. #对于同一个线段来说,把所有点的位置准确点
  182. listp1 = []
  183. with arcpy.da.SearchCursor(LayerObject.currentLayer, ['SHAPE@', '宗地编号']) as cursorB:
  184. for curB in cursorB:
  185. zdbh1 = curB[1]
  186. #排除自己
  187. if zdbh1 == zdbh:
  188. pass
  189. else:
  190. try:
  191. shpB = curB[0]
  192. lineB = shpB.boundary ()
  193. rln = lineB.intersect (ln2, 2)
  194. fpoint1 = rln.firstPoint
  195. lpoint1 = rln.lastPoint
  196. fx = round(fpoint1.X,4)
  197. fy = round(fpoint1.Y,4)
  198. lx = round(lpoint1.X,4)
  199. ly = round(lpoint1.Y,4)
  200. #判断这两个点是否都在里面
  201. fp = arcpy.Point(fx, fy)
  202. lp = arcpy.Point(lx, ly)
  203. list2 = []
  204. bl1 = addpoint(list1,fp)
  205. if bl1 == 1:
  206. #判断与起点的距离
  207. dis1 = math.sqrt(((fpx - fx)*(fpx - fx)) + ((fpy-fy)*(fpy-fy)))
  208. slist2 = []
  209. slist2.append(dis1)
  210. slist2.append(fp)
  211. list2.append(slist2)
  212. bl1 = addpoint(list1,lp)
  213. if bl1 == 1:
  214. dis1 = math.sqrt(((fpx - lx)*(fpx - lx)) + ((fpy-ly)*(fpy-ly)))
  215. slist2 = []
  216. slist2.append(dis1)
  217. slist2.append(lp)
  218. list2.append(slist2)
  219. if len(list2) == 1:
  220. listp1.append(list2[0][1])
  221. elif len(list2) == 2:
  222. #对2排序
  223. if list2[0][0] < list2[1][0]:
  224. listp1.append(list2[0][1])
  225. listp1.append(list2[1][1])
  226. else:
  227. listp1.append(list2[1][1])
  228. listp1.append(list2[0][1])
  229. except:
  230. pass
  231. #得到所有的点,进行一个与起点的排序
  232. nn2 = 0
  233. listp2 =[]
  234. while nn2 < len(listp1):
  235. num0 = fpx - listp1[nn2].X
  236. num1 = fpy - listp1[nn2].Y
  237. dis2 = math.sqrt((num0*num0)+(num1*num1))
  238. slistp2 = []
  239. slistp2.append(dis2)
  240. slistp2.append(listp1[nn2])
  241. listp2.append(slistp2)
  242. nn2 = nn2 + 1
  243. listp2.sort()
  244. nn3 = 0
  245. bl3 = 0
  246. while nn3 < len(listp2):
  247. #这里要看下要不要除重
  248. bl3 = addpoint(list1,listp2[nn3][1])
  249. if bl3 == 1:
  250. list1.append(listp2[nn3][1])
  251. nn3 = nn3 + 1
  252. bl4 = addpoint(list1,lpoint)
  253. if bl4 == 1:
  254. list1.append(lpoint)
  255. nn1 = nn1 + 1
  256. with arcpy.da.InsertCursor('JZX', ['SHAPE@']) as cursorY:
  257. if len(linelist) == 1:
  258. listF = linezd(list1,poly1)
  259. #直接写
  260. ii = 0
  261. zdh = 0
  262. while ii < len(listF):
  263. if ii == 0:
  264. zdh = zdh + 1
  265. bsm = bsm + 1
  266. # shape
  267. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  268. # 要素代码
  269. ysdm = '6002010300'
  270. # 界址点号
  271. jzdh = 'J' + str(zdh)
  272. # 宗地代码
  273. zddm = curA[1]
  274. ag = calcangle(listF[ii-1],listF[ii])
  275. rlist = []
  276. rlist.append(point1)
  277. rlist.append(bsm)
  278. rlist.append(ysdm)
  279. rlist.append(jzdh)
  280. rlist.append('')
  281. rlist.append('')
  282. rlist.append(zddm)
  283. rlist.append(zdh)
  284. rlist.append(1)
  285. rlist.append(ag)
  286. cursorZ.insertRow(rlist)
  287. point = arcpy.Point(listF[ii][0], listF[ii][1])
  288. if ii == len(listF) - 1:
  289. point0 = arcpy.Point(listF[0][0], listF[0][1])
  290. else:
  291. point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1])
  292. array=arcpy.Array()
  293. array.add(point)
  294. array.add(point0)
  295. pl=arcpy.Polyline(array)
  296. cursorY.insertRow([pl])
  297. else:
  298. numdis0 = listF[ii][0] - listF[ii - 1][0]
  299. numdis1 = listF[ii][1] - listF[ii - 1][1]
  300. numdis2 = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1))
  301. if numdis2 < 0.1:
  302. pass
  303. else:
  304. zdh = zdh + 1
  305. bsm = bsm + 1
  306. # shape
  307. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  308. # 要素代码
  309. ysdm = '6002010300'
  310. # 界址点号
  311. jzdh = 'J' + str(zdh)
  312. # 宗地代码
  313. zddm = curA[1]
  314. ag = calcangle(listF[ii-1],listF[ii])
  315. rlist = []
  316. rlist.append(point1)
  317. rlist.append(bsm)
  318. rlist.append(ysdm)
  319. rlist.append(jzdh)
  320. rlist.append('')
  321. rlist.append('')
  322. rlist.append(zddm)
  323. rlist.append(zdh)
  324. rlist.append(1)
  325. rlist.append(ag)
  326. cursorZ.insertRow(rlist)
  327. point = arcpy.Point(listF[ii][0], listF[ii][1])
  328. if ii == len(listF) - 1:
  329. point0 = arcpy.Point(listF[0][0], listF[0][1])
  330. else:
  331. point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1])
  332. array=arcpy.Array()
  333. array.add(point)
  334. array.add(point0)
  335. pl=arcpy.Polyline(array)
  336. cursorY.insertRow([pl])
  337. ii = ii + 1
  338. else:
  339. mm0 = 0
  340. zdh = 0
  341. while mm0 < len(linelist):
  342. if mm0 == 0:
  343. listF = linezd(list1,poly1)
  344. ii = 0
  345. while ii < len(listF):
  346. if ii == 0:
  347. zdh = zdh + 1
  348. bsm = bsm + 1
  349. # shape
  350. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  351. # 要素代码
  352. ysdm = '6002010300'
  353. # 界址点号
  354. jzdh = 'J' + str(zdh)
  355. # 宗地代码
  356. zddm = curA[1]
  357. ag = calcangle(listF[ii-1],listF[ii])
  358. rlist = []
  359. rlist.append(point1)
  360. rlist.append(bsm)
  361. rlist.append(ysdm)
  362. rlist.append(jzdh)
  363. rlist.append('')
  364. rlist.append('')
  365. rlist.append(zddm)
  366. rlist.append(zdh)
  367. rlist.append(mm0+1)
  368. rlist.append(ag)
  369. cursorZ.insertRow(rlist)
  370. point = arcpy.Point(listF[ii][0], listF[ii][1])
  371. if ii == len(listF) - 1:
  372. point0 = arcpy.Point(listF[0][0], listF[0][1])
  373. else:
  374. point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1])
  375. array=arcpy.Array()
  376. array.add(point)
  377. array.add(point0)
  378. pl=arcpy.Polyline(array)
  379. cursorY.insertRow([pl])
  380. else:
  381. numdis0 = listF[ii][0] - listF[ii - 1][0]
  382. numdis1 = listF[ii][1] - listF[ii - 1][1]
  383. numdis2 = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1))
  384. if numdis2 < 0.1:
  385. pass
  386. else:
  387. zdh = zdh + 1
  388. bsm = bsm + 1
  389. # shape
  390. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  391. # 要素代码
  392. ysdm = '6002010300'
  393. # 界址点号
  394. jzdh = 'J' + str(zdh)
  395. # 宗地代码
  396. zddm = curA[1]
  397. ag = calcangle(listF[ii-1],listF[ii])
  398. rlist = []
  399. rlist.append(point1)
  400. rlist.append(bsm)
  401. rlist.append(ysdm)
  402. rlist.append(jzdh)
  403. rlist.append('')
  404. rlist.append('')
  405. rlist.append(zddm)
  406. rlist.append(zdh)
  407. rlist.append(mm0+1)
  408. rlist.append(ag)
  409. cursorZ.insertRow(rlist)
  410. point = arcpy.Point(listF[ii][0], listF[ii][1])
  411. if ii == len(listF) - 1:
  412. point0 = arcpy.Point(listF[0][0], listF[0][1])
  413. else:
  414. point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1])
  415. array=arcpy.Array()
  416. array.add(point)
  417. array.add(point0)
  418. pl=arcpy.Polyline(array)
  419. cursorY.insertRow([pl])
  420. ii = ii + 1
  421. else:
  422. listF = linezd(linelist[mm0],poly1)
  423. ii = len(listF) - 1
  424. while ii >= 0:
  425. if ii == len(listF) - 1:
  426. zdh = zdh + 1
  427. bsm = bsm + 1
  428. # shape
  429. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  430. # 要素代码
  431. ysdm = '6002010300'
  432. # 界址点号
  433. jzdh = 'J' + str(zdh)
  434. # 宗地代码
  435. zddm = curA[1]
  436. ag = calcangle(listF[ii-1],listF[ii])
  437. rlist = []
  438. rlist.append(point1)
  439. rlist.append(bsm)
  440. rlist.append(ysdm)
  441. rlist.append(jzdh)
  442. rlist.append('')
  443. rlist.append('')
  444. rlist.append(zddm)
  445. rlist.append(zdh)
  446. rlist.append(mm0+1)
  447. rlist.append(ag)
  448. cursorZ.insertRow(rlist)
  449. point = arcpy.Point(listF[ii][0], listF[ii][1])
  450. if ii == len(listF) - 1:
  451. point0 = arcpy.Point(listF[0][0], listF[0][1])
  452. else:
  453. point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1])
  454. array=arcpy.Array()
  455. array.add(point)
  456. array.add(point0)
  457. pl=arcpy.Polyline(array)
  458. cursorY.insertRow([pl])
  459. else:
  460. numdis0 = listF[ii][0] - listF[ii - 1][0]
  461. numdis1 = listF[ii][1] - listF[ii - 1][1]
  462. numdis2 = math.sqrt((numdis0 * numdis0)+(numdis1*numdis1))
  463. if numdis2 < 0.1:
  464. pass
  465. else:
  466. zdh = zdh + 1
  467. bsm = bsm + 1
  468. # shape
  469. point1 = arcpy.Point(listF[ii][0], listF[ii][1])
  470. # 要素代码
  471. ysdm = '6002010300'
  472. # 界址点号
  473. jzdh = 'J' + str(zdh)
  474. # 宗地代码
  475. zddm = curA[1]
  476. ag = calcangle(listF[ii-1],listF[ii])
  477. rlist = []
  478. rlist.append(point1)
  479. rlist.append(bsm)
  480. rlist.append(ysdm)
  481. rlist.append(jzdh)
  482. rlist.append('')
  483. rlist.append('')
  484. rlist.append(zddm)
  485. rlist.append(zdh)
  486. rlist.append(mm0+1)
  487. rlist.append(ag)
  488. cursorZ.insertRow(rlist)
  489. point = arcpy.Point(listF[ii][0], listF[ii][1])
  490. if ii == len(listF) - 1:
  491. point0 = arcpy.Point(listF[0][0], listF[0][1])
  492. else:
  493. point0 = arcpy.Point(listF[ii+1][0], listF[ii+1][1])
  494. array=arcpy.Array()
  495. array.add(point)
  496. array.add(point0)
  497. pl=arcpy.Polyline(array)
  498. cursorY.insertRow([pl])
  499. ii = ii - 1
  500. mm0 = mm0 + 1
  501. def pdlen(list1):
  502. ii = 0
  503. list2 = []
  504. while ii < len(list1):
  505. pts = list1[ii]
  506. length1 = 0
  507. mm = 0
  508. while mm < len(pts):
  509. if mm == len(pts) - 1:
  510. pt1 = pts[mm]
  511. pt2 = pts[0]
  512. dis1 = math.sqrt(((pt1.X-pt2.X)*(pt1.X-pt2.X))+((pt1.Y-pt2.Y)*(pt1.Y-pt2.Y)))
  513. length1 = length1 + dis1
  514. mm = mm + 1
  515. else:
  516. pt1 = pts[mm]
  517. pt2 = pts[mm + 1]
  518. dis1 = math.sqrt(((pt1.X-pt2.X)*(pt1.X-pt2.X))+((pt1.Y-pt2.Y)*(pt1.Y-pt2.Y)))
  519. length1 = length1 + dis1
  520. mm = mm + 1
  521. slist2 = []
  522. slist2.append(length1)
  523. slist2.append(pts)
  524. list2.append(slist2)
  525. ii = ii + 1
  526. list2.sort(reverse=True)
  527. nn = 0
  528. list3 = []
  529. while nn < len(list2):
  530. list3.append(list2[nn][1])
  531. nn = nn + 1
  532. return list3
  533. def calc_angle(lastp,p):
  534. angle = 0
  535. if p[0]-lastp[0]>0 and p[1]-lastp[1]>0:#↗
  536. angle = 0
  537. if p[0]-lastp[0]>0 and p[1]-lastp[1]<0:#↘
  538. angle = 90
  539. if p[0]-lastp[0]<0 and p[1]-lastp[1]<0:#↙
  540. angle = 180
  541. if p[0]-lastp[0]<0 and p[1]-lastp[1]>0:#↖
  542. angle = 270
  543. return angle
  544. def cal_stdangle(lastp,p):
  545. stdangle=0
  546. dy= p[1]-lastp[1]
  547. dx= p[0]-lastp[0]
  548. if dx==0 and dy>0:
  549. stdangle = 0
  550. if dx==0 and dy<0:
  551. stdangle = 180
  552. if dy==0 and dx>0:
  553. stdangle = 90
  554. if dy==0 and dx<0:
  555. stdangle = 270
  556. if dx>0 and dy>0:
  557. stdangle = math.atan(dx/dy)*180/math.pi
  558. elif dx<0 and dy>0:
  559. stdangle = 360 + math.atan(dx/dy)*180/math.pi
  560. elif dx<0 and dy<0:
  561. stdangle = 180 + math.atan(dx/dy)*180/math.pi
  562. elif dx>0 and dy<0:
  563. stdangle = 180 + math.atan(dx/dy)*180/math.pi
  564. return stdangle
  565. def calcangle(lastp,p):
  566. angle=0
  567. stdangle = cal_stdangle(lastp,p)
  568. if (stdangle < 45 and stdangle >= 0) or (stdangle <= 360 and stdangle >= 315):
  569. angle = 270
  570. if (stdangle < 135 and stdangle >= 45):
  571. angle = 0
  572. if (stdangle < 225 and stdangle >= 135):
  573. angle = 90
  574. if (stdangle < 315 and stdangle >= 225):
  575. angle = 180
  576. return angle
  577. def addpoint(list1,pt):
  578. ptx = round(pt.X,4)
  579. pty = round(pt.Y,4)
  580. ii = 0
  581. bl = 1
  582. while ii < len(list1):
  583. if ptx == round(list1[ii].X,4) and pty == round(list1[ii].Y,4):
  584. bl = -1
  585. ii = ii + 1
  586. return bl
  587. def linezd(line1,poly):
  588. # 获取折点
  589. mm = 0
  590. pp = 0
  591. list1 = []
  592. while mm < len(line1):
  593. plist = []
  594. jzd = line1[mm]
  595. try:
  596. xx = jzd.X
  597. yy = jzd.Y
  598. plist.append(xx)
  599. plist.append(yy)
  600. # 初始顺序
  601. plist.append(pp)
  602. list1.append(plist)
  603. pp = pp + 1
  604. mm = mm + 1
  605. except:
  606. mm = mm + 1
  607. # 对折点排序(算法)
  608. if len(list1) < 3:
  609. listF = []
  610. else:
  611. # listF = zdsort(list1, zx)
  612. listF = jzdsort(list1,poly)
  613. return listF
  614. def jzdsort(list0,poly):
  615. m = 0
  616. n = 0
  617. northwest = []
  618. northwest.append(poly.extent.XMin)
  619. northwest.append(poly.extent.YMax)
  620. minDis = (northwest[1] - list0[0][1])*(northwest[1] - list0[0][1]) + (northwest[0] - list0[0][0])*(northwest[0] - list0[0][0])
  621. maxStdangle = cal_stdangle(northwest,list0[0])
  622. while m < len(list0):
  623. Dis = (northwest[1] - list0[m][1])*(northwest[1] - list0[m][1]) + (northwest[0] - list0[m][0])*(northwest[0] - list0[m][0])
  624. if Dis < minDis:
  625. minDis = Dis
  626. maxStdangle = cal_stdangle(northwest,list0[m])
  627. n = m
  628. if abs(Dis-minDis) < 0.000001:
  629. stdangle = cal_stdangle(northwest,list0[m])
  630. if stdangle > maxStdangle:
  631. maxStdangle = stdangle
  632. n = m
  633. m = m + 1
  634. i = 0
  635. while i < len(list0):
  636. if n == 0:
  637. vectangleN = cal_stdangle(list0[0], list0[1])
  638. vectangleNl = cal_stdangle(list0[len(list0)-1], list0[0])
  639. if abs(vectangleN-vectangleNl) < 3:
  640. n = len(list0)-1
  641. else:
  642. if n == len(list0)-1:
  643. vectangleN = cal_stdangle(list0[n], list0[0])
  644. else:
  645. vectangleN = cal_stdangle(list0[n], list0[n+1])
  646. vectangleNl = cal_stdangle(list0[n-1], list0[n])
  647. if abs(vectangleN-vectangleNl) < 3:
  648. n = n-1
  649. i = i + 1
  650. list4 = []
  651. a = n
  652. num = 0
  653. while a < len(list0):
  654. slist4 = []
  655. num = num + 1
  656. slist4.append(list0[a][0])
  657. slist4.append(list0[a][1])
  658. slist4.append(num)
  659. list4.append(slist4)
  660. a = a + 1
  661. a = 0
  662. while a < n:
  663. slist4 = []
  664. num = num + 1
  665. slist4.append(list0[a][0])
  666. slist4.append(list0[a][1])
  667. slist4.append(num)
  668. list4.append(slist4)
  669. a = a + 1
  670. return list4
  671. def mkdir(path):
  672. folder = os.path.exists(path)
  673. if not folder: #判断是否存在文件夹如果不存在则创建为文件夹
  674. os.makedirs(path) #makedirs 创建文件时如果路径不存在会创建这个路径
  675. def getScale(scale):
  676. yushu = float(str(scale).split(".", 1)[0]) % 50
  677. lastTWO = 0
  678. if yushu < 25:
  679. lastTWO = float(str(scale).split(".", 1)[0]) // 50 * 50
  680. if yushu < 75 and yushu >= 25:
  681. lastTWO = float(str(scale).split(".", 1)[0]) // 50 * 50 + 50
  682. if yushu < 100 and yushu >= 75:
  683. lastTWO = float(str(scale).split(".", 1)[0]) // 50 * 50 + 100
  684. scale = "1:" + str(lastTWO).split(".",1)[0]
  685. return scale
  686. class LayerObject:
  687. currentLayer = ""
  688. mxd = arcpy.mapping.MapDocument('current')
  689. df = arcpy.mapping.ListDataFrames(mxd)[0]
  690. LayerObject.currentLayer = arcpy.GetParameterAsText(0)
  691. #设置保存路径
  692. savePath1 = arcpy.GetParameterAsText(1)
  693. #当前数据源
  694. arcpy.env.workspace = arcpy.GetParameterAsText(2)
  695. #宗地添加面积字段并计算
  696. arcpy.AddField_management(LayerObject.currentLayer, "Area", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
  697. arcpy.CalculateField_management(LayerObject.currentLayer, "Area", "round(!shape.area!,2)", "PYTHON_9.3")
  698. #面转线
  699. line = arcpy.PolygonToLine_management("Copy", savePath1 +"\line.shp")
  700. #遍历宗地
  701. rows = arcpy.SearchCursor(LayerObject.currentLayer)
  702. for row in rows:
  703. for lyr in arcpy.mapping.ListLayers(mxd, "", df):
  704. if lyr.name == "dlclip":
  705. arcpy.mapping.RemoveLayer(df, lyr)
  706. if lyr.name == "JZD":
  707. arcpy.mapping.RemoveLayer(df, lyr)
  708. if lyr.name == "JZX":
  709. arcpy.mapping.RemoveLayer(df, lyr)
  710. if lyr.name == "lineclip":
  711. arcpy.mapping.RemoveLayer(df, lyr)
  712. filename = row.getValue(u"宗地编号")
  713. savePath = os.path.join(savePath1, filename)
  714. mkdir(savePath)
  715. arcpy.SelectLayerByAttribute_management(LayerObject.currentLayer, "CLEAR_SELECTION")
  716. arcpy.SelectLayerByAttribute_management("Copy", "CLEAR_SELECTION")
  717. OBJECTID_1 = row.getValue(u"OBJECTID_1")
  718. query = "OBJECTID_1={0}".format(OBJECTID_1)
  719. #
  720. arcpy.SelectLayerByAttribute_management(LayerObject.currentLayer, "NEW_SELECTION", query)
  721. #
  722. df.zoomToSelectedFeatures()
  723. df.scale *= 1.3
  724. shape = row.shape
  725. #编辑文本元素
  726. qlr = row.getValue(u"权利人")
  727. title = filename + qlr + u"宗地图"
  728. tfh = " "
  729. zdmj = "{}".format(row.getValue(u"Area"))
  730. # east = row.getValue(u"四至东")
  731. # east =east.replace(")","/0301)")
  732. # south = row.getValue(u"四至南")
  733. # south =south.replace(")","/0301)")
  734. # west = row.getValue(u"四至西")
  735. # west =west.replace(")","/0301)")
  736. # north = row.getValue(u"四至北")
  737. # north =north.replace(")","/0301)")
  738. time_tuple = time.localtime(time.time())
  739. rq = "{}年{}月{}日".format(time_tuple[0],time_tuple[1],time_tuple[2])
  740. ny = "{}年{}月".format(time_tuple[0],time_tuple[1])
  741. elemlist = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")
  742. for elem in elemlist:
  743. if elem.name == "zddm":
  744. elem.text = filename
  745. if elem.name == "qlr":
  746. elem.text = qlr
  747. if elem.name == "tfh":
  748. elem.text = tfh
  749. #预先添加标准TF图层,计算得到图幅号
  750. TFtoTB(row.shape, elem)
  751. if elem.name == "zdmj":
  752. elem.text = zdmj
  753. # if elem.name == "east":
  754. # elem.text = east
  755. # if elem.name == "south":
  756. # elem.text = south
  757. # if elem.name == "west":
  758. # elem.text = west
  759. # if elem.name == "north":
  760. # elem.text = north
  761. if elem.name == "scale":
  762. elem.text = getScale(df.scale)
  763. if elem.name == "ztz":
  764. elem.text = random.choice(['王荣华','余思翰','阮梦妍','杨玉婧','赵芩','屈亚星'])
  765. if elem.name == "shz":
  766. elem.text = random.choice(['何杰','张学仪'])
  767. if elem.name == "ztrq":
  768. elem.text = rq
  769. if elem.name == "shrq":
  770. elem.text = rq
  771. if elem.name == "jzdtjf":
  772. elem.text = ny + "图解法测绘界址点"
  773. if elem.name == "zlz":
  774. title = filename + qlr + u"宗地草图"
  775. elem.text = random.choice(['王荣华','余思翰','阮梦妍','杨玉婧','赵芩','屈亚星'])
  776. if elem.name == "jcz":
  777. elem.text = random.choice(['何杰','张学仪'])
  778. if elem.name == "zlrq":
  779. elem.text = rq
  780. if elem.name == "jcrq":
  781. elem.text = rq
  782. arcpy.SelectLayerByAttribute_management("TF", "CLEAR_SELECTION")
  783. #生成当前宗地界址点线
  784. JZGenerate(savePath, filename)
  785. arcpy.SelectLayerByAttribute_management(LayerObject.currentLayer, "NEW_SELECTION", query)
  786. #界址线添加长度字段
  787. arcpy.AddField_management("JZX", "LENGTH", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
  788. arcpy.CalculateField_management("JZX", "LENGTH", "round(!shape.length!,2)", "PYTHON_9.3")
  789. #建立缓冲区
  790. if df.scale > 1000:
  791. shapebuffer = arcpy.Polygon.buffer(shape, 10)
  792. elif df.scale > 900:
  793. shapebuffer = arcpy.Polygon.buffer(shape, 9)
  794. elif df.scale > 800:
  795. shapebuffer = arcpy.Polygon.buffer(shape, 8)
  796. elif df.scale > 700:
  797. shapebuffer = arcpy.Polygon.buffer(shape, 7)
  798. elif df.scale > 600:
  799. shapebuffer = arcpy.Polygon.buffer(shape, 6)
  800. elif df.scale > 500:
  801. shapebuffer = arcpy.Polygon.buffer(shape, 5)
  802. elif df.scale > 400:
  803. shapebuffer = arcpy.Polygon.buffer(shape, 4)
  804. elif df.scale > 300:
  805. shapebuffer = arcpy.Polygon.buffer(shape, 3)
  806. elif df.scale > 200:
  807. shapebuffer = arcpy.Polygon.buffer(shape, 2)
  808. elif df.scale > 100:
  809. shapebuffer = arcpy.Polygon.buffer(shape, 1)
  810. else:
  811. shapebuffer = arcpy.Polygon.buffer(shape, 0.5)
  812. #裁剪线图层
  813. arcpy.Clip_analysis(line, shapebuffer, savePath + "\lineclip.shp")
  814. lyrline = arcpy.mapping.Layer(savePath + "\lineclip.shp")
  815. arcpy.mapping.AddLayer(df, lyrline)
  816. arcpy.SelectLayerByLocation_management(lyrline, "BOUNDARY_TOUCHES", shape)
  817. arcpy.SelectLayerByAttribute_management(lyrline, "SWITCH_SELECTION")
  818. arcpy.DeleteFeatures_management(lyrline)
  819. arcpy.SelectLayerByLocation_management("BCDW", "BOUNDARY_TOUCHES", shape)
  820. arcpy.CopyFeatures_management("BCDW", savePath + "\BCDW.shp")
  821. arcpy.SelectLayerByLocation_management("ZJD", "BOUNDARY_TOUCHES", shape)
  822. arcpy.CopyFeatures_management("ZJD", savePath + "\ZJD.shp")
  823. arcpy.SelectLayerByLocation_management("Copy", "SHARE_A_LINE_SEGMENT_WITH", shape)
  824. neighlist = []
  825. m = int(arcpy.GetCount_management("Copy").getOutput(0))
  826. if m != 0:
  827. with arcpy.da.SearchCursor("Copy", ['宗地编号']) as cursor:
  828. for cur in cursor:
  829. f = cur[0]
  830. if f == filename:
  831. pass
  832. else:
  833. if neighlist.__contains__(f):
  834. pass
  835. else:
  836. neighlist.append(f)
  837. txtfile = open(os.path.join(savePath1, "neighbourList.txt"),'a')
  838. txtfile.write(filename + ':' + str(len(neighlist)) + str(tuple(neighlist)).replace("u","").replace("510811100205JE00","") + '\n')
  839. arcpy.Clip_analysis("DLTB", shapebuffer, savePath + "\dlbfclip.shp")
  840. shape1 = arcpy.Polygon.buffer(shape, 0.1)
  841. #
  842. arcpy.Erase_analysis(savePath + "\dlbfclip.shp", shape1, savePath + "\dlclip.shp")
  843. fcs = arcpy.ListFeatureClasses()
  844. for fc in fcs:
  845. if fc.__contains__("dlclip"):
  846. arcpy.Delete_management(os.path.join(arcpy.env.workspace,fc))
  847. arcpy.FeatureClassToGeodatabase_conversion([savePath + '\dlclip.shp'], arcpy.env.workspace)
  848. target_lyr = arcpy.env.workspace + "\dlclip"
  849. #
  850. arcpy.AddRepresentation_cartography(target_lyr,
  851. "DL_Rep",
  852. import_rule_layer = "DLTB")
  853. arcpy.CalculateField_management(target_lyr, "RuleID_1", '!RuleID!', "PYTHON_9.3")
  854. lyrdl = arcpy.mapping.Layer(target_lyr)
  855. refLayer = arcpy.mapping.ListLayers(mxd, "", df)
  856. arcpy.mapping.InsertLayer(df, refLayer[len(refLayer) - 1], lyrdl, "AFTER")
  857. arcpy.SetLayerRepresentation_cartography("dlclip", "DL_Rep")
  858. for lyr in arcpy.mapping.ListLayers(mxd, "", df):
  859. if lyr.name == "Point":
  860. lyr.replaceDataSource(savePath, "SHAPEFILE_WORKSPACE", "JZD")
  861. lyr.showLabels = True
  862. lyr.labelClasses[0].SQLQuery = "ANGLE = 0"
  863. lyr.labelClasses[0].expression = "[JZDH]"
  864. lyr.labelClasses[1].SQLQuery = "ANGLE = 90"
  865. lyr.labelClasses[1].expression = "[JZDH]"
  866. lyr.labelClasses[2].SQLQuery = "ANGLE = 180"
  867. lyr.labelClasses[2].expression = "[JZDH]"
  868. lyr.labelClasses[3].SQLQuery = "ANGLE = 270"
  869. lyr.labelClasses[3].expression = "[JZDH]"
  870. lyr.visible = True
  871. elif lyr.name == "Polyline":
  872. lyr.replaceDataSource(savePath, "SHAPEFILE_WORKSPACE", "JZX")
  873. lyr.showLabels = True
  874. lyr.labelClasses[0].expression = "[LENGTH]"
  875. lyr.visible = True
  876. elif lyr.name == LayerObject.currentLayer:
  877. lyr.showLabels = True
  878. lyr.labelClasses[0].SQLQuery = '"OBJECTID_1" = ' + str(OBJECTID_1)
  879. lyr.visible = True
  880. elif lyr.name == "Copy":
  881. lyr.showLabels = True
  882. sqlquery = str(tuple(neighlist))
  883. if sqlquery.endswith(",)"):
  884. query = sqlquery.replace(",)", ")")
  885. else:
  886. query = sqlquery
  887. lyr.labelClasses[0].SQLQuery = '"宗地编号" IN ' + query.replace("u","")
  888. lyr.visible = True
  889. elif lyr.name == "line":
  890. lyr.replaceDataSource(savePath, "SHAPEFILE_WORKSPACE", "lineclip")
  891. lyr.visible = True
  892. elif lyr.name == "dlclip":
  893. lyr.visible = True
  894. elif lyr.name == "BCDW":
  895. lyr.replaceDataSource(savePath, "SHAPEFILE_WORKSPACE", "BCDW")
  896. lyr.visible = True
  897. elif lyr.name == "ZJD":
  898. lyr.replaceDataSource(savePath, "SHAPEFILE_WORKSPACE", "ZJD")
  899. lyr.visible = True
  900. else:
  901. lyr.visible = False
  902. #saveMxdPath = savePath + "\\" + title + ".mxd"
  903. #mxd.saveACopy(saveMxdPath)
  904. arcpy.mapping.ExportToJPEG( mxd, savePath + "\\" + title + ".jpg", resolution = 300)
  905. arcpy.Delete_management(savePath +"\dlbfclip.shp")
  906. arcpy.Delete_management(savePath +"\dlclip.shp")
  907. #arcpy.Delete_management(savePath +"\lineclip.shp")
  908. #arcpy.Delete_management(savePath +"\JZD.shp")
  909. #arcpy.Delete_management(savePath +"\JZX.shp")
  910. arcpy.AddMessage (filename + ' Finish!')
  911. txtfile.close()
  912. arcpy.Delete_management(savePath1 +"\line.shp")