samplePointLayer.js 17 KB

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