BufferAnalysis.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>周边分析</title>
  7. <link rel="stylesheet" href="../../dist/index.css" />
  8. <link href="../../cesium/Source/Widgets/widgets.css" rel="stylesheet">
  9. <script src="../../cesium/Build/Cesium/Cesium.js"></script>
  10. <script src="../../dist/index.js"></script>
  11. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  12. <style>
  13. * {
  14. padding: 0;
  15. margin: 0;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div style="display: flex;">
  21. <div id="info" style="height: 100vh; width: 30vw;">
  22. <button class="btn" onclick="addpointentity()">新增点</button><br>
  23. <button class="btn" onclick="addpolylineentity()">新增线</button>
  24. <span>线长倍数</span>
  25. <span style="margin-left: 2em;">
  26. 0倍
  27. <input type="range" min="0" max="10" value="1" step="0.01" onchange="changelinelength(this.value)">
  28. 10倍
  29. </span><br>
  30. <button class="btn" onclick="addpolygonentity()">新增多边形</button>
  31. <span>多边形倍数</span>
  32. <span style="margin-left: 2em;">
  33. 0倍
  34. <input type="range" min="0" max="10" value="1" step="0.01" onchange="changepolygonlength(this.value)">
  35. 10倍
  36. </span><br>
  37. <span>周边缓冲距离:</span>
  38. <span style="margin-left: 2em;">
  39. 0km
  40. <input type="range" min="0" max="10" value="0.5" step="0.01" onchange="changebuffervalue(this.value)">
  41. 10km
  42. </span>
  43. <canvas id="testcanvas" style="width: 100px;height: 100px;"></canvas>
  44. </div>
  45. <div id="map" style="height: 100vh; width: 70vw;"></div>
  46. </div>
  47. </body>
  48. <script>
  49. window.CESIUM_BASE_URL = '../../cesium/Build/CesiumUnminified';
  50. Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2MjIyODA4ZS0zNmRmLTQwNGMtYjZkNC0wOGY2NDdmODgyZTUiLCJpZCI6ODMyOCwiaWF0IjoxNjA2MjkyNDE1fQ.ZhhxNIJ-TzstpcavQBNL7xRwytBat3G2__3jr7lJ8yg'
  51. var viewer = CTMapOl.thrdime.viewercontrol.globalviewer('map', CTMapOl.thrdime.viewercontrol.globalvieweroptions())
  52. viewer._cesiumWidget._creditContainer.style.display = 'none'
  53. CTMapOl.thrdime.imagerylayercontrol.removeAll(viewer, true)
  54. CTMapOl.thrdime.imagerylayercontrol.addImageryProvider(viewer, CTMapOl.thrdime.imagerylayercontrol.gettdtImagery('img', "img_w"))
  55. CTMapOl.thrdime.imagerylayercontrol.addImageryProvider(viewer, CTMapOl.thrdime.imagerylayercontrol.gettdtImagery('cia', "cia_w"))
  56. CTMapOl.thrdime.cameracontrol.setDEFAULT_VIEW_RECTANGLE(116.38, 39.90, 116.40, 39.92)
  57. CTMapOl.thrdime.cameracontrol.setcamera(viewer, CTMapOl.thrdime.cameracontrol.getcameraoptionswithdetile(116.39, 39.91, 5000, 0, -90, 0))
  58. CTMapOl.thrdime.viewercontrol.addterrain(viewer)
  59. let coloroptions = {
  60. pointcolor: '#4F9FFF', // 点内填充颜色
  61. pointoutlineColor: '#4F9FFF', // 点外圈
  62. linecolor: '#4F9FFF', // 线颜色
  63. polygoncolor: '#4F9FFF40', // 多边形颜色
  64. buffercolor: '#4F9FF1A0', // 缓冲区颜色及透明度
  65. linewidth: 2 // 线宽
  66. }
  67. let BufferAnalysis = new CTMapOl.cesiumComponent.BufferAnalysis(viewer, 0.5, coloroptions)
  68. let linescale = 1
  69. let polygonscale = 1
  70. let muentitys = []
  71. let mucoods = []
  72. let index = 0
  73. for (let i = 116.28; i < 116.50; i += 0.001) {
  74. for (let j = 39.80; j < 40.02; j += 0.001) {
  75. mucoods.push({
  76. cood: [i, j],
  77. id: index
  78. })
  79. index++
  80. }
  81. }
  82. async function addpointentity() {
  83. let tarpointbufferentity = await BufferAnalysis.addpointentity([116.39, 39.91])
  84. console.log(tarpointbufferentity)
  85. getcenterandradi(tarpointbufferentity._polygon._hierarchy._value.positions)
  86. let coods1 = filterforcoods(tarpointbufferentity._polygon._hierarchy._value.positions, mucoods)
  87. testaddpoints(coods1)
  88. }
  89. async function addpolylineentity() {
  90. let coods = [[116.392, 39.917], [116.39, 39.91], [116.391, 39.921]]
  91. let length = CTMapOl.turf.length(CTMapOl.turf.lineString(coods))
  92. coods = CTMapOl.turf.transformScale(CTMapOl.turf.lineString(coods), linescale).geometry.coordinates
  93. if (BufferAnalysis.checkpolylineentity(coods)) {
  94. alert('超限')
  95. }
  96. else {
  97. let tarpolylinebufferentity = await BufferAnalysis.addpolylineentity(coods)
  98. getcenterandradi(tarpolylinebufferentity._polygon._hierarchy._value.positions)
  99. let coods2 = filterforcoods(tarpolylinebufferentity._polygon._hierarchy._value.positions, mucoods)
  100. testaddpoints(coods2)
  101. }
  102. }
  103. function changelinelength(scale) {
  104. linescale = Number(scale)
  105. }
  106. async function addpolygonentity() {
  107. let coods = [[[116.392, 39.917], [116.39, 39.91], [116.391, 39.921], [116.392, 39.917]]]
  108. let length = CTMapOl.turf.length(CTMapOl.turf.polygon(coods))
  109. coods = CTMapOl.turf.transformScale(CTMapOl.turf.polygon(coods), polygonscale).geometry.coordinates
  110. if (BufferAnalysis.checkpolygonrntity(coods)) {
  111. alert('超限')
  112. } else {
  113. let tarpolygonbufferentity = await BufferAnalysis.addpolygonrntity(coods)
  114. getcenterandradi(tarpolygonbufferentity._polygon._hierarchy._value.positions)
  115. let coods3 = filterforcoods(tarpolygonbufferentity._polygon._hierarchy._value.positions, mucoods)
  116. testaddpoints(coods3)
  117. }
  118. }
  119. function changepolygonlength(scale) {
  120. polygonscale = Number(scale)
  121. }
  122. async function changebuffervalue(buffervalue) {
  123. let newbufferentitys = await BufferAnalysis.changebuffervalue(buffervalue)
  124. console.log(newbufferentitys)
  125. }
  126. //获取中心点和半径
  127. function getcenterandradi(entitycoods) {
  128. let poses = []
  129. for (let i = 0; i < entitycoods.length; i++) {
  130. let temp = Cesium.Cartographic.fromCartesian(entitycoods[i])
  131. poses.push([temp.longitude, temp.latitude])
  132. }
  133. var coods = CTMapOl.turf.points(poses);
  134. var center = CTMapOl.turf.center(coods);
  135. let radi = Number.MIN_VALUE
  136. let centerpoint = Cesium.Cartesian3.fromRadians(center.geometry.coordinates[0], center.geometry.coordinates[1])
  137. for (let i = 0; i < entitycoods.length; i++) {
  138. let temp = Cesium.Cartesian3.distance(centerpoint, entitycoods[i])
  139. radi = Math.max(radi, temp)
  140. }
  141. let res = {
  142. center: {
  143. longitude: center.geometry.coordinates[0] / Math.PI * 180,
  144. latitude: center.geometry.coordinates[1] / Math.PI * 180,
  145. },
  146. radi: radi
  147. }
  148. console.log(res)
  149. return res
  150. }
  151. //过滤
  152. function filterforcoods(entitycoods, testpoints) {
  153. let poses = []
  154. for (let i = 0; i < entitycoods.length; i++) {
  155. let temp = Cesium.Cartographic.fromCartesian(entitycoods[i])
  156. poses.push([temp.longitude / Math.PI * 180, temp.latitude / Math.PI * 180])
  157. }
  158. var polygon = CTMapOl.turf.polygon([poses]);
  159. let res = []
  160. for (let i = 0; i < testpoints.length; i++) {
  161. var point = CTMapOl.turf.point([testpoints[i].cood[0], testpoints[i].cood[1]]);
  162. if (CTMapOl.turf.booleanContains(polygon, point)) {
  163. res.push(testpoints[i])
  164. }
  165. }
  166. console.log(res)
  167. return res
  168. }
  169. //测试准确性
  170. function testaddpoints(testpoints) {
  171. for (let i = 0; i < muentitys.length; i++) {
  172. viewer.entities.remove(muentitys[i])
  173. }
  174. muentitys = []
  175. for (let i = 0; i < testpoints.length; i++) {
  176. let testpoint = viewer.entities.add({
  177. position: Cesium.Cartesian3.fromDegrees(testpoints[i].cood[0], testpoints[i].cood[1], 100),
  178. ellipsoid: {
  179. radii: new Cesium.Cartesian3(5.0, 5.0, 5.0),
  180. material: Cesium.Color.BLUE
  181. },
  182. });
  183. muentitys.push(this.testpoint);
  184. }
  185. }
  186. </script>
  187. </html>