authenticMap.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 Style from "ol/style/Style";
  5. import Icon from "ol/style/Icon";
  6. import VectorLayer from "ol/layer/Vector.js";
  7. import WKT from "ol/format/WKT.js";
  8. import { reactive } from "vue";
  9. import Point from "ol/geom/Point.js";
  10. import Feature from "ol/Feature";
  11. import { newPoint } from "@/utils/map.js";
  12. import { Fill, Text,Circle,Stroke } from "ol/style";
  13. import { getArea } from "ol/sphere.js";
  14. import * as proj from "ol/proj";
  15. import proj4 from "proj4";
  16. import { register } from "ol/proj/proj4";
  17. import GeometryCollection from 'ol/geom/GeometryCollection.js';
  18. import { ElMessage } from "element-plus";
  19. import { useStore } from "vuex";
  20. import {bboxToFeature} from "../../utils/map";
  21. import * as turf from "@turf/turf";
  22. proj4.defs(
  23. "EPSG:38572",
  24. "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs"
  25. );
  26. register(proj4);
  27. export let mapData = reactive({
  28. isEdit: false,
  29. isEditArea: false,
  30. curPointData: {},
  31. point: null,
  32. selectPointArr: [],
  33. isPointHide: null,
  34. disabledForm : false,
  35. selectPoint:'',
  36. pointIndex:-1
  37. });
  38. function resetMapData(){
  39. mapData.isEdit= false
  40. mapData.isEditArea= false
  41. mapData.curPointData= {}
  42. mapData.point= null
  43. mapData.selectPointArr= []
  44. mapData.isPointHide= null
  45. mapData.disabledForm= false
  46. mapData.selectPoint= ''
  47. mapData.pointIndex=-1
  48. }
  49. /**
  50. * @description 地图层对象
  51. */
  52. class AuthenticMap {
  53. constructor() {
  54. let that = this;
  55. that.store = useStore();
  56. let vectorStyle = new KMap.VectorStyle();
  57. this.vectorStyle = vectorStyle;
  58. // 位置图标
  59. this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
  60. style: (f) => {
  61. const style1 = new Style({
  62. image: new Icon({
  63. src: require(`@/assets/images/map/${f.get("icon")}-icon.png`),
  64. scale: 0.45,
  65. }),
  66. });
  67. const style2 = new Style({
  68. text: new Text({
  69. font: "16px sans-serif",
  70. text: f.get("masterName"),
  71. offsetY: -40,
  72. padding: [4, 3, 2, 106],
  73. fill: new Fill({ color: "#fff" }), // 字体颜色
  74. }),
  75. });
  76. const style3 = new Style({
  77. image: new Icon({
  78. src: require(`@/assets/images/map/${f.get("iconBg")}.png`),
  79. scale: 0.45,
  80. displacement: [0, 90],
  81. }),
  82. });
  83. return [style1, style2, style3];
  84. },
  85. });
  86. this.locationLayer = new KMap.VectorLayer("locationLayer", 9999, {
  87. style: () => {
  88. return new Style({
  89. image: new Icon({
  90. src: require("@/assets/images/map/location.png"),
  91. scale: 0.45,
  92. }),
  93. });
  94. },
  95. });
  96. this.selectPointLayer = new KMap.VectorLayer("selectPointLayer", 10000, {
  97. style: () => {
  98. return new Style({
  99. // geometry: new Point(coord[0][i]),
  100. image: new Circle({
  101. radius: 6,
  102. fill: new Fill({
  103. color: 'red'
  104. }),
  105. stroke: new Stroke({
  106. color: '#fff',
  107. width: 1
  108. }),
  109. }),
  110. });
  111. },
  112. });
  113. // 存储绘制的地块特征
  114. // this.drawnFeatures = [];
  115. }
  116. initMap(location, target) {
  117. let that = this
  118. let level = 16;
  119. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  120. this.kmap = new KMap.Map(
  121. target,
  122. level,
  123. coordinate[0],
  124. coordinate[1],
  125. null,
  126. 6,
  127. 22
  128. );
  129. this.kmap.initDraw((e) => {
  130. if (e.type === "drawend") {
  131. mapData.isEdit = true;
  132. mapData.point = e.feature;
  133. }
  134. });
  135. this.kmap.modifyDraw((e) => {
  136. if(e.type === "modifyend"){
  137. mapData.isEditArea = false;
  138. mapData.isEditArea = true;
  139. }
  140. }, function(e){
  141. let f = null
  142. that.kmap.map.forEachFeatureAtPixel(e.pixel, function (feature, layer) {
  143. f= feature
  144. },{hitTolerance:10});
  145. if(!f){
  146. return false
  147. }
  148. let res = f.get("id") === mapData.curPointData.id || !f.get("id")
  149. if(!res){
  150. ElMessage.warning("编辑中")
  151. }
  152. console.log('f.get("height")',f);
  153. res = f.get("height").length>0?false:res
  154. return res
  155. });
  156. this.kmap.addLayer(this.clickPointLayer.layer);
  157. this.kmap.addLayer(this.locationLayer.layer);
  158. this.kmap.addLayer(this.selectPointLayer.layer);
  159. this.addMapSingerClick();
  160. }
  161. fit(geometriesWkt){
  162. let geometries = []
  163. let f = new WKT();
  164. for(let wkt of geometriesWkt){
  165. geometries.push(f.readGeometry(wkt))
  166. }
  167. let extent = new GeometryCollection(geometries).getExtent()
  168. this.kmap.fit(extent)
  169. }
  170. //撤销上一个点
  171. undoLastDraw() {
  172. // const coordinates = this.kmap.getLayerFeatures()[0].getGeometry().getCoordinates()
  173. // coordinates[0][0].pop()
  174. // this.kmap.getLayerFeatures()[0].getGeometry().getCoordinates(coordinates)
  175. }
  176. // 取消地块
  177. cancelDraw() {
  178. this.kmap.polygonLayer.source.removeFeature(mapData.point);
  179. }
  180. // 添加点位
  181. addPoint(points) {
  182. const arrPoints = [];
  183. if (points && points.length > 0) {
  184. points.forEach((item) => {
  185. let f = newPoint({ ...item, icon: "point", iconBg: "name-bg" }, "point");
  186. arrPoints.push(f);
  187. });
  188. this.clickPointLayer.source.addFeatures(arrPoints);
  189. }
  190. }
  191. // 设置地图中心点位
  192. setMapCenter(v,isPoint = true) {
  193. let arrayOfNumbers = [];
  194. const arrayOfStrings = v.split(",");
  195. arrayOfNumbers = [arrayOfStrings[1], arrayOfStrings[0]];
  196. this.kmap.map.getView().setCenter(arrayOfNumbers);
  197. if(!isPoint) return
  198. this.locationLayer.source.clear();
  199. let point = new Feature(new Point(arrayOfNumbers));
  200. this.locationLayer.addFeature(point);
  201. }
  202. // 开始勾画
  203. startDraw() {
  204. this.kmap.setDefaultCursor("crosshair");
  205. this.kmap.startDraw();
  206. this.kmap.endModify();
  207. }
  208. //结束勾画
  209. endDraw() {
  210. this.kmap.endDraw();
  211. this.kmap.endModify();
  212. }
  213. // 开始编辑
  214. startModify(type) {
  215. this.kmap.startModify();
  216. this.kmap.endDraw();
  217. if(type==='upload'){
  218. const arr = this.kmap.getLayerFeatures()
  219. const lastItem = arr[arr.length - 1]
  220. mapData.point = lastItem
  221. mapData.point.set("icon", "point-act");
  222. this.kmap.polygonStyle(mapData.point);
  223. mapData.isPointHide = mapData.point;
  224. }else{
  225. mapData.point.set("icon", "point-act");
  226. }
  227. }
  228. //结束编辑
  229. endModify() {
  230. this.kmap.endModify();
  231. }
  232. // 清空单个数据
  233. clearMapData(name, val, id) {
  234. name && (mapData[name] = val);
  235. this.clickPointLayer.source.forEachFeature((feature) => {
  236. feature.set("icon", "point");
  237. feature.set("iconBg", "name-bg");
  238. });
  239. const points = this.kmap.getLayerFeatures();
  240. points.forEach((feature) => {
  241. feature.set("icon", "point");
  242. });
  243. resetMapData()
  244. }
  245. //全选
  246. allSelect(ids){
  247. let arr = []
  248. this.clickPointLayer.source.forEachFeature((feature) => {
  249. if(!ids || ids.findIndex((id)=> id == feature.get('id')) > -1){
  250. feature.set("icon", "point-act");
  251. feature.set("iconBg", "name-act-bg");
  252. }
  253. });
  254. const points = this.kmap.getLayerFeatures();
  255. points.forEach((feature) => {
  256. if(!ids || ids.findIndex((id)=> id == feature.get('id')) > -1) {
  257. feature.set("icon", "point-act");
  258. this.kmap.polygonStyle(feature);
  259. mapData.isPointHide = feature;
  260. arr.push(feature)
  261. }
  262. });
  263. mapData.selectPointArr = arr;
  264. }
  265. //no全选
  266. allUnSelect(){
  267. this.clickPointLayer.source.forEachFeature((feature) => {
  268. feature.set("icon", "point");
  269. feature.set("iconBg", "name-bg");
  270. });
  271. const points = this.kmap.getLayerFeatures();
  272. points.forEach((feature) => {
  273. feature.set("icon", "point");
  274. this.kmap.polygonStyle(feature);
  275. mapData.isPointHide = feature;
  276. });
  277. mapData.selectPointArr = [];
  278. }
  279. //选中高亮样式
  280. //选中点位方法
  281. selectPonitFun(map,evt){
  282. let that = this
  283. map.forEachFeatureAtPixel(evt.pixel, function (f, layer) {
  284. let d = 0.0002
  285. let buff = bboxToFeature(evt.pixel[0] - d,evt.pixel[1] - d, evt.pixel[0]+ d, evt.pixel[1] + d)
  286. if (
  287. layer instanceof VectorLayer &&
  288. (
  289. layer.get("name") === "defaultPolygonLayer")
  290. ) {
  291. const fs = map.getFeaturesAtPixel(evt.pixel);
  292. let lonlat = map.getCoordinateFromPixel(evt.pixel);
  293. if (fs.length > 0) {
  294. const feature = fs[0];
  295. const geometry = feature.getGeometry();
  296. let coordinates = geometry.getCoordinates()[0]; // 获取多边形的顶点坐标
  297. ElMessage.success("已选择该点位")
  298. const features = f
  299. //代码可优化,明天优化
  300. if(mapData.selectPointArr.length>0){
  301. if(features.get("id")===mapData.selectPointArr[0].get("id")){
  302. const list = features.getGeometry().getCoordinates();
  303. coordinates = list[0][0]
  304. // const arr = list[0][0].find(subArray => subArray.includes(coordinates))
  305. let arr = []
  306. let i = 0;let curDistance = 0
  307. for(let subArray of coordinates){
  308. let from = turf.point(subArray);
  309. let to = turf.point(lonlat);
  310. let options = { units: "miles" };
  311. let distance = turf.distance(from, to, options);
  312. if(distance < 0.1){
  313. if(curDistance == 0){
  314. arr = subArray
  315. curDistance = distance
  316. mapData.pointIndex = i
  317. }else if(curDistance > distance){
  318. arr = subArray
  319. curDistance = distance
  320. mapData.pointIndex = i
  321. }
  322. }
  323. i++
  324. }
  325. that.selectPointLayer.refresh();
  326. let point = new Feature(new Point(arr));
  327. that.selectPointLayer.addFeature(point);
  328. }
  329. }
  330. mapData.selectPoint = coordinates
  331. }
  332. }
  333. });
  334. }
  335. // 地图点击事件
  336. addMapSingerClick() {
  337. let that = this;
  338. that.kmap.on("singleclick", (evt) => {
  339. if(mapData.curPointData.id && !mapData.disabledForm && mapData.selectPointArr.length===1){
  340. that.selectPonitFun(that.kmap.map,evt)
  341. return;
  342. }
  343. if(!mapData.curPointData.id && mapData.isEdit){
  344. that.selectPonitFun(that.kmap.map,evt)
  345. return;
  346. }
  347. //判断是否是导入的数据
  348. if(that.store.state.authentic.isEditStatus){
  349. that.selectPonitFun(that.kmap.map,evt)
  350. return;
  351. }
  352. let num = 0;
  353. that.kmap.map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  354. // 点击的图层是否是VectorLayer
  355. if (
  356. layer instanceof VectorLayer &&
  357. (layer.get("name") === "clickPointLayer" ||
  358. layer.get("name") === "defaultPolygonLayer")
  359. ) {
  360. // 每次点击,只走一遍该方法
  361. num = num + 1;
  362. if (num === 1) {
  363. that.getSelectPointArr(feature.get("id"));
  364. that.kmap.endDraw();
  365. }
  366. }
  367. });
  368. });
  369. }
  370. clearSelectPoint(){
  371. this.selectPointLayer.source.clear();
  372. }
  373. setPoint(name) {
  374. const arr = mapData.selectPointArr.filter(
  375. (item) => item.values_.icon === "point-act"
  376. );
  377. if (arr.length > 0) {
  378. mapData.point = arr[0];
  379. mapData.point.set("icon", name);
  380. mapData.isPointHide.set("icon", name);
  381. mapData.point.set("iconBg", "name-bg");
  382. }
  383. if (arr.length === 1) {
  384. mapData.selectPointArr = [];
  385. }
  386. }
  387. //添加地块
  388. setAreaGeometry(geometryArr) {
  389. let that = this;
  390. geometryArr.map((item) => {
  391. item.icon = "point";
  392. item.iconHide = "false";
  393. that.kmap.setLayerWkt(item.featureWkt, item);
  394. });
  395. }
  396. deletePointFun(point,callback){
  397. const coordinates = point.getGeometry().getCoordinates();
  398. let array = coordinates[0][0]
  399. // 如果点存在,则删除它
  400. if (mapData.pointIndex > -1) {
  401. array.splice(mapData.pointIndex, 1); // 删除点
  402. // 更新多边形的坐标
  403. point.getGeometry().setCoordinates(coordinates); // 更新几何形状
  404. mapData.pointIndex = -1
  405. }
  406. this.clearSelectPoint()
  407. callback && callback(mapData.pointIndex)
  408. }
  409. // 移除点的功能
  410. removePoint(callback) {
  411. // 获取多边形的所有坐标
  412. const features = this.kmap.getLayerFeatures()
  413. if(mapData.selectPointArr.length>0){
  414. features.forEach(item =>{
  415. if(item.get("id")===mapData.selectPointArr[0].get("id")){
  416. this.deletePointFun(item,callback)
  417. }
  418. })
  419. }else{
  420. this.deletePointFun(mapData.point,callback)
  421. }
  422. }
  423. // 获取所有选中点位
  424. getSelectPointArr(id) {
  425. const arr = [];
  426. this.clickPointLayer.source.forEachFeature((feature) => {
  427. if (feature.get("id") === id) {
  428. // 修改当前点位高亮
  429. const icon = feature.get("icon") === "point" ? "point-act" : "point";
  430. const iconBg =
  431. feature.get("iconBg") === "name-bg" ? "name-act-bg" : "name-bg";
  432. feature.set("icon", icon);
  433. feature.set("iconBg", iconBg);
  434. mapData.point = feature;
  435. mapData.curPointData = feature.values_;
  436. }
  437. if (feature.get("icon") === "point-act") {
  438. arr.push(feature);
  439. }
  440. });
  441. const points = this.kmap.getLayerFeatures();
  442. points.forEach((feature) => {
  443. if (feature.get("id") === id) {
  444. const icon = feature.get("icon") === "point" ? "point-act" : "point";
  445. feature.set("icon", icon);
  446. this.kmap.polygonStyle(feature);
  447. mapData.isPointHide = feature;
  448. }
  449. });
  450. mapData.selectPointArr = arr;
  451. }
  452. hidePoint() {
  453. const feature = mapData.isPointHide;
  454. feature.set("iconHide", "true");
  455. this.kmap.polygonStyle(feature);
  456. }
  457. clearLayer() {
  458. this.clickPointLayer.source.clear();
  459. this.kmap.polygonLayer.source.clear();
  460. }
  461. addLayer() {
  462. this.kmap.addLayer(this.kmap.polygonLayer.layer);
  463. this.kmap.addLayer(this.clickPointLayer.layer);
  464. }
  465. //获取地块信息
  466. getAreaGeometry(type) {
  467. let features = null
  468. if(type==='add') {
  469. features = [mapData.point]
  470. }else if(type==='upload'){
  471. features = this.kmap.getLayerFeatures()
  472. }else{
  473. if(mapData.curPointData.id){
  474. features = [this.kmap.getFeatureById(mapData.curPointData.id)];
  475. }else{
  476. features = [mapData.point]
  477. }
  478. }
  479. let geometryArr = [];
  480. let area = 0;
  481. // 获取图层上的Polygon,转成geoJson用于回显
  482. features.forEach((item) => {
  483. geometryArr.push({ featureWkt: new WKT().writeFeature(item) });
  484. let geom = item.getGeometry().clone();
  485. geom.transform(proj.get("EPSG:4326"), proj.get("EPSG:38572"));
  486. let areaItem = getArea(geom);
  487. area = (areaItem + areaItem / 2) / 1000;
  488. });
  489. return { geometryArr, area: area.toFixed(2) };
  490. }
  491. /**
  492. address
  493. farmName
  494. masterName
  495. masterTel
  496. speciesTypeName
  497. ""
  498. * @param form
  499. */
  500. search(form){
  501. const points = this.kmap.getLayerFeatures();
  502. let arr = []
  503. let geomWkt = []
  504. points.forEach((feature) => {
  505. let condition = []
  506. if(form.address != ''){
  507. condition.push(feature.get("address").includes(form.address))
  508. }
  509. if(form.farmName != ''){
  510. const text = feature.get("farmName")
  511. if(text!==form.farmName) return
  512. condition.push(feature.get("farmName").includes(form.farmName))
  513. }
  514. if(form.masterName != ''){
  515. const text = feature.get("masterName")
  516. if(text!==form.masterName) return
  517. condition.push(feature.get("masterName").includes(form.masterName))
  518. }
  519. if(form.masterTel != ''){
  520. const text = feature.get("masterTel")
  521. if(text!==form.masterTel) return
  522. condition.push(feature.get("masterTel").includes(form.masterTel))
  523. }
  524. if(form.speciesTypeName != ''){
  525. const text = feature.get("speciesTypeName")
  526. if(text!==form.speciesTypeName) return
  527. condition.push(feature.get("speciesTypeName").includes(form.speciesTypeName))
  528. }
  529. let b = false
  530. for(let item of condition){
  531. if(item){
  532. b = true
  533. }else{
  534. b = false
  535. break
  536. }
  537. }
  538. if(b){
  539. arr.push(feature.get("id"))
  540. geomWkt.push(new WKT().writeGeometry(feature.getGeometry()))
  541. }
  542. });
  543. this.allUnSelect()
  544. this.allSelect(arr)
  545. if(geomWkt.length>0){
  546. this.fit(geomWkt)
  547. }
  548. }
  549. }
  550. export default AuthenticMap;