samplePointLayer.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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-home", 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. const layers = that.mapRef.map.getLayers().getArray();
  142. const exists = layers.includes(that.treeClusterLayer.layer);
  143. if (!exists) {
  144. that.mapRef.addLayer(that.treeClusterLayer.layer);
  145. }
  146. setTimeout(() => {
  147. that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] })
  148. }, 100)
  149. })
  150. }
  151. togglePointType(sampleData, colorObj, isFile) {
  152. console.log('toggle3PointType------------', sampleData, colorObj);
  153. let that = this
  154. that.clearCluster()
  155. if (!sampleData) {
  156. let features = []
  157. for (let item of this.pointArr) {
  158. item.iconName = 'defalut'
  159. that.getIcon(item)
  160. if (isFile && colorObj) {
  161. const fieldVal = Number(item[colorObj.code])
  162. const legendItem = colorObj.items.find((colorItem) => {
  163. return fieldVal >= colorItem.range[0] && fieldVal <= colorItem.range[1];
  164. })
  165. const color = legendItem ? legendItem.color : null;
  166. item.color = color
  167. } else {
  168. item.color = null
  169. }
  170. let point = newPoint(item);
  171. features.push(point)
  172. // console.log('item.dyImg',item.dyImg);
  173. }
  174. const source = new VectorSource({
  175. features: features,
  176. });
  177. that.clusterSource.setSource(source)
  178. setTimeout(() => {
  179. that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] })
  180. }, 100)
  181. return
  182. }
  183. eventBus.emit("resetFileActive", colorObj.key || colorObj.name)
  184. // 遍历 sampleData
  185. Object.entries(sampleData.obj).forEach(([key, geoHashSample]) => {
  186. // 根据 key 找到 legend 中的颜色
  187. const legendItem = colorObj.list.find(item => item.val === key);
  188. const color = legendItem ? legendItem.color : null;
  189. if (color && geoHashSample.length > 0) {
  190. let features = []
  191. this.pointArr.forEach(point => {
  192. if (geoHashSample.includes(point.geoHashSample)) {
  193. point.iconName = 'defalut'
  194. point.color = color
  195. that.getIcon(point)
  196. let pointRes = newPoint(point);
  197. features.push(pointRes)
  198. } else {
  199. point.iconName = 'defalut'
  200. point.color = "#DDDDDD"
  201. that.getIcon(point)
  202. let pointRes = newPoint(point);
  203. features.push(pointRes)
  204. }
  205. });
  206. const source = new VectorSource({
  207. features: features,
  208. });
  209. that.clusterSource.setSource(source)
  210. // setTimeout(() => {
  211. // that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] })
  212. // }, 100)
  213. }
  214. });
  215. }
  216. toggleFilePoint(arr) {
  217. let that = this
  218. // 清除旧的 blueRegionLayer 图层
  219. // that.clearCluster()
  220. if (that.blueRegionLayer) {
  221. that.blueRegionLayer.source.clear();
  222. }
  223. if (arr && arr.length > 0) {
  224. let features = []
  225. for (let item of arr) {
  226. item.iconName = 'defalut'
  227. that.getIcon(item)
  228. let point = newPoint(item);
  229. features.push(point)
  230. }
  231. const source = new VectorSource({
  232. features: features,
  233. });
  234. that.clusterSource.setSource(source)
  235. setTimeout(() => {
  236. that.mapRef.fit(that.clusterSource.source.getExtent(), { padding: [100, 100, 100, 100] })
  237. }, 100)
  238. }
  239. };
  240. addMapSingerClick(kmap) {
  241. let that = this
  242. // 创建弹窗图层
  243. this.popup = new Overlay({
  244. element: document.getElementById('popup-file'),
  245. positioning: 'right-center',
  246. offset: [0, 0],
  247. });
  248. kmap.addOverlay(this.popup);
  249. // 点击地图弹窗的关闭-销毁dom
  250. // eventBus.on("map:destroyPopup", () => {
  251. // that.popup.setPosition(undefined)
  252. // })
  253. kmap.on("singleclick", (evt) => {
  254. let hasFeature = false
  255. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  256. if (layer instanceof VectorLayer && layer.get("name") === "tree-cluster-home") {
  257. hasFeature = true
  258. if (that.curPoint) {
  259. that.curPoint.set("iconName", "defalut");
  260. }
  261. const featureArr = feature.get("features")
  262. let fs = featureArr[0]
  263. for (let item of featureArr) {
  264. fs = fs.get('status') > item.get('status') ? fs : item
  265. }
  266. that.curPoint = fs
  267. if (that.isUpdatePoint) {
  268. fs.set("iconName", "active");
  269. }
  270. console.log('fs.getProperties()', fs.getProperties());
  271. if (!that.isCompare) {
  272. const noImg = fs.get("noImg")
  273. if (noImg === 1) {
  274. VE_API.mini_farm.getSampleFiles({ geoHashSample: fs.get('geoHashSample') }).then(({data}) => {
  275. // 无照片
  276. document.getElementById('file-text').innerHTML = `
  277. <div class="list-item">
  278. <div class="list-name">
  279. <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
  280. ${data.meta_info.dp_alert_info.key}
  281. </div>
  282. ${data.meta_info.dp_alert_info.statement}
  283. </div>
  284. <div class="list-item">
  285. <div class="list-name">
  286. <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
  287. ${data.meta_info.grow_alert_info.key}
  288. </div>
  289. ${data.meta_info.grow_alert_info.statement}
  290. </div>
  291. <div class="list-item">
  292. <div class="list-name">
  293. <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
  294. ${data.meta_info.nutrition_info.key}
  295. </div>
  296. ${data.meta_info.nutrition_info.statement}
  297. </div>
  298. <div class="list-item">
  299. <div class="list-name">
  300. <img src="${require('@/assets/images/common/title-icon.png')}" alt="" />
  301. ${data.meta_info.prescription_info.key}
  302. </div>
  303. ${data.meta_info.prescription_info.statement}
  304. </div>
  305. `;
  306. document.getElementById('file-overview').innerHTML = `
  307. <div class="base-item">
  308. <span class="label">品种</span>
  309. <div class="value">${fs.get("pz")}</div>
  310. </div>
  311. <div class="base-item">
  312. <span class="label">冠幅表面积</span>
  313. <div class="value">${data.meta_info.crown}平方米</div>
  314. </div>
  315. <div class="base-item">
  316. <span class="label">总枝条</span>
  317. <div class="value">${data.meta_info.branch_num}</div>
  318. </div>
  319. <div class="base-item">
  320. <span class="label">树龄</span>
  321. <div class="value">${data.meta_info.age}年</div>
  322. </div>
  323. `;
  324. document.getElementById('file-output').innerHTML = `
  325. <div class="box-item">
  326. <div class="item-name">产量估计</div>
  327. <div class="item-val">${data.production_info.production}斤</div>
  328. </div>
  329. <div class="box-item">
  330. <div class="item-name">高质果率</div>
  331. <div class="item-val">${data.production_info.quality.toFixed(0)}%</div>
  332. </div>
  333. <div class="box-item">
  334. <div class="item-name">雌花比例</div>
  335. <div class="item-val">${data.production_info.cihua_ratio.toFixed(0)}%</div>
  336. </div>
  337. `;
  338. document.getElementById('file-quality').innerHTML = `
  339. <div class="box-item">
  340. <div class="item-name">通风率</div>
  341. <div class="item-val">${data.ecology_info.ventilation}%</div>
  342. </div>
  343. <div class="box-item">
  344. <div class="item-name">透光率</div>
  345. <div class="item-val">${data.ecology_info.transmittance}%</div>
  346. </div>
  347. <div class="box-item">
  348. <div class="item-name">病虫比例</div>
  349. <div class="item-val">${data.ecology_info.dp_situation}%</div>
  350. </div>
  351. `;
  352. document.getElementById('tag-nh').style.display = fs.get("nonghu") == 1 ? "inline-block" : 'none'
  353. that.popup.setPosition(evt.coordinate)
  354. })
  355. } else {
  356. eventBus.emit("click:point", { farmId: fs.get("farmId"), sampleId: fs.get("sampleId"), data: fs.getProperties() })
  357. }
  358. } else {
  359. // fs.set("iconName", "active")
  360. fs.set("activeCompare", fs.get("activeCompare") ? false : true);
  361. if (fs.get("activeCompare")) {
  362. that.comparePointArr.push(fs)
  363. } else {
  364. that.comparePointArr = that.comparePointArr.filter(item => item.get("id") !== fs.get("id"))
  365. }
  366. if (that.comparePointArr.length > 2) {
  367. that.comparePointArr[0].set("activeCompare", false);
  368. that.comparePointArr.shift();
  369. }
  370. // fs.set("icon", require('@/assets/images/map/active-icon-small.png'));
  371. eventBus.emit("clickToCompare:point", that.comparePointArr)
  372. }
  373. }
  374. if (layer instanceof VectorLayer && layer.get("name") === "yellow-block") {
  375. hasFeature = true
  376. eventBus.emit("click:yellowBlock", feature.get("geometry").flatCoordinates[0])
  377. }
  378. })
  379. if (!hasFeature) {
  380. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  381. if (layer instanceof VectorLayer && layer.get("name") === "regionLayer") {
  382. hasFeature = false
  383. if (that.curArea) {
  384. that.curArea.set("bgName", "defalut");
  385. }
  386. that.curArea = feature
  387. if (that.isUpdateArea) {
  388. feature.set("bgName", "active");
  389. eventBus.emit("click:updateArea", { name: feature.get("id"), value: feature.get("blueZone") })
  390. } else {
  391. eventBus.emit("click:area", { name: feature.get("id"), value: feature.get("highYield") })
  392. }
  393. }
  394. })
  395. if (that.isCompare) {
  396. eventBus.emit("quitCompare")
  397. eventBus.emit("compareTree", false)
  398. }
  399. if (that.popup) {
  400. that.popup.setPosition(undefined)
  401. }
  402. }
  403. })
  404. }
  405. resetPoint() {
  406. this.isUpdatePoint = null
  407. console.log('this.curPoint', this.curPoint);
  408. if (this.curPoint) {
  409. // this.curPoint.set("iconName", "defalut");
  410. this.curPoint.set("activeCompare", false);
  411. }
  412. }
  413. updatePointStatus(e) {
  414. this.isUpdatePoint = e
  415. }
  416. updateAreaStatus(e) {
  417. this.isUpdateArea = e
  418. }
  419. // 切换点位数据
  420. getIcon(item) {
  421. // let imgSrc = require(`@/assets/images/map/${item.iconName}-icon.png`)
  422. let imgSrc = require('@/assets/images/map/status/status-zc.png')
  423. let scale = 0.8
  424. if (item.status == 1) {
  425. imgSrc = require('@/assets/images/map/status/status-szyc.png')
  426. }
  427. if (item.status == 2) {
  428. imgSrc = require('@/assets/images/map/status/status-bh.png')
  429. }
  430. if (item.status == 3) {
  431. imgSrc = require('@/assets/images/map/status/status-ch.png')
  432. }
  433. if (item.wys === '1') {
  434. scale = 0.3
  435. imgSrc = require('@/assets/images/map/status/wns.png')
  436. }
  437. // if (item.farmId === 90263) {
  438. // }
  439. if (item.noImg === 1) {
  440. imgSrc = require('@/assets/images/map/status/defalut-icon.png')
  441. scale = 0.3
  442. }
  443. if (item.nonghu === 1) {
  444. imgSrc = require('@/assets/images/map/status/nonghu-icon.png')
  445. scale = 0.3
  446. }
  447. item["icon"] = imgSrc
  448. item["scale"] = scale
  449. }
  450. reset(farm, region) {
  451. this.clearCluster()
  452. this.initData(farm.id, region.id)
  453. }
  454. // 清除聚合图层,解除绑定
  455. clearCluster() {
  456. if (this.treeClusterLayer && this.treeClusterLayer.layer.getSource().getSource()) {
  457. this.treeClusterLayer.layer.getSource().getSource().clear()
  458. }
  459. }
  460. }
  461. export default SamplePointLayer;