import PointModel from "./PointModel"; import { Feature, FeatureCollection, featureCollection, simplify, polygon } from "@turf/turf"; import { AreaStructure } from "../hpaas-core/types"; type Point = [number, number]; /** * 区域模型 */ class AreaModel { id = ""; name = ""; type?: number; /** * 区域边缘线string格式 */ private _areaPointText = ""; /** * 区域边缘线array格式 */ points?: Point[][]; /** * 区域边缘线GeoJSON格式 */ features: FeatureCollection = featureCollection([]); politicalType = 0; postalCode = 0; /** * 是否是主区域 */ isMain = false; /** * 设置属性 */ setData(data: AreaStructure) { this.name = data.name; this.id = data.uid; this.type = data.type; this.areaPointText = data.areaPointText; } /** * 获取属性 */ getData(): AreaStructure { return { name: this.name || "", uid: this.id || "", type: this.type, areaPointText: this.areaPointText, }; } /** * 私有_areaPointText setter * 获取areaPointText的两种转换格式:points、features */ set areaPointText(path: string) { this._areaPointText = path; const allData: Feature[] = []; if (path) { this.points = path.split("-").map((p) => { const coods = p.split(";").map((p) => { const lnglats = p.split(","); return [parseFloat(lnglats[0]), parseFloat(lnglats[1])] as Point; }); if (coods[0][0] !== coods[coods.length - 1][0]) { coods.push(coods[0]); } allData.push(polygon([coods])); return coods; }); } this.features = simplify(featureCollection(allData), { tolerance: 0.002, highQuality: true, }); } /** * 私有属性_areaPointText getter */ get areaPointText() { return this._areaPointText; } /** * 增加点 */ public addPoint(p: PointModel) { if (this.points) { this.points[0].push([p.lng, p.lat]); } else { this.points = [[[p.lng, p.lat]]]; } const allData: Feature[] = []; const coods = this.points[0]; if (coods[coods.length - 1][0] != coods[0][0]) { coods.push(coods[0]); } if (coods.length > 3) { allData.push(polygon([coods])); this.features = featureCollection(allData); } } toServerJson(): AreaStructure { return { name: this.name, uid: this.id, type: this.type, areaPointText: this._areaPointText, politicalType: this.politicalType, }; } fromServerJson(json: any) { this.name = json.name; this.id = json.uid; this.type = json.type; this.politicalType = json.politicalType; this.areaPointText = json.areaPointText; this.postalCode = json.postalCode; } } export default AreaModel;