当前位置:   article > 正文

openlayers结合turf相交分析-航路预警demo_openlayer预警

openlayer预警

更多专栏文章参考 http://www.xiaobaigis.com/GiSarticles/Index/OpenLayers

openlayers是gis渲染引擎,turf.js是一个gis辅助工具-gis空间关系分析库,结合二者可以做很多空间分析工作,比如简单的点线是否相交,如果有类似航路预警的业务需求可以借鉴

1 核心代码思路

(1) 模拟航路线轨迹坐标,途径危险点坐标

(2) 渲染航路线图层

(3) 渲染途径的危险区域(点-有一定半径区域)

(4) 用轨迹点和危险点,利用turf做相交运算,获取几何分析结果

(5) 渲染预警结果图层,将几何结果处理成openlayers支持的features展示即可

2 效果

3 全部代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>openlayers结合turf相交分析-航路预警demo</title>
  5. <link rel="stylesheet" href="../../static/data/ol.css" type="text/css" />
  6. <script src="../../static/data/ol.js"></script>
  7. <script src="../../static/data/turf.js"></script>
  8. <style>
  9. .blingAnimation {
  10. height: 30px;
  11. width: 30px;
  12. border-radius: 25px;
  13. background: rgb(255, 0, 0);
  14. transform: scale(0.3);
  15. animation: myfirst 1s;
  16. animation-iteration-count: infinite;
  17. z-index: 200;
  18. border: solid 1px purple;
  19. }
  20. @keyframes myfirst {
  21. to {
  22. transform: scale(0.8);
  23. background: rgba(255, 0, 0, 0);
  24. }
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="map" class="map"></div>
  30. <script>
  31. var map = new ol.Map({
  32. target: "map",
  33. layers: [
  34. new ol.layer.Tile({
  35. source: new ol.source.OSM()
  36. })
  37. ],
  38. view: new ol.View({
  39. center: [112, 34],
  40. zoom: 5,
  41. projection: "EPSG:4326"
  42. })
  43. })
  44. // 航路轨迹点
  45. let AirCoors = [
  46. [112, 34],
  47. [114, 35],
  48. [116, 37],
  49. [122, 41],
  50. [130, 43],
  51. [132, 44],
  52. [134, 34],
  53. [135, 34]
  54. ]
  55. // 危险点
  56. let ExtremeCoors = [
  57. [112, 34],
  58. [112.5, 36],
  59. [113, 34],
  60. [116, 34],
  61. [121, 44],
  62. [130.232, 43],
  63. [132, 34],
  64. [137, 45],
  65. [134, 34.99]
  66. ]
  67. let bufferArr = []
  68. renderAirRoute()
  69. renderExtremeElement()
  70. handleAnalyze()
  71. // 渲染航路
  72. function renderAirRoute() {
  73. let feature = new ol.Feature({
  74. geometry: new ol.geom.LineString(AirCoors)
  75. })
  76. let style = new ol.style.Style({
  77. stroke: new ol.style.Stroke({
  78. //边界样式
  79. color: "blue",
  80. width: 5,
  81. lineDash: [1, 2, 3, 4, 5, 6]
  82. })
  83. })
  84. feature.setStyle(style)
  85. // 创建source对象
  86. var airRouteSource = new ol.source.Vector({
  87. features: [feature]
  88. })
  89. // 创建layer对象
  90. var airRouteLayer = new ol.layer.Vector({
  91. source: airRouteSource,
  92. zIndex: 10
  93. })
  94. //将layer添加到map
  95. map.addLayer(airRouteLayer)
  96. }
  97. // 渲染危险点
  98. function renderExtremeElement() {
  99. // 创建source对象
  100. let extremeElementSource = new ol.source.Vector({
  101. features: []
  102. })
  103. // 创建layer对象
  104. let extremeElementLayer = new ol.layer.Vector({
  105. source: extremeElementSource,
  106. zIndex: 10,
  107. style:new ol.style.Style({
  108. image: new ol.style.Circle({
  109. radius: 10,
  110. fill: new ol.style.Fill({
  111. color: 'orange'
  112. })
  113. })
  114. })
  115. })
  116. //将layer添加到map
  117. map.addLayer(extremeElementLayer)
  118. // 创建feature 对象
  119. let pointFeature = new ol.Feature({
  120. geometry: new ol.geom.MultiPoint(ExtremeCoors)
  121. })
  122. extremeElementSource.addFeature(pointFeature)
  123. }
  124. // turf分析
  125. function handleAnalyze() {
  126. let turfLine = turf.lineString(AirCoors)
  127. ExtremeCoors.forEach(function(coor) {
  128. let turfCircle = turf.circle(coor, 50) //半径单位是km
  129. let iScross = turf.booleanCrosses(turfLine, turfCircle)
  130. if (iScross) {
  131. // renderBufferResult(coor)
  132. createBlingOverlay(coor)
  133. }
  134. })
  135. }
  136. // 创建预警结果图层
  137. function createBlingOverlay(coor) {
  138. let dom = document.createElement("div")
  139. dom.classList.add("blingAnimation")
  140. var point_overlay = new ol.Overlay({
  141. //定义为全局变量
  142. element: dom,
  143. positioning: "center-center"
  144. })
  145. map.addOverlay(point_overlay)
  146. point_overlay.setPosition(coor)
  147. }
  148. // 渲染危险buffer区域
  149. function renderBufferResult(coor) {
  150. let olCircle = new ol.geom.Circle(coor, 1)
  151. let feature = new ol.Feature({
  152. geometry: olCircle
  153. // labelPoint: new ol.geom.Point([center_x, center_y]),
  154. // name: "333"
  155. })
  156. var source = new ol.source.Vector({
  157. features: [feature]
  158. })
  159. // 创建layer对象
  160. var layer = new ol.layer.Vector({
  161. source: source,
  162. zIndex: 9
  163. })
  164. //将layer添加到map
  165. map.addLayer(layer)
  166. let style = new ol.style.Style({
  167. stroke: new ol.style.Stroke({
  168. color: "purple",
  169. width: 3
  170. }),
  171. fill: new ol.style.Fill({
  172. color: "red"
  173. })
  174. })
  175. let style1 = new ol.style.Style({
  176. stroke: new ol.style.Stroke({
  177. color: "purple",
  178. width: 1
  179. }),
  180. fill: new ol.style.Fill({
  181. color: "orange"
  182. })
  183. })
  184. let setIntervalId = setInterval(() => {
  185. layer.setStyle(style)
  186. }, 500)
  187. let setIntervalId1 = setInterval(() => {
  188. layer.setStyle(style1)
  189. }, 1000)
  190. }
  191. </script>
  192. </body>
  193. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/595124
推荐阅读
相关标签
  

闽ICP备14008679号