mapManage.js 25 KB

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