@tak-ps/node-cot 12.36.0 → 13.0.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.
package/dist/lib/cot.js CHANGED
@@ -1,38 +1,8 @@
1
1
  import crypto from 'node:crypto';
2
- import protobuf from 'protobufjs';
3
2
  import Err from '@openaddresses/batch-error';
4
- import { diff } from 'json-diff-ts';
5
- import xmljs from 'xml-js';
6
- import { InputFeature, } from './types/feature.js';
7
3
  import Sensor from './sensor.js';
8
- import PointOnFeature from '@turf/point-on-feature';
9
- import Truncate from '@turf/truncate';
10
- import { destination } from '@turf/destination';
11
- import Ellipse from '@turf/ellipse';
12
4
  import Util from './utils/util.js';
13
- import Color from './utils/color.js';
14
5
  import JSONCoT, { Detail } from './types/types.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
6
  /**
37
7
  * Convert to and from an XML CoT message
38
8
  * @class
@@ -50,27 +20,14 @@ export default class CoT {
50
20
  // Does the CoT belong to a folder - defaults to "/"
51
21
  path;
52
22
  constructor(cot, opts = {}) {
53
- if (typeof cot === 'string' || cot instanceof Buffer) {
54
- const raw = xmljs.xml2js(String(cot), { compact: true });
55
- this.raw = raw;
56
- }
57
- else {
58
- this.raw = cot;
59
- }
23
+ this.raw = cot;
60
24
  this.metadata = {};
61
25
  this.path = '/';
62
- if (!this.raw.event._attributes.uid)
26
+ if (!this.raw.event._attributes.uid) {
63
27
  this.raw.event._attributes.uid = Util.cot_uuid();
64
- if (process.env.DEBUG_COTS)
65
- console.log(JSON.stringify(this.raw));
66
- checkXML(this.raw);
67
- if (checkXML.errors)
68
- throw new Err(400, null, `${checkXML.errors[0].message} (${checkXML.errors[0].instancePath})`);
28
+ }
69
29
  if (!this.raw.event.detail)
70
30
  this.raw.event.detail = {};
71
- if (!this.raw.event.detail['_flow-tags_'])
72
- this.raw.event.detail['_flow-tags_'] = {};
73
- this.raw.event.detail['_flow-tags_'][`NodeCoT-${pkg.version}`] = new Date().toISOString();
74
31
  if (this.raw.event.detail.archive && Object.keys(this.raw.event.detail.archive).length === 0) {
75
32
  this.raw.event.detail.archive = { _attributes: {} };
76
33
  }
@@ -82,42 +39,8 @@ export default class CoT {
82
39
  time: opts.creator instanceof CoT ? new Date() : opts.creator.time
83
40
  });
84
41
  }
85
- }
86
- /**
87
- * Detect difference between CoT messages
88
- * Note: This diffs based on GeoJSON Representation of message
89
- * So if unknown properties are present they will be excluded from the diff
90
- */
91
- isDiff(cot, opts = {
92
- diffMetadata: false,
93
- diffStaleStartTime: false,
94
- diffDest: false,
95
- diffFlow: false
96
- }) {
97
- const a = this.to_geojson();
98
- const b = cot.to_geojson();
99
- if (!opts.diffDest) {
100
- delete a.properties.dest;
101
- delete b.properties.dest;
102
- }
103
- if (!opts.diffMetadata) {
104
- delete a.properties.metadata;
105
- delete b.properties.metadata;
106
- }
107
- if (!opts.diffFlow) {
108
- delete a.properties.flow;
109
- delete b.properties.flow;
110
- }
111
- if (!opts.diffStaleStartTime) {
112
- delete a.properties.time;
113
- delete a.properties.stale;
114
- delete a.properties.start;
115
- delete b.properties.time;
116
- delete b.properties.stale;
117
- delete b.properties.start;
118
- }
119
- const diffs = diff(a, b);
120
- return diffs.length > 0;
42
+ if (process.env.DEBUG_COTS)
43
+ console.log(JSON.stringify(this.raw));
121
44
  }
122
45
  /**
123
46
  * Returns or sets the UID of the CoT
@@ -241,8 +164,8 @@ export default class CoT {
241
164
  }
242
165
  position(position) {
243
166
  if (position) {
244
- this.raw.event.point._attributes.lon = String(position[0]);
245
- this.raw.event.point._attributes.lat = String(position[1]);
167
+ this.raw.event.point._attributes.lon = position[0];
168
+ this.raw.event.point._attributes.lat = position[1];
246
169
  }
247
170
  return [
248
171
  Number(this.raw.event.point._attributes.lon),
@@ -299,332 +222,6 @@ export default class CoT {
299
222
  detail.link = linkArr;
300
223
  return this;
301
224
  }
302
- /**
303
- * Return an ATAK Compliant Protobuf
304
- */
305
- to_proto(version = 1) {
306
- if (version < 1 || version > 1)
307
- throw new Err(400, null, `Unsupported Proto Version: ${version}`);
308
- const ProtoMessage = RootMessage.lookupType(`atakmap.commoncommo.protobuf.v${version}.TakMessage`);
309
- // The spread operator is important to make sure the delete doesn't modify the underlying detail object
310
- const detail = { ...this.raw.event.detail };
311
- const msg = {
312
- cotEvent: {
313
- ...this.raw.event._attributes,
314
- sendTime: new Date(this.raw.event._attributes.time).getTime(),
315
- startTime: new Date(this.raw.event._attributes.start).getTime(),
316
- staleTime: new Date(this.raw.event._attributes.stale).getTime(),
317
- ...this.raw.event.point._attributes,
318
- detail: {
319
- xmlDetail: ''
320
- }
321
- }
322
- };
323
- let key;
324
- for (key in detail) {
325
- if (['contact', 'group', 'precisionlocation', 'status', 'takv', 'track'].includes(key)) {
326
- msg.cotEvent.detail[key] = detail[key]._attributes;
327
- delete detail[key];
328
- }
329
- }
330
- msg.cotEvent.detail.xmlDetail = xmljs.js2xml({
331
- ...detail,
332
- metadata: this.metadata
333
- }, { compact: true });
334
- return ProtoMessage.encode(msg).finish();
335
- }
336
- /**
337
- * Return a GeoJSON Feature from an XML CoT message
338
- */
339
- to_geojson() {
340
- const raw = JSON.parse(JSON.stringify(this.raw));
341
- if (!raw.event.detail)
342
- raw.event.detail = {};
343
- if (!raw.event.detail.contact)
344
- raw.event.detail.contact = { _attributes: { callsign: 'UNKNOWN' } };
345
- if (!raw.event.detail.contact._attributes)
346
- raw.event.detail.contact._attributes = { callsign: 'UNKNOWN' };
347
- const feat = {
348
- id: raw.event._attributes.uid,
349
- type: 'Feature',
350
- properties: {
351
- callsign: raw.event.detail.contact._attributes.callsign || 'UNKNOWN',
352
- center: [Number(raw.event.point._attributes.lon), Number(raw.event.point._attributes.lat), Number(raw.event.point._attributes.hae)],
353
- type: raw.event._attributes.type,
354
- how: raw.event._attributes.how || '',
355
- time: raw.event._attributes.time,
356
- start: raw.event._attributes.start,
357
- stale: raw.event._attributes.stale,
358
- },
359
- geometry: {
360
- type: 'Point',
361
- coordinates: [Number(raw.event.point._attributes.lon), Number(raw.event.point._attributes.lat), Number(raw.event.point._attributes.hae)]
362
- }
363
- };
364
- const contact = JSON.parse(JSON.stringify(raw.event.detail.contact._attributes));
365
- delete contact.callsign;
366
- if (Object.keys(contact).length) {
367
- feat.properties.contact = contact;
368
- }
369
- if (this.creator()) {
370
- feat.properties.creator = this.creator();
371
- }
372
- if (raw.event.detail.remarks && raw.event.detail.remarks._text) {
373
- feat.properties.remarks = raw.event.detail.remarks._text;
374
- }
375
- if (raw.event.detail.fileshare) {
376
- feat.properties.fileshare = raw.event.detail.fileshare._attributes;
377
- if (feat.properties.fileshare && typeof feat.properties.fileshare.sizeInBytes === 'string') {
378
- feat.properties.fileshare.sizeInBytes = parseInt(feat.properties.fileshare.sizeInBytes);
379
- }
380
- }
381
- if (raw.event.detail.sensor) {
382
- feat.properties.sensor = raw.event.detail.sensor._attributes;
383
- }
384
- if (raw.event.detail.range) {
385
- feat.properties.range = raw.event.detail.range._attributes.value;
386
- }
387
- if (raw.event.detail.bearing) {
388
- feat.properties.bearing = raw.event.detail.bearing._attributes.value;
389
- }
390
- if (raw.event.detail.__video && raw.event.detail.__video._attributes) {
391
- feat.properties.video = raw.event.detail.__video._attributes;
392
- if (raw.event.detail.__video.ConnectionEntry) {
393
- feat.properties.video.connection = raw.event.detail.__video.ConnectionEntry._attributes;
394
- }
395
- }
396
- if (raw.event.detail.__geofence) {
397
- feat.properties.geofence = raw.event.detail.__geofence._attributes;
398
- }
399
- if (raw.event.detail.ackrequest) {
400
- feat.properties.ackrequest = raw.event.detail.ackrequest._attributes;
401
- }
402
- if (raw.event.detail.attachment_list) {
403
- feat.properties.attachments = JSON.parse(raw.event.detail.attachment_list._attributes.hashes);
404
- }
405
- if (raw.event.detail.link) {
406
- if (!Array.isArray(raw.event.detail.link))
407
- raw.event.detail.link = [raw.event.detail.link];
408
- feat.properties.links = raw.event.detail.link.filter((link) => {
409
- return !!link._attributes.url;
410
- }).map((link) => {
411
- return link._attributes;
412
- });
413
- if (!feat.properties.links || !feat.properties.links.length)
414
- delete feat.properties.links;
415
- }
416
- if (raw.event.detail.archive) {
417
- feat.properties.archived = true;
418
- }
419
- if (raw.event.detail.__chat) {
420
- feat.properties.chat = {
421
- ...raw.event.detail.__chat._attributes,
422
- chatgrp: raw.event.detail.__chat.chatgrp
423
- };
424
- }
425
- if (raw.event.detail.track && raw.event.detail.track._attributes) {
426
- if (raw.event.detail.track._attributes.course)
427
- feat.properties.course = Number(raw.event.detail.track._attributes.course);
428
- if (raw.event.detail.track._attributes.slope)
429
- feat.properties.slope = Number(raw.event.detail.track._attributes.slope);
430
- if (raw.event.detail.track._attributes.course)
431
- feat.properties.speed = Number(raw.event.detail.track._attributes.speed);
432
- }
433
- if (raw.event.detail.marti && raw.event.detail.marti.dest) {
434
- if (!Array.isArray(raw.event.detail.marti.dest))
435
- raw.event.detail.marti.dest = [raw.event.detail.marti.dest];
436
- const dest = raw.event.detail.marti.dest.map((d) => {
437
- return { ...d._attributes };
438
- });
439
- feat.properties.dest = dest.length === 1 ? dest[0] : dest;
440
- }
441
- if (raw.event.detail.usericon && raw.event.detail.usericon._attributes && raw.event.detail.usericon._attributes.iconsetpath) {
442
- feat.properties.icon = raw.event.detail.usericon._attributes.iconsetpath;
443
- }
444
- if (raw.event.detail.uid && raw.event.detail.uid._attributes && raw.event.detail.uid._attributes.Droid) {
445
- feat.properties.droid = raw.event.detail.uid._attributes.Droid;
446
- }
447
- if (raw.event.detail.takv && raw.event.detail.takv._attributes) {
448
- feat.properties.takv = raw.event.detail.takv._attributes;
449
- }
450
- if (raw.event.detail.__group && raw.event.detail.__group._attributes) {
451
- feat.properties.group = raw.event.detail.__group._attributes;
452
- }
453
- if (raw.event.detail['_flow-tags_'] && raw.event.detail['_flow-tags_']._attributes) {
454
- feat.properties.flow = raw.event.detail['_flow-tags_']._attributes;
455
- }
456
- if (raw.event.detail.status && raw.event.detail.status._attributes) {
457
- feat.properties.status = raw.event.detail.status._attributes;
458
- }
459
- if (raw.event.detail.mission && raw.event.detail.mission._attributes) {
460
- const mission = {
461
- ...raw.event.detail.mission._attributes
462
- };
463
- if (raw.event.detail.mission && raw.event.detail.mission.MissionChanges) {
464
- const changes = Array.isArray(raw.event.detail.mission.MissionChanges)
465
- ? raw.event.detail.mission.MissionChanges
466
- : [raw.event.detail.mission.MissionChanges];
467
- mission.missionChanges = [];
468
- for (const change of changes) {
469
- mission.missionChanges.push({
470
- contentUid: change.MissionChange.contentUid._text,
471
- creatorUid: change.MissionChange.creatorUid._text,
472
- isFederatedChange: change.MissionChange.isFederatedChange._text,
473
- missionName: change.MissionChange.missionName._text,
474
- timestamp: change.MissionChange.timestamp._text,
475
- type: change.MissionChange.type._text,
476
- details: {
477
- ...change.MissionChange.details._attributes,
478
- ...change.MissionChange.details.location
479
- ? change.MissionChange.details.location._attributes
480
- : {}
481
- }
482
- });
483
- }
484
- }
485
- if (raw.event.detail.mission && raw.event.detail.mission.missionLayer) {
486
- const missionLayer = {};
487
- if (raw.event.detail.mission.missionLayer.name && raw.event.detail.mission.missionLayer.name._text) {
488
- missionLayer.name = raw.event.detail.mission.missionLayer.name._text;
489
- }
490
- if (raw.event.detail.mission.missionLayer.parentUid && raw.event.detail.mission.missionLayer.parentUid._text) {
491
- missionLayer.parentUid = raw.event.detail.mission.missionLayer.parentUid._text;
492
- }
493
- if (raw.event.detail.mission.missionLayer.type && raw.event.detail.mission.missionLayer.type._text) {
494
- missionLayer.type = raw.event.detail.mission.missionLayer.type._text;
495
- }
496
- if (raw.event.detail.mission.missionLayer.uid && raw.event.detail.mission.missionLayer.uid._text) {
497
- missionLayer.uid = raw.event.detail.mission.missionLayer.uid._text;
498
- }
499
- mission.missionLayer = missionLayer;
500
- }
501
- feat.properties.mission = mission;
502
- }
503
- if (raw.event.detail.precisionlocation && raw.event.detail.precisionlocation._attributes) {
504
- feat.properties.precisionlocation = raw.event.detail.precisionlocation._attributes;
505
- }
506
- // Line or Polygon style types
507
- if (['u-d-f', 'u-d-r', 'b-m-r', 'u-rb-a'].includes(raw.event._attributes.type) && Array.isArray(raw.event.detail.link)) {
508
- const coordinates = [];
509
- for (const l of raw.event.detail.link) {
510
- if (!l._attributes.point)
511
- continue;
512
- coordinates.push(l._attributes.point.split(',').map((p) => { return Number(p.trim()); }).splice(0, 2).reverse());
513
- }
514
- if (raw.event.detail.strokeColor && raw.event.detail.strokeColor._attributes && raw.event.detail.strokeColor._attributes.value) {
515
- const stroke = new Color(Number(raw.event.detail.strokeColor._attributes.value));
516
- feat.properties.stroke = stroke.as_hex();
517
- feat.properties['stroke-opacity'] = stroke.as_opacity() / 255;
518
- }
519
- if (raw.event.detail.strokeWeight && raw.event.detail.strokeWeight._attributes && raw.event.detail.strokeWeight._attributes.value) {
520
- feat.properties['stroke-width'] = Number(raw.event.detail.strokeWeight._attributes.value);
521
- }
522
- if (raw.event.detail.strokeStyle && raw.event.detail.strokeStyle._attributes && raw.event.detail.strokeStyle._attributes.value) {
523
- feat.properties['stroke-style'] = raw.event.detail.strokeStyle._attributes.value;
524
- }
525
- // Range & Bearing Line
526
- if (raw.event._attributes.type === 'u-rb-a') {
527
- const detail = this.detail();
528
- if (!detail.range)
529
- throw new Error('Range value not provided');
530
- if (!detail.bearing)
531
- throw new Error('Bearing value not provided');
532
- // TODO Support inclination
533
- const dest = destination(this.position(), detail.range._attributes.value / 1000, detail.bearing._attributes.value).geometry.coordinates;
534
- feat.geometry = {
535
- type: 'LineString',
536
- coordinates: [this.position(), dest]
537
- };
538
- }
539
- 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])) {
540
- if (raw.event._attributes.type === 'u-d-r') {
541
- // CoT rectangles are only 4 points - GeoJSON needs to be closed
542
- coordinates.push(coordinates[0]);
543
- }
544
- feat.geometry = {
545
- type: 'Polygon',
546
- coordinates: [coordinates]
547
- };
548
- if (raw.event.detail.fillColor && raw.event.detail.fillColor._attributes && raw.event.detail.fillColor._attributes.value) {
549
- const fill = new Color(Number(raw.event.detail.fillColor._attributes.value));
550
- feat.properties['fill-opacity'] = fill.as_opacity() / 255;
551
- feat.properties['fill'] = fill.as_hex();
552
- }
553
- }
554
- else {
555
- feat.geometry = {
556
- type: 'LineString',
557
- coordinates
558
- };
559
- }
560
- }
561
- else if (raw.event._attributes.type.startsWith('u-d-c-c')) {
562
- if (!raw.event.detail.shape)
563
- throw new Err(400, null, 'u-d-c-c (Circle) must define shape value');
564
- if (!raw.event.detail.shape.ellipse
565
- || !raw.event.detail.shape.ellipse._attributes)
566
- throw new Err(400, null, 'u-d-c-c (Circle) must define ellipse shape value');
567
- const ellipse = {
568
- major: Number(raw.event.detail.shape.ellipse._attributes.major),
569
- minor: Number(raw.event.detail.shape.ellipse._attributes.minor),
570
- angle: Number(raw.event.detail.shape.ellipse._attributes.angle)
571
- };
572
- feat.geometry = Truncate(Ellipse(feat.geometry.coordinates, Number(ellipse.major) / 1000, Number(ellipse.minor) / 1000, {
573
- angle: ellipse.angle
574
- }), {
575
- precision: COORDINATE_PRECISION,
576
- mutate: true
577
- }).geometry;
578
- feat.properties.shape = {};
579
- feat.properties.shape.ellipse = ellipse;
580
- }
581
- else if (raw.event._attributes.type.startsWith('b-m-p-s-p-i')) {
582
- // TODO: Currently the "shape" tag is only parsed here - asking ARA for clarification if it is a general use tag
583
- if (raw.event.detail.shape && raw.event.detail.shape.polyline && raw.event.detail.shape.polyline.vertex) {
584
- const coordinates = [];
585
- const vertices = Array.isArray(raw.event.detail.shape.polyline.vertex) ? raw.event.detail.shape.polyline.vertex : [raw.event.detail.shape.polyline.vertex];
586
- for (const v of vertices) {
587
- coordinates.push([Number(v._attributes.lon), Number(v._attributes.lat)]);
588
- }
589
- if (coordinates.length === 1) {
590
- feat.geometry = { type: 'Point', coordinates: coordinates[0] };
591
- }
592
- else if (raw.event.detail.shape.polyline._attributes && raw.event.detail.shape.polyline._attributes.closed === true) {
593
- coordinates.push(coordinates[0]);
594
- feat.geometry = { type: 'Polygon', coordinates: [coordinates] };
595
- }
596
- else {
597
- feat.geometry = { type: 'LineString', coordinates };
598
- }
599
- }
600
- if (raw.event.detail.shape
601
- && raw.event.detail.shape.polyline
602
- && raw.event.detail.shape.polyline._attributes
603
- && raw.event.detail.shape.polyline._attributes) {
604
- if (raw.event.detail.shape.polyline._attributes.fillColor) {
605
- const fill = new Color(Number(raw.event.detail.shape.polyline._attributes.fillColor));
606
- feat.properties['fill-opacity'] = fill.as_opacity() / 255;
607
- feat.properties['fill'] = fill.as_hex();
608
- }
609
- if (raw.event.detail.shape.polyline._attributes.color) {
610
- const stroke = new Color(Number(raw.event.detail.shape.polyline._attributes.color));
611
- feat.properties.stroke = stroke.as_hex();
612
- feat.properties['stroke-opacity'] = stroke.as_opacity() / 255;
613
- }
614
- }
615
- }
616
- if (raw.event.detail.color && raw.event.detail.color._attributes && raw.event.detail.color._attributes.argb) {
617
- const color = new Color(Number(raw.event.detail.color._attributes.argb));
618
- feat.properties['marker-color'] = color.as_hex();
619
- feat.properties['marker-opacity'] = color.as_opacity() / 255;
620
- }
621
- feat.properties.metadata = this.metadata;
622
- feat.path = this.path;
623
- return feat;
624
- }
625
- to_xml() {
626
- return xmljs.js2xml(this.raw, { compact: true });
627
- }
628
225
  is_stale() {
629
226
  return new Date(this.raw.event._attributes.stale) < new Date();
630
227
  }
@@ -788,64 +385,6 @@ export default class CoT {
788
385
  is_uav() {
789
386
  return !!this.raw.event._attributes.type.match(/^a-f-A-M-F-Q-r/);
790
387
  }
791
- /**
792
- * Parse an ATAK compliant Protobuf to a JS Object
793
- */
794
- static from_proto(raw, version = 1) {
795
- const ProtoMessage = RootMessage.lookupType(`atakmap.commoncommo.protobuf.v${version}.TakMessage`);
796
- // TODO Type this
797
- const msg = ProtoMessage.decode(raw);
798
- if (!msg.cotEvent)
799
- throw new Err(400, null, 'No cotEvent Data');
800
- const detail = {};
801
- const metadata = {};
802
- for (const key in msg.cotEvent.detail) {
803
- if (key === 'xmlDetail') {
804
- const parsed = xmljs.xml2js(`<detail>${msg.cotEvent.detail.xmlDetail}</detail>`, { compact: true });
805
- Object.assign(detail, parsed.detail);
806
- if (detail.metadata) {
807
- for (const key in detail.metadata) {
808
- metadata[key] = detail.metadata[key]._text;
809
- }
810
- delete detail.metadata;
811
- }
812
- }
813
- else if (key === 'group') {
814
- if (msg.cotEvent.detail[key]) {
815
- detail.__group = { _attributes: msg.cotEvent.detail[key] };
816
- }
817
- }
818
- else if (['contact', 'precisionlocation', 'status', 'takv', 'track'].includes(key)) {
819
- if (msg.cotEvent.detail[key]) {
820
- detail[key] = { _attributes: msg.cotEvent.detail[key] };
821
- }
822
- }
823
- }
824
- const cot = new CoT({
825
- event: {
826
- _attributes: {
827
- version: '2.0',
828
- uid: msg.cotEvent.uid, type: msg.cotEvent.type, how: msg.cotEvent.how,
829
- qos: msg.cotEvent.qos, opex: msg.cotEvent.opex, access: msg.cotEvent.access,
830
- time: new Date(msg.cotEvent.sendTime.toNumber()).toISOString(),
831
- start: new Date(msg.cotEvent.startTime.toNumber()).toISOString(),
832
- stale: new Date(msg.cotEvent.staleTime.toNumber()).toISOString(),
833
- },
834
- detail,
835
- point: {
836
- _attributes: {
837
- lat: msg.cotEvent.lat,
838
- lon: msg.cotEvent.lon,
839
- hae: msg.cotEvent.hae,
840
- le: msg.cotEvent.le,
841
- ce: msg.cotEvent.ce,
842
- },
843
- }
844
- }
845
- });
846
- cot.metadata = metadata;
847
- return cot;
848
- }
849
388
  /**
850
389
  * Return a CoT Message
851
390
  */
@@ -858,252 +397,5 @@ export default class CoT {
858
397
  }
859
398
  });
860
399
  }
861
- /**
862
- * Return an CoT Message given a GeoJSON Feature
863
- *
864
- * @param {Object} feature GeoJSON Point Feature
865
- *
866
- * @return {CoT}
867
- */
868
- static from_geojson(feature) {
869
- checkFeat(feature);
870
- if (checkFeat.errors)
871
- throw new Err(400, null, `${checkFeat.errors[0].message} (${checkFeat.errors[0].instancePath})`);
872
- const cot = {
873
- event: {
874
- _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),
875
- point: Util.cot_point(),
876
- detail: Util.cot_event_detail(feature.properties.callsign)
877
- }
878
- };
879
- if (feature.id)
880
- cot.event._attributes.uid = String(feature.id);
881
- if (feature.properties.callsign && !feature.id)
882
- cot.event._attributes.uid = feature.properties.callsign;
883
- if (!cot.event.detail)
884
- cot.event.detail = {};
885
- if (feature.properties.droid) {
886
- cot.event.detail.uid = { _attributes: { Droid: feature.properties.droid } };
887
- }
888
- if (feature.properties.archived) {
889
- cot.event.detail.archive = { _attributes: {} };
890
- }
891
- if (feature.properties.links) {
892
- if (!cot.event.detail.link)
893
- cot.event.detail.link = [];
894
- else if (!Array.isArray(cot.event.detail.link))
895
- cot.event.detail.link = [cot.event.detail.link];
896
- cot.event.detail.link.push(...feature.properties.links.map((link) => {
897
- return { _attributes: link };
898
- }));
899
- }
900
- if (feature.properties.dest) {
901
- const dest = !Array.isArray(feature.properties.dest) ? [feature.properties.dest] : feature.properties.dest;
902
- cot.event.detail.marti = {
903
- dest: dest.map((dest) => {
904
- return { _attributes: { ...dest } };
905
- })
906
- };
907
- }
908
- if (feature.properties.takv) {
909
- cot.event.detail.takv = { _attributes: { ...feature.properties.takv } };
910
- }
911
- if (feature.properties.creator) {
912
- cot.event.detail.creator = { _attributes: { ...feature.properties.creator } };
913
- }
914
- if (feature.properties.range !== undefined) {
915
- cot.event.detail.range = { _attributes: { value: feature.properties.range } };
916
- }
917
- if (feature.properties.bearing !== undefined) {
918
- cot.event.detail.bearing = { _attributes: { value: feature.properties.bearing } };
919
- }
920
- if (feature.properties.geofence) {
921
- cot.event.detail.__geofence = { _attributes: { ...feature.properties.geofence } };
922
- }
923
- if (feature.properties.sensor) {
924
- cot.event.detail.sensor = { _attributes: { ...feature.properties.sensor } };
925
- }
926
- if (feature.properties.ackrequest) {
927
- cot.event.detail.ackrequest = { _attributes: { ...feature.properties.ackrequest } };
928
- }
929
- if (feature.properties.video) {
930
- if (feature.properties.video.connection) {
931
- const video = JSON.parse(JSON.stringify(feature.properties.video));
932
- const connection = video.connection;
933
- delete video.connection;
934
- cot.event.detail.__video = {
935
- _attributes: { ...video },
936
- ConnectionEntry: {
937
- _attributes: connection
938
- }
939
- };
940
- }
941
- else {
942
- cot.event.detail.__video = { _attributes: { ...feature.properties.video } };
943
- }
944
- }
945
- if (feature.properties.attachments) {
946
- cot.event.detail.attachment_list = { _attributes: { hashes: JSON.stringify(feature.properties.attachments) } };
947
- }
948
- if (feature.properties.contact) {
949
- cot.event.detail.contact = {
950
- _attributes: {
951
- ...feature.properties.contact,
952
- callsign: feature.properties.callsign || 'UNKNOWN',
953
- }
954
- };
955
- }
956
- if (feature.properties.fileshare) {
957
- cot.event.detail.fileshare = { _attributes: { ...feature.properties.fileshare } };
958
- }
959
- if (feature.properties.course !== undefined || feature.properties.speed !== undefined || feature.properties.slope !== undefined) {
960
- cot.event.detail.track = {
961
- _attributes: Util.cot_track_attr(feature.properties.course, feature.properties.speed, feature.properties.slope)
962
- };
963
- }
964
- if (feature.properties.group) {
965
- cot.event.detail.__group = { _attributes: { ...feature.properties.group } };
966
- }
967
- if (feature.properties.flow) {
968
- cot.event.detail['_flow-tags_'] = { _attributes: { ...feature.properties.flow } };
969
- }
970
- if (feature.properties.status) {
971
- cot.event.detail.status = { _attributes: { ...feature.properties.status } };
972
- }
973
- if (feature.properties.precisionlocation) {
974
- cot.event.detail.precisionlocation = { _attributes: { ...feature.properties.precisionlocation } };
975
- }
976
- if (feature.properties.icon) {
977
- cot.event.detail.usericon = { _attributes: { iconsetpath: feature.properties.icon } };
978
- }
979
- if (feature.properties.mission) {
980
- cot.event.detail.mission = {
981
- _attributes: {
982
- type: feature.properties.mission.type,
983
- guid: feature.properties.mission.guid,
984
- tool: feature.properties.mission.tool,
985
- name: feature.properties.mission.name,
986
- authorUid: feature.properties.mission.authorUid,
987
- }
988
- };
989
- if (feature.properties.mission.missionLayer) {
990
- cot.event.detail.mission.missionLayer = {};
991
- if (feature.properties.mission.missionLayer.name) {
992
- cot.event.detail.mission.missionLayer.name = { _text: feature.properties.mission.missionLayer.name };
993
- }
994
- if (feature.properties.mission.missionLayer.parentUid) {
995
- cot.event.detail.mission.missionLayer.parentUid = { _text: feature.properties.mission.missionLayer.parentUid };
996
- }
997
- if (feature.properties.mission.missionLayer.type) {
998
- cot.event.detail.mission.missionLayer.type = { _text: feature.properties.mission.missionLayer.type };
999
- }
1000
- if (feature.properties.mission.missionLayer.uid) {
1001
- cot.event.detail.mission.missionLayer.uid = { _text: feature.properties.mission.missionLayer.uid };
1002
- }
1003
- }
1004
- }
1005
- cot.event.detail.remarks = { _attributes: {}, _text: feature.properties.remarks || '' };
1006
- if (!feature.geometry) {
1007
- throw new Err(400, null, 'Must have Geometry');
1008
- }
1009
- else if (!['Point', 'Polygon', 'LineString'].includes(feature.geometry.type)) {
1010
- throw new Err(400, null, 'Unsupported Geometry Type');
1011
- }
1012
- if (feature.geometry.type === 'Point') {
1013
- cot.event.point._attributes.lon = String(feature.geometry.coordinates[0]);
1014
- cot.event.point._attributes.lat = String(feature.geometry.coordinates[1]);
1015
- cot.event.point._attributes.hae = String(feature.geometry.coordinates[2] || '0.0');
1016
- if (feature.properties['marker-color']) {
1017
- const color = new Color(feature.properties['marker-color'] || -1761607936);
1018
- color.a = feature.properties['marker-opacity'] !== undefined ? feature.properties['marker-opacity'] * 255 : 128;
1019
- cot.event.detail.color = { _attributes: { argb: String(color.as_32bit()) } };
1020
- }
1021
- }
1022
- else if (feature.geometry.type === 'Polygon' && feature.properties.type === 'u-d-c-c') {
1023
- if (!feature.properties.shape || !feature.properties.shape.ellipse) {
1024
- throw new Err(400, null, 'u-d-c-c (Circle) must define a feature.properties.shape.ellipse property');
1025
- }
1026
- cot.event.detail.shape = { ellipse: { _attributes: feature.properties.shape.ellipse } };
1027
- if (feature.properties.center) {
1028
- cot.event.point._attributes.lon = String(feature.properties.center[0]);
1029
- cot.event.point._attributes.lat = String(feature.properties.center[1]);
1030
- }
1031
- else {
1032
- const centre = PointOnFeature(feature);
1033
- cot.event.point._attributes.lon = String(centre.geometry.coordinates[0]);
1034
- cot.event.point._attributes.lat = String(centre.geometry.coordinates[1]);
1035
- cot.event.point._attributes.hae = '0.0';
1036
- }
1037
- }
1038
- else if (['Polygon', 'LineString'].includes(feature.geometry.type)) {
1039
- const stroke = new Color(feature.properties.stroke || -1761607936);
1040
- stroke.a = feature.properties['stroke-opacity'] !== undefined ? feature.properties['stroke-opacity'] * 255 : 128;
1041
- cot.event.detail.strokeColor = { _attributes: { value: String(stroke.as_32bit()) } };
1042
- if (!feature.properties['stroke-width'])
1043
- feature.properties['stroke-width'] = 3;
1044
- cot.event.detail.strokeWeight = { _attributes: {
1045
- value: String(feature.properties['stroke-width'])
1046
- } };
1047
- if (!feature.properties['stroke-style'])
1048
- feature.properties['stroke-style'] = 'solid';
1049
- cot.event.detail.strokeStyle = { _attributes: {
1050
- value: feature.properties['stroke-style']
1051
- } };
1052
- if (feature.geometry.type === 'LineString') {
1053
- cot.event._attributes.type = 'u-d-f';
1054
- if (!cot.event.detail.link)
1055
- cot.event.detail.link = [];
1056
- else if (!Array.isArray(cot.event.detail.link))
1057
- cot.event.detail.link = [cot.event.detail.link];
1058
- for (const coord of feature.geometry.coordinates) {
1059
- cot.event.detail.link.push({
1060
- _attributes: { point: `${coord[1]},${coord[0]}` }
1061
- });
1062
- }
1063
- }
1064
- else if (feature.geometry.type === 'Polygon') {
1065
- cot.event._attributes.type = 'u-d-f';
1066
- if (!cot.event.detail.link)
1067
- cot.event.detail.link = [];
1068
- else if (!Array.isArray(cot.event.detail.link))
1069
- cot.event.detail.link = [cot.event.detail.link];
1070
- // Inner rings are not yet supported
1071
- for (const coord of feature.geometry.coordinates[0]) {
1072
- cot.event.detail.link.push({
1073
- _attributes: { point: `${coord[1]},${coord[0]}` }
1074
- });
1075
- }
1076
- const fill = new Color(feature.properties.fill || -1761607936);
1077
- fill.a = feature.properties['fill-opacity'] !== undefined ? feature.properties['fill-opacity'] * 255 : 128;
1078
- cot.event.detail.fillColor = { _attributes: { value: String(fill.as_32bit()) } };
1079
- }
1080
- cot.event.detail.labels_on = { _attributes: { value: 'false' } };
1081
- cot.event.detail.tog = { _attributes: { enabled: '0' } };
1082
- if (feature.properties.center && Array.isArray(feature.properties.center) && feature.properties.center.length >= 2) {
1083
- cot.event.point._attributes.lon = String(feature.properties.center[0]);
1084
- cot.event.point._attributes.lat = String(feature.properties.center[1]);
1085
- if (feature.properties.center.length >= 3) {
1086
- cot.event.point._attributes.hae = String(feature.properties.center[2] || '0.0');
1087
- }
1088
- else {
1089
- cot.event.point._attributes.hae = '0.0';
1090
- }
1091
- }
1092
- else {
1093
- const centre = PointOnFeature(feature);
1094
- cot.event.point._attributes.lon = String(centre.geometry.coordinates[0]);
1095
- cot.event.point._attributes.lat = String(centre.geometry.coordinates[1]);
1096
- cot.event.point._attributes.hae = '0.0';
1097
- }
1098
- }
1099
- const newcot = new CoT(cot);
1100
- if (feature.properties.metadata) {
1101
- newcot.metadata = feature.properties.metadata;
1102
- }
1103
- if (feature.path) {
1104
- newcot.path = feature.path;
1105
- }
1106
- return newcot;
1107
- }
1108
400
  }
1109
401
  //# sourceMappingURL=cot.js.map