-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMPPolygon.ts
219 lines (200 loc) · 5.05 KB
/
MPPolygon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { MPPoint, MPGeometry, MPBounds } from "../../index";
import MPUtils from "./MPUtils";
/**
* MPPolygon is a collection of {@link MPPoint}s that combine
* to form a single geographical area with a single body.
*
* @export
* @class MPPolygon
* @typedef {MPPolygon}
* @extends {MPGeometry}
*/
export default class MPPolygon extends MPGeometry {
/**
* bounds cache.
*
* @private
* @type {?MPBounds}
*/
private _bounds?: MPBounds;
/**
* area cache.
*
* @private
* @type {?number}
*/
private _area?: number;
/**
* Creates an instance of MPPolygon.
*
* @constructor
* @private
* @param {number[][][]} _coordinates
* @param {number[]} _bbox
*/
private constructor(
private _coordinates: number[][][],
private _bbox: number[]
) {
super();
}
/**
* Update the polygon's bounding box, the bounding box must be in
* [GeoJson format]{@link https://stevage.github.io/geojson-spec/#section-5}
*
* @public
* @type {{}}
*/
public set bbox(box: number[]) {
this._bbox = box;
this.clearCache();
}
/**
* Get the polygon's bounding box, the bounding box is in
* [GeoJson format]{@link https://stevage.github.io/geojson-spec/#section-5}
*
* @public
* @type {number[]}
*/
public get bbox(): number[] {
return this._bbox;
}
/**
* Update the coordinates list for the polygon, the coordinates must be in
* [GeoJSON format]{@link https://stevage.github.io/geojson-spec/#section-3.1.6}
*
* @public
* @type {number[][][]}
*/
public set coordinates(coordinates: number[][][]) {
this._coordinates = coordinates;
this.clearCache();
}
/**
* Get the coordinates as a collection in
* [GeoJSON format]{@link https://stevage.github.io/geojson-spec/#section-3.1.6}.
*
* @public
* @type {number[][][]}
*/
public get coordinates(): number[][][] {
return this._coordinates;
}
/**
* Get the polygon's bounds. If {@link bbox} is present then that will be used.
*
* @public
* @readonly
* @type {MPBounds}
*/
public get bounds(): MPBounds {
if (this._bounds == undefined) {
this._bounds = MPBounds.fromBBox(this._bbox)
}
return this._bounds;
}
/**
* Get the polygon's area.
*
* @public
* @async
* @returns {Promise<number>}
*/
public async getArea(): Promise<number> {
if (this._area == undefined) {
this._area = await MPUtils.geometryArea(this);
}
return Promise.resolve(this._area);
}
/**
* Get the position of the polygon, which is roughly its center.
*
* @public
* @readonly
* @type {MPPoint}
*/
public get position(): MPPoint {
return this.bounds.center;
}
/**
* Get the type of the polygon. see {@link MPGeometry#polygon}
*
* @public
* @readonly
* @type {string}
*/
public get type(): string {
return MPGeometry.polygon;
}
/**
* Creator for MPPolygon, used to decode JSON from the MapsIndoors SDK.
*
* @public
* @static
* @param {MPPolygonParams} object
* @returns {MPPolygon}
*/
public static create(object: MPPolygonParams): MPPolygon {
return new MPPolygon(object?.coordinates, object?.bbox);
}
/**
* Calculates the squared distance from the point to the closest edge in the polygon.
*
* @public
* @async
* @param {MPPoint} point
* @returns {Promise<number>}
*/
public async distanceToClosestEdge(point: MPPoint): Promise<number> {
return MPUtils.polygonDistanceToClosestEdge(point, this);
}
/**
* Clears area and bounds cache due to geometry recalculation.
*
* @private
*/
private clearCache() {
this._area = undefined;
this._bounds = undefined;
}
/**
* Parses the object to a JSON object that is compatible with the MapsIndoors SDK.
*
* @public
* @returns {MPPolygonParams}
*/
public toJSON(): MPPolygonParams {
return {
type: this.type,
coordinates: this._coordinates,
bbox: this._bbox,
}
}
}
/**
* Parameter interface for {@link MPPolygon}.
*
* @export
* @interface MPPolygonParams
* @typedef {MPPolygonParams}
*/
export interface MPPolygonParams {
/**
* the type of the geometry, leave this blank as it is just used to parse the geometry to JSON
*
* @type {string}
*/
type?: string,
/**
* Geometry, as specified in the [GeoJSON format]{@link https://stevage.github.io/geojson-spec/#section-3.1.6}.
*
* @type {number[][][]}
*/
coordinates: number[][][],
/**
* bounding box, as specified in the [GeoJson format]{@link https://stevage.github.io/geojson-spec/#section-5}.
*
* @type {number[]}
*/
bbox: number[],
}