// import { Bounds, ILayer, Zoom } from "@antv/l7"; import PointModel from "./PointModel"; // import RoadModel from "./RoadModel"; // import api from "@/http/api_road"; import { feature, Feature, Geometry, lineString } from "@turf/turf"; import { parse } from "wkt"; // import shareData from "../share/shareData"; interface ISegmentServer { roadName?: string; roadType?: number; /** * 终止控制点uid */ destinationControlPointUid: string; /** * 名称 */ name: string; /** * 起始控制点uid */ originControlPointUid: string; /** * 道路uid */ roadUid?: string; /** * 路段类型 */ segmentType?: number; /** * 唯一标识 */ uid?: string; points?: any; out_id?: string; geometry?: string; } export interface LineDrawJSON { id: string; segmentUid: string; active: boolean; name: string; roadName: string; roadArrow: string; roadId: string; x: number; y: number; roadType?: number; x1: number; y1: number; geometry?: Feature; coords?: any[]; } class LineModel { // 起始点 point1!: PointModel; // 终止点 point2!: PointModel; // 名字 name!: string; id = ""; uuid; roadId?: string; roadName = ""; roadType?: number; type?: number; active = false; geometry?: Feature; private toTurfCache: any; /********* 暂时无用的特征 end ************/ constructor() { this.uuid = new Date().getTime() + "" + Math.random(); } /** * 获取 turf 对象 */ toTurf() { return lineString([ [this.point1.lng, this.point1.lat], [this.point2.lng, this.point2.lat], ]); } updateDrawJSON() {} /** * 带有偏移算法,用来画双向线 * @param offset 米 */ toDrawJSON(): LineDrawJSON { // const offset = shareData.pixelMeter * 3; const json = { id: this.id, segmentUid: this.id, active: this.active, name: this.name, roadName: this.roadName ?.replace(/.向/, "") .replace(/[((].*?[))]/, "") .replace(/未命名道路/, "") .replace(/无道路路段/, ""), // points: this._points, roadArrow: "→", roadId: this.roadId || "", x: this.point1.lng, y: this.point1.lat, roadType: this.roadType, x1: this.point2 ? this.point2.lng : this.point1.lng, y1: this.point2 ? this.point2.lat : this.point1.lat, geometry: this.geometry, coords: (this.geometry?.geometry as Geometry).coordinates, }; return json; } toServerJson(): ISegmentServer { return { destinationControlPointUid: this.point2.id, name: this.name, originControlPointUid: this.point1.id, roadUid: this.roadId, roadName: this.roadName, roadType: this.roadType, segmentType: this.type || 2, uid: this.id, }; } fromServerJson(json: ISegmentServer) { this.name = json.name; this.roadId = json.roadUid; this.id = json.uid || ""; this.type = json.segmentType; this.roadName = json.roadName || ""; this.roadType = json.roadType || 0; this.geometry = feature(parse(json.geometry)); } copyFromLine(p: LineModel) { Object.assign(this, p); return this; } } export default LineModel;