@tak-ps/node-cot 2.5.0 → 2.7.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/CHANGELOG.md CHANGED
@@ -10,6 +10,15 @@
10
10
 
11
11
  ## Version History
12
12
 
13
+ ### v2.7.0
14
+
15
+ - :rocket: Improved date parsing in CoT messages created using from_geojson
16
+
17
+ ### v2.6.0
18
+
19
+ - :tada: Add support for passing style properties via GeoJSON Properties
20
+ - :rocket: Add support for encoding/decoding 32bit signed ARGB values
21
+
13
22
  ### v2.5.0
14
23
 
15
24
  - :tada: Automatically perform basic schema validation on CoT Creation
package/lib/color.js ADDED
@@ -0,0 +1,46 @@
1
+ import _color from 'color';
2
+
3
+ /**
4
+ * Helper functions for working with CoT Colours
5
+ *
6
+ * @param {Number|Number[]} color 32bit packged ARGB or [A, R, G, B]
7
+ * @class
8
+ */
9
+ export default class Color {
10
+ constructor(color) {
11
+ if (!isNaN(Number(color))) {
12
+ this.r = (color >> 16) & 255;
13
+ this.g = (color >> 8) & 255;
14
+ this.b = (color >> 0) & 255;
15
+ this.a = ((color >> 24) & 255) / 255;
16
+ } else if (Array.isArray(color)) {
17
+ this.a = color[0];
18
+ this.r = color[1];
19
+ this.g = color[2];
20
+ this.b = color[3];
21
+ } else {
22
+ const c = _color(color);
23
+
24
+ this.a = c.valpha;
25
+ this.r = c.color[0];
26
+ this.g = c.color[1];
27
+ this.b = c.color[2];
28
+ }
29
+ }
30
+
31
+ as_32bit() {
32
+ return (this.a << 24) | (this.r << 16) | (this.g << 8) | this.b;
33
+ }
34
+
35
+ as_opacity() {
36
+ return this.a;
37
+ }
38
+
39
+ as_argb() {
40
+ return [this.a, this.r, this.b, this.g];
41
+ }
42
+
43
+ as_rgb() {
44
+ return [this.r, this.b, this.g];
45
+ }
46
+ }
package/lib/util.js CHANGED
@@ -98,9 +98,11 @@ export default class Util {
98
98
  if (!start) start = new Date(now).toISOString();
99
99
 
100
100
  if (!stale) {
101
- stale = new Date(now + 20 * 1000).toISOString();
101
+ stale = new Date(new Date(start).getTime() + 20 * 1000).toISOString();
102
102
  } else if (!isNaN(parseInt(stale))) {
103
- stale = new Date(now + 20 * 1000).toISOString();
103
+ stale = new Date(new Date(start).getTime() + stale * 1000).toISOString();
104
+ } else if (typeof stale === 'string') {
105
+ stale = new Date(stale).toISOString();
104
106
  }
105
107
 
106
108
  return { time, start, stale };
package/lib/xml.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import xmljs from 'xml-js';
2
2
  import Util from './util.js';
3
+ import Color from './color.js';
3
4
  import PointOnFeature from '@turf/point-on-feature';
4
5
  import AJV from 'ajv';
5
6
  import fs from 'fs';
@@ -60,10 +61,13 @@ export default class XMLCot {
60
61
  if (feature.id) cot.event._attributes.uid = feature.id;
61
62
  if (feature.properties.callsign && !feature.id) cot.event._attributes.uid = feature.properties.callsign;
62
63
 
63
- for (const key of ['time', 'start', 'stale', 'type', 'how']) {
64
+ for (const key of ['type', 'how']) {
64
65
  if (feature.properties[key]) cot.event._attributes[key] = feature.properties[key];
65
66
  }
66
67
 
68
+ const dts = Util.cot_date(feature.properties.time, feature.properties.start, feature.properties.stale);
69
+ Object.assign(cot.event._attributes, dts);
70
+
67
71
  if (!feature.geometry) throw new Error('Must have Geometry');
68
72
  if (!['Point', 'Polygon', 'LineString'].includes(feature.geometry.type)) throw new Error('Unsupported Geoemtry Type');
69
73
 
@@ -71,36 +75,48 @@ export default class XMLCot {
71
75
  cot.event.point._attributes.lon = feature.geometry.coordinates[0];
72
76
  cot.event.point._attributes.lat = feature.geometry.coordinates[1];
73
77
  } else if (['Polygon', 'LineString'].includes(feature.geometry.type)) {
74
- cot.event._attributes.type = 'u-d-f';
78
+ const stroke = new Color(feature.properties.stroke || -1761607936);
79
+ if (feature.properties['stroke-opacity']) stroke.a = feature.properties['stroke-opacity'];
80
+ cot.event.detail.strokeColor = { _attributes: { value: stroke.as_32bit() } };
75
81
 
76
- if (feature.geometry.type === 'Polygon') {
77
- cot.event._attributes.type = 'u-d-r';
82
+ if (!feature.properties['stroke-width']) feature.properties['stroke-width'] = 3;
83
+ cot.event.detail.strokeWeight = { _attributes: {
84
+ value: feature.properties['stroke-width']
85
+ } };
86
+
87
+ if (!feature.properties['stroke-style']) feature.properties['stroke-style'] = 'solid';
88
+ cot.event.detail.strokeStyle = { _attributes: {
89
+ value: feature.properties['stroke-style']
90
+ } };
91
+
92
+ if (feature.geometry.type === 'LineString') {
93
+ cot.event._attributes.type = 'u-d-f';
78
94
 
79
- // Inner rings are not yet supported
80
95
  cot.event.detail.link = [];
81
- feature.geometry.coordinates[0].pop(); // Dont' Close Loop in COT
82
- for (const coord of feature.geometry.coordinates[0]) {
96
+ for (const coord of feature.geometry.coordinates) {
83
97
  cot.event.detail.link.push({
84
98
  _attributes: { point: `${coord[1]},${coord[0]}` }
85
99
  });
86
100
  }
87
- } else if (feature.geometry.type === 'LineString') {
88
- cot.event._attributes.type = 'u-d-f';
101
+ } else if (feature.geometry.type === 'Polygon') {
102
+ cot.event._attributes.type = 'u-d-r';
89
103
 
104
+ // Inner rings are not yet supported
90
105
  cot.event.detail.link = [];
91
- for (const coord of feature.geometry.coordinates) {
106
+ feature.geometry.coordinates[0].pop(); // Dont' Close Loop in COT
107
+ for (const coord of feature.geometry.coordinates[0]) {
92
108
  cot.event.detail.link.push({
93
109
  _attributes: { point: `${coord[1]},${coord[0]}` }
94
110
  });
95
111
  }
112
+
113
+ const fill = new Color(feature.properties.fill || -1761607936);
114
+ if (feature.properties['fill-opacity']) fill.a = feature.properties['fill-opacity'];
115
+ cot.event.detail.fillColor = { _attributes: { value: fill.as_32bit() } };
96
116
  }
97
117
 
98
118
  cot.event.detail.labels_on = { _attributes: { value: 'false' } };
99
119
  cot.event.detail.tog = { _attributes: { enabled: '0' } };
100
- cot.event.detail.strokeColor = { _attributes: { value: '-256' } };
101
- cot.event.detail.strokeWeight = { _attributes: { value: '3.0' } };
102
- cot.event.detail.strokeStyle = { _attributes: { value: 'solid' } };
103
- cot.event.detail.fillColor = { _attributes: { value: '-1761607936' } };
104
120
 
105
121
  const centre = PointOnFeature(feature);
106
122
  cot.event.point._attributes.lon = centre.geometry.coordinates[0];