Layers.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Style from "ol/style/Style";
  2. import {deepClone} from '@/common/commonFun'
  3. import {Cluster, Vector as VectorSource} from 'ol/source.js';
  4. import { Vector as VectorLayer} from 'ol/layer.js';
  5. import {boundingExtent} from 'ol/extent.js';
  6. import {
  7. Circle as CircleStyle,
  8. Fill,
  9. Text,
  10. } from 'ol/style.js';
  11. import {unByKey} from 'ol/Observable';
  12. import ImageLayer from 'ol/layer/Image';
  13. import ImageStatic from 'ol/source/ImageStatic';
  14. import XYZLayer from "../../utils/ol-map/XYZLayer";
  15. import GeoJsonLayer from "../../utils/ol-map/GeoJsonLayer";
  16. import eventBus from "@/api/eventBus";
  17. import {VectorStyle} from "../../utils/ol-map/KMap";
  18. import StaticImgLayer from "../../utils/ol-map/StaticImgLayer";
  19. function getColorByVal(val, legendData) {
  20. for (let i = 0; i < legendData.level.length; i++) {
  21. let [min, max] = legendData.level[i];
  22. if (val >= min && val <= max) {
  23. return legendData.colors[i];
  24. }
  25. }
  26. return undefined; // 如果 val 不在任何区间内,返回 undefined
  27. }
  28. /**
  29. * @description 全景化地图层对象
  30. */
  31. class StaticMapLayers {
  32. constructor(map){
  33. this.initStaticMapLayers(map)
  34. this.vectorStyle = new VectorStyle()
  35. this.layerData = {}
  36. this.cacheStyle = {}
  37. }
  38. initStaticMapLayers(map){
  39. let that = this
  40. VE_API.warning.fetchWarningLayer({
  41. k: "static_map",
  42. resultType: "json",
  43. }).then(({data}) => {
  44. for(let key in data){
  45. let item = data[key]
  46. if(item.type === "xyz"){
  47. that.layerData[key] = {legend:item.legend, layer:that.addXyzLayer(map, item)}
  48. }else if(item.type === "geojson"){
  49. that.layerData[key] = {legend:item.legend, legendData:item.legendData, layer:that.addGeoJsonLayer(map, item, key)}
  50. }else if(item.type === "img"){
  51. that.layerData[key] = {legend:item.legend, layer:that.addStaticImgLayer(map, item)}
  52. }
  53. }
  54. // that.autoTest()
  55. })
  56. }
  57. show(key){
  58. this.hideAll()
  59. let layer = this.layerData[key].layer
  60. eventBus.emit("alarmList:changeMapLayer", {legendUrl:this.layerData[key].legend});
  61. layer.show()
  62. }
  63. hideAll(){
  64. for(let key in this.layerData){
  65. let layer = this.layerData[key].layer
  66. layer.hide()
  67. }
  68. }
  69. addStaticImgLayer(map, item){
  70. let imgLayer = new StaticImgLayer(item.url, item, 3, map);
  71. imgLayer.hide()
  72. return imgLayer
  73. }
  74. addXyzLayer(map, item){
  75. let xyz = new XYZLayer(item.url, item, 3, map);
  76. xyz.hide()
  77. return xyz
  78. }
  79. addGeoJsonLayer(map, item, key){
  80. let that = this
  81. item["style"] = function(feature){
  82. let val = feature.get(item.legendData.paramKey)
  83. let cacheKey = `${key}_${item.legendData.paramKey}_${val}`
  84. let style = that.cacheStyle[cacheKey]
  85. if(!style){
  86. let color = getColorByVal(val, item.legendData)
  87. let fillColor = color
  88. let strokeColor = color
  89. style = that.vectorStyle.getPolygonStyle(fillColor, strokeColor, 1)
  90. that.cacheStyle[cacheKey] = style
  91. }
  92. return style
  93. }
  94. let geoJsonLayer = new GeoJsonLayer(item.url, item, 3, map);
  95. geoJsonLayer.hide()
  96. return geoJsonLayer
  97. }
  98. autoTest(){
  99. let that = this
  100. let keys = []
  101. for(let key in that.layerData){
  102. keys.push(key)
  103. }
  104. let index = 0
  105. setInterval(() => {
  106. that.show(keys[index])
  107. index = (index + 1) % keys.length
  108. }, 1000);
  109. }
  110. }
  111. export default StaticMapLayers