samplePointLayer.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 {newPoint} from "@/utils/map";
  15. import eventBus from "@/api/eventBus";
  16. /**
  17. * @description 地图层对象
  18. */
  19. class SamplePointLayer {
  20. constructor(map, organId, regionId) {
  21. let that = this;
  22. this.farmId = organId
  23. this.regionId = regionId
  24. let vectorStyle = new KMap.VectorStyle();
  25. this.vectorStyle = vectorStyle;
  26. this.clusterSource = new Cluster({
  27. distance: 15,
  28. minDistance: 60,
  29. });
  30. this.mapRef = map
  31. this.curPoint = null
  32. this.curArea = null
  33. this.isUpdatePoint = null
  34. this.isUpdateArea = null
  35. this.treeClusterLayer = new KMap.VectorLayer("tree-cluster",999,{
  36. minZoom:15,
  37. source:this.clusterSource,
  38. style:(f)=> this.getStyle(f)})
  39. this.yellowBlockLayer = new KMap.VectorLayer("yellow-block",999,{
  40. minZoom:15,
  41. // source:this.clusterSource,
  42. // source:"POINT(113.61396985128522 23.5859386716038)",
  43. style: () => {
  44. return new Style({
  45. image: new Icon({
  46. src: require("@/assets/images/map/yellow-block.png"),
  47. scale: 0.4,
  48. }),
  49. });
  50. },
  51. })
  52. map.addLayer(this.treeClusterLayer.layer)
  53. // map.addLayer(this.yellowBlockLayer.layer);
  54. let point = new Feature(new Point([113.61396985128522,23.5859386716038]));
  55. let point1 = new Feature(new Point([113.61390710255375 ,23.586379215663726]));
  56. let point2 = new Feature(new Point([113.61491218688275 ,23.58671519555776]));
  57. // this.yellowBlockLayer.addFeature(point);
  58. // this.yellowBlockLayer.addFeature(point1);
  59. // this.yellowBlockLayer.addFeature(point2);
  60. this.addMapSingerClick(map.map);
  61. this.isCompare = false
  62. eventBus.off("compareTree")
  63. eventBus.on("compareTree", (val) => {
  64. that.isCompare = val
  65. })
  66. eventBus.off("closeCompare")
  67. // eventBus.on("closeCompare", () => {
  68. // })
  69. }
  70. getIconStyle(feature){
  71. let style = new Style({
  72. image: new Icon({
  73. src: feature.get('icon'),
  74. // src: require(`@/assets/images/map/${feature.get('iconName')}-icon.png`),
  75. scale:feature.get('scale'),
  76. })
  77. });
  78. return style
  79. }
  80. //多点的过滤方法
  81. manyFeatureFilter(features){
  82. let res = features[0]
  83. if(features.length == 1){
  84. return res
  85. }
  86. for(let item of features){
  87. res = res.get('status') > item.get('status') ? res : item
  88. }
  89. return res;
  90. }
  91. //得到点样式
  92. getStyle(feature){
  93. feature = this.manyFeatureFilter(feature.get('features'))
  94. return this.getIconStyle(feature)
  95. }
  96. initData(farmId, regionId){
  97. let that = this
  98. let selectAll = undefined
  99. if(regionId===0){
  100. selectAll = 1
  101. }
  102. VE_API.variety.pointList({farmId,regionId,selectAll}).then(({data})=>{
  103. let features = []
  104. for(let item of data){
  105. item.iconName='defalut'
  106. that.getIcon(item)
  107. let point = newPoint(item);
  108. features.push(point)
  109. }
  110. const source = new VectorSource({
  111. features: features,
  112. });
  113. that.clusterSource.setSource(source)
  114. setTimeout(()=>{
  115. that.mapRef.fit(that.clusterSource.source.getExtent(), {padding:[100,100,100,100]})
  116. },100)
  117. })
  118. }
  119. addMapSingerClick(kmap){
  120. let that = this
  121. kmap.on("singleclick", (evt) => {
  122. let hasFeature = false
  123. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  124. if (layer instanceof VectorLayer && layer.get("name") === "tree-cluster") {
  125. hasFeature = true
  126. if(that.curPoint){
  127. that.curPoint.set("iconName", "defalut");
  128. }
  129. const featureArr = feature.get("features")
  130. const fs = featureArr[0]
  131. that.curPoint = fs
  132. if(that.isUpdatePoint){
  133. fs.set("iconName", "active");
  134. }
  135. console.log('that.isCompare', that.isCompare);
  136. if (!that.isCompare) {
  137. eventBus.emit("click:point",{farmId:fs.get("farmId"),sampleId:fs.get("sampleId"), data: fs.getProperties()})
  138. } else {
  139. fs.set("icon", require('@/assets/images/map/active-icon-small.png'));
  140. fs.set("activeCompare", true);
  141. eventBus.emit("clickToCompare:point",{farmId:fs.get("farmId"),sampleId:fs.get("sampleId"), data: fs.getProperties()})
  142. }
  143. }
  144. if (layer instanceof VectorLayer && layer.get("name") === "yellow-block") {
  145. hasFeature = true
  146. eventBus.emit("click:yellowBlock",feature.get("geometry").flatCoordinates[0])
  147. }
  148. })
  149. if(!hasFeature){
  150. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  151. if (layer instanceof VectorLayer && layer.get("name") === "regionLayer") {
  152. hasFeature = false
  153. if(that.curArea){
  154. that.curArea.set("bgName", "defalut");
  155. }
  156. that.curArea = feature
  157. if(that.isUpdateArea){
  158. feature.set("bgName", "active");
  159. eventBus.emit("click:updateArea",{name:feature.get("id"),value:feature.get("blueZone")})
  160. }else{
  161. eventBus.emit("click:area",{name:feature.get("id"),value:feature.get("highYield")})
  162. }
  163. }
  164. })
  165. }
  166. })
  167. }
  168. resetPoint(){
  169. this.isUpdatePoint = null
  170. if(this.curPoint){
  171. this.curPoint.set("iconName", "defalut");
  172. }
  173. }
  174. updatePointStatus(e){
  175. this.isUpdatePoint = e
  176. }
  177. updateAreaStatus(e){
  178. this.isUpdateArea = e
  179. }
  180. getIcon(item){
  181. // let imgSrc = require(`@/assets/images/map/${item.iconName}-icon.png`)
  182. let imgSrc = require('@/assets/images/map/status/status-zc.png')
  183. let scale = 0.8
  184. if(item.status == 1){
  185. imgSrc = require('@/assets/images/map/status/status-szyc.png')
  186. }
  187. if(item.status == 2){
  188. imgSrc = require('@/assets/images/map/status/status-bh.png')
  189. }
  190. if(item.status == 3){
  191. imgSrc = require('@/assets/images/map/status/status-ch.png')
  192. }
  193. if(item.status == 101){
  194. imgSrc = require('@/assets/images/map/status/status-ch.png')
  195. }
  196. console.log('item', item.iconName);
  197. item["icon"] = imgSrc
  198. item["scale"] = scale
  199. }
  200. reset(farm, region){
  201. console.log('farm',farm);
  202. this.clearCluster()
  203. this.initData(farm.id, region.id)
  204. }
  205. // 清除聚合图层,解除绑定
  206. clearCluster() {
  207. if (this.treeClusterLayer) {
  208. this.treeClusterLayer.layer.getSource().getSource().clear()
  209. }
  210. }
  211. // 对比树
  212. toggleCompareTree() {
  213. let that = this
  214. that.isCompare = true
  215. }
  216. }
  217. export default SamplePointLayer;