| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- 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 = `
- <div class="list-item">
- <div class="list-name">
- <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
- ${data.meta_info.dp_alert_info.key}
- </div>
- ${data.meta_info.dp_alert_info.statement}
- </div>
- <div class="list-item">
- <div class="list-name">
- <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
- ${data.meta_info.grow_alert_info.key}
- </div>
- ${data.meta_info.grow_alert_info.statement}
- </div>
- <div class="list-item">
- <div class="list-name">
- <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
- ${data.meta_info.nutrition_info.key}
- </div>
- ${data.meta_info.nutrition_info.statement}
- </div>
- <div class="list-item">
- <div class="list-name">
- <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
- ${data.meta_info.prescription_info.key}
- </div>
- ${data.meta_info.prescription_info.statement}
- </div>
- `;
- document.getElementById('file-overview').innerHTML = `
- <div class="base-item">
- <span class="label">品种</span>
- <div class="value">${fs.get("pz")}</div>
- </div>
- <div class="base-item">
- <span class="label">冠幅表面积</span>
- <div class="value">${data.meta_info.crown}平方米</div>
- </div>
- <div class="base-item">
- <span class="label">总枝条</span>
- <div class="value">${data.meta_info.branch_num}</div>
- </div>
- <div class="base-item">
- <span class="label">树龄</span>
- <div class="value">${data.meta_info.age}年</div>
- </div>
- `;
- document.getElementById('file-output').innerHTML = `
- <div class="box-item">
- <div class="item-name">产量估计</div>
- <div class="item-val">${data.production_info.production}斤</div>
- </div>
- <div class="box-item">
- <div class="item-name">高质果率</div>
- <div class="item-val">${data.production_info.quality.toFixed(0)}%</div>
- </div>
- <div class="box-item">
- <div class="item-name">坐果率</div>
- <div class="item-val">${(data.production_info.cihua_ratio || data.production_info.zuoguo_ratio).toFixed(0)}%</div>
- </div>
- `;
- document.getElementById('file-quality').innerHTML = `
- <div class="box-item">
- <div class="item-name">通风率</div>
- <div class="item-val">${data.ecology_info.ventilation}%</div>
- </div>
- <div class="box-item">
- <div class="item-name">透光率</div>
- <div class="item-val">${data.ecology_info.transmittance}%</div>
- </div>
- <div class="box-item">
- <div class="item-name">病虫比例</div>
- <div class="item-val">${data.ecology_info.dp_situation}%</div>
- </div>
- `;
- 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;
|