samplePointLayer.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import config from "@/api/config.js";
  2. import * as KMap from "@/utils/ol-map/KMap";
  3. import * as util from "@/common/ol_common.js";
  4. import Point from "ol/geom/Point.js";
  5. import Feature from "ol/Feature";
  6. import VectorLayer from "ol/layer/Vector.js";
  7. import WKT from "ol/format/WKT.js";
  8. import ScaleLine from "ol/control/ScaleLine";
  9. import { useRouter } from "vue-router";
  10. import { unByKey } from "ol/Observable";
  11. import Style from "ol/style/Style";
  12. import Icon from "ol/style/Icon";
  13. import { Cluster, Vector as VectorSource } from "ol/source";
  14. import { Fill, Circle, Stroke } from "ol/style";
  15. import { newPoint } from "@/utils/map";
  16. import eventBus from "@/api/eventBus";
  17. import Overlay from 'ol/Overlay'
  18. /**
  19. * @description 地图层对象
  20. */
  21. class SamplePointLayer {
  22. constructor(map, organId, regionId) {
  23. let that = this;
  24. this.farmId = organId
  25. this.regionId = regionId
  26. let vectorStyle = new KMap.VectorStyle();
  27. this.vectorStyle = vectorStyle;
  28. this.clusterSource = new Cluster({
  29. distance: 10,
  30. minDistance: 10,
  31. });
  32. this.mapRef = map
  33. this.curPoint = null
  34. this.curArea = null
  35. this.isUpdatePoint = null
  36. this.isUpdateArea = null
  37. this.treeClusterLayer = new KMap.VectorLayer("tree-cluster-home", 999, {
  38. minZoom: 15,
  39. source: this.clusterSource,
  40. style: (f) => this.getStyle(f)
  41. })
  42. this.addMapSingerClick(map.map);
  43. }
  44. getIconStyle(feature) {
  45. const color = feature.get("color")
  46. const noImg = feature.get("noImg")
  47. const activeCompare = feature.get('activeCompare')
  48. let style = new Style({
  49. image: new Icon({
  50. src: activeCompare ? require('@/assets/images/map/active-icon-small.png') : feature.get('icon'),
  51. // src: require(`@/assets/images/map/${feature.get('iconName')}-icon.png`),
  52. scale: activeCompare ? 0.5 : feature.get('scale'),
  53. })
  54. });
  55. let style2 = new Style({
  56. image: new Circle({
  57. radius: 6, // 半径
  58. stroke: new Stroke({ // 边界样式
  59. color: noImg ? 'transparent' : '#fff', // 边界颜色
  60. width: 2 // 边界宽度
  61. }),
  62. fill: new Fill({ // 填充样式
  63. color // 填充颜色
  64. })
  65. })
  66. });
  67. return color ? (activeCompare ? style : style2) : style
  68. }
  69. //多点的过滤方法
  70. manyFeatureFilter(features) {
  71. let res = features[0]
  72. if (features.length == 1) {
  73. return res
  74. }
  75. for (let item of features) {
  76. res = res.get('status') > item.get('status') && item.get('noImg') === 0 && item.get('wys') === 1 ? res : item
  77. }
  78. return res;
  79. }
  80. //得到点样式
  81. getStyle(feature) {
  82. feature = this.manyFeatureFilter(feature.get('features'))
  83. return this.getIconStyle(feature)
  84. }
  85. initData(farmId, regionId) {
  86. let that = this
  87. let selectAll = undefined
  88. if (regionId === 0) {
  89. selectAll = 1
  90. }
  91. const areaId = selectAll ? undefined : regionId
  92. VE_API.variety.pointList({ farmId, regionId: areaId, selectAll }).then(({ data }) => {
  93. this.pointArr = data
  94. let features = []
  95. for (let item of data) {
  96. item.iconName = 'defalut'
  97. that.getIcon(item)
  98. let point = newPoint(item);
  99. features.push(point)
  100. // console.log('item.dyImg',item.dyImg);
  101. }
  102. const source = new VectorSource({
  103. features: features,
  104. });
  105. that.clusterSource.setSource(source)
  106. // console.log('this.treeClusterLayer.layer.getSource()', that.treeClusterLayer.layer.getSource());
  107. // if (!that.treeClusterLayer || !that.treeClusterLayer.layer.getSource()) {
  108. // that.mapRef.addLayer(that.treeClusterLayer.layer)
  109. // }
  110. const layers = that.mapRef.map.getLayers().getArray();
  111. const exists = layers.includes(that.treeClusterLayer.layer);
  112. if (!exists) {
  113. that.mapRef.addLayer(that.treeClusterLayer.layer);
  114. }
  115. setTimeout(() => {
  116. that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] })
  117. }, 100)
  118. })
  119. }
  120. addMapSingerClick(kmap) {
  121. let that = this
  122. // 创建弹窗图层
  123. this.popup = new Overlay({
  124. element: document.getElementById('popup-file'),
  125. positioning: 'right-center',
  126. offset: [0, 0],
  127. });
  128. kmap.addOverlay(this.popup);
  129. // 点击地图弹窗的关闭-销毁dom
  130. // eventBus.on("map:destroyPopup", () => {
  131. // that.popup.setPosition(undefined)
  132. // })
  133. kmap.on("singleclick", (evt) => {
  134. let hasFeature = false
  135. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  136. if (layer instanceof VectorLayer && layer.get("name") === "tree-cluster-home") {
  137. hasFeature = true
  138. if (that.curPoint) {
  139. that.curPoint.set("iconName", "defalut");
  140. }
  141. const featureArr = feature.get("features")
  142. let fs = featureArr[0]
  143. for (let item of featureArr) {
  144. fs = fs.get('status') > item.get('status') ? fs : item
  145. }
  146. that.curPoint = fs
  147. if (that.isUpdatePoint) {
  148. fs.set("iconName", "active");
  149. }
  150. eventBus.emit("click:point", { farmId: fs.get("farmId"), sampleId: fs.get("sampleId"), data: fs.getProperties() })
  151. }
  152. })
  153. if (!hasFeature) {
  154. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  155. if (layer instanceof VectorLayer && layer.get("name") === "regionLayer") {
  156. hasFeature = false
  157. if (that.curArea) {
  158. that.curArea.set("bgName", "defalut");
  159. }
  160. that.curArea = feature
  161. if (that.isUpdateArea) {
  162. feature.set("bgName", "active");
  163. eventBus.emit("click:updateArea", { name: feature.get("id"), value: feature.get("blueZone") })
  164. } else {
  165. eventBus.emit("click:area", { name: feature.get("id"), value: feature.get("highYield") })
  166. }
  167. }
  168. })
  169. if (that.isCompare) {
  170. eventBus.emit("quitCompare")
  171. eventBus.emit("compareTree", false)
  172. }
  173. if (that.popup) {
  174. that.popup.setPosition(undefined)
  175. }
  176. }
  177. })
  178. }
  179. resetPoint() {
  180. this.isUpdatePoint = null
  181. console.log('this.curPoint', this.curPoint);
  182. if (this.curPoint) {
  183. // this.curPoint.set("iconName", "defalut");
  184. this.curPoint.set("activeCompare", false);
  185. }
  186. }
  187. updatePointStatus(e) {
  188. this.isUpdatePoint = e
  189. }
  190. updateAreaStatus(e) {
  191. this.isUpdateArea = e
  192. }
  193. // 切换点位数据
  194. getIcon(item) {
  195. // let imgSrc = require(`@/assets/images/map/${item.iconName}-icon.png`)
  196. let imgSrc = require('@/assets/images/map/status/status-zc.png')
  197. let scale = 0.8
  198. if (item.status == 1) {
  199. imgSrc = require('@/assets/images/map/status/status-szyc.png')
  200. }
  201. if (item.status == 2) {
  202. imgSrc = require('@/assets/images/map/status/status-bh.png')
  203. }
  204. if (item.status == 3) {
  205. imgSrc = require('@/assets/images/map/status/status-ch.png')
  206. }
  207. if (item.wys === '1') {
  208. scale = 0.3
  209. imgSrc = require('@/assets/images/map/status/wns.png')
  210. }
  211. // if (item.farmId === 90263) {
  212. // }
  213. if (item.noImg === 1) {
  214. imgSrc = require('@/assets/images/map/status/defalut-icon.png')
  215. scale = 0.3
  216. }
  217. if (item.nonghu === 1) {
  218. imgSrc = require('@/assets/images/map/status/nonghu-icon.png')
  219. scale = 0.3
  220. }
  221. item["icon"] = imgSrc
  222. item["scale"] = scale
  223. }
  224. reset(farm, region) {
  225. this.clearCluster()
  226. this.initData(farm.id, region.id)
  227. }
  228. // 清除聚合图层,解除绑定
  229. clearCluster() {
  230. if (this.treeClusterLayer && this.treeClusterLayer.layer.getSource().getSource()) {
  231. this.treeClusterLayer.layer.getSource().getSource().clear()
  232. }
  233. }
  234. }
  235. export default SamplePointLayer;