-
Notifications
You must be signed in to change notification settings - Fork 1
/
MPBuilding.ts
334 lines (311 loc) · 8.34 KB
/
MPBuilding.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import MPEntity from "./MPEntity";
import { MPPoint, MPPolygon, MPFloor, MPBounds, MPDataField, MPPointParams, MPFloorParams } from "../../index";
import MPBuildingInfo, { MPBuildingInfoParams } from "./MPBuildingInfo";
import { Platform } from "react-native";
/**
* An entity that contains information about a specific building.
*
* @export
* @class MPBuilding
* @typedef {MPBuilding}
* @extends {MPEntity}
*/
export default class MPBuilding extends MPEntity {
/**
* Creates an instance of MPBuilding.
*
* @constructor
* @private
* @param {string} id The building's ID.
* @param {string} administrativeId The building's readable ID.
* @param {string} externalId An ID used to map the building to a customer's ID.
* @param {string} venueId The ID of the venue this building lies within.
* @param {MPPoint} anchor The anchor point for the building.
* @param {MPPolygon} geometry The geometry of the building.
* @param {Map<number, MPFloor>} _floors A map of the floors inside the building.
* @param {number} defaultFloor The default floor index for the building.
* @param {string} address The building's address.
* @param {?MPBuildingInfo} [buildingInfo] A collection of information about the building.
*/
private constructor(
private readonly id: string,
public readonly administrativeId: string,
public readonly externalId: string,
public readonly venueId: string,
private readonly anchor: MPPoint,
public readonly geometry: MPPolygon,
private readonly _floors: Map<number, MPFloor>,
public readonly defaultFloor: number,
public readonly address: string,
private readonly buildingInfo?: MPBuildingInfo,
) {
super("MPBuilding");
}
/**
* Creator for MPBounds, used to decode JSON from the MapsIndoors SDK.
*
* @public
* @static
* @param {MPBuildingParams} object
* @returns {MPBuilding}
*/
public static create(object: MPBuildingParams): MPBuilding {
let floors: Map<number, MPFloor> = new Map();
if (object?.floors !== undefined) {
floors = new Map(Object.entries(object?.floors).map(([key, value]) => [Number.parseInt(key), MPFloor.create(value as MPFloorParams)]));
// Object.keys(object?.floors).forEach(key => {
// floors.set(Number.parseInt(key), MPFloor.create(object?.floors[key] as unknown as MPFloorParams))
// });
}
floors.forEach((value: MPFloor) => {
value.buildingId = object?.id;
});
var polygon;
if(Platform.OS === 'ios') {
polygon = MPPolygon.create({coordinates: object?.geometry, bbox: [].concat(object?.boundingBox?.northeast, object?.boundingBox?.southwest)});
}else {
polygon = MPPolygon.create(object?.geometry);
}
return new MPBuilding(
object?.id,
object?.administrativeId,
object?.externalId,
object?.venueId,
MPPoint.create(object?.anchor),
polygon,
floors,
object?.defaultFloor,
object?.address,
MPBuildingInfo.create(object?.buildingInfo as MPBuildingInfoParams),
);
}
/**
* Get the building's id.
*
* @public
* @readonly
* @type {string}
*/
public get buildingId(): string {
return this.id;
}
/**
* Get the building's bounds.
*
* @public
* @readonly
* @type {MPBounds}
*/
public get bounds(): MPBounds {
return this.geometry.bounds;
}
/**
* Get the position of the building, this will correspond to the building's anchor point.
*
* @public
* @readonly
* @type {MPPoint}
*/
public get position(): MPPoint {
return this.anchor;
}
/**
* Overload from MPEntity, will return false.
*
* @public
* @readonly
* @type {boolean}
*/
public get isPoint(): boolean {
return false;
}
/**
* Get the building's name.
*
* @public
* @readonly
* @type {string}
*/
public get name(): string {
return this.buildingInfo?.name ?? "";
}
/**
* Get a list of aliases for the building.
*
* @public
* @readonly
* @type {string[]}
*/
public get aliases(): string[] {
return this.buildingInfo?.aliases ?? [];
}
/**
* Get the number of {@link MPFloor}s in the building.
*
* @public
* @readonly
* @type {number}
*/
public get floorCount(): number {
return this._floors.size;
}
/**
* Get a list of the floors in the building.
*
* @public
* @readonly
* @type {MPFloor[]}
*/
public get floors(): MPFloor[] {
return Array.from(this._floors.values());
}
/**
* Get the default floor index.
*
* @public
* @readonly
* @type {number}
*/
public get initialFloorIndex(): number {
return this.defaultFloor ?? this.floors[0].floorIndex ?? 0;
}
/**
* Check whether the building contains a floor with the {@link floorIndex}.
*
* @public
* @param {number} floorIndex
* @returns {boolean}
*/
public hasFloorIndex(floorIndex: number): boolean {
return this._floors.has(floorIndex);
}
/**
* Get a floor by its floor index.
*
* @public
* @param {number} floorIndex
* @returns {MPFloor}
*/
public getFloorByIndex(floorIndex: number): MPFloor | undefined {
return this._floors.get(floorIndex);
}
/**
* Fetch a custom field from the building.
*
* @public
* @param {?string} [key]
* @returns {(MPDataField | undefined)}
*/
public getField(key?: string): MPDataField | undefined {
if (key !== undefined) {
return this.buildingInfo?.fields?.get(key);
}
}
/**
* Check whether the building's geometry contains the point.
*
* @public
* @async
* @param {MPPoint} point
* @returns {Promise<boolean>}
*/
public async contains(point: MPPoint): Promise<boolean> {
return this.geometry.contains(point);
}
/**
* Parses the object to a JSON object that is compatible with the MapsIndoors SDK.
*
* @public
* @returns {MPBuildingParams}
*/
public toJSON(): MPBuildingParams {
return {
"id": this.id,
"administrativeId": this.administrativeId,
"externalId": this.externalId,
"venueId": this.venueId,
"anchor": this.anchor.toJSON(),
"buildingInfo": this.buildingInfo,
"geometry": this.geometry,
"floors": this._floors,
"defaultFloor": this.defaultFloor,
"address": this.address,
"boundingBox": this.bounds.toJSON()
}
}
}
/**
* Parameter interface for {@link MPBuilding}.
*
* @export
* @interface MPBuildingParams
* @typedef {MPBuildingParams}
*/
export interface MPBuildingParams {
/**
* The building's ID.
*
* @type {string}
*/
id: string,
/**
* The building's readable ID.
*
* @type {string}
*/
administrativeId: string,
/**
* An ID used to map the building to a customer's ID.
*
* @type {string}
*/
externalId: string,
/**
* The ID of the venue this building lies within.
*
* @type {string}
*/
venueId: string,
/**
* The anchor point for the building.
*
* @type {MPPointParams}
*/
anchor: MPPointParams,
/**
* The geometry of the building.
*
* @type {MPPolygon}
*/
geometry: MPPolygon | any,
/**
* A map of the floors inside the building.
*
*/
floors: object,
/**
* The default floor index for the building.
*
* @type {number}
*/
defaultFloor: number,
/**
* The building's address.
*
* @type {string}
*/
address: string,
/**
* A collection of information about the building.
*
* @type {?MPBuildingInfo}
*/
buildingInfo?: MPBuildingInfo,
/**
* A object of two arrays representing coordinates
* Northeast & Southwest
*
* @type {any}
*/
boundingBox?: any
}