import config from "@/api/config.js"; import * as KMap from "@/utils/ol-map/KMap"; import * as util from "@/common/ol_common.js"; import Point from "ol/geom/Point.js"; import Feature from "ol/Feature"; import VectorLayer from "ol/layer/Vector.js"; import WKT from "ol/format/WKT.js"; import ScaleLine from "ol/control/ScaleLine"; import { useRouter } from "vue-router"; import { unByKey } from "ol/Observable"; import Style from "ol/style/Style"; import Icon from "ol/style/Icon"; import { Cluster, Vector as VectorSource } from "ol/source"; import { Fill, Circle, Stroke } from "ol/style"; import { newPoint } from "@/utils/map"; import eventBus from "@/api/eventBus"; import Overlay from 'ol/Overlay' /** * @description 地图层对象 */ class SamplePointLayer { constructor(map, organId, regionId) { let that = this; this.farmId = organId this.regionId = regionId let vectorStyle = new KMap.VectorStyle(); this.vectorStyle = vectorStyle; this.clusterSource = new Cluster({ distance: 10, minDistance: 10, }); this.mapRef = map this.curPoint = null this.curArea = null this.isUpdatePoint = null this.isUpdateArea = null this.treeClusterLayer = new KMap.VectorLayer("tree-cluster", 999, { minZoom: 15, source: this.clusterSource, style: (f) => this.getStyle(f) }) // map.addLayer(this.treeClusterLayer.layer) this.addMapSingerClick(map.map); // 对比 this.isCompare = false this.comparePointArr = [] eventBus.off("compareTree") eventBus.on("compareTree", (val) => { that.isCompare = val if (val === false) { that.comparePointArr.map(fs => { fs.set("activeCompare", false) }); } }) // 果园档案 that.fileLegend = [] eventBus.off("change:mapPoint") eventBus.on("change:mapPoint", async (field) => { if (!this.fileLegend.length) { await VE_API.farm.fetchLegendList().then(({data}) => { this.fileLegend = data }) } const colorObj = that.fileLegend.find(item => item.code === field) that.togglePointType(null, colorObj, true) colorObj.list = colorObj.items eventBus.emit("changePointLegend", {colorObj}) }) that.blueRegionLayer = null that.pointType = "" that.pointArr = [] // 切换点位图标 eventBus.off("changePointType") eventBus.on("changePointType", ({ legend, colorObj }) => { console.log('legend, colorObj', legend, colorObj); that.togglePointType(legend, colorObj) }) } getIconStyle(feature) { const color = feature.get("color") const noImg = feature.get("noImg") const activeCompare = feature.get('activeCompare') let style = new Style({ image: new Icon({ src: activeCompare ? require('@/assets/images/map/active-icon-small.png') : feature.get('icon'), // src: require(`@/assets/images/map/${feature.get('iconName')}-icon.png`), scale: activeCompare ? 0.5 : feature.get('scale'), }) }); let style2 = new Style({ image: new Circle({ radius: 6, // 半径 stroke: new Stroke({ // 边界样式 color: noImg ? 'transparent' : '#fff', // 边界颜色 width: 2 // 边界宽度 }), fill: new Fill({ // 填充样式 color // 填充颜色 }) }) }); return color ? (activeCompare ? style : style2) : style } //多点的过滤方法 manyFeatureFilter(features) { let res = features[0] if (features.length == 1) { return res } for (let item of features) { res = res.get('status') > item.get('status') && item.get('noImg') === 0 && item.get('wys') === 1 ? res : item } return res; } //得到点样式 getStyle(feature) { feature = this.manyFeatureFilter(feature.get('features')) return this.getIconStyle(feature) } initData(farmId, regionId) { let that = this let selectAll = undefined if (regionId === 0) { selectAll = 1 } const areaId = selectAll ? undefined : regionId VE_API.variety.pointList({ farmId, regionId: areaId, selectAll }).then(({ data }) => { this.pointArr = data let features = [] for (let item of data) { item.iconName = 'defalut' that.getIcon(item) let point = newPoint(item); features.push(point) // console.log('item.dyImg',item.dyImg); } const source = new VectorSource({ features: features, }); that.clusterSource.setSource(source) const layers = that.mapRef.map.getLayers().getArray(); const exists = layers.includes(that.treeClusterLayer.layer); if (!exists) { that.mapRef.addLayer(that.treeClusterLayer.layer); } setTimeout(() => { that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] }) }, 100) }) } togglePointType(sampleData, colorObj, isFile) { console.log('toggle3PointType------------', sampleData, colorObj); let that = this that.clearCluster() if (!sampleData) { let features = [] for (let item of this.pointArr) { item.iconName = 'defalut' that.getIcon(item) if (isFile && colorObj) { const fieldVal = Number(item[colorObj.code]) const legendItem = colorObj.items.find((colorItem) => { return fieldVal >= colorItem.range[0] && fieldVal <= colorItem.range[1]; }) const color = legendItem ? legendItem.color : null; item.color = color } else { item.color = null } let point = newPoint(item); features.push(point) // console.log('item.dyImg',item.dyImg); } const source = new VectorSource({ features: features, }); that.clusterSource.setSource(source) setTimeout(() => { that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] }) }, 100) return } eventBus.emit("resetFileActive", colorObj.key || colorObj.name) // 创建一个映射表:geoHashSample -> color const geoHashToColorMap = {}; colorObj.list.forEach(legendItem => { const geoHashes = sampleData.obj[legendItem.val] || []; geoHashes.forEach(geoHash => { geoHashToColorMap[geoHash] = legendItem.color; }); }); // 3. 更新pointArr中的颜色 let features = [] this.pointArr.map(point => { if (point.geoHashSample in geoHashToColorMap) { point.iconName = 'defalut' point.color = geoHashToColorMap[point.geoHashSample] that.getIcon(point) let pointRes = newPoint(point); features.push(pointRes) } else { point.iconName = 'defalut' point.color = "#DDDDDD" that.getIcon(point) let pointRes = newPoint(point); features.push(pointRes) } }) const source = new VectorSource({ features: features, }); that.clusterSource.setSource(source) } toggleFilePoint(arr) { let that = this // 清除旧的 blueRegionLayer 图层 // that.clearCluster() if (that.blueRegionLayer) { that.blueRegionLayer.source.clear(); } if (arr && arr.length > 0) { let features = [] for (let item of arr) { item.iconName = 'defalut' that.getIcon(item) let point = newPoint(item); features.push(point) } const source = new VectorSource({ features: features, }); that.clusterSource.setSource(source) setTimeout(() => { that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] }) }, 100) } }; addMapSingerClick(kmap) { let that = this // 创建弹窗图层 this.popup = new Overlay({ element: document.getElementById('popup-file'), positioning: 'right-center', offset: [0, 0], }); kmap.addOverlay(this.popup); // 点击地图弹窗的关闭-销毁dom // eventBus.on("map:destroyPopup", () => { // that.popup.setPosition(undefined) // }) kmap.on("singleclick", (evt) => { let hasFeature = false kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) { if (layer instanceof VectorLayer && layer.get("name") === "tree-cluster") { hasFeature = true if (that.curPoint) { that.curPoint.set("iconName", "defalut"); } const featureArr = feature.get("features") let fs = featureArr[0] for (let item of featureArr) { fs = fs.get('status') > item.get('status') ? fs : item } that.curPoint = fs if (that.isUpdatePoint) { fs.set("iconName", "active"); } console.log('fs.getProperties()', fs.getProperties()); if (!that.isCompare) { const noImg = fs.get("noImg") if (noImg === 1) { VE_API.mini_farm.getSampleFiles({farmId: fs.get("farmId"), geoHashSample: fs.get('geoHashSample') }).then(({data}) => { // 无照片 document.getElementById('file-text').innerHTML = `