authenticMap.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. const 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. // const arr = list[0][0].find(subArray => subArray.includes(coordinates))
  304. let arr = []
  305. let i = 0
  306. for(let subArray of coordinates[0]){
  307. let from = turf.point(subArray);
  308. let to = turf.point(lonlat);
  309. let options = { units: "miles" };
  310. let distance = turf.distance(from, to, options);
  311. if(distance < 0.001){
  312. arr = subArray
  313. mapData.pointIndex = i
  314. break
  315. }
  316. i++
  317. }
  318. that.selectPointLayer.refresh();
  319. let point = new Feature(new Point(arr));
  320. that.selectPointLayer.addFeature(point);
  321. }
  322. }
  323. mapData.selectPoint = coordinates
  324. }
  325. }
  326. });
  327. }
  328. // 地图点击事件
  329. addMapSingerClick() {
  330. let that = this;
  331. that.kmap.on("singleclick", (evt) => {
  332. if(mapData.curPointData.id && !mapData.disabledForm && mapData.selectPointArr.length===1){
  333. that.selectPonitFun(that.kmap.map,evt)
  334. return;
  335. }
  336. if(!mapData.curPointData.id && mapData.isEdit){
  337. that.selectPonitFun(that.kmap.map,evt)
  338. return;
  339. }
  340. //判断是否是导入的数据
  341. if(that.store.state.authentic.isEditStatus){
  342. that.selectPonitFun(that.kmap.map,evt)
  343. return;
  344. }
  345. let num = 0;
  346. that.kmap.map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  347. // 点击的图层是否是VectorLayer
  348. if (
  349. layer instanceof VectorLayer &&
  350. (layer.get("name") === "clickPointLayer" ||
  351. layer.get("name") === "defaultPolygonLayer")
  352. ) {
  353. // 每次点击,只走一遍该方法
  354. num = num + 1;
  355. if (num === 1) {
  356. that.getSelectPointArr(feature.get("id"));
  357. that.kmap.endDraw();
  358. }
  359. }
  360. });
  361. });
  362. }
  363. clearSelectPoint(){
  364. this.selectPointLayer.source.clear();
  365. }
  366. setPoint(name) {
  367. const arr = mapData.selectPointArr.filter(
  368. (item) => item.values_.icon === "point-act"
  369. );
  370. if (arr.length > 0) {
  371. mapData.point = arr[0];
  372. mapData.point.set("icon", name);
  373. mapData.isPointHide.set("icon", name);
  374. mapData.point.set("iconBg", "name-bg");
  375. }
  376. if (arr.length === 1) {
  377. mapData.selectPointArr = [];
  378. }
  379. }
  380. //添加地块
  381. setAreaGeometry(geometryArr) {
  382. let that = this;
  383. geometryArr.map((item) => {
  384. item.icon = "point";
  385. item.iconHide = "false";
  386. that.kmap.setLayerWkt(item.featureWkt, item);
  387. });
  388. }
  389. deletePointFun(point,callback){
  390. const coordinates = point.getGeometry().getCoordinates();
  391. let array = coordinates[0][0]
  392. // 如果点存在,则删除它
  393. if (mapData.pointIndex > -1) {
  394. array.splice(mapData.pointIndex, 1); // 删除点
  395. // 更新多边形的坐标
  396. point.getGeometry().setCoordinates(coordinates); // 更新几何形状
  397. mapData.pointIndex = -1
  398. }
  399. this.clearSelectPoint()
  400. callback && callback(mapData.pointIndex)
  401. }
  402. // 移除点的功能
  403. removePoint(callback) {
  404. // 获取多边形的所有坐标
  405. const features = this.kmap.getLayerFeatures()
  406. if(mapData.selectPointArr.length>0){
  407. features.forEach(item =>{
  408. if(item.get("id")===mapData.selectPointArr[0].get("id")){
  409. this.deletePointFun(item,callback)
  410. }
  411. })
  412. }else{
  413. this.deletePointFun(mapData.point,callback)
  414. }
  415. }
  416. // 获取所有选中点位
  417. getSelectPointArr(id) {
  418. const arr = [];
  419. this.clickPointLayer.source.forEachFeature((feature) => {
  420. if (feature.get("id") === id) {
  421. // 修改当前点位高亮
  422. const icon = feature.get("icon") === "point" ? "point-act" : "point";
  423. const iconBg =
  424. feature.get("iconBg") === "name-bg" ? "name-act-bg" : "name-bg";
  425. feature.set("icon", icon);
  426. feature.set("iconBg", iconBg);
  427. mapData.point = feature;
  428. mapData.curPointData = feature.values_;
  429. }
  430. if (feature.get("icon") === "point-act") {
  431. arr.push(feature);
  432. }
  433. });
  434. const points = this.kmap.getLayerFeatures();
  435. points.forEach((feature) => {
  436. if (feature.get("id") === id) {
  437. const icon = feature.get("icon") === "point" ? "point-act" : "point";
  438. feature.set("icon", icon);
  439. this.kmap.polygonStyle(feature);
  440. mapData.isPointHide = feature;
  441. }
  442. });
  443. mapData.selectPointArr = arr;
  444. }
  445. hidePoint() {
  446. const feature = mapData.isPointHide;
  447. feature.set("iconHide", "true");
  448. this.kmap.polygonStyle(feature);
  449. }
  450. clearLayer() {
  451. this.clickPointLayer.source.clear();
  452. this.kmap.polygonLayer.source.clear();
  453. }
  454. addLayer() {
  455. this.kmap.addLayer(this.kmap.polygonLayer.layer);
  456. this.kmap.addLayer(this.clickPointLayer.layer);
  457. }
  458. //获取地块信息
  459. getAreaGeometry(type) {
  460. let features = null
  461. if(type==='add') {
  462. features = [mapData.point]
  463. }else if(type==='upload'){
  464. features = this.kmap.getLayerFeatures()
  465. }else{
  466. if(mapData.curPointData.id){
  467. features = [this.kmap.getFeatureById(mapData.curPointData.id)];
  468. }else{
  469. features = [mapData.point]
  470. }
  471. }
  472. let geometryArr = [];
  473. let area = 0;
  474. // 获取图层上的Polygon,转成geoJson用于回显
  475. features.forEach((item) => {
  476. geometryArr.push({ featureWkt: new WKT().writeFeature(item) });
  477. let geom = item.getGeometry().clone();
  478. geom.transform(proj.get("EPSG:4326"), proj.get("EPSG:38572"));
  479. let areaItem = getArea(geom);
  480. area = (areaItem + areaItem / 2) / 1000;
  481. });
  482. return { geometryArr, area: area.toFixed(2) };
  483. }
  484. /**
  485. address
  486. farmName
  487. masterName
  488. masterTel
  489. speciesTypeName
  490. ""
  491. * @param form
  492. */
  493. search(form){
  494. const points = this.kmap.getLayerFeatures();
  495. let arr = []
  496. let geomWkt = []
  497. points.forEach((feature) => {
  498. let condition = []
  499. if(form.address != ''){
  500. condition.push(feature.get("address").includes(form.address))
  501. }
  502. if(form.farmName != ''){
  503. const text = feature.get("farmName")
  504. if(text!==form.farmName) return
  505. condition.push(feature.get("farmName").includes(form.farmName))
  506. }
  507. if(form.masterName != ''){
  508. const text = feature.get("masterName")
  509. if(text!==form.masterName) return
  510. condition.push(feature.get("masterName").includes(form.masterName))
  511. }
  512. if(form.masterTel != ''){
  513. const text = feature.get("masterTel")
  514. if(text!==form.masterTel) return
  515. condition.push(feature.get("masterTel").includes(form.masterTel))
  516. }
  517. if(form.speciesTypeName != ''){
  518. const text = feature.get("speciesTypeName")
  519. if(text!==form.speciesTypeName) return
  520. condition.push(feature.get("speciesTypeName").includes(form.speciesTypeName))
  521. }
  522. let b = false
  523. for(let item of condition){
  524. if(item){
  525. b = true
  526. }else{
  527. b = false
  528. break
  529. }
  530. }
  531. if(b){
  532. arr.push(feature.get("id"))
  533. geomWkt.push(new WKT().writeGeometry(feature.getGeometry()))
  534. }
  535. });
  536. this.allUnSelect()
  537. this.allSelect(arr)
  538. if(geomWkt.length>0){
  539. this.fit(geomWkt)
  540. }
  541. }
  542. }
  543. export default AuthenticMap;