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 = `
${data.meta_info.dp_alert_info.key}
${data.meta_info.dp_alert_info.statement}
${data.meta_info.grow_alert_info.key}
${data.meta_info.grow_alert_info.statement}
${data.meta_info.nutrition_info.key}
${data.meta_info.nutrition_info.statement}
${data.meta_info.prescription_info.key}
${data.meta_info.prescription_info.statement}
`; document.getElementById('file-overview').innerHTML = `
品种
${fs.get("pz")}
冠幅表面积
${data.meta_info.crown}平方米
总枝条
${data.meta_info.branch_num}
树龄
${data.meta_info.age}年
`; document.getElementById('file-output').innerHTML = `
产量估计
${data.production_info.production}斤
高质果率
${data.production_info.quality.toFixed(0)}%
坐果率
${(data.production_info.cihua_ratio || data.production_info.zuoguo_ratio).toFixed(0)}%
`; document.getElementById('file-quality').innerHTML = `
通风率
${data.ecology_info.ventilation}%
透光率
${data.ecology_info.transmittance}%
病虫比例
${data.ecology_info.dp_situation}%
`; document.getElementById('tag-nh').style.display = fs.get("nonghu") == 1 ? "inline-block" : 'none' that.popup.setPosition(evt.coordinate) }) } else { eventBus.emit("click:point", { farmId: fs.get("farmId"), sampleId: fs.get("sampleId"), data: fs.getProperties() }) } } else { // fs.set("iconName", "active") fs.set("activeCompare", fs.get("activeCompare") ? false : true); if (fs.get("activeCompare")) { that.comparePointArr.push(fs) } else { that.comparePointArr = that.comparePointArr.filter(item => item.get("id") !== fs.get("id")) } if (that.comparePointArr.length > 2) { that.comparePointArr[0].set("activeCompare", false); that.comparePointArr.shift(); } // fs.set("icon", require('@/assets/images/map/active-icon-small.png')); eventBus.emit("clickToCompare:point", that.comparePointArr) } } }) if (!hasFeature) { kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) { if (layer instanceof VectorLayer && layer.get("name") === "regionLayer") { hasFeature = false if (that.curArea) { that.curArea.set("bgName", "defalut"); } that.curArea = feature if (that.isUpdateArea) { feature.set("bgName", "active"); eventBus.emit("click:updateArea", { name: feature.get("id"), value: feature.get("blueZone") }) } else { eventBus.emit("click:area", { name: feature.get("id"), value: feature.get("highYield") }) } } }) if (that.isCompare) { eventBus.emit("quitCompare") eventBus.emit("compareTree", false) } if (that.popup) { that.popup.setPosition(undefined) } } }) } resetPoint() { this.isUpdatePoint = null console.log('this.curPoint', this.curPoint); if (this.curPoint) { // this.curPoint.set("iconName", "defalut"); this.curPoint.set("activeCompare", false); } } updatePointStatus(e) { this.isUpdatePoint = e } updateAreaStatus(e) { this.isUpdateArea = e } // 切换点位数据 getIcon(item) { // let imgSrc = require(`@/assets/images/map/${item.iconName}-icon.png`) let imgSrc = require('@/assets/images/map/status/status-zc.png') let scale = 0.8 if (item.status == 1) { imgSrc = require('@/assets/images/map/status/status-szyc.png') } if (item.status == 2) { imgSrc = require('@/assets/images/map/status/status-bh.png') } if (item.status == 3) { imgSrc = require('@/assets/images/map/status/status-ch.png') } if (item.wys === '1') { scale = 0.3 imgSrc = require('@/assets/images/map/status/wns.png') } // if (item.farmId === 90263) { // } if (item.noImg === 1) { imgSrc = require('@/assets/images/map/status/defalut-icon.png') scale = 0.3 } if (item.nonghu === 1) { imgSrc = require('@/assets/images/map/status/nonghu-icon.png') scale = 0.3 } item["icon"] = imgSrc item["scale"] = scale } reset(farm, region) { this.clearCluster() this.initData(farm.id, region.id) } // 清除聚合图层,解除绑定 clearCluster() { if (this.treeClusterLayer && this.treeClusterLayer.layer.getSource().getSource()) { this.treeClusterLayer.layer.getSource().getSource().clear() } } } export default SamplePointLayer;