工具箱相关
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. import arcpy
  2. import xlwt
  3. import os
  4. import random
  5. def dmorder(dmxname,zxx,list1):
  6. #循环一遍list1,找到交点和交点离起点的距离
  7. ii = 0
  8. #创建一个list,存放线形、距离,交点(point)
  9. list2 = []
  10. while ii < len(list1):
  11. xjpg = zxx.intersect (list1[ii], 1)
  12. xjp = xjpg.firstPoint
  13. disxj = zxx.measureOnLine (xjp)
  14. list3 = []
  15. list3.append(list1[ii])
  16. list3.append(disxj)
  17. list3.append(xjp)
  18. list2.append(list3)
  19. ii = ii + 1
  20. #排序(用第二个参数排序,排升序)
  21. list2.sort(key=lambda x: x[1], reverse=False)
  22. #再创建一个list,放线形、线名
  23. list4 = []
  24. mm = 0
  25. while mm < len(list2):
  26. list3 = []
  27. list3.append(list2[mm][0])
  28. num = mm + 1
  29. #不足两位的用0填补
  30. if mm < 9:
  31. lname = dmxname + '#0' + str(num)
  32. else:
  33. lname = dmxname + '#' + str(num)
  34. list3.append(lname)
  35. list3.append(list2[mm][2])
  36. list4.append(list3)
  37. mm = mm + 1
  38. return list4
  39. def diszd(zxx,zds):
  40. ii = 0
  41. #存放折点,距离
  42. list1 = []
  43. while ii < zds.count:
  44. dis1 = zxx.measureOnLine (zds[ii])
  45. list2 = []
  46. list2.append(zds[ii])
  47. list2.append(dis1)
  48. list1.append(list2)
  49. ii = ii + 1
  50. return list1
  51. def jdpd(line,listzd,disxjp):
  52. fp = line.firstPoint
  53. #挨个和交点比对,得到距离刚好大于当前点,且小于下一个点的
  54. ii = 0
  55. #以防出现空的
  56. strResult = ''
  57. while ii < listzd.count:
  58. disf = listzd[ii][1]
  59. disl = listzd[ii + 1][1]
  60. #有可能正好在某个点上,所以要细分情况
  61. if disxjp > disf and disxjp < disl:
  62. #获得A和B两个点,然后就是套用算法
  63. lnfpx = listzd[ii][0].X
  64. lnfpy = listzd[ii][0].Y
  65. lnlpx = listzd[ii + 1][0].X
  66. lnlpy = listzd[ii + 1][0].Y
  67. ax = lnlpx - lnfpx
  68. ay = lnlpy - lnfpy
  69. bx = fp.X - lnfpx
  70. by = fp.Y - lnfpy
  71. judge = ax * by - ay * bx
  72. if judge > 0 :
  73. strResult= "LEFT"
  74. else:
  75. strResult = "RIGHT"
  76. break
  77. elif disxjp == disf and disxjp < disl:
  78. #获得A和B两个点,然后就是套用算法
  79. lnfpx = listzd[ii][0].X
  80. lnfpy = listzd[ii][0].Y
  81. lnlpx = listzd[ii + 1][0].X
  82. lnlpy = listzd[ii + 1][0].Y
  83. ax = lnlpx - lnfpx
  84. ay = lnlpy - lnfpy
  85. bx = fp.X - lnfpx
  86. by = fp.Y - lnfpy
  87. judge = ax * by - ay * bx
  88. if judge > 0 :
  89. strResult= "LEFT"
  90. else:
  91. strResult = "RIGHT"
  92. break
  93. elif disxjp > disf and disxjp == disl:
  94. #获得A和B两个点,然后就是套用算法
  95. lnfpx = listzd[ii][0].X
  96. lnfpy = listzd[ii][0].Y
  97. lnlpx = listzd[ii + 1][0].X
  98. lnlpy = listzd[ii + 1][0].Y
  99. ax = lnlpx - lnfpx
  100. ay = lnlpy - lnfpy
  101. bx = fp.X - lnfpx
  102. by = fp.Y - lnfpy
  103. judge = ax * by - ay * bx
  104. if judge > 0 :
  105. strResult= "LEFT"
  106. else:
  107. strResult = "RIGHT"
  108. break
  109. else:
  110. ii = ii + 1
  111. return strResult
  112. def tbclip(dxtgpath,dxtapath,outpath):
  113. #创建一个shp文件
  114. gcpath = outpath + '\\GCtb.shp'
  115. try:
  116. arcpy.CreateFeatureclass_management(outpath, 'GCtb.shp', 'POLYGON')
  117. except:
  118. arcpy.Delete_management(gcpath)
  119. arcpy.CreateFeatureclass_management(outpath, 'GCtb.shp', 'POLYGON')
  120. #读取
  121. arcpy.MakeFeatureLayer_management(dxtgpath, 'dxtg')
  122. arcpy.MakeFeatureLayer_management(dxtapath, 'dxta')
  123. arcpy.MakeFeatureLayer_management(gcpath, 'gc')
  124. #加图斑类型字段(英文)
  125. arcpy.AddField_management('gc','LX','TEXT')
  126. #先存图形,再更新图形,再赋值
  127. with arcpy.da.InsertCursor('gc', ['SHAPE@']) as cursorH:
  128. with arcpy.da.SearchCursor('dxtg',['SHAPE@','Layer']) as cursorI:
  129. for curI in cursorI:
  130. if curI[1] == '9611':
  131. list1 = []
  132. list1.append(curI[0])
  133. cursorH.insertRow(list1)
  134. with arcpy.da.UpdateCursor('gc',['SHAPE@','FID','LX']) as cursorJ:
  135. for curJ in cursorJ:
  136. pg4 = curJ[0]
  137. id1 = curJ[1]
  138. #判断是否有没有相交的
  139. arcpy.SelectLayerByLocation_management('gc',"INTERSECT",pg4)
  140. count1 = int(arcpy.GetCount_management('gc')[0])
  141. if count1 != 0:
  142. #其中小的裁大的
  143. with arcpy.da.SearchCursor('gc',['SHAPE@','FID']) as cursorK:
  144. for curK in cursorK:
  145. pg5 = curK[0]
  146. id2 = curK[1]
  147. if id1 == id2:
  148. pass
  149. else:
  150. if pg4.area >= pg5.area:
  151. #再看下是否相交
  152. pg7 = pg4.intersect (pg5, 4)
  153. if pg7.area != 0:
  154. #直接裁剪,再赋值
  155. #如果差异没办法用的话,换成交集取反
  156. pg6 = pg4.difference (pg5)
  157. if pg6.area == 0:
  158. pg8 = pg4.symmetricDifference (pg7)
  159. if pg8.area != 0:
  160. curJ[0] = pg8
  161. pg4 = pg8
  162. cursorJ.updateRow(curJ)
  163. else:
  164. curJ[0] = pg6
  165. pg4 = pg6
  166. cursorJ.updateRow(curJ)
  167. #判断是否有包含的
  168. arcpy.SelectLayerByLocation_management('gc',"WITHIN",pg4)
  169. count2 = int(arcpy.GetCount_management('gc')[0])
  170. if count2 != 0:
  171. with arcpy.da.SearchCursor('gc',['SHAPE@','FID']) as cursorL:
  172. for curL in cursorL:
  173. pg7 = curL[0]
  174. id3 = curL[1]
  175. if id3 == id1:
  176. pass
  177. else:
  178. pg8 = pg4.symmetricDifference (pg7)
  179. curJ[0] = pg8
  180. pg4 = pg8
  181. cursorJ.updateRow(curJ)
  182. #在这里找赋值
  183. arcpy.SelectLayerByLocation_management('dxta',"WITHIN",pg4)
  184. count2 = int(arcpy.GetCount_management('dxta')[0])
  185. if count2 != 0:
  186. with arcpy.da.SearchCursor('dxta',['Layer','RefName']) as cursorG:
  187. for curG in cursorG:
  188. if curG[0] == '9611':
  189. if curG[1] == '0101' or curG[1] == '0102' or curG[1] == '0103':
  190. #耕地
  191. curJ[2] = 'GD'
  192. cursorJ.updateRow(curJ)
  193. break
  194. elif curG[1] == '0301' or curG[1] == '0302' or curG[1] == '0307':
  195. #林地
  196. curJ[2] = 'LD'
  197. cursorJ.updateRow(curJ)
  198. break
  199. elif curG[1] == '0401' or curG[1] == '0402' or curG[1] == '0403' or curG[1] == '0404':
  200. #都是草地
  201. curJ[2] = 'CD'
  202. cursorJ.updateRow(curJ)
  203. break
  204. elif curG[1] == '1101':
  205. #河流
  206. curJ[2] = 'HL'
  207. cursorJ.updateRow(curJ)
  208. break
  209. elif curG[1] == '1003':
  210. #公路
  211. curJ[2] = 'GL'
  212. cursorJ.updateRow(curJ)
  213. break
  214. elif curG[1] == '1004' or curG[1] == '1006':
  215. #道路
  216. curJ[2] = 'DL'
  217. cursorJ.updateRow(curJ)
  218. break
  219. elif curG[1] == '1107':
  220. #沟渠
  221. curJ[2] = 'GQ'
  222. cursorJ.updateRow(curJ)
  223. break
  224. elif curG[1] == '1106':
  225. #滩涂
  226. curJ[2] = 'TT'
  227. cursorJ.updateRow(curJ)
  228. break
  229. elif curG[1] == '1104':
  230. #坑塘
  231. curJ[2] = 'KT'
  232. cursorJ.updateRow(curJ)
  233. break
  234. elif curG[1] == '0701' or curG[1] == '0702':
  235. #房屋
  236. curJ[2] = 'FW'
  237. cursorJ.updateRow(curJ)
  238. break
  239. elif curG[1] == '1204':
  240. #盐碱地
  241. curJ[2] = 'YJD'
  242. cursorJ.updateRow(curJ)
  243. break
  244. elif curG[1] == '1205':
  245. #沙地
  246. curJ[2] = 'SD'
  247. cursorJ.updateRow(curJ)
  248. break
  249. elif curG[1] == '1206':
  250. #裸土地
  251. curJ[2] = 'LTD'
  252. cursorJ.updateRow(curJ)
  253. break
  254. elif curG[1] == '1207':
  255. #石砾地
  256. curJ[2] = 'SLD'
  257. cursorJ.updateRow(curJ)
  258. break
  259. elif curG[1] == '0305':
  260. #灌木
  261. curJ[2] = 'GM'
  262. cursorJ.updateRow(curJ)
  263. break
  264. return gcpath
  265. def dwpd(lx):
  266. dw = ''
  267. if lx == 'GD':
  268. dw = '耕地'
  269. elif lx == 'LD':
  270. dw = '林地'
  271. elif lx == 'CD':
  272. dw = '草地'
  273. elif lx == 'TT':
  274. dw = '滩涂'
  275. elif lx == 'KT':
  276. dw = '坑塘'
  277. elif lx == 'YJD':
  278. dw = '盐碱地'
  279. elif lx == 'SD':
  280. dw = '沙地'
  281. elif lx == 'LTD':
  282. dw = '裸土地'
  283. elif lx == 'SLD':
  284. dw = '石砾地'
  285. elif lx == 'GM':
  286. dw = '灌木'
  287. elif lx == '':
  288. dw = 'XX'
  289. return dw
  290. def randomjj(jjs,jjz):
  291. #随机一个数
  292. rand_num = round(random.uniform(jjs, jjz),3)
  293. return rand_num
  294. def DM(dmxpath,dxtpath,dempath,jjs0,jjz0,outpath):
  295. #间距
  296. jjs = float(jjs0)
  297. jjz = float(jjz0)
  298. #把图名的开头提出来
  299. dmxfile = dmxpath.split('\\',-1)[-1]
  300. dmxname = dmxfile.split('.',-1)[0]
  301. #读取断面线
  302. dmxlpath = dmxpath + '\\Polyline'
  303. arcpy.MakeFeatureLayer_management(dmxlpath,'dmx')
  304. #读取水下点
  305. sxdpath = dmxpath + '\\Point'
  306. arcpy.MakeFeatureLayer_management(sxdpath,'sxd')
  307. #读取地形图的线、面、注记
  308. dxtlpath = dxtpath + '\\Polyline'
  309. arcpy.MakeFeatureLayer_management(dxtlpath,'dxtl')
  310. dxtgpath = dxtpath + '\\Polygon'
  311. dxtapath = dxtpath + '\\Annotation'
  312. #需要一个过程文件,把图斑裁出来
  313. gcpath = tbclip(dxtgpath,dxtapath,outpath)
  314. arcpy.MakeFeatureLayer_management(gcpath,'gcshp')
  315. #找寻所有的断面线,生成一个包含断面线形状的list
  316. #找到zzx
  317. list1 = []
  318. with arcpy.da.SearchCursor('dmx',['SHAPE@','Layer']) as cursorA:
  319. for curA in cursorA:
  320. layer = curA[1]
  321. if layer == 'DMX':
  322. list1.append(curA[0])
  323. elif layer == 'zxx':
  324. zxx = curA[0]
  325. #通过中心线找到断面名(断面前缀、zxx、断面线list)
  326. #list2包括线形和线名,交点
  327. list2 = dmorder(dmxname,zxx,list1)
  328. #对每根线做操作
  329. ii = 0
  330. #先得到zxx的所有折点
  331. zds = zxx.getPart(0)
  332. #把折点距起点的距离全部算出来,再存放为listzd(折点,距离)
  333. listzd = diszd(zxx,zds)
  334. while ii < len(list2):
  335. line = list2[ii][0]
  336. xjp = list2[ii][2]
  337. disxjp = zxx.measureOnLine (xjp)
  338. #首先判定线的起点是左基点还是右基点
  339. bl = jdpd(line,listzd,disxjp)
  340. #对应线的水下点,其距起点的距离,高程,地物类型搞出来
  341. list4 = []
  342. #这个是最终的不管左右的list
  343. list40 = []
  344. linebuf = line.buffer(0.1)
  345. arcpy.SelectLayerByLocation_management('sxd',"WITHIN",linebuf)
  346. with arcpy.da.SearchCursor('sxd',['SHAPE@','Layer','Elevation']) as cursorB:
  347. for curB in cursorB:
  348. layer1 = curB[1]
  349. if layer1 == 'sxd':
  350. list3 = []
  351. #先把点挪到线上
  352. disp = line.distanceTo (curB[0])
  353. if disp == 0:
  354. #那就直接用
  355. list3.append(curB[0])
  356. dissxd = line.measureOnLine (curB[0])
  357. list3.append(dissxd)
  358. list3.append(curB[2])
  359. list3.append('水下')
  360. list4.append(list3)
  361. else:
  362. sxd1 = line.queryPointAndDistance(curB[0])
  363. sxd = sxd1[0].firstPoint
  364. list3.append(sxd)
  365. list3.append(sxd1[1])
  366. list3.append(curB[2])
  367. list3.append('水下')
  368. list4.append(list3)
  369. #把河流图斑踢出来
  370. #坎子、房子、沟渠、道路需要岔线上的先添加
  371. #线地形图涉及到坎子
  372. arcpy.SelectLayerByLocation_management('dxtl',"INTERSECT",line)
  373. count1 = int(arcpy.GetCount_management('dxtl')[0])
  374. if count1 != 0:
  375. with arcpy.da.SearchCursor('dxtl',['SHAPE@','Layer','Linetype']) as cursorC:
  376. for curC in cursorC:
  377. layer2 = curC[1]
  378. lt = curC[2]
  379. if layer2 == 'DMTZ' and lt == '10421':
  380. line1 = curC[0]
  381. kjdg = line.intersect (line1, 1)
  382. kjd = kjdg.firstPoint
  383. kjdx = kjd.X
  384. kjdy = kjd.Y
  385. kjdstr = str(kjdx) + ' ' + str(kjdy)
  386. list5 = []
  387. list5.append(kjd)
  388. #距起点的距离
  389. disk = line.measureOnLine (kjd)
  390. list5.append(disk)
  391. #z值
  392. kjz = arcpy.GetCellValue_management(dempath, kjdstr)
  393. try:
  394. list5.append(float(kjz.getOutput(0)))
  395. except:
  396. list5.append(0)
  397. # list5.append(-100)
  398. #地物类型
  399. list5.append('坎上')
  400. list4.append(list5)
  401. #找出所有相交的图斑
  402. arcpy.SelectLayerByLocation_management('gcshp',"INTERSECT",line)
  403. with arcpy.da.SearchCursor('gcshp',['SHAPE@','LX']) as cursorD:
  404. for curD in cursorD:
  405. layer3 = curD[1]
  406. if layer3 == 'FW':
  407. pg1 = curD[0]
  408. pgb = pg1.boundary ()
  409. pjdg = pgb.intersect (line, 1)
  410. pjdgc = pjdg.pointCount
  411. mm = 0
  412. while mm < pjdgc:
  413. list6 = []
  414. pjd = pjdg[mm]
  415. pjdx = pjd.X
  416. pjdy = pjd.Y
  417. pjdstr = str(pjdx) + ' ' + str(pjdy)
  418. list6.append(pjd)
  419. #距离
  420. dispj = line.measureOnLine (pjd)
  421. list6.append(dispj)
  422. #z值
  423. pjz = arcpy.GetCellValue_management(dempath, pjdstr)
  424. try:
  425. list6.append(float(pjz.getOutput(0)))
  426. except:
  427. list6.append(0)
  428. # list6.append(-100)
  429. #备注
  430. list6.append('房屋')
  431. list4.append(list6)
  432. mm = mm + 1
  433. elif layer3 == 'GL':
  434. pg1 = curD[0]
  435. pgb = pg1.boundary ()
  436. pjdg = pgb.intersect (line, 1)
  437. pjdgc = pjdg.pointCount
  438. mm = 0
  439. while mm < pjdgc:
  440. list6 = []
  441. pjd = pjdg[mm]
  442. pjdx = pjd.X
  443. pjdy = pjd.Y
  444. pjdstr = str(pjdx) + ' ' + str(pjdy)
  445. list6.append(pjd)
  446. #距离
  447. dispj = line.measureOnLine (pjd)
  448. list6.append(dispj)
  449. #z值
  450. pjz = arcpy.GetCellValue_management(dempath, pjdstr)
  451. try:
  452. list6.append(float(pjz.getOutput(0)))
  453. except:
  454. list6.append(0)
  455. # list6.append(-100)
  456. #备注
  457. list6.append('公路')
  458. list4.append(list6)
  459. mm = mm + 1
  460. elif layer3 == 'DL':
  461. pg1 = curD[0]
  462. pgb = pg1.boundary ()
  463. pjdg = pgb.intersect (line, 1)
  464. pjdgc = pjdg.pointCount
  465. mm = 0
  466. while mm < pjdgc:
  467. list6 = []
  468. pjd = pjdg[mm]
  469. pjdx = pjd.X
  470. pjdy = pjd.Y
  471. pjdstr = str(pjdx) + ' ' + str(pjdy)
  472. list6.append(pjd)
  473. #距离
  474. dispj = line.measureOnLine (pjd)
  475. list6.append(dispj)
  476. #z值
  477. pjz = arcpy.GetCellValue_management(dempath, pjdstr)
  478. try:
  479. list6.append(float(pjz.getOutput(0)))
  480. except:
  481. list6.append(0)
  482. # list6.append(-100)
  483. #备注
  484. list6.append('道路')
  485. list4.append(list6)
  486. mm = mm + 1
  487. elif layer3 == 'GQ':
  488. pg1 = curD[0]
  489. pgb = pg1.boundary ()
  490. pjdg = pgb.intersect (line, 1)
  491. pjdgc = pjdg.pointCount
  492. mm = 0
  493. while mm < pjdgc:
  494. list6 = []
  495. pjd = pjdg[mm]
  496. pjdx = pjd.X
  497. pjdy = pjd.Y
  498. pjdstr = str(pjdx) + ' ' + str(pjdy)
  499. list6.append(pjd)
  500. #距离
  501. dispj = line.measureOnLine (pjd)
  502. list6.append(dispj)
  503. #z值
  504. pjz = arcpy.GetCellValue_management(dempath, pjdstr)
  505. try:
  506. list6.append(float(pjz.getOutput(0)))
  507. except:
  508. list6.append(0)
  509. # list6.append(-100)
  510. #备注
  511. list6.append('沟渠')
  512. list4.append(list6)
  513. mm = mm + 1
  514. #一个布尔值,用于判断端点是否需要挪
  515. bl1 = 0
  516. #从左基点开始,每2.8m取一个点,避开上面已经剃过点的地方
  517. if bl == "LEFT":
  518. #那么前面的距离顺序都是对的,直接找就成了
  519. #先存放左基点
  520. #判定是否在水里
  521. fp = line.firstPoint
  522. fpp = arcpy.PointGeometry(fp)
  523. #首先看点落在哪个图斑里
  524. arcpy.SelectLayerByLocation_management('gcshp',"CONTAINS",fpp)
  525. with arcpy.da.SearchCursor('gcshp',['SHAPE@','LX']) as cursorF:
  526. for curF in cursorF:
  527. lx = curF[1]
  528. dw = dwpd(lx)
  529. if dw != '':
  530. list6 = []
  531. pjdx = fp.X
  532. pjdy = fp.Y
  533. pjdstr = str(pjdx) + ' ' + str(pjdy)
  534. list6.append(fp)
  535. #距离
  536. list6.append(0)
  537. #z值
  538. pjz = arcpy.GetCellValue_management(dempath, pjdstr)
  539. try:
  540. list6.append(float(pjz.getOutput(0)))
  541. except:
  542. list6.append(0)
  543. # list6.append(-100)
  544. #备注
  545. list6.append(dw)
  546. list4.append(list6)
  547. else:
  548. #如果不是就记录下它的坐标,到时候套第一个水下点的高程
  549. list6 = []
  550. pjdx = fp.X
  551. pjdy = fp.Y
  552. list6.append(fp)
  553. #距离
  554. list6.append(0)
  555. #z值
  556. pjz = -9999
  557. list6.append(float(pjz))
  558. #备注
  559. list6.append(dw)
  560. list4.append(list6)
  561. bl1 = 1
  562. #隔一定距离存一个点
  563. length = line.length
  564. #总长
  565. zlen = 0
  566. while length - zlen > jjz:
  567. #如果比最大值小的话就直接取
  568. jj1 = randomjj(jjs,jjz)
  569. zlen = zlen + jj1
  570. ptg2 = line.positionAlongLine(zlen)
  571. pt2 = ptg2.firstPoint
  572. list6 = []
  573. arcpy.SelectLayerByLocation_management('gcshp',"CONTAINS",ptg2)
  574. count2 = int(arcpy.GetCount_management('gcshp')[0])
  575. with arcpy.da.SearchCursor('gcshp',['SHAPE@','LX']) as cursorG:
  576. for curG in cursorG:
  577. lx = curG[1]
  578. dw = dwpd(lx)
  579. if dw != '':
  580. list6 = []
  581. pjdx = pt2.X
  582. pjdy = pt2.Y
  583. pjdstr = str(pjdx) + ' ' + str(pjdy)
  584. list6.append(pt2)
  585. #距离
  586. list6.append(zlen)
  587. #z值
  588. pjz = arcpy.GetCellValue_management(dempath, pjdstr)
  589. try:
  590. list6.append(float(pjz.getOutput(0)))
  591. except:
  592. list6.append(0)
  593. # list6.append(-100)
  594. #备注
  595. list6.append(dw)
  596. list4.append(list6)
  597. #右基点
  598. fp = line.lastPoint
  599. fpp = arcpy.PointGeometry(fp)
  600. #首先看点落在哪个图斑里
  601. arcpy.SelectLayerByLocation_management('gcshp',"CONTAINS",fpp)
  602. with arcpy.da.SearchCursor('gcshp',['SHAPE@','LX']) as cursorF:
  603. for curF in cursorF:
  604. lx = curF[1]
  605. dw = dwpd(lx)
  606. if dw != '':
  607. list6 = []
  608. pjdx = fp.X
  609. pjdy = fp.Y
  610. pjdstr = str(pjdx) + ' ' + str(pjdy)
  611. list6.append(fp)
  612. #距离
  613. list6.append(length)
  614. #z值
  615. pjz = arcpy.GetCellValue_management(dempath, pjdstr)
  616. try:
  617. list6.append(float(pjz.getOutput(0)))
  618. except:
  619. list6.append(0)
  620. # list6.append(-100)
  621. #备注
  622. list6.append(dw)
  623. list4.append(list6)
  624. else:
  625. list6 = []
  626. list6.append(fp)
  627. #距离
  628. list6.append(length)
  629. #z值
  630. pjz = -9999
  631. list6.append(float(pjz))
  632. #备注
  633. list6.append(dw)
  634. list4.append(list6)
  635. bl1 = -1
  636. list40 = list4
  637. else:
  638. fp = line.lastPoint
  639. fpp = arcpy.PointGeometry(fp)
  640. #首先看点落在哪个图斑里
  641. arcpy.SelectLayerByLocation_management('gcshp',"CONTAINS",fpp)
  642. with arcpy.da.SearchCursor('gcshp',['SHAPE@','LX']) as cursorF:
  643. for curF in cursorF:
  644. lx = curF[1]
  645. dw = dwpd(lx)
  646. if dw != '':
  647. list6 = []
  648. pjdx = fp.X
  649. pjdy = fp.Y
  650. pjdstr = str(pjdx) + ' ' + str(pjdy)
  651. list6.append(fp)
  652. #距离
  653. list6.append(0)
  654. #z值
  655. pjz = arcpy.GetCellValue_management(dempath, pjdstr)
  656. try:
  657. list6.append(float(pjz.getOutput(0)))
  658. except:
  659. list6.append(0)
  660. # list6.append(-100)
  661. #备注
  662. list6.append(dw)
  663. list40.append(list6)
  664. else:
  665. list6 = []
  666. list6.append(fp)
  667. #距离
  668. list6.append(0)
  669. #z值
  670. pjz = -9999
  671. list6.append(float(pjz))
  672. #备注
  673. list6.append(dw)
  674. list40.append(list6)
  675. bl1 = 1
  676. #隔一定距离存一个点
  677. length = line.length
  678. zlen = 0
  679. while length - zlen > jjz:
  680. jj1 = randomjj(jjs,jjz)
  681. zlen = zlen + jj1
  682. jj2 = length - zlen
  683. ptg2 = line.positionAlongLine(jj2)
  684. pt2 = ptg2.firstPoint
  685. list6 = []
  686. arcpy.SelectLayerByLocation_management('gcshp',"CONTAINS",ptg2)
  687. with arcpy.da.SearchCursor('gcshp',['SHAPE@','LX']) as cursorG:
  688. for curG in cursorG:
  689. lx = curG[1]
  690. dw = dwpd(lx)
  691. if dw != '':
  692. list6 = []
  693. pjdx = pt2.X
  694. pjdy = pt2.Y
  695. pjdstr = str(pjdx) + ' ' + str(pjdy)
  696. list6.append(pt2)
  697. #距离
  698. list6.append(zlen)
  699. #z值
  700. pjz = arcpy.GetCellValue_management(dempath, pjdstr)
  701. try:
  702. list6.append(float(pjz.getOutput(0)))
  703. except:
  704. list6.append(0)
  705. # list6.append(-100)
  706. #备注
  707. list6.append(dw)
  708. list40.append(list6)
  709. #右基点
  710. fp = line.firstPoint
  711. fpp = arcpy.PointGeometry(fp)
  712. #首先看点落在哪个图斑里
  713. arcpy.SelectLayerByLocation_management('gcshp',"CONTAINS",fpp)
  714. with arcpy.da.SearchCursor('gcshp',['SHAPE@','LX']) as cursorF:
  715. for curF in cursorF:
  716. lx = curF[1]
  717. dw = dwpd(lx)
  718. if dw != '':
  719. list6 = []
  720. pjdx = fp.X
  721. pjdy = fp.Y
  722. pjdstr = str(pjdx) + ' ' + str(pjdy)
  723. list6.append(fp)
  724. #距离
  725. list6.append(length)
  726. #z值
  727. pjz = arcpy.GetCellValue_management(dempath, pjdstr)
  728. try:
  729. list6.append(float(pjz.getOutput(0)))
  730. except:
  731. list6.append(0)
  732. # list6.append(-100)
  733. #备注
  734. list6.append(dw)
  735. list40.append(list6)
  736. else:
  737. list6 = []
  738. list6.append(fp)
  739. #距离
  740. list6.append(length)
  741. #z值
  742. pjz = -9999
  743. list6.append(float(pjz))
  744. #备注
  745. list6.append(dw)
  746. list40.append(list6)
  747. bl1 = -1
  748. #把list4里的距离重新计算后放入40
  749. oo = 0
  750. while oo < len(list4):
  751. list9 = []
  752. list9.append(list4[oo][0])
  753. dis1 = length - list4[oo][1]
  754. list9.append(dis1)
  755. list9.append(list4[oo][2])
  756. list9.append(list4[oo][3])
  757. list40.append(list9)
  758. oo = oo + 1
  759. #排序
  760. list40.sort(key=lambda x: x[1], reverse=False)
  761. #把起点或者终点的水下点挪到第一个
  762. if bl1 == 1:
  763. #左端点
  764. if list40[0][1] == 0 and list40[0][2] == -9999:
  765. fp1 = list40[0][0]
  766. list40[1][0] = fp1
  767. list40[1][1] = 0
  768. del list40[0]
  769. else:
  770. fp1 = list40[1][0]
  771. list40[0][0] = fp1
  772. list40[0][1] = 0
  773. del list40[1]
  774. if bl1 == -1:
  775. #右端点
  776. len1 = len(list40)-1
  777. if list40[len1][1] == length and list40[len1][2] == -9999:
  778. fp1 = list40[len(list40)-1][0]
  779. list40[len(list40)-2][0] = fp1
  780. list40[len(list40)-2][1] = length
  781. del list40[len(list40)-1]
  782. else:
  783. fp1 = list40[len(list40)-2][0]
  784. list40[len(list40)-1][0] = fp1
  785. list40[len(list40)-1][1] = length
  786. del list40[len(list40)-2]
  787. #排除距离相同的尾巴
  788. if round(list40[len(list40)-1][1],3) == round(list40[len(list40)-2][1],3):
  789. del list40[len(list40)-1]
  790. if round(list40[0][1],3) == round(list40[1][1],3):
  791. del list40[1]
  792. #这里把地形变换的第一个水下,和最后一个水下写成水边线
  793. qq = 0
  794. while qq < (len(list40) - 1):
  795. if list40[qq][3] == '水下':
  796. #判断它的前后是否不是水下
  797. if list40[qq-1][3] != '水下' and list40[qq-1][3] != '左端点' and list40[qq-1][3] != '水边线':
  798. list40[qq][3] = '水边线'
  799. elif list40[qq+1][3] != '水下' and list40[qq+1][3] != '右端点' and list40[qq+1][3] != '水边线':
  800. list40[qq][3] = '水边线'
  801. qq = qq + 1
  802. else:
  803. qq = qq + 1
  804. #新建一个excel
  805. expath = outpath + '\\' + list2[ii][1] + '.xls'
  806. #判断下存不存在,存在则删除
  807. if os.path.exists(expath):
  808. # 删除文件
  809. os.remove(expath)
  810. #写个抬头
  811. workBook = xlwt.Workbook(encoding='utf-8')
  812. sheet = workBook.add_sheet("sheet1")
  813. sheet.write(0,0,'点号')
  814. sheet.write(0,1,'X')
  815. sheet.write(0,2,'Y')
  816. sheet.write(0,3,'Z')
  817. sheet.write(0,4,'备注')
  818. sheet.write(0,5,'累距')
  819. pp = 0
  820. while pp < len(list40):
  821. if pp == 0:
  822. pp1 = pp + 1
  823. sheet.write(pp1,0,(pp+1))
  824. try:
  825. sheet.write(pp1,1,round(list40[pp][0].X,4))
  826. sheet.write(pp1,2,round(list40[pp][0].Y,4))
  827. except:
  828. pt3 = list40[pp][0].firstPoint
  829. sheet.write(pp1,1,round(pt3.X,4))
  830. sheet.write(pp1,2,round(pt3.Y,4))
  831. sheet.write(pp1,3,round(list40[pp][2],3))
  832. sheet.write(pp1,4,'左端点')
  833. sheet.write(pp1,5,round(list40[pp][1],3))
  834. pp = pp + 1
  835. elif pp == len(list40) - 1:
  836. pp1 = pp + 1
  837. sheet.write(pp1,0,(pp+1))
  838. try:
  839. sheet.write(pp1,1,round(list40[pp][0].X,4))
  840. sheet.write(pp1,2,round(list40[pp][0].Y,4))
  841. except:
  842. pt3 = list40[pp][0].firstPoint
  843. sheet.write(pp1,1,round(pt3.X,4))
  844. sheet.write(pp1,2,round(pt3.Y,4))
  845. sheet.write(pp1,3,round(list40[pp][2],3))
  846. sheet.write(pp1,4,'右端点')
  847. sheet.write(pp1,5,round(list40[pp][1],3))
  848. pp = pp + 1
  849. else:
  850. pp1 = pp + 1
  851. sheet.write(pp1,0,(pp+1))
  852. try:
  853. sheet.write(pp1,1,round(list40[pp][0].X,4))
  854. sheet.write(pp1,2,round(list40[pp][0].Y,4))
  855. except:
  856. pt3 = list40[pp][0].firstPoint
  857. sheet.write(pp1,1,round(pt3.X,4))
  858. sheet.write(pp1,2,round(pt3.Y,4))
  859. sheet.write(pp1,3,round(list40[pp][2],3))
  860. sheet.write(pp1,4,list40[pp][3])
  861. sheet.write(pp1,5,round(list40[pp][1],3))
  862. pp = pp + 1
  863. workBook.save(expath)
  864. arcpy.AddMessage (list2[ii][1] + ' finish')
  865. ii = ii + 1
  866. if __name__ == '__main__':
  867. try:
  868. #断面线所在的dwg,地形图,dem,上间距,下间距,输出文件夹
  869. DM(arcpy.GetParameterAsText(0),arcpy.GetParameterAsText(1),arcpy.GetParameterAsText(2),arcpy.GetParameterAsText(3),arcpy.GetParameterAsText(4),arcpy.GetParameterAsText(5))
  870. # DM(r"D:\3work_now\20231108HHdm\1121\QJM2.dwg",r'D:\3work_now\20231108HHdm\1121\21求吉玛村2#护岸工程.dwg',r'D:\3work_now\20231108HHdm\1121\21.tif',2,3,r'D:\3work_now\20231108HHdm\1121\19')
  871. except arcpy.ExecuteError:
  872. arcpy.AddMessage (arcpy.GetMessages())