mapManage.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. import * as KMap from "@/utils/ol-map/KMap";
  2. import * as util from "@/common/ol_common.js";
  3. import config from "@/api/config.js";
  4. import Style from "ol/style/Style";
  5. import Icon from "ol/style/Icon";
  6. import Fill from "ol/style/Fill";
  7. import Stroke from "ol/style/Stroke";
  8. import { Point, Polygon, MultiPolygon } from "ol/geom";
  9. import Feature from "ol/Feature";
  10. import DragPan from "ol/interaction/DragPan";
  11. import MouseWheelZoom from "ol/interaction/MouseWheelZoom";
  12. import PinchZoom from "ol/interaction/PinchZoom";
  13. import PinchRotate from "ol/interaction/PinchRotate";
  14. import DoubleClickZoom from "ol/interaction/DoubleClickZoom";
  15. import KeyboardPan from "ol/interaction/KeyboardPan";
  16. import KeyboardZoom from "ol/interaction/KeyboardZoom";
  17. import DragRotateAndZoom from "ol/interaction/DragRotateAndZoom";
  18. import Select from "ol/interaction/Select";
  19. import { singleClick } from "ol/events/condition";
  20. import { reactive } from "vue";
  21. import WKT from "ol/format/WKT.js";
  22. import GeoJSON from "ol/format/GeoJSON";
  23. import * as proj from "ol/proj";
  24. import { getArea } from "ol/sphere.js";
  25. import * as turf from "@turf/turf";
  26. const VIEWPORT_INTERACTION_TYPES = [
  27. DragPan,
  28. MouseWheelZoom,
  29. PinchZoom,
  30. PinchRotate,
  31. DoubleClickZoom,
  32. KeyboardPan,
  33. KeyboardZoom,
  34. DragRotateAndZoom,
  35. ];
  36. function setViewportInteractionsActive(olMap, active) {
  37. olMap.getInteractions().forEach((ix) => {
  38. if (VIEWPORT_INTERACTION_TYPES.some((T) => ix instanceof T)) {
  39. ix.setActive(active);
  40. }
  41. });
  42. }
  43. export let mapLocation = reactive({
  44. data: null,
  45. });
  46. /**
  47. * @description 地图层对象
  48. */
  49. class MapManage {
  50. constructor() {
  51. let vectorStyle = new KMap.VectorStyle();
  52. this.vectorStyle = vectorStyle;
  53. this.regionDrawingActive = false;
  54. this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
  55. style: () => {
  56. return new Style({
  57. image: new Icon({
  58. src: require("@/assets/img/home/garden-point.png"),
  59. scale: 0.5,
  60. anchor: [0.5, 0.5],
  61. }),
  62. });
  63. },
  64. });
  65. this.boundaryLayer = new KMap.VectorLayer("drawBoundaryLayer", 1050, {
  66. style: () => this.createBoundaryStyle(),
  67. });
  68. this.boundaryGeometry = null;
  69. this.constrainedDrawing = false;
  70. this.constrainedDrawingReady = false;
  71. this.gridLayer = new KMap.VectorLayer("terrainGridLayer", 1100, {
  72. style: (feature) => {
  73. const selected = !!feature.get("selected");
  74. return new Style({
  75. fill: new Fill({
  76. color: selected ? "rgba(100, 0, 0, 0.5)" : "rgba(255, 255, 255, 0.01)",
  77. }),
  78. stroke: new Stroke({
  79. color: selected ? "#E03131" : "#fff",
  80. width: selected ? 1.8 : 1.2,
  81. }),
  82. });
  83. },
  84. });
  85. this.gridToggleSelect = null;
  86. this.selectedGridIds = new Set();
  87. this.terrainGridItems = [];
  88. this.wktFormat = new WKT();
  89. this.editable = true;
  90. }
  91. createReadonlyPolygonStyle() {
  92. return new Style({
  93. fill: new Fill({
  94. color: "rgba(124, 124, 124, 0.5)",
  95. }),
  96. stroke: new Stroke({
  97. color: "rgba(255, 255, 255, 0.55)",
  98. width: 2,
  99. }),
  100. });
  101. }
  102. createBoundaryStyle() {
  103. return new Style({
  104. fill: new Fill({
  105. color: "rgba(124, 124, 124, 0.12)",
  106. }),
  107. stroke: new Stroke({
  108. color: "rgba(255, 255, 255, 0.85)",
  109. width: 2,
  110. lineDash: [8, 4],
  111. }),
  112. });
  113. }
  114. createDrawnPestStyle() {
  115. return new Style({
  116. fill: new Fill({
  117. color: "rgba(100, 0, 0, 0.45)",
  118. }),
  119. stroke: new Stroke({
  120. color: "#E03131",
  121. width: 1.8,
  122. }),
  123. });
  124. }
  125. initMap(location, target, options = {}) {
  126. const { editable = true, constrainedDrawing = false } = options;
  127. this.editable = editable;
  128. this.constrainedDrawing = constrainedDrawing;
  129. this.constrainedDrawingReady = false;
  130. this.boundaryGeometry = null;
  131. let level = 16;
  132. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  133. this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 8, 22);
  134. let xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png";
  135. this.kmap.addXYZLayer(xyz2, { minZoom: 8, maxZoom: 22 }, 2);
  136. // this.kmap.addLayer(this.clickPointLayer.layer);
  137. this.kmap.addLayer(this.boundaryLayer.layer);
  138. this.kmap.addLayer(this.gridLayer.layer);
  139. if (this.editable) {
  140. this.kmap.initDraw(() => {});
  141. this.kmap.modifyDraw();
  142. this.setRegionDrawingActive(false);
  143. } else if (this.constrainedDrawing) {
  144. this.setupConstrainedDrawing();
  145. this.setConstrainedDrawingActive(false);
  146. }
  147. }
  148. setupConstrainedDrawing() {
  149. if (!this.kmap || this.constrainedDrawingReady) return;
  150. this.constrainedDrawingReady = true;
  151. this.kmap.initDraw((e) => {
  152. if (e.feature) {
  153. this.clipFeatureToBoundary(e.feature);
  154. }
  155. });
  156. this.kmap.modifyDraw((e) => {
  157. e.features.forEach((feature) => this.clipFeatureToBoundary(feature));
  158. });
  159. }
  160. setConstrainedDrawingActive(active) {
  161. if (!this.kmap) return;
  162. if (this.kmap.draw) {
  163. this.kmap.draw.setActive(active);
  164. }
  165. if (this.kmap.modify) {
  166. this.kmap.modify.setActive(active);
  167. }
  168. }
  169. enableConstrainedDrawing() {
  170. if (!this.boundaryGeometry) return;
  171. this.setupConstrainedDrawing();
  172. this.setConstrainedDrawingActive(true);
  173. }
  174. intersectWithBoundary(geometry) {
  175. if (!this.boundaryGeometry || !geometry || !this.kmap) return null;
  176. const geoJson = new GeoJSON();
  177. const projection = this.kmap.map.getView().getProjection();
  178. const opts = {
  179. dataProjection: "EPSG:4326",
  180. featureProjection: projection,
  181. };
  182. try {
  183. const drawGeo = geoJson.writeGeometryObject(geometry, opts);
  184. const boundaryGeo = geoJson.writeGeometryObject(this.boundaryGeometry, opts);
  185. const drawnFeature = turf.feature(drawGeo);
  186. const boundaryFeature = turf.feature(boundaryGeo);
  187. let result = null;
  188. try {
  189. result = turf.intersect(turf.featureCollection([drawnFeature, boundaryFeature]));
  190. } catch {
  191. result = turf.intersect(drawnFeature, boundaryFeature);
  192. }
  193. if (!result?.geometry) return null;
  194. return geoJson.readGeometry(result.geometry, opts);
  195. } catch {
  196. return null;
  197. }
  198. }
  199. clipFeatureToBoundary(feature) {
  200. if (!feature || !this.boundaryGeometry || !this.kmap?.polygonLayer?.source) return;
  201. const geometry = feature.getGeometry();
  202. if (!geometry) return;
  203. const clipped = this.intersectWithBoundary(geometry);
  204. if (!clipped) {
  205. this.kmap.polygonLayer.source.removeFeature(feature);
  206. return;
  207. }
  208. feature.setGeometry(clipped);
  209. feature.setStyle(this.createDrawnPestStyle());
  210. }
  211. setBoundaryWkt(wkt) {
  212. if (!this.kmap || !this.boundaryLayer?.source || !wkt) return;
  213. this.boundaryLayer.source.clear();
  214. const mapProjection = this.kmap.map.getView().getProjection();
  215. const geometry = this.wktFormat.readGeometry(String(wkt).trim(), {
  216. dataProjection: "EPSG:4326",
  217. featureProjection: mapProjection,
  218. });
  219. this.boundaryGeometry = geometry.clone();
  220. const feature = new Feature({ geometry });
  221. feature.set("isBoundary", true);
  222. this.boundaryLayer.addFeature(feature);
  223. this.fitBoundaryView();
  224. }
  225. clearBoundaryLayer() {
  226. this.boundaryLayer?.source?.clear();
  227. this.boundaryGeometry = null;
  228. }
  229. fitBoundaryView() {
  230. if (!this.kmap || !this.boundaryLayer?.source) return;
  231. const extent = this.boundaryLayer.source.getExtent();
  232. if (!extent || extent.some((v) => !Number.isFinite(v))) return;
  233. this.kmap.getView().fit(extent, { duration: 500, padding: [40, 40, 40, 40] });
  234. }
  235. setDrawnAreaGeometry(geometryArr) {
  236. if (!this.kmap?.polygonLayer?.source || !Array.isArray(geometryArr)) return;
  237. this.kmap.polygonLayer.source.clear();
  238. const mapProjection = this.kmap.map.getView().getProjection();
  239. geometryArr.forEach((item) => {
  240. try {
  241. const geometry = this.wktFormat.readGeometry(String(item).trim(), {
  242. dataProjection: "EPSG:4326",
  243. featureProjection: mapProjection,
  244. });
  245. const feature = new Feature({ geometry });
  246. this.clipFeatureToBoundary(feature);
  247. if (feature.getGeometry()) {
  248. this.kmap.polygonLayer.source.addFeature(feature);
  249. }
  250. } catch {
  251. /* 单条解析失败则跳过 */
  252. }
  253. });
  254. }
  255. /**
  256. * 是否允许平移/缩放、勾画与编辑;为 false 时同时隐藏中心点位图标
  257. */
  258. setRegionDrawingActive(active) {
  259. if (!this.kmap) return;
  260. this.regionDrawingActive = active;
  261. setViewportInteractionsActive(this.kmap.map, active);
  262. if (this.kmap.draw) {
  263. this.kmap.draw.setActive(active);
  264. }
  265. if (this.kmap.modify) {
  266. this.kmap.modify.setActive(active);
  267. }
  268. if (active) {
  269. const c = this.kmap.getView().getCenter();
  270. this.setMapPoint(c);
  271. } else {
  272. // this.clickPointLayer.source.clear();
  273. }
  274. }
  275. enableRegionDrawing() {
  276. this.setRegionDrawingActive(true);
  277. }
  278. enableMapInteraction() {
  279. if (!this.kmap) return;
  280. setViewportInteractionsActive(this.kmap.map, true);
  281. }
  282. /**
  283. * 根据中心点和亩数生成正方形 WKT
  284. * @param {number[]} center [lng, lat]
  285. * @param {number} mu 面积(亩)
  286. */
  287. generateSquareWktByMu(center, mu = 60) {
  288. const lng = parseFloat(center[0]);
  289. const lat = parseFloat(center[1]);
  290. const halfSide = Math.sqrt(mu * 666.67) / 2;
  291. const latDelta = halfSide / 111000;
  292. const lngDelta = halfSide / (111000 * Math.cos((lat * Math.PI) / 180));
  293. const ring = [
  294. [lng - lngDelta, lat + latDelta],
  295. [lng + lngDelta, lat + latDelta],
  296. [lng + lngDelta, lat - latDelta],
  297. [lng - lngDelta, lat - latDelta],
  298. [lng - lngDelta, lat + latDelta],
  299. ];
  300. const coordinates = ring.map((point) => `${point[0]} ${point[1]}`).join(", ");
  301. return `MULTIPOLYGON (((${coordinates})))`;
  302. }
  303. /**
  304. * 以当前地图中心生成指定亩数的正方形区域
  305. * @param {number} mu 面积(亩)
  306. * @returns {string|null} WKT
  307. */
  308. setDefaultSquareAtCenter(mu = 60) {
  309. if (!this.kmap) return null;
  310. const center = this.kmap.getView().getCenter();
  311. const wkt = this.generateSquareWktByMu(center, mu);
  312. this.setBoundaryWkt(wkt);
  313. this.setMapPoint(center);
  314. this.enableConstrainedDrawing();
  315. return wkt;
  316. }
  317. setCenterAndSquare(center, mu = 60) {
  318. if (!this.kmap) return null;
  319. this.kmap.getView().animate({
  320. center,
  321. zoom: 16,
  322. duration: 0,
  323. });
  324. this.setMapPoint(center);
  325. const wkt = this.generateSquareWktByMu(center, mu);
  326. this.setBoundaryWkt(wkt);
  327. this.enableConstrainedDrawing();
  328. return wkt;
  329. }
  330. setMapPoint(coordinate) {
  331. // this.clickPointLayer.source.clear();
  332. let point = new Feature(new Point(coordinate));
  333. // this.clickPointLayer.addFeature(point);
  334. }
  335. setMapPosition(center) {
  336. this.kmap.getView().animate({
  337. center,
  338. zoom: 16,
  339. duration: 0,
  340. });
  341. if (this.regionDrawingActive) {
  342. this.setMapPoint(center);
  343. }
  344. }
  345. clearLayer() {
  346. if (!this.kmap?.polygonLayer?.source) return;
  347. if (this.kmap.draw && typeof this.kmap.draw.abortDrawing === "function") {
  348. this.kmap.draw.abortDrawing();
  349. }
  350. this.kmap.polygonLayer.source.clear();
  351. this.clearGridLayer();
  352. }
  353. clearAllLayers() {
  354. this.clearLayer();
  355. this.clearBoundaryLayer();
  356. }
  357. clearGridLayer() {
  358. this.unbindGridClick();
  359. this.gridLayer?.source?.clear();
  360. this.selectedGridIds?.clear();
  361. this.terrainGridItems = [];
  362. }
  363. unbindGridClick() {
  364. if (this.gridToggleSelect && this.kmap?.map) {
  365. this.kmap.map.removeInteraction(this.gridToggleSelect);
  366. }
  367. this.gridToggleSelect = null;
  368. }
  369. bindGridClick() {
  370. if (!this.kmap?.map || !this.gridLayer?.layer) return;
  371. this.unbindGridClick();
  372. const selectedStyle = new Style({
  373. fill: new Fill({
  374. color: "rgba(100, 0, 0, 0.5)",
  375. }),
  376. stroke: new Stroke({
  377. color: "#E03131",
  378. width: 1.8,
  379. }),
  380. });
  381. this.gridToggleSelect = new Select({
  382. condition: singleClick,
  383. toggleCondition: singleClick,
  384. layers: [this.gridLayer.layer],
  385. multi: true,
  386. hitTolerance: 8,
  387. style: selectedStyle,
  388. });
  389. this.gridToggleSelect.on("select", (e) => {
  390. e.selected.forEach((feature) => {
  391. feature.set("selected", true);
  392. this.selectedGridIds.add(feature.get("gridId"));
  393. feature.changed();
  394. });
  395. e.deselected.forEach((feature) => {
  396. feature.set("selected", false);
  397. this.selectedGridIds.delete(feature.get("gridId"));
  398. feature.changed();
  399. });
  400. this.gridLayer.layer.changed();
  401. });
  402. this.kmap.map.addInteraction(this.gridToggleSelect);
  403. }
  404. getSelectedGrids() {
  405. const empty = {
  406. gridIds: [],
  407. geometryArr: [],
  408. parcels: [],
  409. mianji: "0.00",
  410. mergedGeometry: "",
  411. };
  412. if (!this.kmap || !this.gridLayer?.source) return empty;
  413. const projection = this.kmap.map.getView().getProjection();
  414. const gridIds = [];
  415. const geometryArr = [];
  416. const parcels = [];
  417. let totalMu = 0;
  418. this.gridLayer.source.getFeatures().forEach((feature) => {
  419. if (!feature.get("selected")) return;
  420. const geometry = feature.getGeometry();
  421. if (!geometry) return;
  422. const gridId = feature.get("gridId");
  423. gridIds.push(gridId);
  424. const wkt = this.wktFormat.writeGeometry(geometry, {
  425. dataProjection: "EPSG:4326",
  426. featureProjection: projection,
  427. });
  428. geometryArr.push(wkt);
  429. let geom = geometry.clone();
  430. geom.transform(proj.get("EPSG:4326"), proj.get("EPSG:38572"));
  431. let mu = getArea(geom);
  432. mu = (mu + mu / 2) / 1000;
  433. totalMu += mu;
  434. parcels.push({ gridId, wkt, mianji: Number(mu.toFixed(2)) });
  435. });
  436. return {
  437. gridIds,
  438. geometryArr,
  439. parcels,
  440. mianji: totalMu.toFixed(2),
  441. mergedGeometry: this.mergeGeometryWkts(geometryArr),
  442. };
  443. }
  444. polygonCoordSetsFromGeometry(geometry) {
  445. if (!geometry || typeof geometry.getType !== "function") return [];
  446. const type = geometry.getType();
  447. if (type === "Polygon") return [geometry.getCoordinates()];
  448. if (type === "MultiPolygon") return geometry.getCoordinates();
  449. return [];
  450. }
  451. /**
  452. * 将多个 WKT 面合并为一个(单块返回 POLYGON,多块返回 MULTIPOLYGON)
  453. * @param {string[]} wktArr
  454. * @returns {string}
  455. */
  456. mergeGeometryWkts(wktArr) {
  457. if (!Array.isArray(wktArr) || wktArr.length === 0) return "";
  458. const trimmed = wktArr
  459. .map((item) => String(item).trim())
  460. .filter((item) => item.length > 10);
  461. if (trimmed.length === 0) return "";
  462. if (trimmed.length === 1) return trimmed[0];
  463. const wktOpts = {
  464. dataProjection: "EPSG:4326",
  465. featureProjection: "EPSG:4326",
  466. };
  467. const coordSets = [];
  468. trimmed.forEach((wkt) => {
  469. try {
  470. const geometry = this.wktFormat.readGeometry(wkt, wktOpts);
  471. coordSets.push(...this.polygonCoordSetsFromGeometry(geometry));
  472. } catch {
  473. /* 单条解析失败则跳过 */
  474. }
  475. });
  476. if (coordSets.length === 0) return trimmed[0];
  477. if (coordSets.length === 1) {
  478. return this.wktFormat.writeGeometry(new Polygon(coordSets[0]), wktOpts);
  479. }
  480. return this.wktFormat.writeGeometry(new MultiPolygon(coordSets), wktOpts);
  481. }
  482. getSelectedGridIds() {
  483. return this.getSelectedGrids().gridIds;
  484. }
  485. /**
  486. * 接口网格 geometry 转 Polygon(支持 MULTIPOINT / POLYGON)
  487. */
  488. gridGeometryToPolygon(geometryWkt) {
  489. if (!this.kmap || !geometryWkt) return null;
  490. const projection = this.kmap.map.getView().getProjection();
  491. let geom;
  492. try {
  493. geom = this.wktFormat.readGeometry(String(geometryWkt).trim(), {
  494. dataProjection: "EPSG:4326",
  495. featureProjection: projection,
  496. });
  497. } catch {
  498. return null;
  499. }
  500. const type = geom.getType();
  501. if (type === "Polygon") return geom;
  502. if (type === "MultiPolygon") {
  503. const polygons = geom.getPolygons();
  504. return polygons.length ? polygons[0] : null;
  505. }
  506. if (type !== "MultiPoint") return null;
  507. const coords = geom.getCoordinates();
  508. if (!coords || coords.length < 3) return null;
  509. const ring = coords.map((c) => [...c]);
  510. const first = ring[0];
  511. const last = ring[ring.length - 1];
  512. if (first[0] !== last[0] || first[1] !== last[1]) {
  513. ring.push([...first]);
  514. }
  515. return new Polygon([ring]);
  516. }
  517. /**
  518. * 渲染地形网格(generateGrid 接口返回)
  519. * @param {{ id: number, geometry: string, area_m2?: number }[]} gridItems
  520. */
  521. setTerrainGrids(gridItems) {
  522. if (!this.kmap || !this.gridLayer?.source) return;
  523. this.clearGridLayer();
  524. if (!Array.isArray(gridItems) || !gridItems.length) return;
  525. this.terrainGridItems = gridItems.map((item) => ({
  526. id: item.id,
  527. geometry: item.geometry,
  528. area_m2: item.area_m2,
  529. }));
  530. gridItems.forEach((item) => {
  531. const polygon = this.gridGeometryToPolygon(item?.geometry);
  532. if (!polygon) return;
  533. const feature = new Feature({ geometry: polygon });
  534. feature.set("gridId", item.id);
  535. feature.set("area_m2", item.area_m2);
  536. feature.set("selected", false);
  537. this.gridLayer.addFeature(feature);
  538. });
  539. this.bindGridClick();
  540. }
  541. fitGridView() {
  542. if (!this.kmap || !this.gridLayer?.source) return;
  543. const extent = this.gridLayer.source.getExtent();
  544. if (!extent || extent.some((v) => !Number.isFinite(v))) return;
  545. this.kmap.getView().fit(extent, { duration: 500, padding: [40, 40, 40, 40] });
  546. }
  547. getBoundaryWkt() {
  548. if (!this.boundaryGeometry || !this.kmap) return "";
  549. return this.wktFormat.writeGeometry(this.boundaryGeometry, {
  550. dataProjection: "EPSG:4326",
  551. featureProjection: this.kmap.map.getView().getProjection(),
  552. });
  553. }
  554. getDisplayAreaWkt() {
  555. if (!this.kmap?.polygonLayer?.source) return "";
  556. const projection = this.kmap.map.getView().getProjection();
  557. const geometryArr = [];
  558. this.kmap.polygonLayer.source.getFeatures().forEach((feature) => {
  559. const geometry = feature.getGeometry();
  560. if (!geometry) return;
  561. geometryArr.push(
  562. this.wktFormat.writeGeometry(geometry, {
  563. dataProjection: "EPSG:4326",
  564. featureProjection: projection,
  565. })
  566. );
  567. });
  568. return this.mergeGeometryWkts(geometryArr);
  569. }
  570. getTerrainGridItems() {
  571. if (this.terrainGridItems.length) {
  572. return this.terrainGridItems.map((item) => ({ ...item }));
  573. }
  574. if (!this.kmap || !this.gridLayer?.source) return [];
  575. const projection = this.kmap.map.getView().getProjection();
  576. return this.gridLayer.source.getFeatures().map((feature) => {
  577. const geometry = feature.getGeometry();
  578. return {
  579. id: feature.get("gridId"),
  580. geometry: geometry
  581. ? this.wktFormat.writeGeometry(geometry, {
  582. dataProjection: "EPSG:4326",
  583. featureProjection: projection,
  584. })
  585. : "",
  586. area_m2: feature.get("area_m2"),
  587. };
  588. });
  589. }
  590. restoreSelectedGrids(gridIds) {
  591. if (!this.gridLayer?.source || !Array.isArray(gridIds)) return;
  592. const idSet = new Set(gridIds.map((id) => String(id)));
  593. this.selectedGridIds.clear();
  594. this.gridLayer.source.getFeatures().forEach((feature) => {
  595. const gridId = feature.get("gridId");
  596. const selected = idSet.has(String(gridId));
  597. feature.set("selected", selected);
  598. if (selected) {
  599. this.selectedGridIds.add(gridId);
  600. }
  601. feature.changed();
  602. });
  603. this.gridLayer.layer.changed();
  604. }
  605. destroyMap() {
  606. this.unbindGridClick();
  607. this.clearAllLayers();
  608. if (this.kmap && typeof this.kmap.destroy === "function") {
  609. this.kmap.destroy();
  610. }
  611. this.kmap = null;
  612. this.regionDrawingActive = false;
  613. this.constrainedDrawing = false;
  614. this.constrainedDrawingReady = false;
  615. this.boundaryGeometry = null;
  616. this.selectedGridIds?.clear();
  617. }
  618. /**
  619. * 地图上全部已勾画地块:WKT 列表、每块亩数、合计亩数(亩换算与互动勾画页一致)
  620. */
  621. getAreaGeometry() {
  622. if (!this.kmap) {
  623. return { geometryArr: [], mianji: "0.00", parcels: [] };
  624. }
  625. const features = this.kmap.getLayerFeatures();
  626. const format = new WKT();
  627. const projection = this.kmap.map.getView().getProjection();
  628. const geometryArr = [];
  629. const parcels = [];
  630. let totalMu = 0;
  631. features.forEach((item) => {
  632. const geometry = item.getGeometry();
  633. if (!geometry) return;
  634. const wkt = format.writeGeometry(geometry, {
  635. dataProjection: "EPSG:4326",
  636. featureProjection: projection,
  637. });
  638. geometryArr.push(wkt);
  639. let geom = geometry.clone();
  640. geom.transform(proj.get("EPSG:4326"), proj.get("EPSG:38572"));
  641. let mu = getArea(geom);
  642. mu = (mu + mu / 2) / 1000;
  643. totalMu += mu;
  644. parcels.push({ wkt, mianji: Number(mu.toFixed(2)) });
  645. });
  646. return {
  647. geometryArr,
  648. mianji: totalMu.toFixed(2),
  649. parcels,
  650. };
  651. }
  652. setAreaGeometry(geometryArr) {
  653. this.clearLayer();
  654. if (!this.kmap) return;
  655. const format = new WKT();
  656. const mapProjection = this.kmap.map.getView().getProjection();
  657. geometryArr.forEach((item) => {
  658. const geometry = format.readGeometry(item, {
  659. dataProjection: "EPSG:4326",
  660. featureProjection: mapProjection,
  661. });
  662. const feature = new Feature({ geometry });
  663. if (!this.editable) {
  664. feature.setStyle(this.createReadonlyPolygonStyle());
  665. }
  666. this.kmap.polygonLayer.source.addFeature(feature);
  667. });
  668. if (this.boundaryGeometry) {
  669. this.fitBoundaryView();
  670. } else {
  671. this.fitView();
  672. }
  673. }
  674. fitView(){
  675. let extent = this.kmap.polygonLayer.source.getExtent()
  676. // 地图自适应到区域可视范围
  677. this.kmap.getView().fit(extent, { duration: 500, padding: [40, 40, 40, 40] });
  678. }
  679. }
  680. export default MapManage;