authenticMap.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 } 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, ElMessageBox } from "element-plus";
  19. proj4.defs(
  20. "EPSG:38572",
  21. "+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"
  22. );
  23. register(proj4);
  24. export let mapData = reactive({
  25. isEdit: false,
  26. isEditArea: false,
  27. curPointData: {},
  28. point: null,
  29. selectPointArr: [],
  30. isPointHide: null,
  31. disabledForm : false
  32. });
  33. function resetMapData(){
  34. mapData.isEdit= false
  35. mapData.isEditArea= false
  36. mapData.curPointData= {}
  37. mapData.point= null
  38. mapData.selectPointArr= []
  39. mapData.isPointHide= null
  40. mapData.disabledForm= false
  41. }
  42. /**
  43. * @description 地图层对象
  44. */
  45. class AuthenticMap {
  46. constructor() {
  47. let that = this;
  48. let vectorStyle = new KMap.VectorStyle();
  49. this.vectorStyle = vectorStyle;
  50. // 位置图标
  51. this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
  52. style: (f) => {
  53. const style1 = new Style({
  54. image: new Icon({
  55. src: require(`@/assets/images/map/${f.get("icon")}-icon.png`),
  56. scale: 0.45,
  57. }),
  58. });
  59. const style2 = new Style({
  60. text: new Text({
  61. font: "16px sans-serif",
  62. text: f.get("masterName"),
  63. offsetY: -40,
  64. padding: [4, 3, 2, 106],
  65. fill: new Fill({ color: "#fff" }), // 字体颜色
  66. }),
  67. });
  68. const style3 = new Style({
  69. image: new Icon({
  70. src: require(`@/assets/images/map/${f.get("iconBg")}.png`),
  71. scale: 0.45,
  72. displacement: [0, 90],
  73. }),
  74. });
  75. return [style1, style2, style3];
  76. },
  77. });
  78. this.locationLayer = new KMap.VectorLayer("locationLayer", 9999, {
  79. style: () => {
  80. return new Style({
  81. image: new Icon({
  82. src: require("@/assets/images/map/location.png"),
  83. scale: 0.45,
  84. }),
  85. });
  86. },
  87. });
  88. // 存储绘制的地块特征
  89. // this.drawnFeatures = [];
  90. }
  91. initMap(location, target) {
  92. let that = this
  93. let level = 16;
  94. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  95. this.kmap = new KMap.Map(
  96. target,
  97. level,
  98. coordinate[0],
  99. coordinate[1],
  100. null,
  101. 6,
  102. 22
  103. );
  104. this.kmap.initDraw((e) => {
  105. if (e.type === "drawend") {
  106. mapData.isEdit = true;
  107. mapData.point = e.feature;
  108. }
  109. });
  110. this.kmap.modifyDraw((e) => {
  111. if(e.type === "modifyend"){
  112. mapData.isEditArea = false;
  113. mapData.isEditArea = true;
  114. }
  115. }, function(e){
  116. let f = null
  117. that.kmap.map.forEachFeatureAtPixel(e.pixel, function (feature, layer) {
  118. f= feature
  119. },{hitTolerance:10});
  120. if(!f){
  121. return false
  122. }
  123. let res = f.get("id") === mapData.curPointData.id || !f.get("id")
  124. if(!res){
  125. ElMessage.warning("编辑中")
  126. }
  127. return res
  128. });
  129. this.kmap.addLayer(this.clickPointLayer.layer);
  130. this.kmap.addLayer(this.locationLayer.layer);
  131. this.addMapSingerClick();
  132. }
  133. fit(geometriesWkt){
  134. let geometries = []
  135. let f = new WKT();
  136. for(let wkt of geometriesWkt){
  137. geometries.push(f.readGeometry(wkt))
  138. }
  139. let extent = new GeometryCollection(geometries).getExtent()
  140. this.kmap.fit(extent)
  141. }
  142. undoLastDraw() {
  143. const features = this.kmap.getLayerFeatures();
  144. features.forEach((feature) => {
  145. // console.log("feature", feature, mapData.point);
  146. const coord = feature.getGeometry().getCoordinates()[0];
  147. });
  148. // this.kmap.setFeatureCursor("move")
  149. // const lastFeature = this.drawnFeatures.pop();
  150. // if (lastFeature) {
  151. // // const features = this.kmap.getLayerFeatures();
  152. // console.log("this", this.kmap.polygonLayer.source);
  153. // this.kmap.polygonLayer.source.removeFeature(lastFeature);
  154. // // this.kmap.getLayers().getArray()[1].getSource().removeFeature(lastFeature);
  155. // }
  156. }
  157. // 取消地块
  158. cancelDraw() {
  159. this.kmap.polygonLayer.source.removeFeature(mapData.point);
  160. }
  161. // 添加点位
  162. addPoint(points) {
  163. const arrPoints = [];
  164. if (points && points.length > 0) {
  165. points.forEach((item) => {
  166. let f = newPoint({ ...item, icon: "point", iconBg: "name-bg" }, "point");
  167. arrPoints.push(f);
  168. });
  169. this.clickPointLayer.source.addFeatures(arrPoints);
  170. }
  171. }
  172. // 设置地图中心点位
  173. setMapCenter(v) {
  174. let arrayOfNumbers = [];
  175. const arrayOfStrings = v.split(",");
  176. arrayOfNumbers = [arrayOfStrings[1], arrayOfStrings[0]];
  177. this.kmap.map.getView().setCenter(arrayOfNumbers);
  178. this.locationLayer.source.clear();
  179. let point = new Feature(new Point(arrayOfNumbers));
  180. this.locationLayer.addFeature(point);
  181. }
  182. // 开始勾画
  183. startDraw() {
  184. this.kmap.setDefaultCursor("crosshair");
  185. this.kmap.startDraw();
  186. this.kmap.endModify();
  187. }
  188. //结束勾画
  189. endDraw() {
  190. this.kmap.endDraw();
  191. this.kmap.endModify();
  192. }
  193. // 开始编辑
  194. startModify() {
  195. this.kmap.startModify();
  196. this.kmap.endDraw();
  197. mapData.point.set("icon", "point-act");
  198. }
  199. //结束编辑
  200. endModify() {
  201. this.kmap.endModify();
  202. }
  203. // 清空单个数据
  204. clearMapData(name, val, id) {
  205. name && (mapData[name] = val);
  206. this.clickPointLayer.source.forEachFeature((feature) => {
  207. feature.set("icon", "point");
  208. feature.set("iconBg", "name-bg");
  209. });
  210. const points = this.kmap.getLayerFeatures();
  211. points.forEach((feature) => {
  212. feature.set("icon", "point");
  213. });
  214. resetMapData()
  215. }
  216. //全选
  217. allSelect(ids){
  218. let arr = []
  219. this.clickPointLayer.source.forEachFeature((feature) => {
  220. if(!ids || ids.findIndex((id)=> id == feature.get('id')) > -1){
  221. feature.set("icon", "point-act");
  222. feature.set("iconBg", "name-act-bg");
  223. }
  224. });
  225. const points = this.kmap.getLayerFeatures();
  226. points.forEach((feature) => {
  227. if(!ids || ids.findIndex((id)=> id == feature.get('id')) > -1) {
  228. feature.set("icon", "point-act");
  229. this.kmap.polygonStyle(feature);
  230. mapData.isPointHide = feature;
  231. arr.push(feature)
  232. }
  233. });
  234. mapData.selectPointArr = arr;
  235. }
  236. //no全选
  237. allUnSelect(){
  238. this.clickPointLayer.source.forEachFeature((feature) => {
  239. feature.set("icon", "point");
  240. feature.set("iconBg", "name-bg");
  241. });
  242. const points = this.kmap.getLayerFeatures();
  243. points.forEach((feature) => {
  244. feature.set("icon", "point");
  245. this.kmap.polygonStyle(feature);
  246. mapData.isPointHide = feature;
  247. });
  248. mapData.selectPointArr = [];
  249. }
  250. // 地图点击事件
  251. addMapSingerClick() {
  252. let that = this;
  253. that.kmap.on("singleclick", (evt) => {
  254. if(mapData.curPointData.id && !mapData.disabledForm){
  255. console.log(mapData)
  256. ElMessage.warning("编辑中")
  257. return;
  258. }
  259. if(!mapData.curPointData.id && mapData.isEdit){
  260. console.log(mapData)
  261. ElMessage.warning("编辑中")
  262. return;
  263. }
  264. let num = 0;
  265. that.kmap.map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  266. // 点击的图层是否是VectorLayer
  267. if (
  268. layer instanceof VectorLayer &&
  269. (layer.get("name") === "clickPointLayer" ||
  270. layer.get("name") === "defaultPolygonLayer")
  271. ) {
  272. // 每次点击,只走一遍该方法
  273. num = num + 1;
  274. if (num === 1) {
  275. that.getSelectPointArr(feature.get("id"));
  276. that.kmap.endDraw();
  277. }
  278. }
  279. });
  280. });
  281. }
  282. setPoint(name) {
  283. const arr = mapData.selectPointArr.filter(
  284. (item) => item.values_.icon === "point-act"
  285. );
  286. if (arr.length > 0) {
  287. mapData.point = arr[0];
  288. mapData.point.set("icon", name);
  289. mapData.isPointHide.set("icon", name);
  290. mapData.point.set("iconBg", "name-bg");
  291. }
  292. if (arr.length === 1) {
  293. mapData.selectPointArr = [];
  294. }
  295. }
  296. //添加地块
  297. setAreaGeometry(geometryArr) {
  298. let that = this;
  299. geometryArr.map((item) => {
  300. item.icon = "point";
  301. item.iconHide = "false";
  302. that.kmap.setLayerWkt(item.featureWkt, item);
  303. });
  304. }
  305. // 移除点的功能
  306. removePoint() {
  307. // console.log('getDefaultCursor',this.kmap.getDefaultCursor());
  308. // this.kmap.setDefaultCursor('text')
  309. // console.log('getDefaultCursor',this.kmap.getDefaultCursor());
  310. // var source = this.kmap.setDefaultCursor('move');
  311. // console.log('source',this.kmap);
  312. // var source = this.kmap.polygonLayer.source
  313. // console.log('mapData.curPointData',mapData.curPointData);
  314. // source.removeFeature(mapData.curPointData)
  315. // source.removeFeature(pointFeature);
  316. // this.kmap.endDraw1()
  317. }
  318. // 获取所有选中点位
  319. getSelectPointArr(id) {
  320. const arr = [];
  321. this.clickPointLayer.source.forEachFeature((feature) => {
  322. if (feature.get("id") === id) {
  323. // 修改当前点位高亮
  324. const icon = feature.get("icon") === "point" ? "point-act" : "point";
  325. const iconBg =
  326. feature.get("iconBg") === "name-bg" ? "name-act-bg" : "name-bg";
  327. feature.set("icon", icon);
  328. feature.set("iconBg", iconBg);
  329. mapData.point = feature;
  330. mapData.curPointData = feature.values_;
  331. }
  332. if (feature.get("icon") === "point-act") {
  333. arr.push(feature);
  334. }
  335. });
  336. const points = this.kmap.getLayerFeatures();
  337. points.forEach((feature) => {
  338. if (feature.get("id") === id) {
  339. const icon = feature.get("icon") === "point" ? "point-act" : "point";
  340. feature.set("icon", icon);
  341. this.kmap.polygonStyle(feature);
  342. mapData.isPointHide = feature;
  343. }
  344. });
  345. mapData.selectPointArr = arr;
  346. }
  347. hidePoint() {
  348. const feature = mapData.isPointHide;
  349. feature.set("iconHide", "true");
  350. this.kmap.polygonStyle(feature);
  351. }
  352. clearLayer() {
  353. this.clickPointLayer.source.clear();
  354. this.kmap.polygonLayer.source.clear();
  355. }
  356. addLayer() {
  357. this.kmap.addLayer(this.kmap.polygonLayer.layer);
  358. this.kmap.addLayer(this.clickPointLayer.layer);
  359. }
  360. //获取地块信息
  361. getAreaGeometry(type) {
  362. let features = null
  363. if(type==='add') {
  364. features = [mapData.point]
  365. }else{
  366. features = this.kmap.getLayerFeatures();
  367. }
  368. let geometryArr = [];
  369. let area = 0;
  370. // 获取图层上的Polygon,转成geoJson用于回显
  371. features.forEach((item) => {
  372. geometryArr.push({ featureWkt: new WKT().writeFeature(item) });
  373. let geom = item.getGeometry().clone();
  374. geom.transform(proj.get("EPSG:4326"), proj.get("EPSG:38572"));
  375. let areaItem = getArea(geom);
  376. area = (areaItem + areaItem / 2) / 1000;
  377. });
  378. return { geometryArr, area: area.toFixed(2) };
  379. }
  380. /**
  381. address
  382. farmName
  383. masterName
  384. masterTel
  385. speciesTypeName
  386. ""
  387. * @param form
  388. */
  389. search(form){
  390. const points = this.kmap.getLayerFeatures();
  391. let arr = []
  392. let geomWkt = []
  393. points.forEach((feature) => {
  394. let condition = []
  395. if(form.address != ''){
  396. condition.push(feature.get("address").includes(form.address))
  397. }
  398. if(form.farmName != ''){
  399. const text = feature.get("farmName")
  400. if(text!==form.farmName) return
  401. condition.push(feature.get("farmName").includes(form.farmName))
  402. }
  403. if(form.masterName != ''){
  404. const text = feature.get("masterName")
  405. if(text!==form.masterName) return
  406. condition.push(feature.get("masterName").includes(form.masterName))
  407. }
  408. if(form.masterTel != ''){
  409. const text = feature.get("masterTel")
  410. if(text!==form.masterTel) return
  411. condition.push(feature.get("masterTel").includes(form.masterTel))
  412. }
  413. if(form.speciesTypeName != ''){
  414. const text = feature.get("speciesTypeName")
  415. if(text!==form.speciesTypeName) return
  416. condition.push(feature.get("speciesTypeName").includes(form.speciesTypeName))
  417. }
  418. let b = false
  419. for(let item of condition){
  420. if(item){
  421. b = true
  422. }else{
  423. b = false
  424. break
  425. }
  426. }
  427. if(b){
  428. arr.push(feature.get("id"))
  429. geomWkt.push(new WKT().writeGeometry(feature.getGeometry()))
  430. }
  431. });
  432. this.allUnSelect()
  433. this.allSelect(arr)
  434. if(geomWkt.length>0){
  435. this.fit(geomWkt)
  436. }
  437. }
  438. }
  439. export default AuthenticMap;