import { helpers, multiLineString } from "@turf/turf"; import LineModel from "./LineModel"; import PointModel from "./PointModel"; interface IRoadServer { /** * 终止控制点uid */ destinationControlPointUid: string | null; /** * 名称 */ name: string; /** * 起始控制点uid */ originControlPointUid: string | null; /** * 唯一标识 */ uid?: string; /** * 是否可见 */ visible?: number; roadType?: number; } /** * 道路的模型 */ class RoadModel { id!: string; name!: string; beginPoint: PointModel | null = null; endPoint: PointModel | null = null; originControlPointUid!: string; destinationControlPointUid!: string; // 相关路段 lines: LineModel[] = []; // 相关控制点 points: PointModel[] = []; constructor(o?: any) { if (o) { Object.assign(this, o); } } /** * 获取 turf 对象 */ toTurf() { const positions: helpers.Position[][] = []; this.lines.forEach((l: LineModel) => { positions.push([ [l.point1.lng, l.point1.lat], [l.point2.lng, l.point2.lat], ]); }); return multiLineString(positions); } toServerJson(): IRoadServer { return { destinationControlPointUid: this.endPoint ? this.endPoint.id : null, name: this.name, originControlPointUid: this.beginPoint ? this.beginPoint.id : null, uid: this.id, visible: 1, }; } fromServerJson(json: any) { this.name = json.name; this.id = json.uid; this.destinationControlPointUid = json.destinationControlPointUid; this.originControlPointUid = json.originControlPointUid; return this; // if (json.originControlPointUid) { // this.beginPoint = new PointModel(); // this.beginPoint.id = (json.originControlPointUid); // this.beginPoint.isRoadBeginPoint = true; // } // if (json.destinationControlPointUid) { // this.endPoint = new PointModel(); // this.endPoint.id = (json.destinationControlPointUid); // this.endPoint.isRoadEndPoint = true; // } } fromServer(uid: string) {} } export default RoadModel;