@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/lib/cot.ts CHANGED
@@ -1,15 +1,9 @@
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
3
  import type { Static } from '@sinclair/typebox';
7
4
  import type {
8
- Feature,
9
5
  Polygon,
10
6
  Position,
11
- FeaturePropertyMission,
12
- FeaturePropertyMissionLayer,
13
7
  } from './types/feature.js';
14
8
  import type {
15
9
  MartiDest,
@@ -21,44 +15,18 @@ import type {
21
15
  SensorAttributes,
22
16
  VideoConnectionEntryAttributes,
23
17
  } from './types/types.js'
24
- import {
25
- InputFeature,
26
- } from './types/feature.js';
27
18
  import Sensor from './sensor.js';
28
- import type { AllGeoJSON } from "@turf/helpers";
29
- import PointOnFeature from '@turf/point-on-feature';
30
- import Truncate from '@turf/truncate';
31
- import { destination } from '@turf/destination';
32
- import Ellipse from '@turf/ellipse';
33
19
  import Util from './utils/util.js';
34
- import Color from './utils/color.js';
35
20
  import JSONCoT, { Detail } from './types/types.js'
36
- import AJV from 'ajv';
37
- import fs from 'fs';
38
- import path from 'node:path';
39
- import { fileURLToPath } from 'node:url';
40
-
41
- // GeoJSON Geospatial ops will truncate to the below
42
- const COORDINATE_PRECISION = 6;
43
-
44
- const protoPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'proto', 'takmessage.proto');
45
- const RootMessage = await protobuf.load(protoPath);
46
-
47
- const pkg = JSON.parse(String(fs.readFileSync(new URL('../package.json', import.meta.url))));
48
-
49
- const checkXML = (new AJV({
50
- allErrors: true,
51
- coerceTypes: true,
52
- allowUnionTypes: true
53
- }))
54
- .compile(JSONCoT);
55
21
 
56
- const checkFeat = (new AJV({
57
- allErrors: true,
58
- coerceTypes: true,
59
- allowUnionTypes: true
60
- }))
61
- .compile(InputFeature);
22
+ export type CoTOptions = {
23
+ creator?: CoT | {
24
+ uid: string,
25
+ type: string,
26
+ callsign: string,
27
+ time?: Date | string,
28
+ }
29
+ }
62
30
 
63
31
  /**
64
32
  * Convert to and from an XML CoT message
@@ -80,36 +48,19 @@ export default class CoT {
80
48
  path: string;
81
49
 
82
50
  constructor(
83
- cot: Buffer | Static<typeof JSONCoT> | object | string,
84
- opts: {
85
- creator?: CoT | {
86
- uid: string,
87
- type: string,
88
- callsign: string,
89
- time?: Date | string,
90
- }
91
- } = {}
51
+ cot: Static<typeof JSONCoT>,
52
+ opts: CoTOptions = {}
92
53
  ) {
93
- if (typeof cot === 'string' || cot instanceof Buffer) {
94
- const raw = xmljs.xml2js(String(cot), { compact: true });
95
- this.raw = raw as Static<typeof JSONCoT>;
96
- } else {
97
- this.raw = cot as Static<typeof JSONCoT>;
98
- }
54
+ this.raw = cot;
99
55
 
100
56
  this.metadata = {};
101
57
  this.path = '/';
102
58
 
103
- if (!this.raw.event._attributes.uid) this.raw.event._attributes.uid = Util.cot_uuid();
104
-
105
- if (process.env.DEBUG_COTS) console.log(JSON.stringify(this.raw))
106
-
107
- checkXML(this.raw);
108
- if (checkXML.errors) throw new Err(400, null, `${checkXML.errors[0].message} (${checkXML.errors[0].instancePath})`);
59
+ if (!this.raw.event._attributes.uid) {
60
+ this.raw.event._attributes.uid = Util.cot_uuid();
61
+ }
109
62
 
110
63
  if (!this.raw.event.detail) this.raw.event.detail = {};
111
- if (!this.raw.event.detail['_flow-tags_']) this.raw.event.detail['_flow-tags_'] = {};
112
- this.raw.event.detail['_flow-tags_'][`NodeCoT-${pkg.version}`] = new Date().toISOString()
113
64
 
114
65
  if (this.raw.event.detail.archive && Object.keys(this.raw.event.detail.archive).length === 0) {
115
66
  this.raw.event.detail.archive = { _attributes: {} };
@@ -123,52 +74,8 @@ export default class CoT {
123
74
  time: opts.creator instanceof CoT ? new Date() : opts.creator.time
124
75
  });
125
76
  }
126
- }
127
77
 
128
- /**
129
- * Detect difference between CoT messages
130
- * Note: This diffs based on GeoJSON Representation of message
131
- * So if unknown properties are present they will be excluded from the diff
132
- */
133
- isDiff(
134
- cot: CoT,
135
- opts = {
136
- diffMetadata: false,
137
- diffStaleStartTime: false,
138
- diffDest: false,
139
- diffFlow: false
140
- }
141
- ): boolean {
142
- const a = this.to_geojson() as Static<typeof InputFeature>;
143
- const b = cot.to_geojson() as Static<typeof InputFeature>;
144
-
145
- if (!opts.diffDest) {
146
- delete a.properties.dest;
147
- delete b.properties.dest;
148
- }
149
-
150
- if (!opts.diffMetadata) {
151
- delete a.properties.metadata;
152
- delete b.properties.metadata;
153
- }
154
-
155
- if (!opts.diffFlow) {
156
- delete a.properties.flow;
157
- delete b.properties.flow;
158
- }
159
-
160
- if (!opts.diffStaleStartTime) {
161
- delete a.properties.time;
162
- delete a.properties.stale;
163
- delete a.properties.start;
164
- delete b.properties.time;
165
- delete b.properties.stale;
166
- delete b.properties.start;
167
- }
168
-
169
- const diffs = diff(a, b);
170
-
171
- return diffs.length > 0;
78
+ if (process.env.DEBUG_COTS) console.log(JSON.stringify(this.raw))
172
79
  }
173
80
 
174
81
  /**
@@ -306,8 +213,8 @@ export default class CoT {
306
213
 
307
214
  position(position?: Static<typeof Position>): Static<typeof Position> {
308
215
  if (position) {
309
- this.raw.event.point._attributes.lon = String(position[0]);
310
- this.raw.event.point._attributes.lat = String(position[1]);
216
+ this.raw.event.point._attributes.lon = position[0];
217
+ this.raw.event.point._attributes.lat = position[1];
311
218
  }
312
219
 
313
220
  return [
@@ -386,392 +293,6 @@ export default class CoT {
386
293
  return this;
387
294
  }
388
295
 
389
- /**
390
- * Return an ATAK Compliant Protobuf
391
- */
392
- to_proto(version = 1): Uint8Array {
393
- if (version < 1 || version > 1) throw new Err(400, null, `Unsupported Proto Version: ${version}`);
394
- const ProtoMessage = RootMessage.lookupType(`atakmap.commoncommo.protobuf.v${version}.TakMessage`)
395
-
396
- // The spread operator is important to make sure the delete doesn't modify the underlying detail object
397
- const detail = { ...this.raw.event.detail };
398
-
399
- const msg: any = {
400
- cotEvent: {
401
- ...this.raw.event._attributes,
402
- sendTime: new Date(this.raw.event._attributes.time).getTime(),
403
- startTime: new Date(this.raw.event._attributes.start).getTime(),
404
- staleTime: new Date(this.raw.event._attributes.stale).getTime(),
405
- ...this.raw.event.point._attributes,
406
- detail: {
407
- xmlDetail: ''
408
- }
409
- }
410
- };
411
-
412
- let key: keyof Static<typeof Detail>;
413
- for (key in detail) {
414
- if(['contact', 'group', 'precisionlocation', 'status', 'takv', 'track'].includes(key)) {
415
- msg.cotEvent.detail[key] = detail[key]._attributes;
416
- delete detail[key]
417
- }
418
- }
419
-
420
- msg.cotEvent.detail.xmlDetail = xmljs.js2xml({
421
- ...detail,
422
- metadata: this.metadata
423
- }, { compact: true });
424
-
425
- return ProtoMessage.encode(msg).finish();
426
- }
427
-
428
- /**
429
- * Return a GeoJSON Feature from an XML CoT message
430
- */
431
- to_geojson(): Static<typeof Feature> {
432
- const raw: Static<typeof JSONCoT> = JSON.parse(JSON.stringify(this.raw));
433
- if (!raw.event.detail) raw.event.detail = {};
434
- if (!raw.event.detail.contact) raw.event.detail.contact = { _attributes: { callsign: 'UNKNOWN' } };
435
- if (!raw.event.detail.contact._attributes) raw.event.detail.contact._attributes = { callsign: 'UNKNOWN' };
436
-
437
- const feat: Static<typeof Feature> = {
438
- id: raw.event._attributes.uid,
439
- type: 'Feature',
440
- properties: {
441
- callsign: raw.event.detail.contact._attributes.callsign || 'UNKNOWN',
442
- center: [ Number(raw.event.point._attributes.lon), Number(raw.event.point._attributes.lat), Number(raw.event.point._attributes.hae) ],
443
- type: raw.event._attributes.type,
444
- how: raw.event._attributes.how || '',
445
- time: raw.event._attributes.time,
446
- start: raw.event._attributes.start,
447
- stale: raw.event._attributes.stale,
448
- },
449
- geometry: {
450
- type: 'Point',
451
- coordinates: [ Number(raw.event.point._attributes.lon), Number(raw.event.point._attributes.lat), Number(raw.event.point._attributes.hae) ]
452
- }
453
- };
454
-
455
- const contact = JSON.parse(JSON.stringify(raw.event.detail.contact._attributes));
456
- delete contact.callsign;
457
- if (Object.keys(contact).length) {
458
- feat.properties.contact = contact;
459
- }
460
-
461
- if (this.creator()) {
462
- feat.properties.creator = this.creator();
463
- }
464
-
465
- if (raw.event.detail.remarks && raw.event.detail.remarks._text) {
466
- feat.properties.remarks = raw.event.detail.remarks._text;
467
- }
468
-
469
- if (raw.event.detail.fileshare) {
470
- feat.properties.fileshare = raw.event.detail.fileshare._attributes;
471
- if (feat.properties.fileshare && typeof feat.properties.fileshare.sizeInBytes === 'string') {
472
- feat.properties.fileshare.sizeInBytes = parseInt(feat.properties.fileshare.sizeInBytes)
473
- }
474
- }
475
-
476
- if (raw.event.detail.sensor) {
477
- feat.properties.sensor = raw.event.detail.sensor._attributes;
478
- }
479
-
480
- if (raw.event.detail.range) {
481
- feat.properties.range = raw.event.detail.range._attributes.value;
482
- }
483
-
484
- if (raw.event.detail.bearing) {
485
- feat.properties.bearing = raw.event.detail.bearing._attributes.value;
486
- }
487
-
488
- if (raw.event.detail.__video && raw.event.detail.__video._attributes) {
489
- feat.properties.video = raw.event.detail.__video._attributes;
490
-
491
- if (raw.event.detail.__video.ConnectionEntry) {
492
- feat.properties.video.connection = raw.event.detail.__video.ConnectionEntry._attributes;
493
- }
494
- }
495
-
496
- if (raw.event.detail.__geofence) {
497
- feat.properties.geofence = raw.event.detail.__geofence._attributes;
498
- }
499
-
500
- if (raw.event.detail.ackrequest) {
501
- feat.properties.ackrequest = raw.event.detail.ackrequest._attributes;
502
- }
503
-
504
- if (raw.event.detail.attachment_list) {
505
- feat.properties.attachments = JSON.parse(raw.event.detail.attachment_list._attributes.hashes);
506
- }
507
-
508
- if (raw.event.detail.link) {
509
- if (!Array.isArray(raw.event.detail.link)) raw.event.detail.link = [raw.event.detail.link];
510
-
511
- feat.properties.links = raw.event.detail.link.filter((link: Static<typeof Link>) => {
512
- return !!link._attributes.url
513
- }).map((link: Static<typeof Link>): Static<typeof LinkAttributes> => {
514
- return link._attributes;
515
- });
516
-
517
- if (!feat.properties.links || !feat.properties.links.length) delete feat.properties.links;
518
- }
519
-
520
- if (raw.event.detail.archive) {
521
- feat.properties.archived = true;
522
- }
523
-
524
- if (raw.event.detail.__chat) {
525
- feat.properties.chat = {
526
- ...raw.event.detail.__chat._attributes,
527
- chatgrp: raw.event.detail.__chat.chatgrp
528
- }
529
- }
530
-
531
- if (raw.event.detail.track && raw.event.detail.track._attributes) {
532
- if (raw.event.detail.track._attributes.course) feat.properties.course = Number(raw.event.detail.track._attributes.course);
533
- if (raw.event.detail.track._attributes.slope) feat.properties.slope = Number(raw.event.detail.track._attributes.slope);
534
- if (raw.event.detail.track._attributes.course) feat.properties.speed = Number(raw.event.detail.track._attributes.speed);
535
- }
536
-
537
- if (raw.event.detail.marti && raw.event.detail.marti.dest) {
538
- if (!Array.isArray(raw.event.detail.marti.dest)) raw.event.detail.marti.dest = [raw.event.detail.marti.dest];
539
-
540
- const dest: Array<Static<typeof MartiDestAttributes>> = raw.event.detail.marti.dest.map((d: Static<typeof MartiDest>) => {
541
- return { ...d._attributes };
542
- });
543
-
544
- feat.properties.dest = dest.length === 1 ? dest[0] : dest
545
- }
546
-
547
- if (raw.event.detail.usericon && raw.event.detail.usericon._attributes && raw.event.detail.usericon._attributes.iconsetpath) {
548
- feat.properties.icon = raw.event.detail.usericon._attributes.iconsetpath;
549
- }
550
-
551
-
552
- if (raw.event.detail.uid && raw.event.detail.uid._attributes && raw.event.detail.uid._attributes.Droid) {
553
- feat.properties.droid = raw.event.detail.uid._attributes.Droid;
554
- }
555
-
556
- if (raw.event.detail.takv && raw.event.detail.takv._attributes) {
557
- feat.properties.takv = raw.event.detail.takv._attributes;
558
- }
559
-
560
- if (raw.event.detail.__group && raw.event.detail.__group._attributes) {
561
- feat.properties.group = raw.event.detail.__group._attributes;
562
- }
563
-
564
- if (raw.event.detail['_flow-tags_'] && raw.event.detail['_flow-tags_']._attributes) {
565
- feat.properties.flow = raw.event.detail['_flow-tags_']._attributes;
566
- }
567
-
568
- if (raw.event.detail.status && raw.event.detail.status._attributes) {
569
- feat.properties.status = raw.event.detail.status._attributes;
570
- }
571
-
572
- if (raw.event.detail.mission && raw.event.detail.mission._attributes) {
573
- const mission: Static<typeof FeaturePropertyMission> = {
574
- ...raw.event.detail.mission._attributes
575
- };
576
-
577
- if (raw.event.detail.mission && raw.event.detail.mission.MissionChanges) {
578
- const changes =
579
- Array.isArray(raw.event.detail.mission.MissionChanges)
580
- ? raw.event.detail.mission.MissionChanges
581
- : [ raw.event.detail.mission.MissionChanges ]
582
-
583
- mission.missionChanges = []
584
- for (const change of changes) {
585
- mission.missionChanges.push({
586
- contentUid: change.MissionChange.contentUid._text,
587
- creatorUid: change.MissionChange.creatorUid._text,
588
- isFederatedChange: change.MissionChange.isFederatedChange._text,
589
- missionName: change.MissionChange.missionName._text,
590
- timestamp: change.MissionChange.timestamp._text,
591
- type: change.MissionChange.type._text,
592
- details: {
593
- ...change.MissionChange.details._attributes,
594
- ...change.MissionChange.details.location
595
- ? change.MissionChange.details.location._attributes
596
- : {}
597
- }
598
- })
599
- }
600
- }
601
-
602
-
603
- if (raw.event.detail.mission && raw.event.detail.mission.missionLayer) {
604
- const missionLayer: Static<typeof FeaturePropertyMissionLayer> = {};
605
-
606
- if (raw.event.detail.mission.missionLayer.name && raw.event.detail.mission.missionLayer.name._text) {
607
- missionLayer.name = raw.event.detail.mission.missionLayer.name._text;
608
- }
609
- if (raw.event.detail.mission.missionLayer.parentUid && raw.event.detail.mission.missionLayer.parentUid._text) {
610
- missionLayer.parentUid = raw.event.detail.mission.missionLayer.parentUid._text;
611
- }
612
- if (raw.event.detail.mission.missionLayer.type && raw.event.detail.mission.missionLayer.type._text) {
613
- missionLayer.type = raw.event.detail.mission.missionLayer.type._text;
614
- }
615
- if (raw.event.detail.mission.missionLayer.uid && raw.event.detail.mission.missionLayer.uid._text) {
616
- missionLayer.uid = raw.event.detail.mission.missionLayer.uid._text;
617
- }
618
-
619
- mission.missionLayer = missionLayer;
620
- }
621
-
622
- feat.properties.mission = mission;
623
- }
624
-
625
- if (raw.event.detail.precisionlocation && raw.event.detail.precisionlocation._attributes) {
626
- feat.properties.precisionlocation = raw.event.detail.precisionlocation._attributes;
627
- }
628
-
629
- // Line or Polygon style types
630
- if (['u-d-f', 'u-d-r', 'b-m-r', 'u-rb-a'].includes(raw.event._attributes.type) && Array.isArray(raw.event.detail.link)) {
631
- const coordinates = [];
632
-
633
- for (const l of raw.event.detail.link) {
634
- if (!l._attributes.point) continue;
635
- coordinates.push(l._attributes.point.split(',').map((p: string) => { return Number(p.trim()) }).splice(0, 2).reverse());
636
- }
637
-
638
- if (raw.event.detail.strokeColor && raw.event.detail.strokeColor._attributes && raw.event.detail.strokeColor._attributes.value) {
639
- const stroke = new Color(Number(raw.event.detail.strokeColor._attributes.value));
640
- feat.properties.stroke = stroke.as_hex();
641
- feat.properties['stroke-opacity'] = stroke.as_opacity() / 255;
642
- }
643
-
644
- if (raw.event.detail.strokeWeight && raw.event.detail.strokeWeight._attributes && raw.event.detail.strokeWeight._attributes.value) {
645
- feat.properties['stroke-width'] = Number(raw.event.detail.strokeWeight._attributes.value);
646
- }
647
-
648
- if (raw.event.detail.strokeStyle && raw.event.detail.strokeStyle._attributes && raw.event.detail.strokeStyle._attributes.value) {
649
- feat.properties['stroke-style'] = raw.event.detail.strokeStyle._attributes.value;
650
- }
651
-
652
- // Range & Bearing Line
653
- if (raw.event._attributes.type === 'u-rb-a') {
654
- const detail = this.detail();
655
-
656
- if (!detail.range) throw new Error('Range value not provided')
657
- if (!detail.bearing) throw new Error('Bearing value not provided')
658
-
659
- // TODO Support inclination
660
- const dest = destination(
661
- this.position(),
662
- detail.range._attributes.value / 1000,
663
- detail.bearing._attributes.value
664
- ).geometry.coordinates;
665
-
666
- feat.geometry = {
667
- type: 'LineString',
668
- coordinates: [this.position(), dest]
669
- };
670
- } 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])) {
671
- if (raw.event._attributes.type === 'u-d-r') {
672
- // CoT rectangles are only 4 points - GeoJSON needs to be closed
673
- coordinates.push(coordinates[0])
674
- }
675
-
676
- feat.geometry = {
677
- type: 'Polygon',
678
- coordinates: [coordinates]
679
- }
680
-
681
- if (raw.event.detail.fillColor && raw.event.detail.fillColor._attributes && raw.event.detail.fillColor._attributes.value) {
682
- const fill = new Color(Number(raw.event.detail.fillColor._attributes.value));
683
- feat.properties['fill-opacity'] = fill.as_opacity() / 255;
684
- feat.properties['fill'] = fill.as_hex();
685
- }
686
- } else {
687
- feat.geometry = {
688
- type: 'LineString',
689
- coordinates
690
- }
691
- }
692
- } else if (raw.event._attributes.type.startsWith('u-d-c-c')) {
693
- if (!raw.event.detail.shape) throw new Err(400, null, 'u-d-c-c (Circle) must define shape value')
694
- if (
695
- !raw.event.detail.shape.ellipse
696
- || !raw.event.detail.shape.ellipse._attributes
697
- ) throw new Err(400, null, 'u-d-c-c (Circle) must define ellipse shape value')
698
-
699
- const ellipse = {
700
- major: Number(raw.event.detail.shape.ellipse._attributes.major),
701
- minor: Number(raw.event.detail.shape.ellipse._attributes.minor),
702
- angle: Number(raw.event.detail.shape.ellipse._attributes.angle)
703
- }
704
-
705
- feat.geometry = Truncate(Ellipse(
706
- feat.geometry.coordinates as number[],
707
- Number(ellipse.major) / 1000,
708
- Number(ellipse.minor) / 1000,
709
- {
710
- angle: ellipse.angle
711
- }
712
- ), {
713
- precision: COORDINATE_PRECISION,
714
- mutate: true
715
- }).geometry as Static<typeof Polygon>;
716
-
717
- feat.properties.shape = {};
718
- feat.properties.shape.ellipse = ellipse;
719
- } else if (raw.event._attributes.type.startsWith('b-m-p-s-p-i')) {
720
- // TODO: Currently the "shape" tag is only parsed here - asking ARA for clarification if it is a general use tag
721
- if (raw.event.detail.shape && raw.event.detail.shape.polyline && raw.event.detail.shape.polyline.vertex) {
722
- const coordinates = [];
723
-
724
- const vertices = Array.isArray(raw.event.detail.shape.polyline.vertex) ? raw.event.detail.shape.polyline.vertex : [raw.event.detail.shape.polyline.vertex];
725
- for (const v of vertices) {
726
- coordinates.push([Number(v._attributes.lon), Number(v._attributes.lat)]);
727
- }
728
-
729
- if (coordinates.length === 1) {
730
- feat.geometry = { type: 'Point', coordinates: coordinates[0] }
731
- } else if (raw.event.detail.shape.polyline._attributes && raw.event.detail.shape.polyline._attributes.closed === true) {
732
- coordinates.push(coordinates[0]);
733
- feat.geometry = { type: 'Polygon', coordinates: [coordinates] }
734
- } else {
735
- feat.geometry = { type: 'LineString', coordinates }
736
- }
737
- }
738
-
739
- if (
740
- raw.event.detail.shape
741
- && raw.event.detail.shape.polyline
742
- && raw.event.detail.shape.polyline._attributes
743
- && raw.event.detail.shape.polyline._attributes
744
- ) {
745
- if (raw.event.detail.shape.polyline._attributes.fillColor) {
746
- const fill = new Color(Number(raw.event.detail.shape.polyline._attributes.fillColor));
747
- feat.properties['fill-opacity'] = fill.as_opacity() / 255;
748
- feat.properties['fill'] = fill.as_hex();
749
- }
750
-
751
- if (raw.event.detail.shape.polyline._attributes.color) {
752
- const stroke = new Color(Number(raw.event.detail.shape.polyline._attributes.color));
753
- feat.properties.stroke = stroke.as_hex();
754
- feat.properties['stroke-opacity'] = stroke.as_opacity() / 255;
755
- }
756
- }
757
- }
758
-
759
- if (raw.event.detail.color && raw.event.detail.color._attributes && raw.event.detail.color._attributes.argb) {
760
- const color = new Color(Number(raw.event.detail.color._attributes.argb));
761
- feat.properties['marker-color'] = color.as_hex();
762
- feat.properties['marker-opacity'] = color.as_opacity() / 255;
763
- }
764
-
765
- feat.properties.metadata = this.metadata;
766
- feat.path = this.path;
767
-
768
- return feat;
769
- }
770
-
771
- to_xml(): string {
772
- return xmljs.js2xml(this.raw, { compact: true });
773
- }
774
-
775
296
  is_stale(): boolean {
776
297
  return new Date(this.raw.event._attributes.stale) < new Date();
777
298
  }
@@ -956,69 +477,6 @@ export default class CoT {
956
477
  return !!this.raw.event._attributes.type.match(/^a-f-A-M-F-Q-r/)
957
478
  }
958
479
 
959
- /**
960
- * Parse an ATAK compliant Protobuf to a JS Object
961
- */
962
- static from_proto(raw: Uint8Array, version = 1): CoT {
963
- const ProtoMessage = RootMessage.lookupType(`atakmap.commoncommo.protobuf.v${version}.TakMessage`)
964
-
965
- // TODO Type this
966
- const msg: any = ProtoMessage.decode(raw);
967
-
968
- if (!msg.cotEvent) throw new Err(400, null, 'No cotEvent Data');
969
-
970
- const detail: Record<string, any> = {};
971
- const metadata: Record<string, unknown> = {};
972
- for (const key in msg.cotEvent.detail) {
973
- if (key === 'xmlDetail') {
974
- const parsed: any = xmljs.xml2js(`<detail>${msg.cotEvent.detail.xmlDetail}</detail>`, { compact: true });
975
- Object.assign(detail, parsed.detail);
976
-
977
- if (detail.metadata) {
978
- for (const key in detail.metadata) {
979
- metadata[key] = detail.metadata[key]._text;
980
- }
981
- delete detail.metadata;
982
- }
983
- } else if (key === 'group') {
984
- if (msg.cotEvent.detail[key]) {
985
- detail.__group = { _attributes: msg.cotEvent.detail[key] };
986
- }
987
- } else if (['contact', 'precisionlocation', 'status', 'takv', 'track'].includes(key)) {
988
- if (msg.cotEvent.detail[key]) {
989
- detail[key] = { _attributes: msg.cotEvent.detail[key] };
990
- }
991
- }
992
- }
993
-
994
- const cot = new CoT({
995
- event: {
996
- _attributes: {
997
- version: '2.0',
998
- uid: msg.cotEvent.uid, type: msg.cotEvent.type, how: msg.cotEvent.how,
999
- qos: msg.cotEvent.qos, opex: msg.cotEvent.opex, access: msg.cotEvent.access,
1000
- time: new Date(msg.cotEvent.sendTime.toNumber()).toISOString(),
1001
- start: new Date(msg.cotEvent.startTime.toNumber()).toISOString(),
1002
- stale: new Date(msg.cotEvent.staleTime.toNumber()).toISOString(),
1003
- },
1004
- detail,
1005
- point: {
1006
- _attributes: {
1007
- lat: msg.cotEvent.lat,
1008
- lon: msg.cotEvent.lon,
1009
- hae: msg.cotEvent.hae,
1010
- le: msg.cotEvent.le,
1011
- ce: msg.cotEvent.ce,
1012
- },
1013
- }
1014
- }
1015
- });
1016
-
1017
- cot.metadata = metadata;
1018
-
1019
- return cot;
1020
- }
1021
-
1022
480
  /**
1023
481
  * Return a CoT Message
1024
482
  */
@@ -1031,293 +489,4 @@ export default class CoT {
1031
489
  }
1032
490
  });
1033
491
  }
1034
-
1035
- /**
1036
- * Return an CoT Message given a GeoJSON Feature
1037
- *
1038
- * @param {Object} feature GeoJSON Point Feature
1039
- *
1040
- * @return {CoT}
1041
- */
1042
- static from_geojson(feature: Static<typeof InputFeature>): CoT {
1043
- checkFeat(feature);
1044
- if (checkFeat.errors) throw new Err(400, null, `${checkFeat.errors[0].message} (${checkFeat.errors[0].instancePath})`);
1045
-
1046
- const cot: Static<typeof JSONCoT> = {
1047
- event: {
1048
- _attributes: Util.cot_event_attr(
1049
- feature.properties.type || 'a-f-G',
1050
- feature.properties.how || 'm-g',
1051
- feature.properties.time,
1052
- feature.properties.start,
1053
- feature.properties.stale
1054
- ),
1055
- point: Util.cot_point(),
1056
- detail: Util.cot_event_detail(feature.properties.callsign)
1057
- }
1058
- };
1059
-
1060
- if (feature.id) cot.event._attributes.uid = String(feature.id);
1061
- if (feature.properties.callsign && !feature.id) cot.event._attributes.uid = feature.properties.callsign;
1062
- if (!cot.event.detail) cot.event.detail = {};
1063
-
1064
- if (feature.properties.droid) {
1065
- cot.event.detail.uid = { _attributes: { Droid: feature.properties.droid } };
1066
- }
1067
-
1068
- if (feature.properties.archived) {
1069
- cot.event.detail.archive = { _attributes: { } };
1070
- }
1071
-
1072
- if (feature.properties.links) {
1073
- if (!cot.event.detail.link) cot.event.detail.link = [];
1074
- else if (!Array.isArray(cot.event.detail.link)) cot.event.detail.link = [cot.event.detail.link];
1075
-
1076
- cot.event.detail.link.push(...feature.properties.links.map((link: Static<typeof LinkAttributes>) => {
1077
- return { _attributes: link };
1078
- }))
1079
- }
1080
-
1081
- if (feature.properties.dest) {
1082
- const dest = !Array.isArray(feature.properties.dest) ? [ feature.properties.dest ] : feature.properties.dest;
1083
-
1084
- cot.event.detail.marti = {
1085
- dest: dest.map((dest) => {
1086
- return { _attributes: { ...dest } };
1087
- })
1088
- }
1089
- }
1090
-
1091
- if (feature.properties.takv) {
1092
- cot.event.detail.takv = { _attributes: { ...feature.properties.takv } };
1093
- }
1094
-
1095
- if (feature.properties.creator) {
1096
- cot.event.detail.creator = { _attributes: { ...feature.properties.creator } };
1097
- }
1098
-
1099
- if (feature.properties.range !== undefined) {
1100
- cot.event.detail.range = { _attributes: { value: feature.properties.range } }
1101
- }
1102
-
1103
- if (feature.properties.bearing !== undefined) {
1104
- cot.event.detail.bearing = { _attributes: { value: feature.properties.bearing } }
1105
- }
1106
-
1107
- if (feature.properties.geofence) {
1108
- cot.event.detail.__geofence = { _attributes: { ...feature.properties.geofence } };
1109
- }
1110
-
1111
- if (feature.properties.sensor) {
1112
- cot.event.detail.sensor = { _attributes: { ...feature.properties.sensor } };
1113
- }
1114
-
1115
- if (feature.properties.ackrequest) {
1116
- cot.event.detail.ackrequest = { _attributes: { ...feature.properties.ackrequest } };
1117
- }
1118
-
1119
- if (feature.properties.video) {
1120
- if (feature.properties.video.connection) {
1121
- const video = JSON.parse(JSON.stringify(feature.properties.video));
1122
-
1123
- const connection = video.connection;
1124
- delete video.connection;
1125
-
1126
- cot.event.detail.__video = {
1127
- _attributes: { ...video },
1128
- ConnectionEntry: {
1129
- _attributes: connection
1130
- }
1131
- }
1132
- } else {
1133
- cot.event.detail.__video = { _attributes: { ...feature.properties.video } };
1134
- }
1135
- }
1136
-
1137
- if (feature.properties.attachments) {
1138
- cot.event.detail.attachment_list = { _attributes: { hashes: JSON.stringify(feature.properties.attachments) } };
1139
- }
1140
-
1141
- if (feature.properties.contact) {
1142
- cot.event.detail.contact = {
1143
- _attributes: {
1144
- ...feature.properties.contact,
1145
- callsign: feature.properties.callsign || 'UNKNOWN',
1146
- }
1147
- };
1148
- }
1149
-
1150
- if (feature.properties.fileshare) {
1151
- cot.event.detail.fileshare = { _attributes: { ...feature.properties.fileshare } };
1152
- }
1153
-
1154
- if (feature.properties.course !== undefined || feature.properties.speed !== undefined || feature.properties.slope !== undefined) {
1155
- cot.event.detail.track = {
1156
- _attributes: Util.cot_track_attr(feature.properties.course, feature.properties.speed, feature.properties.slope)
1157
- }
1158
- }
1159
-
1160
- if (feature.properties.group) {
1161
- cot.event.detail.__group = { _attributes: { ...feature.properties.group } }
1162
- }
1163
-
1164
- if (feature.properties.flow) {
1165
- cot.event.detail['_flow-tags_'] = { _attributes: { ...feature.properties.flow } }
1166
- }
1167
-
1168
- if (feature.properties.status) {
1169
- cot.event.detail.status = { _attributes: { ...feature.properties.status } }
1170
- }
1171
-
1172
- if (feature.properties.precisionlocation) {
1173
- cot.event.detail.precisionlocation = { _attributes: { ...feature.properties.precisionlocation } }
1174
- }
1175
-
1176
- if (feature.properties.icon) {
1177
- cot.event.detail.usericon = { _attributes: { iconsetpath: feature.properties.icon } }
1178
- }
1179
-
1180
- if (feature.properties.mission) {
1181
- cot.event.detail.mission = {
1182
- _attributes: {
1183
- type: feature.properties.mission.type,
1184
- guid: feature.properties.mission.guid,
1185
- tool: feature.properties.mission.tool,
1186
- name: feature.properties.mission.name,
1187
- authorUid: feature.properties.mission.authorUid,
1188
- }
1189
- }
1190
-
1191
- if (feature.properties.mission.missionLayer) {
1192
- cot.event.detail.mission.missionLayer = {};
1193
-
1194
- if (feature.properties.mission.missionLayer.name) {
1195
- cot.event.detail.mission.missionLayer.name = { _text: feature.properties.mission.missionLayer.name };
1196
- }
1197
-
1198
- if (feature.properties.mission.missionLayer.parentUid) {
1199
- cot.event.detail.mission.missionLayer.parentUid = { _text: feature.properties.mission.missionLayer.parentUid };
1200
- }
1201
-
1202
- if (feature.properties.mission.missionLayer.type) {
1203
- cot.event.detail.mission.missionLayer.type = { _text: feature.properties.mission.missionLayer.type };
1204
- }
1205
-
1206
- if (feature.properties.mission.missionLayer.uid) {
1207
- cot.event.detail.mission.missionLayer.uid = { _text: feature.properties.mission.missionLayer.uid };
1208
- }
1209
- }
1210
- }
1211
-
1212
- cot.event.detail.remarks = { _attributes: { }, _text: feature.properties.remarks || '' };
1213
-
1214
- if (!feature.geometry) {
1215
- throw new Err(400, null, 'Must have Geometry');
1216
- } else if (!['Point', 'Polygon', 'LineString'].includes(feature.geometry.type)) {
1217
- throw new Err(400, null, 'Unsupported Geometry Type');
1218
- }
1219
-
1220
- if (feature.geometry.type === 'Point') {
1221
- cot.event.point._attributes.lon = String(feature.geometry.coordinates[0]);
1222
- cot.event.point._attributes.lat = String(feature.geometry.coordinates[1]);
1223
- cot.event.point._attributes.hae = String(feature.geometry.coordinates[2] || '0.0');
1224
-
1225
-
1226
- if (feature.properties['marker-color']) {
1227
- const color = new Color(feature.properties['marker-color'] || -1761607936);
1228
- color.a = feature.properties['marker-opacity'] !== undefined ? feature.properties['marker-opacity'] * 255 : 128;
1229
- cot.event.detail.color = { _attributes: { argb: String(color.as_32bit()) } };
1230
- }
1231
- } else if (feature.geometry.type === 'Polygon' && feature.properties.type === 'u-d-c-c') {
1232
- if (!feature.properties.shape || !feature.properties.shape.ellipse) {
1233
- throw new Err(400, null, 'u-d-c-c (Circle) must define a feature.properties.shape.ellipse property')
1234
- }
1235
- cot.event.detail.shape = { ellipse: { _attributes: feature.properties.shape.ellipse } }
1236
-
1237
- if (feature.properties.center) {
1238
- cot.event.point._attributes.lon = String(feature.properties.center[0]);
1239
- cot.event.point._attributes.lat = String(feature.properties.center[1]);
1240
- } else {
1241
- const centre = PointOnFeature(feature as AllGeoJSON);
1242
- cot.event.point._attributes.lon = String(centre.geometry.coordinates[0]);
1243
- cot.event.point._attributes.lat = String(centre.geometry.coordinates[1]);
1244
- cot.event.point._attributes.hae = '0.0';
1245
- }
1246
- } else if (['Polygon', 'LineString'].includes(feature.geometry.type)) {
1247
- const stroke = new Color(feature.properties.stroke || -1761607936);
1248
- stroke.a = feature.properties['stroke-opacity'] !== undefined ? feature.properties['stroke-opacity'] * 255 : 128;
1249
- cot.event.detail.strokeColor = { _attributes: { value: String(stroke.as_32bit()) } };
1250
-
1251
- if (!feature.properties['stroke-width']) feature.properties['stroke-width'] = 3;
1252
- cot.event.detail.strokeWeight = { _attributes: {
1253
- value: String(feature.properties['stroke-width'])
1254
- } };
1255
-
1256
- if (!feature.properties['stroke-style']) feature.properties['stroke-style'] = 'solid';
1257
- cot.event.detail.strokeStyle = { _attributes: {
1258
- value: feature.properties['stroke-style']
1259
- } };
1260
-
1261
- if (feature.geometry.type === 'LineString') {
1262
- cot.event._attributes.type = 'u-d-f';
1263
-
1264
- if (!cot.event.detail.link) cot.event.detail.link = [];
1265
- else if (!Array.isArray(cot.event.detail.link)) cot.event.detail.link = [cot.event.detail.link]
1266
-
1267
- for (const coord of feature.geometry.coordinates) {
1268
- cot.event.detail.link.push({
1269
- _attributes: { point: `${coord[1]},${coord[0]}` }
1270
- });
1271
- }
1272
- } else if (feature.geometry.type === 'Polygon') {
1273
- cot.event._attributes.type = 'u-d-f';
1274
-
1275
- if (!cot.event.detail.link) cot.event.detail.link = [];
1276
- else if (!Array.isArray(cot.event.detail.link)) cot.event.detail.link = [cot.event.detail.link]
1277
-
1278
- // Inner rings are not yet supported
1279
- for (const coord of feature.geometry.coordinates[0]) {
1280
- cot.event.detail.link.push({
1281
- _attributes: { point: `${coord[1]},${coord[0]}` }
1282
- });
1283
- }
1284
-
1285
- const fill = new Color(feature.properties.fill || -1761607936);
1286
- fill.a = feature.properties['fill-opacity'] !== undefined ? feature.properties['fill-opacity'] * 255 : 128;
1287
- cot.event.detail.fillColor = { _attributes: { value: String(fill.as_32bit()) } };
1288
- }
1289
-
1290
- cot.event.detail.labels_on = { _attributes: { value: 'false' } };
1291
- cot.event.detail.tog = { _attributes: { enabled: '0' } };
1292
-
1293
- if (feature.properties.center && Array.isArray(feature.properties.center) && feature.properties.center.length >= 2) {
1294
- cot.event.point._attributes.lon = String(feature.properties.center[0]);
1295
- cot.event.point._attributes.lat = String(feature.properties.center[1]);
1296
-
1297
- if (feature.properties.center.length >= 3) {
1298
- cot.event.point._attributes.hae = String(feature.properties.center[2] || '0.0');
1299
- } else {
1300
- cot.event.point._attributes.hae = '0.0';
1301
- }
1302
- } else {
1303
- const centre = PointOnFeature(feature as AllGeoJSON);
1304
- cot.event.point._attributes.lon = String(centre.geometry.coordinates[0]);
1305
- cot.event.point._attributes.lat = String(centre.geometry.coordinates[1]);
1306
- cot.event.point._attributes.hae = '0.0';
1307
- }
1308
- }
1309
-
1310
- const newcot = new CoT(cot);
1311
-
1312
- if (feature.properties.metadata) {
1313
- newcot.metadata = feature.properties.metadata
1314
- }
1315
-
1316
- if (feature.path) {
1317
- newcot.path = feature.path
1318
- }
1319
-
1320
- return newcot;
1321
- }
1322
-
1323
492
  }