samplePointLayer.js 17 KB

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