@tak-ps/node-cot 12.37.0 → 13.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/.github/workflows/doc.yml +1 -1
  2. package/.github/workflows/release.yml +3 -3
  3. package/.github/workflows/test.yml +4 -1
  4. package/CHANGELOG.md +9 -0
  5. package/dist/index.js +5 -3
  6. package/dist/index.js.map +1 -1
  7. package/dist/lib/{chat.js → builders/chat.js} +2 -2
  8. package/dist/lib/builders/chat.js.map +1 -0
  9. package/dist/lib/{fileshare.js → builders/fileshare.js} +2 -2
  10. package/dist/lib/builders/fileshare.js.map +1 -0
  11. package/dist/lib/{force-delete.js → builders/force-delete.js} +2 -2
  12. package/dist/lib/builders/force-delete.js.map +1 -0
  13. package/dist/lib/builders/route.js +23 -0
  14. package/dist/lib/builders/route.js.map +1 -0
  15. package/dist/lib/cot.js +7 -723
  16. package/dist/lib/cot.js.map +1 -1
  17. package/dist/lib/data-package.js +3 -2
  18. package/dist/lib/data-package.js.map +1 -1
  19. package/dist/lib/parser.js +769 -0
  20. package/dist/lib/parser.js.map +1 -0
  21. package/dist/lib/types/types.js +58 -17
  22. package/dist/lib/types/types.js.map +1 -1
  23. package/dist/lib/utils/util.js +5 -5
  24. package/dist/lib/utils/util.js.map +1 -1
  25. package/dist/package.json +4 -5
  26. package/dist/tsconfig.tsbuildinfo +1 -1
  27. package/index.ts +6 -3
  28. package/lib/{chat.ts → builders/chat.ts} +3 -3
  29. package/lib/{fileshare.ts → builders/fileshare.ts} +4 -4
  30. package/lib/{force-delete.ts → builders/force-delete.ts} +3 -3
  31. package/lib/builders/route.ts +27 -0
  32. package/lib/cot.ts +17 -858
  33. package/lib/data-package.ts +3 -2
  34. package/lib/parser.ts +935 -0
  35. package/lib/types/types.ts +66 -17
  36. package/lib/utils/util.ts +5 -5
  37. package/package.json +4 -5
  38. package/tsconfig.json +0 -3
  39. package/dist/lib/chat.js.map +0 -1
  40. package/dist/lib/fileshare.js.map +0 -1
  41. package/dist/lib/force-delete.js.map +0 -1
@@ -0,0 +1,769 @@
1
+ import protobuf from 'protobufjs';
2
+ import Err from '@openaddresses/batch-error';
3
+ import { xml2js, js2xml } from 'xml-js';
4
+ import { diff } from 'json-diff-ts';
5
+ import crypto from 'node:crypto';
6
+ import { InputFeature, } from './types/feature.js';
7
+ import Ellipse from '@turf/ellipse';
8
+ import PointOnFeature from '@turf/point-on-feature';
9
+ import Truncate from '@turf/truncate';
10
+ import { destination } from '@turf/destination';
11
+ import Util from './utils/util.js';
12
+ import Color from './utils/color.js';
13
+ import JSONCoT, { Detail } from './types/types.js';
14
+ import CoT from './cot.js';
15
+ import AJV from 'ajv';
16
+ import fs from 'fs';
17
+ import path from 'node:path';
18
+ import { fileURLToPath } from 'node:url';
19
+ // GeoJSON Geospatial ops will truncate to the below
20
+ const COORDINATE_PRECISION = 6;
21
+ const protoPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'proto', 'takmessage.proto');
22
+ const RootMessage = await protobuf.load(protoPath);
23
+ const pkg = JSON.parse(String(fs.readFileSync(new URL('../package.json', import.meta.url))));
24
+ const checkXML = (new AJV({
25
+ allErrors: true,
26
+ coerceTypes: true,
27
+ allowUnionTypes: true
28
+ }))
29
+ .compile(JSONCoT);
30
+ const checkFeat = (new AJV({
31
+ allErrors: true,
32
+ coerceTypes: true,
33
+ allowUnionTypes: true
34
+ }))
35
+ .compile(InputFeature);
36
+ /**
37
+ * Convert to and from an XML CoT message
38
+ * @class
39
+ *
40
+ * @param cot A string/buffer containing the XML representation or the xml-js object tree
41
+ *
42
+ * @prop raw Raw XML-JS representation of CoT
43
+ */
44
+ export class CoTParser {
45
+ static validate(cot, opts = {
46
+ flow: true
47
+ }) {
48
+ if (opts.flow === undefined)
49
+ opts.flow = true;
50
+ checkXML(cot.raw);
51
+ if (checkXML.errors)
52
+ throw new Err(400, null, `${checkXML.errors[0].message} (${checkXML.errors[0].instancePath})`);
53
+ if (opts.flow) {
54
+ if (!cot.raw.event.detail)
55
+ cot.raw.event.detail = {};
56
+ if (!cot.raw.event.detail['_flow-tags_']) {
57
+ cot.raw.event.detail['_flow-tags_'] = {};
58
+ }
59
+ cot.raw.event.detail['_flow-tags_'][`NodeCoT-${pkg.version}`] = new Date().toISOString();
60
+ }
61
+ return cot;
62
+ }
63
+ /**
64
+ * Detect difference between CoT messages
65
+ * Note: This diffs based on GeoJSON Representation of message
66
+ * So if unknown properties are present they will be excluded from the diff
67
+ */
68
+ static isDiff(aCoT, bCoT, opts = {
69
+ diffMetadata: false,
70
+ diffStaleStartTime: false,
71
+ diffDest: false,
72
+ diffFlow: false
73
+ }) {
74
+ const a = this.to_geojson(aCoT);
75
+ const b = this.to_geojson(bCoT);
76
+ if (!opts.diffDest) {
77
+ delete a.properties.dest;
78
+ delete b.properties.dest;
79
+ }
80
+ if (!opts.diffMetadata) {
81
+ delete a.properties.metadata;
82
+ delete b.properties.metadata;
83
+ }
84
+ if (!opts.diffFlow) {
85
+ delete a.properties.flow;
86
+ delete b.properties.flow;
87
+ }
88
+ if (!opts.diffStaleStartTime) {
89
+ delete a.properties.time;
90
+ delete a.properties.stale;
91
+ delete a.properties.start;
92
+ delete b.properties.time;
93
+ delete b.properties.stale;
94
+ delete b.properties.start;
95
+ }
96
+ const diffs = diff(a, b);
97
+ return diffs.length > 0;
98
+ }
99
+ static from_xml(raw, opts = {}) {
100
+ const cot = new CoT(xml2js(String(raw), { compact: true }), opts);
101
+ return this.validate(cot);
102
+ }
103
+ static to_xml(cot) {
104
+ return js2xml(cot.raw, { compact: true });
105
+ }
106
+ /**
107
+ * Return an ATAK Compliant Protobuf
108
+ */
109
+ static to_proto(cot, version = 1) {
110
+ if (version < 1 || version > 1)
111
+ throw new Err(400, null, `Unsupported Proto Version: ${version}`);
112
+ const ProtoMessage = RootMessage.lookupType(`atakmap.commoncommo.protobuf.v${version}.TakMessage`);
113
+ // The spread operator is important to make sure the delete doesn't modify the underlying detail object
114
+ const detail = { ...cot.raw.event.detail };
115
+ const msg = {
116
+ cotEvent: {
117
+ ...cot.raw.event._attributes,
118
+ sendTime: new Date(cot.raw.event._attributes.time).getTime(),
119
+ startTime: new Date(cot.raw.event._attributes.start).getTime(),
120
+ staleTime: new Date(cot.raw.event._attributes.stale).getTime(),
121
+ ...cot.raw.event.point._attributes,
122
+ detail: {
123
+ xmlDetail: ''
124
+ }
125
+ }
126
+ };
127
+ let key;
128
+ for (key in detail) {
129
+ if (['contact', 'group', 'precisionlocation', 'status', 'takv', 'track'].includes(key)) {
130
+ msg.cotEvent.detail[key] = detail[key]._attributes;
131
+ delete detail[key];
132
+ }
133
+ }
134
+ msg.cotEvent.detail.xmlDetail = js2xml({
135
+ ...detail,
136
+ metadata: cot.metadata
137
+ }, { compact: true });
138
+ return ProtoMessage.encode(msg).finish();
139
+ }
140
+ /**
141
+ * Return a GeoJSON Feature from an XML CoT message
142
+ */
143
+ static to_geojson(cot) {
144
+ const raw = JSON.parse(JSON.stringify(cot.raw));
145
+ if (!raw.event.detail)
146
+ raw.event.detail = {};
147
+ if (!raw.event.detail.contact)
148
+ raw.event.detail.contact = { _attributes: { callsign: 'UNKNOWN' } };
149
+ if (!raw.event.detail.contact._attributes)
150
+ raw.event.detail.contact._attributes = { callsign: 'UNKNOWN' };
151
+ const feat = {
152
+ id: raw.event._attributes.uid,
153
+ type: 'Feature',
154
+ properties: {
155
+ callsign: raw.event.detail.contact._attributes.callsign || 'UNKNOWN',
156
+ center: [Number(raw.event.point._attributes.lon), Number(raw.event.point._attributes.lat), Number(raw.event.point._attributes.hae)],
157
+ type: raw.event._attributes.type,
158
+ how: raw.event._attributes.how || '',
159
+ time: raw.event._attributes.time,
160
+ start: raw.event._attributes.start,
161
+ stale: raw.event._attributes.stale,
162
+ },
163
+ geometry: {
164
+ type: 'Point',
165
+ coordinates: [Number(raw.event.point._attributes.lon), Number(raw.event.point._attributes.lat), Number(raw.event.point._attributes.hae)]
166
+ }
167
+ };
168
+ const contact = JSON.parse(JSON.stringify(raw.event.detail.contact._attributes));
169
+ delete contact.callsign;
170
+ if (Object.keys(contact).length) {
171
+ feat.properties.contact = contact;
172
+ }
173
+ if (cot.creator()) {
174
+ feat.properties.creator = cot.creator();
175
+ }
176
+ if (raw.event.detail.remarks && raw.event.detail.remarks._text) {
177
+ feat.properties.remarks = raw.event.detail.remarks._text;
178
+ }
179
+ if (raw.event.detail.fileshare) {
180
+ feat.properties.fileshare = raw.event.detail.fileshare._attributes;
181
+ if (feat.properties.fileshare && typeof feat.properties.fileshare.sizeInBytes === 'string') {
182
+ feat.properties.fileshare.sizeInBytes = parseInt(feat.properties.fileshare.sizeInBytes);
183
+ }
184
+ }
185
+ if (raw.event.detail.__milsym) {
186
+ feat.properties.milsym = {
187
+ id: raw.event.detail.__milsym._attributes.id
188
+ };
189
+ }
190
+ if (raw.event.detail.sensor) {
191
+ feat.properties.sensor = raw.event.detail.sensor._attributes;
192
+ }
193
+ if (raw.event.detail.range) {
194
+ feat.properties.range = raw.event.detail.range._attributes.value;
195
+ }
196
+ if (raw.event.detail.bearing) {
197
+ feat.properties.bearing = raw.event.detail.bearing._attributes.value;
198
+ }
199
+ if (raw.event.detail.__video && raw.event.detail.__video._attributes) {
200
+ feat.properties.video = raw.event.detail.__video._attributes;
201
+ if (raw.event.detail.__video.ConnectionEntry) {
202
+ feat.properties.video.connection = raw.event.detail.__video.ConnectionEntry._attributes;
203
+ }
204
+ }
205
+ if (raw.event.detail.__geofence) {
206
+ feat.properties.geofence = raw.event.detail.__geofence._attributes;
207
+ }
208
+ if (raw.event.detail.ackrequest) {
209
+ feat.properties.ackrequest = raw.event.detail.ackrequest._attributes;
210
+ }
211
+ if (raw.event.detail.attachment_list) {
212
+ feat.properties.attachments = JSON.parse(raw.event.detail.attachment_list._attributes.hashes);
213
+ }
214
+ if (raw.event.detail.link) {
215
+ if (!Array.isArray(raw.event.detail.link))
216
+ raw.event.detail.link = [raw.event.detail.link];
217
+ feat.properties.links = raw.event.detail.link.filter((link) => {
218
+ return !!link._attributes.url;
219
+ }).map((link) => {
220
+ return link._attributes;
221
+ });
222
+ if (!feat.properties.links || !feat.properties.links.length)
223
+ delete feat.properties.links;
224
+ }
225
+ if (raw.event.detail.archive) {
226
+ feat.properties.archived = true;
227
+ }
228
+ if (raw.event.detail.__chat) {
229
+ feat.properties.chat = {
230
+ ...raw.event.detail.__chat._attributes,
231
+ chatgrp: raw.event.detail.__chat.chatgrp
232
+ };
233
+ }
234
+ if (raw.event.detail.track && raw.event.detail.track._attributes) {
235
+ if (raw.event.detail.track._attributes.course)
236
+ feat.properties.course = Number(raw.event.detail.track._attributes.course);
237
+ if (raw.event.detail.track._attributes.slope)
238
+ feat.properties.slope = Number(raw.event.detail.track._attributes.slope);
239
+ if (raw.event.detail.track._attributes.course)
240
+ feat.properties.speed = Number(raw.event.detail.track._attributes.speed);
241
+ }
242
+ if (raw.event.detail.marti && raw.event.detail.marti.dest) {
243
+ if (!Array.isArray(raw.event.detail.marti.dest))
244
+ raw.event.detail.marti.dest = [raw.event.detail.marti.dest];
245
+ const dest = raw.event.detail.marti.dest.map((d) => {
246
+ return { ...d._attributes };
247
+ });
248
+ feat.properties.dest = dest.length === 1 ? dest[0] : dest;
249
+ }
250
+ if (raw.event.detail.usericon && raw.event.detail.usericon._attributes && raw.event.detail.usericon._attributes.iconsetpath) {
251
+ feat.properties.icon = raw.event.detail.usericon._attributes.iconsetpath;
252
+ }
253
+ if (raw.event.detail.uid && raw.event.detail.uid._attributes && raw.event.detail.uid._attributes.Droid) {
254
+ feat.properties.droid = raw.event.detail.uid._attributes.Droid;
255
+ }
256
+ if (raw.event.detail.takv && raw.event.detail.takv._attributes) {
257
+ feat.properties.takv = raw.event.detail.takv._attributes;
258
+ }
259
+ if (raw.event.detail.__group && raw.event.detail.__group._attributes) {
260
+ feat.properties.group = raw.event.detail.__group._attributes;
261
+ }
262
+ if (raw.event.detail['_flow-tags_'] && raw.event.detail['_flow-tags_']._attributes) {
263
+ feat.properties.flow = raw.event.detail['_flow-tags_']._attributes;
264
+ }
265
+ if (raw.event.detail.status && raw.event.detail.status._attributes) {
266
+ feat.properties.status = raw.event.detail.status._attributes;
267
+ }
268
+ if (raw.event.detail.mission && raw.event.detail.mission._attributes) {
269
+ const mission = {
270
+ ...raw.event.detail.mission._attributes
271
+ };
272
+ if (raw.event.detail.mission && raw.event.detail.mission.MissionChanges) {
273
+ const changes = Array.isArray(raw.event.detail.mission.MissionChanges)
274
+ ? raw.event.detail.mission.MissionChanges
275
+ : [raw.event.detail.mission.MissionChanges];
276
+ mission.missionChanges = [];
277
+ for (const change of changes) {
278
+ mission.missionChanges.push({
279
+ contentUid: change.MissionChange.contentUid._text,
280
+ creatorUid: change.MissionChange.creatorUid._text,
281
+ isFederatedChange: change.MissionChange.isFederatedChange._text,
282
+ missionName: change.MissionChange.missionName._text,
283
+ timestamp: change.MissionChange.timestamp._text,
284
+ type: change.MissionChange.type._text,
285
+ details: {
286
+ ...change.MissionChange.details._attributes,
287
+ ...change.MissionChange.details.location
288
+ ? change.MissionChange.details.location._attributes
289
+ : {}
290
+ }
291
+ });
292
+ }
293
+ }
294
+ if (raw.event.detail.mission && raw.event.detail.mission.missionLayer) {
295
+ const missionLayer = {};
296
+ if (raw.event.detail.mission.missionLayer.name && raw.event.detail.mission.missionLayer.name._text) {
297
+ missionLayer.name = raw.event.detail.mission.missionLayer.name._text;
298
+ }
299
+ if (raw.event.detail.mission.missionLayer.parentUid && raw.event.detail.mission.missionLayer.parentUid._text) {
300
+ missionLayer.parentUid = raw.event.detail.mission.missionLayer.parentUid._text;
301
+ }
302
+ if (raw.event.detail.mission.missionLayer.type && raw.event.detail.mission.missionLayer.type._text) {
303
+ missionLayer.type = raw.event.detail.mission.missionLayer.type._text;
304
+ }
305
+ if (raw.event.detail.mission.missionLayer.uid && raw.event.detail.mission.missionLayer.uid._text) {
306
+ missionLayer.uid = raw.event.detail.mission.missionLayer.uid._text;
307
+ }
308
+ mission.missionLayer = missionLayer;
309
+ }
310
+ feat.properties.mission = mission;
311
+ }
312
+ if (raw.event.detail.precisionlocation && raw.event.detail.precisionlocation._attributes) {
313
+ feat.properties.precisionlocation = raw.event.detail.precisionlocation._attributes;
314
+ }
315
+ // Line or Polygon style types
316
+ if (['u-d-f', 'u-d-r', 'b-m-r', 'u-rb-a'].includes(raw.event._attributes.type) && Array.isArray(raw.event.detail.link)) {
317
+ const coordinates = [];
318
+ for (const l of raw.event.detail.link) {
319
+ if (!l._attributes.point)
320
+ continue;
321
+ coordinates.push(l._attributes.point.split(',').map((p) => { return Number(p.trim()); }).splice(0, 2).reverse());
322
+ }
323
+ if (raw.event.detail.strokeColor && raw.event.detail.strokeColor._attributes && raw.event.detail.strokeColor._attributes.value) {
324
+ const stroke = new Color(Number(raw.event.detail.strokeColor._attributes.value));
325
+ feat.properties.stroke = stroke.as_hex();
326
+ feat.properties['stroke-opacity'] = stroke.as_opacity() / 255;
327
+ }
328
+ if (raw.event.detail.strokeWeight && raw.event.detail.strokeWeight._attributes && raw.event.detail.strokeWeight._attributes.value) {
329
+ feat.properties['stroke-width'] = Number(raw.event.detail.strokeWeight._attributes.value);
330
+ }
331
+ if (raw.event.detail.strokeStyle && raw.event.detail.strokeStyle._attributes && raw.event.detail.strokeStyle._attributes.value) {
332
+ feat.properties['stroke-style'] = raw.event.detail.strokeStyle._attributes.value;
333
+ }
334
+ // Range & Bearing Line
335
+ if (raw.event._attributes.type === 'u-rb-a') {
336
+ const detail = cot.detail();
337
+ if (!detail.range)
338
+ throw new Error('Range value not provided');
339
+ if (!detail.bearing)
340
+ throw new Error('Bearing value not provided');
341
+ // TODO Support inclination
342
+ const dest = destination(cot.position(), detail.range._attributes.value / 1000, detail.bearing._attributes.value).geometry.coordinates;
343
+ feat.geometry = {
344
+ type: 'LineString',
345
+ coordinates: [cot.position(), dest]
346
+ };
347
+ }
348
+ else if (raw.event._attributes.type === 'u-d-r' || (coordinates[0][0] === coordinates[coordinates.length - 1][0] && coordinates[0][1] === coordinates[coordinates.length - 1][1])) {
349
+ if (raw.event._attributes.type === 'u-d-r') {
350
+ // CoT rectangles are only 4 points - GeoJSON needs to be closed
351
+ coordinates.push(coordinates[0]);
352
+ }
353
+ feat.geometry = {
354
+ type: 'Polygon',
355
+ coordinates: [coordinates]
356
+ };
357
+ if (raw.event.detail.fillColor && raw.event.detail.fillColor._attributes && raw.event.detail.fillColor._attributes.value) {
358
+ const fill = new Color(Number(raw.event.detail.fillColor._attributes.value));
359
+ feat.properties['fill-opacity'] = fill.as_opacity() / 255;
360
+ feat.properties['fill'] = fill.as_hex();
361
+ }
362
+ }
363
+ else {
364
+ feat.geometry = {
365
+ type: 'LineString',
366
+ coordinates
367
+ };
368
+ }
369
+ }
370
+ else if (raw.event._attributes.type.startsWith('u-d-c-c')) {
371
+ if (!raw.event.detail.shape)
372
+ throw new Err(400, null, 'u-d-c-c (Circle) must define shape value');
373
+ if (!raw.event.detail.shape.ellipse
374
+ || !raw.event.detail.shape.ellipse._attributes)
375
+ throw new Err(400, null, 'u-d-c-c (Circle) must define ellipse shape value');
376
+ const ellipse = {
377
+ major: Number(raw.event.detail.shape.ellipse._attributes.major),
378
+ minor: Number(raw.event.detail.shape.ellipse._attributes.minor),
379
+ angle: Number(raw.event.detail.shape.ellipse._attributes.angle)
380
+ };
381
+ feat.geometry = Truncate(Ellipse(feat.geometry.coordinates, Number(ellipse.major) / 1000, Number(ellipse.minor) / 1000, {
382
+ angle: ellipse.angle
383
+ }), {
384
+ precision: COORDINATE_PRECISION,
385
+ mutate: true
386
+ }).geometry;
387
+ feat.properties.shape = {};
388
+ feat.properties.shape.ellipse = ellipse;
389
+ }
390
+ else if (raw.event._attributes.type.startsWith('b-m-p-s-p-i')) {
391
+ // TODO: Currently the "shape" tag is only parsed here - asking ARA for clarification if it is a general use tag
392
+ if (raw.event.detail.shape && raw.event.detail.shape.polyline && raw.event.detail.shape.polyline.vertex) {
393
+ const coordinates = [];
394
+ const vertices = Array.isArray(raw.event.detail.shape.polyline.vertex) ? raw.event.detail.shape.polyline.vertex : [raw.event.detail.shape.polyline.vertex];
395
+ for (const v of vertices) {
396
+ coordinates.push([Number(v._attributes.lon), Number(v._attributes.lat)]);
397
+ }
398
+ if (coordinates.length === 1) {
399
+ feat.geometry = { type: 'Point', coordinates: coordinates[0] };
400
+ }
401
+ else if (raw.event.detail.shape.polyline._attributes && raw.event.detail.shape.polyline._attributes.closed === true) {
402
+ coordinates.push(coordinates[0]);
403
+ feat.geometry = { type: 'Polygon', coordinates: [coordinates] };
404
+ }
405
+ else {
406
+ feat.geometry = { type: 'LineString', coordinates };
407
+ }
408
+ }
409
+ if (raw.event.detail.shape
410
+ && raw.event.detail.shape.polyline
411
+ && raw.event.detail.shape.polyline._attributes
412
+ && raw.event.detail.shape.polyline._attributes) {
413
+ if (raw.event.detail.shape.polyline._attributes.fillColor) {
414
+ const fill = new Color(Number(raw.event.detail.shape.polyline._attributes.fillColor));
415
+ feat.properties['fill-opacity'] = fill.as_opacity() / 255;
416
+ feat.properties['fill'] = fill.as_hex();
417
+ }
418
+ if (raw.event.detail.shape.polyline._attributes.color) {
419
+ const stroke = new Color(Number(raw.event.detail.shape.polyline._attributes.color));
420
+ feat.properties.stroke = stroke.as_hex();
421
+ feat.properties['stroke-opacity'] = stroke.as_opacity() / 255;
422
+ }
423
+ }
424
+ }
425
+ if (raw.event.detail.color && raw.event.detail.color._attributes && raw.event.detail.color._attributes.argb) {
426
+ const color = new Color(Number(raw.event.detail.color._attributes.argb));
427
+ feat.properties['marker-color'] = color.as_hex();
428
+ feat.properties['marker-opacity'] = color.as_opacity() / 255;
429
+ }
430
+ feat.properties.metadata = cot.metadata;
431
+ feat.path = cot.path;
432
+ return feat;
433
+ }
434
+ /**
435
+ * Parse an ATAK compliant Protobuf to a JS Object
436
+ */
437
+ static from_proto(raw, version = 1, opts = {}) {
438
+ const ProtoMessage = RootMessage.lookupType(`atakmap.commoncommo.protobuf.v${version}.TakMessage`);
439
+ // TODO Type this
440
+ const msg = ProtoMessage.decode(raw);
441
+ if (!msg.cotEvent)
442
+ throw new Err(400, null, 'No cotEvent Data');
443
+ const detail = {};
444
+ const metadata = {};
445
+ for (const key in msg.cotEvent.detail) {
446
+ if (key === 'xmlDetail') {
447
+ const parsed = xml2js(`<detail>${msg.cotEvent.detail.xmlDetail}</detail>`, { compact: true });
448
+ Object.assign(detail, parsed.detail);
449
+ if (detail.metadata) {
450
+ for (const key in detail.metadata) {
451
+ metadata[key] = detail.metadata[key]._text;
452
+ }
453
+ delete detail.metadata;
454
+ }
455
+ }
456
+ else if (key === 'group') {
457
+ if (msg.cotEvent.detail[key]) {
458
+ detail.__group = { _attributes: msg.cotEvent.detail[key] };
459
+ }
460
+ }
461
+ else if (['contact', 'precisionlocation', 'status', 'takv', 'track'].includes(key)) {
462
+ if (msg.cotEvent.detail[key]) {
463
+ detail[key] = { _attributes: msg.cotEvent.detail[key] };
464
+ }
465
+ }
466
+ }
467
+ const cot = new CoT({
468
+ event: {
469
+ _attributes: {
470
+ version: '2.0',
471
+ uid: msg.cotEvent.uid, type: msg.cotEvent.type, how: msg.cotEvent.how,
472
+ qos: msg.cotEvent.qos, opex: msg.cotEvent.opex, access: msg.cotEvent.access,
473
+ time: new Date(msg.cotEvent.sendTime.toNumber()).toISOString(),
474
+ start: new Date(msg.cotEvent.startTime.toNumber()).toISOString(),
475
+ stale: new Date(msg.cotEvent.staleTime.toNumber()).toISOString(),
476
+ },
477
+ detail,
478
+ point: {
479
+ _attributes: {
480
+ lat: msg.cotEvent.lat,
481
+ lon: msg.cotEvent.lon,
482
+ hae: msg.cotEvent.hae,
483
+ le: msg.cotEvent.le,
484
+ ce: msg.cotEvent.ce,
485
+ },
486
+ }
487
+ }
488
+ }, opts);
489
+ cot.metadata = metadata;
490
+ return this.validate(cot);
491
+ }
492
+ /**
493
+ * Return an CoT Message given a GeoJSON Feature
494
+ *
495
+ * @param {Object} feature GeoJSON Point Feature
496
+ *
497
+ * @return {CoT}
498
+ */
499
+ static from_geojson(feature, opts = {}) {
500
+ checkFeat(feature);
501
+ if (checkFeat.errors)
502
+ throw new Err(400, null, `${checkFeat.errors[0].message} (${checkFeat.errors[0].instancePath})`);
503
+ const cot = {
504
+ event: {
505
+ _attributes: Util.cot_event_attr(feature.properties.type || 'a-f-G', feature.properties.how || 'm-g', feature.properties.time, feature.properties.start, feature.properties.stale),
506
+ point: Util.cot_point(),
507
+ detail: Util.cot_event_detail(feature.properties.callsign)
508
+ }
509
+ };
510
+ if (feature.id)
511
+ cot.event._attributes.uid = String(feature.id);
512
+ if (feature.properties.callsign && !feature.id)
513
+ cot.event._attributes.uid = feature.properties.callsign;
514
+ if (!cot.event.detail)
515
+ cot.event.detail = {};
516
+ if (feature.properties.droid) {
517
+ cot.event.detail.uid = { _attributes: { Droid: feature.properties.droid } };
518
+ }
519
+ if (feature.properties.archived) {
520
+ cot.event.detail.archive = { _attributes: {} };
521
+ }
522
+ if (feature.properties.links) {
523
+ if (!cot.event.detail.link)
524
+ cot.event.detail.link = [];
525
+ else if (!Array.isArray(cot.event.detail.link))
526
+ cot.event.detail.link = [cot.event.detail.link];
527
+ cot.event.detail.link.push(...feature.properties.links.map((link) => {
528
+ return { _attributes: link };
529
+ }));
530
+ }
531
+ if (feature.properties.dest) {
532
+ const dest = !Array.isArray(feature.properties.dest) ? [feature.properties.dest] : feature.properties.dest;
533
+ cot.event.detail.marti = {
534
+ dest: dest.map((dest) => {
535
+ return { _attributes: { ...dest } };
536
+ })
537
+ };
538
+ }
539
+ if (feature.properties.takv) {
540
+ cot.event.detail.takv = { _attributes: { ...feature.properties.takv } };
541
+ }
542
+ if (feature.properties.creator) {
543
+ cot.event.detail.creator = { _attributes: { ...feature.properties.creator } };
544
+ }
545
+ if (feature.properties.range !== undefined) {
546
+ cot.event.detail.range = { _attributes: { value: feature.properties.range } };
547
+ }
548
+ if (feature.properties.bearing !== undefined) {
549
+ cot.event.detail.bearing = { _attributes: { value: feature.properties.bearing } };
550
+ }
551
+ if (feature.properties.geofence) {
552
+ cot.event.detail.__geofence = { _attributes: { ...feature.properties.geofence } };
553
+ }
554
+ if (feature.properties.milsym) {
555
+ cot.event.detail.__milsym = { _attributes: { id: feature.properties.milsym.id } };
556
+ }
557
+ if (feature.properties.sensor) {
558
+ cot.event.detail.sensor = { _attributes: { ...feature.properties.sensor } };
559
+ }
560
+ if (feature.properties.ackrequest) {
561
+ cot.event.detail.ackrequest = { _attributes: { ...feature.properties.ackrequest } };
562
+ }
563
+ if (feature.properties.video) {
564
+ if (feature.properties.video.connection) {
565
+ const video = JSON.parse(JSON.stringify(feature.properties.video));
566
+ const connection = video.connection;
567
+ delete video.connection;
568
+ cot.event.detail.__video = {
569
+ _attributes: { ...video },
570
+ ConnectionEntry: {
571
+ _attributes: connection
572
+ }
573
+ };
574
+ }
575
+ else {
576
+ cot.event.detail.__video = { _attributes: { ...feature.properties.video } };
577
+ }
578
+ }
579
+ if (feature.properties.attachments) {
580
+ cot.event.detail.attachment_list = { _attributes: { hashes: JSON.stringify(feature.properties.attachments) } };
581
+ }
582
+ if (feature.properties.contact) {
583
+ cot.event.detail.contact = {
584
+ _attributes: {
585
+ ...feature.properties.contact,
586
+ callsign: feature.properties.callsign || 'UNKNOWN',
587
+ }
588
+ };
589
+ }
590
+ if (feature.properties.fileshare) {
591
+ cot.event.detail.fileshare = { _attributes: { ...feature.properties.fileshare } };
592
+ }
593
+ if (feature.properties.course !== undefined || feature.properties.speed !== undefined || feature.properties.slope !== undefined) {
594
+ cot.event.detail.track = {
595
+ _attributes: Util.cot_track_attr(feature.properties.course, feature.properties.speed, feature.properties.slope)
596
+ };
597
+ }
598
+ if (feature.properties.group) {
599
+ cot.event.detail.__group = { _attributes: { ...feature.properties.group } };
600
+ }
601
+ if (feature.properties.flow) {
602
+ cot.event.detail['_flow-tags_'] = { _attributes: { ...feature.properties.flow } };
603
+ }
604
+ if (feature.properties.status) {
605
+ cot.event.detail.status = { _attributes: { ...feature.properties.status } };
606
+ }
607
+ if (feature.properties.precisionlocation) {
608
+ cot.event.detail.precisionlocation = { _attributes: { ...feature.properties.precisionlocation } };
609
+ }
610
+ if (feature.properties.icon) {
611
+ cot.event.detail.usericon = { _attributes: { iconsetpath: feature.properties.icon } };
612
+ }
613
+ if (feature.properties.mission) {
614
+ cot.event.detail.mission = {
615
+ _attributes: {
616
+ type: feature.properties.mission.type,
617
+ guid: feature.properties.mission.guid,
618
+ tool: feature.properties.mission.tool,
619
+ name: feature.properties.mission.name,
620
+ authorUid: feature.properties.mission.authorUid,
621
+ }
622
+ };
623
+ if (feature.properties.mission.missionLayer) {
624
+ cot.event.detail.mission.missionLayer = {};
625
+ if (feature.properties.mission.missionLayer.name) {
626
+ cot.event.detail.mission.missionLayer.name = { _text: feature.properties.mission.missionLayer.name };
627
+ }
628
+ if (feature.properties.mission.missionLayer.parentUid) {
629
+ cot.event.detail.mission.missionLayer.parentUid = { _text: feature.properties.mission.missionLayer.parentUid };
630
+ }
631
+ if (feature.properties.mission.missionLayer.type) {
632
+ cot.event.detail.mission.missionLayer.type = { _text: feature.properties.mission.missionLayer.type };
633
+ }
634
+ if (feature.properties.mission.missionLayer.uid) {
635
+ cot.event.detail.mission.missionLayer.uid = { _text: feature.properties.mission.missionLayer.uid };
636
+ }
637
+ }
638
+ }
639
+ cot.event.detail.remarks = { _attributes: {}, _text: feature.properties.remarks || '' };
640
+ if (!feature.geometry) {
641
+ throw new Err(400, null, 'Must have Geometry');
642
+ }
643
+ else if (!['Point', 'Polygon', 'LineString'].includes(feature.geometry.type)) {
644
+ throw new Err(400, null, 'Unsupported Geometry Type');
645
+ }
646
+ if (feature.geometry.type === 'Point') {
647
+ cot.event.point._attributes.lon = feature.geometry.coordinates[0];
648
+ cot.event.point._attributes.lat = feature.geometry.coordinates[1];
649
+ cot.event.point._attributes.hae = feature.geometry.coordinates[2] || 0.0;
650
+ if (feature.properties['marker-color']) {
651
+ const color = new Color(feature.properties['marker-color'] || -1761607936);
652
+ color.a = feature.properties['marker-opacity'] !== undefined ? feature.properties['marker-opacity'] * 255 : 128;
653
+ cot.event.detail.color = { _attributes: { argb: color.as_32bit() } };
654
+ }
655
+ }
656
+ else if (feature.geometry.type === 'Polygon' && feature.properties.type === 'u-d-c-c') {
657
+ if (!feature.properties.shape || !feature.properties.shape.ellipse) {
658
+ throw new Err(400, null, 'u-d-c-c (Circle) must define a feature.properties.shape.ellipse property');
659
+ }
660
+ cot.event.detail.shape = { ellipse: { _attributes: feature.properties.shape.ellipse } };
661
+ if (feature.properties.center) {
662
+ cot.event.point._attributes.lon = feature.properties.center[0];
663
+ cot.event.point._attributes.lat = feature.properties.center[1];
664
+ }
665
+ else {
666
+ const centre = PointOnFeature(feature);
667
+ cot.event.point._attributes.lon = centre.geometry.coordinates[0];
668
+ cot.event.point._attributes.lat = centre.geometry.coordinates[1];
669
+ cot.event.point._attributes.hae = 0.0;
670
+ }
671
+ }
672
+ else if (['Polygon', 'LineString'].includes(feature.geometry.type)) {
673
+ const stroke = new Color(feature.properties.stroke || -1761607936);
674
+ stroke.a = feature.properties['stroke-opacity'] !== undefined ? feature.properties['stroke-opacity'] * 255 : 128;
675
+ cot.event.detail.strokeColor = { _attributes: { value: stroke.as_32bit() } };
676
+ if (!feature.properties['stroke-width'])
677
+ feature.properties['stroke-width'] = 3;
678
+ cot.event.detail.strokeWeight = { _attributes: {
679
+ value: feature.properties['stroke-width']
680
+ } };
681
+ if (!feature.properties['stroke-style'])
682
+ feature.properties['stroke-style'] = 'solid';
683
+ cot.event.detail.strokeStyle = { _attributes: {
684
+ value: feature.properties['stroke-style']
685
+ } };
686
+ if (feature.geometry.type === 'LineString' && feature.properties.type === 'b-m-r') {
687
+ cot.event._attributes.type = 'b-m-r';
688
+ if (!cot.event.detail.link) {
689
+ cot.event.detail.link = [];
690
+ }
691
+ else if (!Array.isArray(cot.event.detail.link)) {
692
+ cot.event.detail.link = [cot.event.detail.link];
693
+ }
694
+ cot.event.detail.__routeinfo = {
695
+ __navcues: {
696
+ __navcue: []
697
+ }
698
+ };
699
+ for (const coord of feature.geometry.coordinates) {
700
+ cot.event.detail.link.push({
701
+ _attributes: {
702
+ type: 'b-m-p-c',
703
+ uid: crypto.randomUUID(),
704
+ callsign: "",
705
+ point: `${coord[1]},${coord[0]}`
706
+ }
707
+ });
708
+ }
709
+ }
710
+ else if (feature.geometry.type === 'LineString') {
711
+ cot.event._attributes.type = 'u-d-f';
712
+ if (!cot.event.detail.link) {
713
+ cot.event.detail.link = [];
714
+ }
715
+ else if (!Array.isArray(cot.event.detail.link)) {
716
+ cot.event.detail.link = [cot.event.detail.link];
717
+ }
718
+ for (const coord of feature.geometry.coordinates) {
719
+ cot.event.detail.link.push({
720
+ _attributes: { point: `${coord[1]},${coord[0]}` }
721
+ });
722
+ }
723
+ }
724
+ else if (feature.geometry.type === 'Polygon') {
725
+ cot.event._attributes.type = 'u-d-f';
726
+ if (!cot.event.detail.link)
727
+ cot.event.detail.link = [];
728
+ else if (!Array.isArray(cot.event.detail.link))
729
+ cot.event.detail.link = [cot.event.detail.link];
730
+ // Inner rings are not yet supported
731
+ for (const coord of feature.geometry.coordinates[0]) {
732
+ cot.event.detail.link.push({
733
+ _attributes: { point: `${coord[1]},${coord[0]}` }
734
+ });
735
+ }
736
+ const fill = new Color(feature.properties.fill || -1761607936);
737
+ fill.a = feature.properties['fill-opacity'] !== undefined ? feature.properties['fill-opacity'] * 255 : 128;
738
+ cot.event.detail.fillColor = { _attributes: { value: fill.as_32bit() } };
739
+ }
740
+ cot.event.detail.labels_on = { _attributes: { value: false } };
741
+ cot.event.detail.tog = { _attributes: { enabled: '0' } };
742
+ if (feature.properties.center && Array.isArray(feature.properties.center) && feature.properties.center.length >= 2) {
743
+ cot.event.point._attributes.lon = feature.properties.center[0];
744
+ cot.event.point._attributes.lat = feature.properties.center[1];
745
+ if (feature.properties.center.length >= 3) {
746
+ cot.event.point._attributes.hae = feature.properties.center[2] || 0.0;
747
+ }
748
+ else {
749
+ cot.event.point._attributes.hae = 0.0;
750
+ }
751
+ }
752
+ else {
753
+ const centre = PointOnFeature(feature);
754
+ cot.event.point._attributes.lon = centre.geometry.coordinates[0];
755
+ cot.event.point._attributes.lat = centre.geometry.coordinates[1];
756
+ cot.event.point._attributes.hae = 0.0;
757
+ }
758
+ }
759
+ const newcot = new CoT(cot, opts);
760
+ if (feature.properties.metadata) {
761
+ newcot.metadata = feature.properties.metadata;
762
+ }
763
+ if (feature.path) {
764
+ newcot.path = feature.path;
765
+ }
766
+ return this.validate(newcot);
767
+ }
768
+ }
769
+ //# sourceMappingURL=parser.js.map