samplePointLayer.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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", 999, {
  38. minZoom: 15,
  39. source: this.clusterSource,
  40. style: (f) => this.getStyle(f)
  41. })
  42. this.yellowBlockLayer = new KMap.VectorLayer("yellow-block", 999, {
  43. minZoom: 15,
  44. // source:this.clusterSource,
  45. // source:"POINT(113.61396985128522 23.5859386716038)",
  46. style: () => {
  47. return new Style({
  48. image: new Icon({
  49. src: require("@/assets/images/map/yellow-block.png"),
  50. scale: 0.4,
  51. }),
  52. });
  53. },
  54. })
  55. map.addLayer(this.treeClusterLayer.layer)
  56. // map.addLayer(this.yellowBlockLayer.layer);
  57. let point = new Feature(new Point([113.61396985128522, 23.5859386716038]));
  58. let point1 = new Feature(new Point([113.61390710255375, 23.586379215663726]));
  59. let point2 = new Feature(new Point([113.61491218688275, 23.58671519555776]));
  60. // this.yellowBlockLayer.addFeature(point);
  61. // this.yellowBlockLayer.addFeature(point1);
  62. // this.yellowBlockLayer.addFeature(point2);
  63. this.addMapSingerClick(map.map);
  64. // 对比
  65. this.isCompare = false
  66. this.comparePointArr = []
  67. eventBus.off("compareTree")
  68. eventBus.on("compareTree", (val) => {
  69. that.isCompare = val
  70. if (val === false) {
  71. that.comparePointArr.map(fs => {
  72. fs.set("activeCompare", false)
  73. });
  74. }
  75. })
  76. // 果园档案
  77. that.fileLegend = []
  78. eventBus.off("change:mapPoint")
  79. eventBus.on("change:mapPoint", async (field) => {
  80. if (!this.fileLegend.length) {
  81. await VE_API.farm.fetchLegendList().then(({data}) => {
  82. this.fileLegend = data
  83. })
  84. }
  85. const colorObj = that.fileLegend.find(item => item.code === field)
  86. that.togglePointType(null, colorObj, true)
  87. colorObj.list = colorObj.items
  88. eventBus.emit("changePointLegend", {colorObj})
  89. })
  90. that.blueRegionLayer = null
  91. that.pointType = ""
  92. that.pointArr = []
  93. // 切换点位图标
  94. eventBus.off("changePointType")
  95. eventBus.on("changePointType", ({ legend, colorObj }) => {
  96. console.log('legend, colorObj', legend, colorObj);
  97. that.togglePointType(legend, colorObj)
  98. })
  99. }
  100. getIconStyle(feature) {
  101. const color = feature.get("color")
  102. const noImg = feature.get("noImg")
  103. const activeCompare = feature.get('activeCompare')
  104. let style = new Style({
  105. image: new Icon({
  106. src: activeCompare ? require('@/assets/images/map/active-icon-small.png') : feature.get('icon'),
  107. // src: require(`@/assets/images/map/${feature.get('iconName')}-icon.png`),
  108. scale: activeCompare ? 0.5 : feature.get('scale'),
  109. })
  110. });
  111. let style2 = new Style({
  112. image: new Circle({
  113. radius: 6, // 半径
  114. stroke: new Stroke({ // 边界样式
  115. color: noImg ? 'transparent' : '#fff', // 边界颜色
  116. width: 2 // 边界宽度
  117. }),
  118. fill: new Fill({ // 填充样式
  119. color // 填充颜色
  120. })
  121. })
  122. });
  123. return color ? (activeCompare ? style : style2) : style
  124. }
  125. //多点的过滤方法
  126. manyFeatureFilter(features) {
  127. let res = features[0]
  128. if (features.length == 1) {
  129. return res
  130. }
  131. for (let item of features) {
  132. res = res.get('status') > item.get('status') && item.get('noImg') === 0 && item.get('wys') === 1 ? res : item
  133. }
  134. return res;
  135. }
  136. //得到点样式
  137. getStyle(feature) {
  138. feature = this.manyFeatureFilter(feature.get('features'))
  139. return this.getIconStyle(feature)
  140. }
  141. initData(farmId, regionId) {
  142. let that = this
  143. let selectAll = undefined
  144. if (regionId === 0) {
  145. selectAll = 1
  146. }
  147. const areaId = selectAll ? undefined : regionId
  148. VE_API.variety.pointList({ farmId, regionId: areaId, selectAll }).then(({ data }) => {
  149. this.pointArr = data
  150. let features = []
  151. for (let item of data) {
  152. item.iconName = 'defalut'
  153. that.getIcon(item)
  154. let point = newPoint(item);
  155. features.push(point)
  156. // console.log('item.dyImg',item.dyImg);
  157. }
  158. const source = new VectorSource({
  159. features: features,
  160. });
  161. that.clusterSource.setSource(source)
  162. setTimeout(() => {
  163. that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] })
  164. }, 100)
  165. })
  166. }
  167. togglePointType(sampleData, colorObj, isFile) {
  168. console.log('toggle3PointType------------', sampleData, colorObj);
  169. let that = this
  170. that.clearCluster()
  171. if (!sampleData) {
  172. let features = []
  173. for (let item of this.pointArr) {
  174. item.iconName = 'defalut'
  175. that.getIcon(item)
  176. if (isFile && colorObj) {
  177. const fieldVal = Number(item[colorObj.code])
  178. const legendItem = colorObj.items.find((colorItem) => {
  179. return fieldVal >= colorItem.range[0] && fieldVal <= colorItem.range[1];
  180. })
  181. const color = legendItem ? legendItem.color : null;
  182. item.color = color
  183. } else {
  184. item.color = null
  185. }
  186. let point = newPoint(item);
  187. features.push(point)
  188. // console.log('item.dyImg',item.dyImg);
  189. }
  190. const source = new VectorSource({
  191. features: features,
  192. });
  193. that.clusterSource.setSource(source)
  194. setTimeout(() => {
  195. that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] })
  196. }, 100)
  197. return
  198. }
  199. // 遍历 sampleData
  200. Object.entries(sampleData.obj).forEach(([key, geoHashSample]) => {
  201. // 根据 key 找到 legend 中的颜色
  202. const legendItem = colorObj.list.find(item => item.val === key);
  203. const color = legendItem ? legendItem.color : null;
  204. if (color && geoHashSample.length > 0) {
  205. let features = []
  206. this.pointArr.forEach(point => {
  207. if (geoHashSample.includes(point.geoHashSample)) {
  208. point.iconName = 'defalut'
  209. point.color = color
  210. that.getIcon(point)
  211. let pointRes = newPoint(point);
  212. features.push(pointRes)
  213. } else {
  214. point.iconName = 'defalut'
  215. point.color = "#DDDDDD"
  216. that.getIcon(point)
  217. let pointRes = newPoint(point);
  218. features.push(pointRes)
  219. }
  220. });
  221. const source = new VectorSource({
  222. features: features,
  223. });
  224. that.clusterSource.setSource(source)
  225. // setTimeout(() => {
  226. // that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] })
  227. // }, 100)
  228. }
  229. });
  230. }
  231. toggleFilePoint(arr) {
  232. let that = this
  233. // 清除旧的 blueRegionLayer 图层
  234. // that.clearCluster()
  235. if (that.blueRegionLayer) {
  236. that.blueRegionLayer.source.clear();
  237. }
  238. if (arr && arr.length > 0) {
  239. let features = []
  240. for (let item of arr) {
  241. item.iconName = 'defalut'
  242. that.getIcon(item)
  243. let point = newPoint(item);
  244. features.push(point)
  245. }
  246. const source = new VectorSource({
  247. features: features,
  248. });
  249. that.clusterSource.setSource(source)
  250. setTimeout(() => {
  251. that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] })
  252. }, 100)
  253. }
  254. };
  255. addMapSingerClick(kmap) {
  256. let that = this
  257. // 创建弹窗图层
  258. this.popup = new Overlay({
  259. element: document.getElementById('popup-file'),
  260. positioning: 'right-center',
  261. offset: [0, 0],
  262. });
  263. kmap.addOverlay(this.popup);
  264. // 点击地图弹窗的关闭-销毁dom
  265. // eventBus.on("map:destroyPopup", () => {
  266. // that.popup.setPosition(undefined)
  267. // })
  268. kmap.on("singleclick", (evt) => {
  269. let hasFeature = false
  270. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  271. if (layer instanceof VectorLayer && layer.get("name") === "tree-cluster") {
  272. hasFeature = true
  273. if (that.curPoint) {
  274. that.curPoint.set("iconName", "defalut");
  275. }
  276. const featureArr = feature.get("features")
  277. let fs = featureArr[0]
  278. for (let item of featureArr) {
  279. fs = fs.get('status') > item.get('status') ? fs : item
  280. }
  281. that.curPoint = fs
  282. if (that.isUpdatePoint) {
  283. fs.set("iconName", "active");
  284. }
  285. console.log('fs.getProperties()', fs.getProperties());
  286. if (!that.isCompare) {
  287. const noImg = fs.get("noImg")
  288. if (noImg === 1) {
  289. VE_API.mini_farm.getSampleFiles({ geoHashSample: fs.get('geoHashSample') }).then(({data}) => {
  290. // 无照片
  291. document.getElementById('file-text').innerHTML = `
  292. <div class="list-item">
  293. <div class="list-name">
  294. <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
  295. ${data.meta_info.dp_alert_info.key}
  296. </div>
  297. ${data.meta_info.dp_alert_info.statement}
  298. </div>
  299. <div class="list-item">
  300. <div class="list-name">
  301. <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
  302. ${data.meta_info.grow_alert_info.key}
  303. </div>
  304. ${data.meta_info.grow_alert_info.statement}
  305. </div>
  306. <div class="list-item">
  307. <div class="list-name">
  308. <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
  309. ${data.meta_info.nutrition_info.key}
  310. </div>
  311. ${data.meta_info.nutrition_info.statement}
  312. </div>
  313. <div class="list-item">
  314. <div class="list-name">
  315. <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
  316. ${data.meta_info.prescription_info.key}
  317. </div>
  318. ${data.meta_info.prescription_info.statement}
  319. </div>
  320. `;
  321. document.getElementById('file-overview').innerHTML = `
  322. <div class="base-item">
  323. <span class="label">品种</span>
  324. <div class="value">${fs.get("pz")}</div>
  325. </div>
  326. <div class="base-item">
  327. <span class="label">冠幅表面积</span>
  328. <div class="value">${data.meta_info.crown}平方米</div>
  329. </div>
  330. <div class="base-item">
  331. <span class="label">总枝条</span>
  332. <div class="value">${data.meta_info.branch_num}</div>
  333. </div>
  334. <div class="base-item">
  335. <span class="label">树龄</span>
  336. <div class="value">${data.meta_info.age}年</div>
  337. </div>
  338. `;
  339. document.getElementById('file-output').innerHTML = `
  340. <div class="box-item">
  341. <div class="item-name">产量估计</div>
  342. <div class="item-val">${data.production_info.production}斤</div>
  343. </div>
  344. <div class="box-item">
  345. <div class="item-name">高质果率</div>
  346. <div class="item-val">${data.production_info.quality.toFixed(0)}%</div>
  347. </div>
  348. <div class="box-item">
  349. <div class="item-name">雌花比例</div>
  350. <div class="item-val">${data.production_info.cihua_ratio.toFixed(0)}%</div>
  351. </div>
  352. `;
  353. document.getElementById('file-quality').innerHTML = `
  354. <div class="box-item">
  355. <div class="item-name">通风率</div>
  356. <div class="item-val">${data.ecology_info.ventilation}%</div>
  357. </div>
  358. <div class="box-item">
  359. <div class="item-name">透光率</div>
  360. <div class="item-val">${data.ecology_info.transmittance}%</div>
  361. </div>
  362. <div class="box-item">
  363. <div class="item-name">病虫比例</div>
  364. <div class="item-val">${data.ecology_info.dp_situation}%</div>
  365. </div>
  366. `;
  367. document.getElementById('tag-nh').style.display = fs.get("nonghu") == 1 ? "inline-block" : 'none'
  368. that.popup.setPosition(evt.coordinate)
  369. })
  370. } else {
  371. eventBus.emit("click:point", { farmId: fs.get("farmId"), sampleId: fs.get("sampleId"), data: fs.getProperties() })
  372. }
  373. } else {
  374. // fs.set("iconName", "active")
  375. fs.set("activeCompare", fs.get("activeCompare") ? false : true);
  376. if (fs.get("activeCompare")) {
  377. that.comparePointArr.push(fs)
  378. } else {
  379. that.comparePointArr = that.comparePointArr.filter(item => item.get("id") !== fs.get("id"))
  380. }
  381. if (that.comparePointArr.length > 2) {
  382. that.comparePointArr[0].set("activeCompare", false);
  383. that.comparePointArr.shift();
  384. }
  385. // fs.set("icon", require('@/assets/images/map/active-icon-small.png'));
  386. eventBus.emit("clickToCompare:point", that.comparePointArr)
  387. }
  388. }
  389. if (layer instanceof VectorLayer && layer.get("name") === "yellow-block") {
  390. hasFeature = true
  391. eventBus.emit("click:yellowBlock", feature.get("geometry").flatCoordinates[0])
  392. }
  393. })
  394. if (!hasFeature) {
  395. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  396. if (layer instanceof VectorLayer && layer.get("name") === "regionLayer") {
  397. hasFeature = false
  398. if (that.curArea) {
  399. that.curArea.set("bgName", "defalut");
  400. }
  401. that.curArea = feature
  402. if (that.isUpdateArea) {
  403. feature.set("bgName", "active");
  404. eventBus.emit("click:updateArea", { name: feature.get("id"), value: feature.get("blueZone") })
  405. } else {
  406. eventBus.emit("click:area", { name: feature.get("id"), value: feature.get("highYield") })
  407. }
  408. }
  409. })
  410. if (that.isCompare) {
  411. eventBus.emit("quitCompare")
  412. eventBus.emit("compareTree", false)
  413. }
  414. if (that.popup) {
  415. that.popup.setPosition(undefined)
  416. }
  417. }
  418. })
  419. }
  420. resetPoint() {
  421. this.isUpdatePoint = null
  422. console.log('this.curPoint', this.curPoint);
  423. if (this.curPoint) {
  424. // this.curPoint.set("iconName", "defalut");
  425. this.curPoint.set("activeCompare", false);
  426. }
  427. }
  428. updatePointStatus(e) {
  429. this.isUpdatePoint = e
  430. }
  431. updateAreaStatus(e) {
  432. this.isUpdateArea = e
  433. }
  434. // 切换点位数据
  435. getIcon(item) {
  436. // let imgSrc = require(`@/assets/images/map/${item.iconName}-icon.png`)
  437. let imgSrc = require('@/assets/images/map/status/status-zc.png')
  438. let scale = 0.8
  439. if (item.status == 1) {
  440. imgSrc = require('@/assets/images/map/status/status-szyc.png')
  441. }
  442. if (item.status == 2) {
  443. imgSrc = require('@/assets/images/map/status/status-bh.png')
  444. }
  445. if (item.status == 3) {
  446. imgSrc = require('@/assets/images/map/status/status-ch.png')
  447. }
  448. if (item.wys === '1') {
  449. scale = 0.3
  450. imgSrc = require('@/assets/images/map/status/wns.png')
  451. }
  452. // if (item.farmId === 90263) {
  453. // }
  454. if (item.noImg === 1) {
  455. imgSrc = require('@/assets/images/map/status/defalut-icon.png')
  456. scale = 0.3
  457. }
  458. item["icon"] = imgSrc
  459. item["scale"] = scale
  460. }
  461. reset(farm, region) {
  462. this.clearCluster()
  463. this.initData(farm.id, region.id)
  464. }
  465. // 清除聚合图层,解除绑定
  466. clearCluster() {
  467. if (this.treeClusterLayer && this.treeClusterLayer.layer.getSource().getSource()) {
  468. this.treeClusterLayer.layer.getSource().getSource().clear()
  469. }
  470. }
  471. }
  472. export default SamplePointLayer;