| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147 |
- import * as KMap from "@/utils/ol-map/KMap";
- import * as util from "@/common/ol_common.js";
- import config from "@/api/config.js";
- import Style from "ol/style/Style";
- import Icon from "ol/style/Icon";
- import Fill from "ol/style/Fill";
- import Stroke from "ol/style/Stroke";
- import Text from "ol/style/Text";
- import { Point, Polygon, MultiPolygon } from "ol/geom";
- import Feature from "ol/Feature";
- import DragPan from "ol/interaction/DragPan";
- import MouseWheelZoom from "ol/interaction/MouseWheelZoom";
- import PinchZoom from "ol/interaction/PinchZoom";
- import PinchRotate from "ol/interaction/PinchRotate";
- import DoubleClickZoom from "ol/interaction/DoubleClickZoom";
- import KeyboardPan from "ol/interaction/KeyboardPan";
- import KeyboardZoom from "ol/interaction/KeyboardZoom";
- import DragRotateAndZoom from "ol/interaction/DragRotateAndZoom";
- import Select from "ol/interaction/Select";
- import { singleClick } from "ol/events/condition";
- import { reactive } from "vue";
- import WKT from "ol/format/WKT.js";
- import GeoJSON from "ol/format/GeoJSON";
- import * as proj from "ol/proj";
- import { getArea } from "ol/sphere.js";
- import * as turf from "@turf/turf";
- const DEFAULT_ZONE_STYLE = {
- fill: "rgba(100, 0, 0, 0.45)",
- fillSelected: "rgba(100, 0, 0, 0.5)",
- stroke: "#E03131",
- };
- const VIEWPORT_INTERACTION_TYPES = [
- DragPan,
- MouseWheelZoom,
- PinchZoom,
- PinchRotate,
- DoubleClickZoom,
- KeyboardPan,
- KeyboardZoom,
- DragRotateAndZoom,
- ];
- function setViewportInteractionsActive(olMap, active) {
- olMap.getInteractions().forEach((ix) => {
- if (VIEWPORT_INTERACTION_TYPES.some((T) => ix instanceof T)) {
- ix.setActive(active);
- }
- });
- }
- export let mapLocation = reactive({
- data: null,
- });
- /**
- * @description 地图层对象
- */
- class MapManage {
- constructor() {
- let vectorStyle = new KMap.VectorStyle();
- this.vectorStyle = vectorStyle;
- this.regionDrawingActive = false;
- this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
- style: () => {
- return new Style({
- image: new Icon({
- src: require("@/assets/img/home/garden-point.png"),
- scale: 0.5,
- anchor: [0.5, 0.5],
- }),
- });
- },
- });
- this.boundaryLayer = new KMap.VectorLayer("drawBoundaryLayer", 1050, {
- style: () => this.createBoundaryStyle(),
- });
- this.boundaryGeometry = null;
- this.constrainedDrawing = false;
- this.constrainedDrawingReady = false;
- this.zoneStyle = { ...DEFAULT_ZONE_STYLE };
- this.gridLayer = new KMap.VectorLayer("terrainGridLayer", 1100, {
- style: (feature) => {
- const selected = !!feature.get("selected");
- return new Style({
- fill: new Fill({
- color: selected ? this.zoneStyle.fillSelected : "rgba(255, 255, 255, 0.01)",
- }),
- stroke: new Stroke({
- color: selected ? this.zoneStyle.stroke : "#fff",
- width: selected ? 1.8 : 1.2,
- }),
- });
- },
- });
- this.existingVarietyLayer = new KMap.VectorLayer("existingVarietyLayer", 1080, {
- style: (feature) => this.createExistingVarietyStyle(feature),
- });
- this.gridToggleSelect = null;
- this.selectedGridIds = new Set();
- this.terrainGridItems = [];
- this.wktFormat = new WKT();
- this.editable = true;
- }
- createReadonlyPolygonStyle() {
- return new Style({
- fill: new Fill({
- color: "rgba(124, 124, 124, 0.5)",
- }),
- stroke: new Stroke({
- color: "rgba(255, 255, 255, 0.55)",
- width: 2,
- }),
- });
- }
- createExistingVarietyStyle(feature) {
- const name = String(feature.get("name") || "");
- const fill = feature.get("fill") || "rgba(76, 175, 80, 0.28)";
- const stroke = feature.get("stroke") || "#4CAF50";
- return [
- new Style({
- fill: new Fill({ color: fill }),
- stroke: new Stroke({
- color: stroke,
- width: 2,
- }),
- }),
- new Style({
- text: new Text({
- text: name,
- font: "13px sans-serif",
- fill: new Fill({ color: "#1F1F1F" }),
- backgroundFill: new Fill({ color: "#ffffff" }),
- padding: [4, 8, 4, 8],
- overflow: true,
- }),
- }),
- ];
- }
- createBoundaryStyle() {
- return new Style({
- fill: new Fill({
- color: "rgba(124, 124, 124, 0.12)",
- }),
- stroke: new Stroke({
- color: "rgba(255, 255, 255, 0.85)",
- width: 2,
- lineDash: [8, 4],
- }),
- });
- }
- createDrawnZoneStyle() {
- return new Style({
- fill: new Fill({
- color: this.zoneStyle.fill,
- }),
- stroke: new Stroke({
- color: this.zoneStyle.stroke,
- width: 1.8,
- }),
- });
- }
- initMap(location, target, options = {}) {
- const {
- editable = true,
- constrainedDrawing = false,
- onDrawOutsideBoundary,
- zoneStyle,
- preventOverlapExisting = false,
- onDrawOverlap,
- singleDraw = false,
- onDrawLimit,
- } = options;
- this.zoneStyle = zoneStyle ? { ...DEFAULT_ZONE_STYLE, ...zoneStyle } : { ...DEFAULT_ZONE_STYLE };
- this.editable = editable;
- this.constrainedDrawing = constrainedDrawing;
- this.preventOverlapExisting = !!preventOverlapExisting;
- this.singleDraw = !!singleDraw;
- this.onDrawOutsideBoundary = typeof onDrawOutsideBoundary === "function" ? onDrawOutsideBoundary : null;
- this.onDrawOverlap = typeof onDrawOverlap === "function" ? onDrawOverlap : null;
- this.onDrawLimit = typeof onDrawLimit === "function" ? onDrawLimit : null;
- this.constrainedDrawingReady = false;
- this.overlapConstraintReady = false;
- this.boundaryGeometry = null;
- let level = 16;
- let coordinate = util.wktCastGeom(location).getFirstCoordinate();
- this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 8, 22);
- let xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png";
- this.kmap.addXYZLayer(xyz2, { minZoom: 8, maxZoom: 22 }, 2);
- // this.kmap.addLayer(this.clickPointLayer.layer);
- this.kmap.addLayer(this.boundaryLayer.layer);
- this.kmap.addLayer(this.existingVarietyLayer.layer);
- this.kmap.addLayer(this.gridLayer.layer);
- if (this.editable) {
- this.kmap.initDraw((e) => {
- if (this.preventOverlapExisting && e?.feature) {
- this.handleDrawEndOverlap(e.feature);
- }
- this.syncSingleDrawState();
- });
- this.kmap.modifyDraw();
- this.setupOverlapConstraintListeners();
- this.setupSingleDrawListeners();
- this.setRegionDrawingActive(false);
- } else if (this.constrainedDrawing) {
- this.setupConstrainedDrawing();
- this.setConstrainedDrawingActive(false);
- }
- }
- getDrawnFeatureCount() {
- return this.kmap?.polygonLayer?.source?.getFeatures?.()?.length || 0;
- }
- /** 单范围模式:已有勾画则关闭继续绘制,仍可拖拽编辑 */
- syncSingleDrawState() {
- if (!this.singleDraw || !this.kmap?.draw) return;
- const canDraw = this.regionDrawingActive && this.getDrawnFeatureCount() === 0;
- this.kmap.draw.setActive(canDraw);
- }
- setupSingleDrawListeners() {
- if (!this.kmap?.draw || !this.singleDraw) return;
- this.kmap.draw.on("drawstart", () => {
- if (!this.singleDraw) return;
- if (this.getDrawnFeatureCount() === 0) return;
- if (typeof this.kmap.draw.abortDrawing === "function") {
- this.kmap.draw.abortDrawing();
- }
- this.onDrawLimit?.();
- this.syncSingleDrawState();
- });
- }
- notifyDrawOverlap() {
- this.onDrawOverlap?.();
- }
- toTurfFeature(geometry) {
- if (!geometry || !this.kmap) return null;
- const geoJson = new GeoJSON();
- const projection = this.kmap.map.getView().getProjection();
- try {
- return turf.feature(
- geoJson.writeGeometryObject(geometry, {
- dataProjection: "EPSG:4326",
- featureProjection: projection,
- })
- );
- } catch {
- return null;
- }
- }
- /** 点是否落在已有品种范围内 */
- isCoordinateInExistingVariety(coordinate) {
- if (!coordinate || !this.existingVarietyLayer?.source) return false;
- return this.existingVarietyLayer.source.getFeatures().some((feature) => {
- try {
- return !!feature.getGeometry()?.intersectsCoordinate(coordinate);
- } catch {
- return false;
- }
- });
- }
- /**
- * 几何是否与禁止区域重叠(已有品种 + 其他已勾画面,可排除自身)
- * 仅面积相交算重叠,边界相接允许
- */
- geometryOverlapsForbidden(geometry, excludeFeature = null) {
- if (!geometry || !this.kmap) return false;
- const drawn = this.toTurfFeature(geometry);
- if (!drawn) return false;
- const overlaps = (otherFeature) => {
- const otherGeom = otherFeature.getGeometry();
- if (!otherGeom) return false;
- const other = this.toTurfFeature(otherGeom);
- if (!other) return false;
- try {
- let result = null;
- try {
- result = turf.intersect(turf.featureCollection([drawn, other]));
- } catch {
- result = turf.intersect(drawn, other);
- }
- if (!result?.geometry) return false;
- return turf.area(result) > 0.5;
- } catch {
- return false;
- }
- };
- const checkFeatures = (features) =>
- features.some((feature) => {
- if (excludeFeature && feature === excludeFeature) return false;
- return overlaps(feature);
- });
- if (checkFeatures(this.existingVarietyLayer?.source?.getFeatures?.() || [])) {
- return true;
- }
- if (checkFeatures(this.kmap.polygonLayer?.source?.getFeatures?.() || [])) {
- return true;
- }
- return false;
- }
- handleDrawEndOverlap(feature) {
- if (!feature) return;
- const geometry = feature.getGeometry();
- if (!this.geometryOverlapsForbidden(geometry, feature)) return;
- if (this.kmap?.polygonLayer?.source) {
- this.kmap.polygonLayer.source.removeFeature(feature);
- }
- this.notifyDrawOverlap();
- }
- handleModifyEndOverlap(feature) {
- if (!feature) return;
- const geometry = feature.getGeometry();
- if (!this.geometryOverlapsForbidden(geometry, feature)) {
- feature.unset("_preModifyGeom");
- return;
- }
- const prev = feature.get("_preModifyGeom");
- if (prev) {
- feature.setGeometry(prev);
- } else if (this.kmap?.polygonLayer?.source) {
- this.kmap.polygonLayer.source.removeFeature(feature);
- }
- feature.unset("_preModifyGeom");
- this.notifyDrawOverlap();
- }
- bindSketchOverlapConstraint(sketch) {
- if (!sketch) return;
- const onSketchChange = () => {
- if (!this.preventOverlapExisting) return;
- const geometry = sketch.getGeometry();
- if (!geometry) return;
- const type = geometry.getType();
- if (type !== "LineString" && type !== "Point") return;
- const last = this.getLastCoordinateFromGeometry(geometry);
- if (last && this.isCoordinateInExistingVariety(last)) {
- sketch.getGeometry()?.un("change", onSketchChange);
- if (this.kmap?.draw && typeof this.kmap.draw.abortDrawing === "function") {
- this.kmap.draw.abortDrawing();
- }
- this.notifyDrawOverlap();
- }
- };
- sketch.getGeometry()?.on("change", onSketchChange);
- const cleanup = () => sketch.getGeometry()?.un("change", onSketchChange);
- this.kmap.draw.once("drawend", cleanup);
- this.kmap.draw.once("drawabort", cleanup);
- }
- setupOverlapConstraintListeners() {
- if (!this.kmap?.draw || this.overlapConstraintReady) return;
- this.overlapConstraintReady = true;
- this.kmap.draw.on("drawstart", (e) => {
- if (!this.preventOverlapExisting) return;
- const coordinate = e.coordinate || this.getLastCoordinateFromGeometry(e.feature?.getGeometry());
- if (coordinate && this.isCoordinateInExistingVariety(coordinate)) {
- if (this.kmap?.draw && typeof this.kmap.draw.abortDrawing === "function") {
- this.kmap.draw.abortDrawing();
- }
- this.notifyDrawOverlap();
- return;
- }
- this.bindSketchOverlapConstraint(e.feature);
- });
- if (this.kmap.modify) {
- this.kmap.modify.on("modifystart", (e) => {
- if (!this.preventOverlapExisting) return;
- e.features?.forEach((feature) => {
- const geom = feature.getGeometry();
- if (geom) feature.set("_preModifyGeom", geom.clone());
- });
- });
- this.kmap.modify.on("modifyend", (e) => {
- if (!this.preventOverlapExisting) return;
- e.features?.forEach((feature) => this.handleModifyEndOverlap(feature));
- });
- }
- }
- /** 当前勾画面是否与已有品种范围重叠(确认前提交校验) */
- hasDrawnOverlapWithExisting() {
- if (!this.kmap?.polygonLayer?.source) return false;
- return this.kmap.polygonLayer.source.getFeatures().some((feature) =>
- this.geometryOverlapsForbidden(feature.getGeometry(), feature)
- );
- }
- isCoordinateInsideBoundary(coordinate) {
- if (!this.boundaryGeometry || !coordinate || !this.kmap) return false;
- try {
- return this.boundaryGeometry.intersectsCoordinate(coordinate);
- } catch {
- return false;
- }
- }
- notifyDrawOutsideBoundary() {
- this.onDrawOutsideBoundary?.();
- }
- getLastCoordinateFromGeometry(geometry) {
- if (!geometry || typeof geometry.getType !== "function") return null;
- const type = geometry.getType();
- if (type === "Point") return geometry.getCoordinates();
- if (type === "LineString") {
- const coords = geometry.getCoordinates();
- return coords.length ? coords[coords.length - 1] : null;
- }
- if (type === "Polygon") {
- const ring = geometry.getCoordinates()[0];
- return ring?.length ? ring[ring.length - 1] : null;
- }
- if (type === "MultiPolygon") {
- const polys = geometry.getCoordinates();
- const ring = polys[polys.length - 1]?.[0];
- return ring?.length ? ring[ring.length - 1] : null;
- }
- return null;
- }
- /** 区域外勾画:中止当前绘制并移除无效地块 */
- rejectOutsideDraw(feature) {
- if (feature && this.kmap?.polygonLayer?.source) {
- this.kmap.polygonLayer.source.removeFeature(feature);
- }
- if (this.kmap?.draw && typeof this.kmap.draw.abortDrawing === "function") {
- this.kmap.draw.abortDrawing();
- }
- this.notifyDrawOutsideBoundary();
- }
- unbindSketchBoundaryConstraint(sketch, listener) {
- sketch?.getGeometry()?.un("change", listener);
- }
- bindSketchBoundaryConstraint(sketch) {
- const onSketchChange = () => {
- const geometry = sketch.getGeometry();
- if (!geometry) return;
- const type = geometry.getType();
- // 仅在手绘轨迹(线)阶段判断越界;面要素未完成时 intersect 会误判
- if (type !== "LineString" && type !== "Point") return;
- const last = this.getLastCoordinateFromGeometry(geometry);
- if (last && !this.isCoordinateInsideBoundary(last)) {
- this.unbindSketchBoundaryConstraint(sketch, onSketchChange);
- this.rejectOutsideDraw();
- }
- };
- sketch.getGeometry()?.on("change", onSketchChange);
- const cleanup = () => this.unbindSketchBoundaryConstraint(sketch, onSketchChange);
- this.kmap.draw.once("drawend", cleanup);
- this.kmap.draw.once("drawabort", cleanup);
- }
- setupConstrainedDrawing() {
- if (!this.kmap || this.constrainedDrawingReady) return;
- this.constrainedDrawingReady = true;
- this.kmap.initDraw((e) => {
- if (!e.feature) return;
- if (!this.clipFeatureToBoundary(e.feature)) {
- if (this.kmap?.polygonLayer?.source) {
- this.kmap.polygonLayer.source.removeFeature(e.feature);
- }
- this.notifyDrawOutsideBoundary();
- }
- });
- this.kmap.draw.on("drawstart", (e) => {
- const coordinate = e.coordinate || this.getLastCoordinateFromGeometry(e.feature?.getGeometry());
- if (!coordinate || !this.isCoordinateInsideBoundary(coordinate)) {
- this.rejectOutsideDraw();
- return;
- }
- this.bindSketchBoundaryConstraint(e.feature);
- });
- this.kmap.modifyDraw((e) => {
- e.features.forEach((feature) => {
- if (!this.clipFeatureToBoundary(feature)) {
- if (this.kmap?.polygonLayer?.source) {
- this.kmap.polygonLayer.source.removeFeature(feature);
- }
- this.notifyDrawOutsideBoundary();
- }
- });
- });
- }
- setConstrainedDrawingActive(active) {
- if (!this.kmap) return;
- if (this.kmap.draw) {
- this.kmap.draw.setActive(active);
- }
- if (this.kmap.modify) {
- this.kmap.modify.setActive(active);
- }
- }
- enableConstrainedDrawing() {
- if (!this.boundaryGeometry) return;
- this.setupConstrainedDrawing();
- this.setConstrainedDrawingActive(true);
- }
- intersectWithBoundary(geometry) {
- if (!this.boundaryGeometry || !geometry || !this.kmap) return null;
- const geoJson = new GeoJSON();
- const projection = this.kmap.map.getView().getProjection();
- const opts = {
- dataProjection: "EPSG:4326",
- featureProjection: projection,
- };
- try {
- const drawGeo = geoJson.writeGeometryObject(geometry, opts);
- const boundaryGeo = geoJson.writeGeometryObject(this.boundaryGeometry, opts);
- const drawnFeature = turf.feature(drawGeo);
- const boundaryFeature = turf.feature(boundaryGeo);
- let result = null;
- try {
- result = turf.intersect(turf.featureCollection([drawnFeature, boundaryFeature]));
- } catch {
- result = turf.intersect(drawnFeature, boundaryFeature);
- }
- if (!result?.geometry) return null;
- return geoJson.readGeometry(result.geometry, opts);
- } catch {
- return null;
- }
- }
- clipFeatureToBoundary(feature) {
- if (!feature || !this.boundaryGeometry || !this.kmap?.polygonLayer?.source) return false;
- const geometry = feature.getGeometry();
- if (!geometry) return false;
- const clipped = this.intersectWithBoundary(geometry);
- if (!clipped) {
- this.kmap.polygonLayer.source.removeFeature(feature);
- return false;
- }
- feature.setGeometry(clipped);
- feature.setStyle(this.createDrawnZoneStyle());
- return true;
- }
- setBoundaryWkt(wkt) {
- if (!this.kmap || !this.boundaryLayer?.source || !wkt) return;
- this.boundaryLayer.source.clear();
- const mapProjection = this.kmap.map.getView().getProjection();
- const geometry = this.wktFormat.readGeometry(String(wkt).trim(), {
- dataProjection: "EPSG:4326",
- featureProjection: mapProjection,
- });
- this.boundaryGeometry = geometry.clone();
- const feature = new Feature({ geometry });
- feature.set("isBoundary", true);
- this.boundaryLayer.addFeature(feature);
- this.fitBoundaryView();
- }
- clearBoundaryLayer() {
- this.boundaryLayer?.source?.clear();
- this.boundaryGeometry = null;
- }
- fitBoundaryView() {
- if (!this.kmap || !this.boundaryLayer?.source) return;
- const extent = this.boundaryLayer.source.getExtent();
- if (!extent || extent.some((v) => !Number.isFinite(v))) return;
- this.kmap.getView().fit(extent, { duration: 500, padding: [40, 40, 40, 40] });
- }
- setDrawnAreaGeometry(geometryArr) {
- if (!this.kmap?.polygonLayer?.source || !Array.isArray(geometryArr)) return;
- this.kmap.polygonLayer.source.clear();
- const mapProjection = this.kmap.map.getView().getProjection();
- geometryArr.forEach((item) => {
- try {
- const geometry = this.wktFormat.readGeometry(String(item).trim(), {
- dataProjection: "EPSG:4326",
- featureProjection: mapProjection,
- });
- const feature = new Feature({ geometry });
- this.clipFeatureToBoundary(feature);
- if (feature.getGeometry()) {
- this.kmap.polygonLayer.source.addFeature(feature);
- }
- } catch {
- /* 单条解析失败则跳过 */
- }
- });
- }
- /**
- * 是否允许平移/缩放、勾画与编辑;为 false 时同时隐藏中心点位图标
- */
- setRegionDrawingActive(active) {
- if (!this.kmap) return;
- this.regionDrawingActive = active;
- setViewportInteractionsActive(this.kmap.map, active);
- if (this.kmap.draw) {
- if (active && this.singleDraw) {
- this.syncSingleDrawState();
- } else {
- this.kmap.draw.setActive(active);
- }
- }
- if (this.kmap.modify) {
- this.kmap.modify.setActive(active);
- }
- if (active) {
- const c = this.kmap.getView().getCenter();
- this.setMapPoint(c);
- } else {
- // this.clickPointLayer.source.clear();
- }
- }
- enableRegionDrawing() {
- this.setRegionDrawingActive(true);
- }
- enableMapInteraction() {
- if (!this.kmap) return;
- setViewportInteractionsActive(this.kmap.map, true);
- }
- /**
- * 根据中心点和亩数生成正方形 WKT
- * @param {number[]} center [lng, lat]
- * @param {number} mu 面积(亩)
- */
- generateSquareWktByMu(center, mu = 60) {
- const lng = parseFloat(center[0]);
- const lat = parseFloat(center[1]);
- const halfSide = Math.sqrt(mu * 666.67) / 2;
- const latDelta = halfSide / 111000;
- const lngDelta = halfSide / (111000 * Math.cos((lat * Math.PI) / 180));
- const ring = [
- [lng - lngDelta, lat + latDelta],
- [lng + lngDelta, lat + latDelta],
- [lng + lngDelta, lat - latDelta],
- [lng - lngDelta, lat - latDelta],
- [lng - lngDelta, lat + latDelta],
- ];
- const coordinates = ring.map((point) => `${point[0]} ${point[1]}`).join(", ");
- return `MULTIPOLYGON (((${coordinates})))`;
- }
- /**
- * 以当前地图中心生成指定亩数的正方形区域
- * @param {number} mu 面积(亩)
- * @returns {string|null} WKT
- */
- setDefaultSquareAtCenter(mu = 60) {
- if (!this.kmap) return null;
- const center = this.kmap.getView().getCenter();
- const wkt = this.generateSquareWktByMu(center, mu);
- this.setBoundaryWkt(wkt);
- this.setMapPoint(center);
- this.enableConstrainedDrawing();
- return wkt;
- }
- setCenterAndSquare(center, mu = 60) {
- if (!this.kmap) return null;
- this.kmap.getView().animate({
- center,
- zoom: 16,
- duration: 0,
- });
- this.setMapPoint(center);
- const wkt = this.generateSquareWktByMu(center, mu);
- this.setBoundaryWkt(wkt);
- this.enableConstrainedDrawing();
- return wkt;
- }
- setMapPoint(coordinate) {
- // this.clickPointLayer.source.clear();
- let point = new Feature(new Point(coordinate));
- // this.clickPointLayer.addFeature(point);
- }
- showCenterMarker(coordinate, iconSrc) {
- if (!this.kmap || !coordinate) return;
- if (!this.centerMarkerLayer) {
- const src = iconSrc || require("@/assets/img/home/garden-point.png");
- this.centerMarkerLayer = new KMap.VectorLayer("centerMarkerLayer", 9998, {
- style: () =>
- new Style({
- image: new Icon({
- src,
- scale: 0.45,
- anchor: [0.5, 1],
- }),
- }),
- });
- this.kmap.addLayer(this.centerMarkerLayer.layer);
- }
- this.centerMarkerLayer.source.clear();
- this.centerMarkerLayer.addFeature(new Feature(new Point(coordinate)));
- }
- setMapPosition(center) {
- this.kmap.getView().animate({
- center,
- zoom: 16,
- duration: 0,
- });
- if (this.regionDrawingActive) {
- this.setMapPoint(center);
- }
- }
- clearLayer() {
- if (!this.kmap?.polygonLayer?.source) return;
- if (this.kmap.draw && typeof this.kmap.draw.abortDrawing === "function") {
- this.kmap.draw.abortDrawing();
- }
- this.kmap.polygonLayer.source.clear();
- this.clearGridLayer();
- this.syncSingleDrawState();
- }
- clearAllLayers() {
- this.clearLayer();
- this.clearBoundaryLayer();
- this.clearExistingVarietyZones();
- }
- clearExistingVarietyZones() {
- this.existingVarietyLayer?.source?.clear();
- }
- /**
- * 展示已有品种种植范围(只读),不参与勾画结果
- * @param {{ id?: string|number, name: string, polygon: string, fill?: string, stroke?: string }[]} zones
- */
- setExistingVarietyZones(zones = []) {
- if (!this.kmap || !this.existingVarietyLayer?.source) return;
- this.clearExistingVarietyZones();
- const projection = this.kmap.map.getView().getProjection();
- const list = Array.isArray(zones) ? zones : [];
- list.forEach((item) => {
- const wkt = item?.polygon || item?.wkt;
- if (!wkt) return;
- try {
- const geometry = this.wktFormat.readGeometry(String(wkt).trim(), {
- dataProjection: "EPSG:4326",
- featureProjection: projection,
- });
- const feature = new Feature({ geometry });
- feature.set("id", item.id);
- feature.set("name", item.name || "");
- feature.set("fill", item.fill);
- feature.set("stroke", item.stroke);
- feature.set("readonly", true);
- this.existingVarietyLayer.source.addFeature(feature);
- } catch (e) {
- console.warn("[MapManage] existing variety polygon parse failed", e);
- }
- });
- this.existingVarietyLayer.layer.changed();
- }
- clearGridLayer() {
- this.unbindGridClick();
- this.gridLayer?.source?.clear();
- this.selectedGridIds?.clear();
- this.terrainGridItems = [];
- }
- unbindGridClick() {
- if (this.gridToggleSelect && this.kmap?.map) {
- this.kmap.map.removeInteraction(this.gridToggleSelect);
- }
- this.gridToggleSelect = null;
- }
- bindGridClick() {
- if (!this.kmap?.map || !this.gridLayer?.layer) return;
- this.unbindGridClick();
- const selectedStyle = new Style({
- fill: new Fill({
- color: this.zoneStyle.fillSelected,
- }),
- stroke: new Stroke({
- color: this.zoneStyle.stroke,
- width: 1.8,
- }),
- });
- this.gridToggleSelect = new Select({
- condition: singleClick,
- toggleCondition: singleClick,
- layers: [this.gridLayer.layer],
- multi: true,
- hitTolerance: 8,
- style: selectedStyle,
- });
- this.gridToggleSelect.on("select", (e) => {
- e.selected.forEach((feature) => {
- feature.set("selected", true);
- this.selectedGridIds.add(feature.get("gridId"));
- feature.changed();
- });
- e.deselected.forEach((feature) => {
- feature.set("selected", false);
- this.selectedGridIds.delete(feature.get("gridId"));
- feature.changed();
- });
- this.gridLayer.layer.changed();
- });
- this.kmap.map.addInteraction(this.gridToggleSelect);
- }
- getSelectedGrids() {
- const empty = {
- gridIds: [],
- geometryArr: [],
- parcels: [],
- mianji: "0.00",
- mergedGeometry: "",
- };
- if (!this.kmap || !this.gridLayer?.source) return empty;
- const projection = this.kmap.map.getView().getProjection();
- const gridIds = [];
- const geometryArr = [];
- const parcels = [];
- let totalMu = 0;
- this.gridLayer.source.getFeatures().forEach((feature) => {
- if (!feature.get("selected")) return;
- const geometry = feature.getGeometry();
- if (!geometry) return;
- const gridId = feature.get("gridId");
- gridIds.push(gridId);
- const wkt = this.wktFormat.writeGeometry(geometry, {
- dataProjection: "EPSG:4326",
- featureProjection: projection,
- });
- geometryArr.push(wkt);
- let geom = geometry.clone();
- geom.transform(proj.get("EPSG:4326"), proj.get("EPSG:38572"));
- let mu = getArea(geom);
- mu = (mu + mu / 2) / 1000;
- totalMu += mu;
- parcels.push({ gridId, wkt, mianji: Number(mu.toFixed(2)) });
- });
- return {
- gridIds,
- geometryArr,
- parcels,
- mianji: totalMu.toFixed(2),
- mergedGeometry: this.mergeGeometryWkts(geometryArr),
- };
- }
- polygonCoordSetsFromGeometry(geometry) {
- if (!geometry || typeof geometry.getType !== "function") return [];
- const type = geometry.getType();
- if (type === "Polygon") return [geometry.getCoordinates()];
- if (type === "MultiPolygon") return geometry.getCoordinates();
- return [];
- }
- /**
- * 将多个 WKT 面合并为一个(单块返回 POLYGON,多块返回 MULTIPOLYGON)
- * @param {string[]} wktArr
- * @returns {string}
- */
- mergeGeometryWkts(wktArr) {
- if (!Array.isArray(wktArr) || wktArr.length === 0) return "";
- const trimmed = wktArr
- .map((item) => String(item).trim())
- .filter((item) => item.length > 10);
- if (trimmed.length === 0) return "";
- if (trimmed.length === 1) return trimmed[0];
- const wktOpts = {
- dataProjection: "EPSG:4326",
- featureProjection: "EPSG:4326",
- };
- const coordSets = [];
- trimmed.forEach((wkt) => {
- try {
- const geometry = this.wktFormat.readGeometry(wkt, wktOpts);
- coordSets.push(...this.polygonCoordSetsFromGeometry(geometry));
- } catch {
- /* 单条解析失败则跳过 */
- }
- });
- if (coordSets.length === 0) return trimmed[0];
- if (coordSets.length === 1) {
- return this.wktFormat.writeGeometry(new Polygon(coordSets[0]), wktOpts);
- }
- return this.wktFormat.writeGeometry(new MultiPolygon(coordSets), wktOpts);
- }
- getSelectedGridIds() {
- return this.getSelectedGrids().gridIds;
- }
- /**
- * 接口网格 geometry 转 Polygon(支持 MULTIPOINT / POLYGON)
- */
- gridGeometryToPolygon(geometryWkt) {
- if (!this.kmap || !geometryWkt) return null;
- const projection = this.kmap.map.getView().getProjection();
- let geom;
- try {
- geom = this.wktFormat.readGeometry(String(geometryWkt).trim(), {
- dataProjection: "EPSG:4326",
- featureProjection: projection,
- });
- } catch {
- return null;
- }
- const type = geom.getType();
- if (type === "Polygon") return geom;
- if (type === "MultiPolygon") {
- const polygons = geom.getPolygons();
- return polygons.length ? polygons[0] : null;
- }
- if (type !== "MultiPoint") return null;
- const coords = geom.getCoordinates();
- if (!coords || coords.length < 3) return null;
- const ring = coords.map((c) => [...c]);
- const first = ring[0];
- const last = ring[ring.length - 1];
- if (first[0] !== last[0] || first[1] !== last[1]) {
- ring.push([...first]);
- }
- return new Polygon([ring]);
- }
- /**
- * 渲染地形网格(generateGrid 接口返回)
- * @param {{ id: number, geometry: string, area_m2?: number }[]} gridItems
- */
- setTerrainGrids(gridItems) {
- if (!this.kmap || !this.gridLayer?.source) return;
- this.clearGridLayer();
- if (!Array.isArray(gridItems) || !gridItems.length) return;
- this.terrainGridItems = gridItems.map((item) => ({
- id: item.id,
- geometry: item.geometry,
- area_m2: item.area_m2,
- }));
- gridItems.forEach((item) => {
- const polygon = this.gridGeometryToPolygon(item?.geometry);
- if (!polygon) return;
- const feature = new Feature({ geometry: polygon });
- feature.set("gridId", item.id);
- feature.set("area_m2", item.area_m2);
- feature.set("selected", false);
- this.gridLayer.addFeature(feature);
- });
- this.bindGridClick();
- }
- fitGridView() {
- if (!this.kmap || !this.gridLayer?.source) return;
- const extent = this.gridLayer.source.getExtent();
- if (!extent || extent.some((v) => !Number.isFinite(v))) return;
- this.kmap.getView().fit(extent, { duration: 500, padding: [40, 40, 40, 40] });
- }
- getBoundaryWkt() {
- if (!this.boundaryGeometry || !this.kmap) return "";
- return this.wktFormat.writeGeometry(this.boundaryGeometry, {
- dataProjection: "EPSG:4326",
- featureProjection: this.kmap.map.getView().getProjection(),
- });
- }
- getDisplayAreaWkt() {
- if (!this.kmap?.polygonLayer?.source) return "";
- const projection = this.kmap.map.getView().getProjection();
- const geometryArr = [];
- this.kmap.polygonLayer.source.getFeatures().forEach((feature) => {
- const geometry = feature.getGeometry();
- if (!geometry) return;
- geometryArr.push(
- this.wktFormat.writeGeometry(geometry, {
- dataProjection: "EPSG:4326",
- featureProjection: projection,
- })
- );
- });
- return this.mergeGeometryWkts(geometryArr);
- }
- getTerrainGridItems() {
- if (this.terrainGridItems.length) {
- return this.terrainGridItems.map((item) => ({ ...item }));
- }
- if (!this.kmap || !this.gridLayer?.source) return [];
- const projection = this.kmap.map.getView().getProjection();
- return this.gridLayer.source.getFeatures().map((feature) => {
- const geometry = feature.getGeometry();
- return {
- id: feature.get("gridId"),
- geometry: geometry
- ? this.wktFormat.writeGeometry(geometry, {
- dataProjection: "EPSG:4326",
- featureProjection: projection,
- })
- : "",
- area_m2: feature.get("area_m2"),
- };
- });
- }
- restoreSelectedGrids(gridIds) {
- if (!this.gridLayer?.source || !Array.isArray(gridIds)) return;
- const idSet = new Set(gridIds.map((id) => String(id)));
- this.selectedGridIds.clear();
- this.gridLayer.source.getFeatures().forEach((feature) => {
- const gridId = feature.get("gridId");
- const selected = idSet.has(String(gridId));
- feature.set("selected", selected);
- if (selected) {
- this.selectedGridIds.add(gridId);
- }
- feature.changed();
- });
- this.gridLayer.layer.changed();
- }
- destroyMap() {
- this.unbindGridClick();
- this.clearAllLayers();
- this.centerMarkerLayer = null;
- if (this.kmap && typeof this.kmap.destroy === "function") {
- this.kmap.destroy();
- }
- this.kmap = null;
- this.regionDrawingActive = false;
- this.constrainedDrawing = false;
- this.constrainedDrawingReady = false;
- this.overlapConstraintReady = false;
- this.preventOverlapExisting = false;
- this.singleDraw = false;
- this.boundaryGeometry = null;
- this.onDrawOutsideBoundary = null;
- this.onDrawOverlap = null;
- this.onDrawLimit = null;
- this.selectedGridIds?.clear();
- }
- /**
- * 地图上全部已勾画地块:WKT 列表、每块亩数、合计亩数(亩换算与互动勾画页一致)
- */
- getAreaGeometry() {
- if (!this.kmap) {
- return { geometryArr: [], mianji: "0.00", parcels: [] };
- }
- const features = this.kmap.getLayerFeatures();
- const format = new WKT();
- const projection = this.kmap.map.getView().getProjection();
- const geometryArr = [];
- const parcels = [];
- let totalMu = 0;
- features.forEach((item) => {
- const geometry = item.getGeometry();
- if (!geometry) return;
- const wkt = format.writeGeometry(geometry, {
- dataProjection: "EPSG:4326",
- featureProjection: projection,
- });
- geometryArr.push(wkt);
- let geom = geometry.clone();
- geom.transform(proj.get("EPSG:4326"), proj.get("EPSG:38572"));
- let mu = getArea(geom);
- mu = (mu + mu / 2) / 1000;
- totalMu += mu;
- parcels.push({ wkt, mianji: Number(mu.toFixed(2)) });
- });
- return {
- geometryArr,
- mianji: totalMu.toFixed(2),
- parcels,
- };
- }
- setAreaGeometry(geometryArr) {
- this.clearLayer();
- if (!this.kmap) return;
- const format = new WKT();
- const mapProjection = this.kmap.map.getView().getProjection();
- geometryArr.forEach((item) => {
- const geometry = format.readGeometry(item, {
- dataProjection: "EPSG:4326",
- featureProjection: mapProjection,
- });
- const feature = new Feature({ geometry });
- if (!this.editable) {
- feature.setStyle(this.createReadonlyPolygonStyle());
- }
- this.kmap.polygonLayer.source.addFeature(feature);
- });
- if (this.boundaryGeometry) {
- this.fitBoundaryView();
- } else {
- this.fitView();
- }
- }
- fitView(){
- let extent = this.kmap.polygonLayer.source.getExtent()
- // 地图自适应到区域可视范围
- this.kmap.getView().fit(extent, { duration: 500, padding: [40, 40, 40, 40] });
- }
- }
- export default MapManage;
|