@tak-ps/node-cot 14.59.0 → 14.60.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 (76) hide show
  1. package/.oxlintrc.json +100 -0
  2. package/CHANGELOG.md +5 -0
  3. package/README.md +13 -0
  4. package/cli.ts +21 -22
  5. package/dist/cli.js +18 -19
  6. package/dist/cli.js.map +1 -1
  7. package/dist/lib/builders/chat.d.ts +4 -4
  8. package/dist/lib/builders/chat.js +3 -6
  9. package/dist/lib/builders/chat.js.map +1 -1
  10. package/dist/lib/builders/route.js +2 -2
  11. package/dist/lib/cot.js +44 -56
  12. package/dist/lib/cot.js.map +1 -1
  13. package/dist/lib/data-package.d.ts +3 -3
  14. package/dist/lib/data-package.js +68 -81
  15. package/dist/lib/data-package.js.map +1 -1
  16. package/dist/lib/parser/from_geojson.js +24 -30
  17. package/dist/lib/parser/from_geojson.js.map +1 -1
  18. package/dist/lib/parser/normalize_geojson.js +22 -22
  19. package/dist/lib/parser/normalize_geojson.js.map +1 -1
  20. package/dist/lib/parser/to_geojson.js +94 -86
  21. package/dist/lib/parser/to_geojson.js.map +1 -1
  22. package/dist/lib/parser-normalize.d.ts +5 -0
  23. package/dist/lib/parser-normalize.js +25 -8
  24. package/dist/lib/parser-normalize.js.map +1 -1
  25. package/dist/lib/parser.js +96 -81
  26. package/dist/lib/parser.js.map +1 -1
  27. package/dist/lib/sensor.js +11 -5
  28. package/dist/lib/sensor.js.map +1 -1
  29. package/dist/lib/type.js +5 -10
  30. package/dist/lib/type.js.map +1 -1
  31. package/dist/lib/types/cot-types.js +4 -4
  32. package/dist/lib/types/cot-types.js.map +1 -1
  33. package/dist/lib/types/feature.d.ts +755 -755
  34. package/dist/lib/types/types.d.ts +55 -9
  35. package/dist/lib/types/types.js +18 -3
  36. package/dist/lib/types/types.js.map +1 -1
  37. package/dist/lib/utils/2525.js +139 -92
  38. package/dist/lib/utils/2525.js.map +1 -1
  39. package/dist/lib/utils/color.js +1 -1
  40. package/dist/lib/utils/color.js.map +1 -1
  41. package/dist/lib/utils/type.js +5 -10
  42. package/dist/lib/utils/type.js.map +1 -1
  43. package/dist/lib/utils/util.d.ts +5 -0
  44. package/dist/lib/utils/util.js +25 -16
  45. package/dist/lib/utils/util.js.map +1 -1
  46. package/dist/lib/xml/basemap.js +10 -9
  47. package/dist/lib/xml/basemap.js.map +1 -1
  48. package/dist/lib/xml/iconset.js +1 -1
  49. package/dist/lib/xml/iconset.js.map +1 -1
  50. package/dist/lib/xml-document.d.ts +1 -1
  51. package/dist/lib/xml-document.js +6 -3
  52. package/dist/lib/xml-document.js.map +1 -1
  53. package/dist/package.json +7 -7
  54. package/dist/tsconfig.tsbuildinfo +1 -1
  55. package/lib/builders/chat.ts +3 -3
  56. package/lib/builders/route.ts +2 -2
  57. package/lib/cot.ts +42 -47
  58. package/lib/data-package.ts +72 -76
  59. package/lib/parser/from_geojson.ts +24 -29
  60. package/lib/parser/normalize_geojson.ts +22 -26
  61. package/lib/parser/to_geojson.ts +105 -94
  62. package/lib/parser-normalize.ts +30 -9
  63. package/lib/parser.ts +121 -92
  64. package/lib/sensor.ts +12 -5
  65. package/lib/type.ts +5 -5
  66. package/lib/types/cot-types.ts +4 -4
  67. package/lib/types/types.ts +20 -3
  68. package/lib/utils/2525.ts +147 -96
  69. package/lib/utils/color.ts +1 -1
  70. package/lib/utils/type.ts +5 -5
  71. package/lib/utils/util.ts +31 -15
  72. package/lib/xml/basemap.ts +10 -9
  73. package/lib/xml/iconset.ts +1 -1
  74. package/lib/xml-document.ts +7 -4
  75. package/package.json +7 -7
  76. package/eslint.config.js +0 -15
@@ -2,7 +2,6 @@ import Err from '@openaddresses/batch-error';
2
2
  import type { Static } from '@sinclair/typebox';
3
3
  import type {
4
4
  Feature,
5
- Polygon,
6
5
  FeaturePropertyMission,
7
6
  FeaturePropertyMissionChange,
8
7
  FeaturePropertyMissionLayer,
@@ -21,11 +20,13 @@ import type {
21
20
  BioMeasurement,
22
21
  BioMeasurementLevel,
23
22
  BioMeasurementLevelAttributes,
23
+ MissionChanges,
24
24
  } from '../types/types.js'
25
25
  import Ellipse from '@turf/ellipse';
26
26
  import Truncate from '@turf/truncate';
27
27
  import { destination } from '@turf/destination';
28
28
  import Color from '../utils/color.js';
29
+ import { jsonClone, toNumber } from '../utils/util.js';
29
30
  import Type2525 from '../utils/2525.js';
30
31
  import JSONCoT from '../types/types.js'
31
32
  import CoT from '../cot.js';
@@ -34,6 +35,10 @@ import CoT from '../cot.js';
34
35
  const COORDINATE_PRECISION = 6;
35
36
  const ELLIPSE_TYPE_PREFIXES = ['u-d-c-c', 'u-r-b-c-c', 'u-d-c-e'];
36
37
 
38
+ function hasFlowAttributes(flow: unknown): flow is { _attributes: Record<string, string> } {
39
+ return typeof flow === 'object' && flow !== null && '_attributes' in flow && Boolean(flow._attributes);
40
+ }
41
+
37
42
  export interface ToGeoJSONOptions {
38
43
  /**
39
44
  * If a numeric 2525D/E SIDC is present in the milicon detail, surface it
@@ -49,17 +54,17 @@ export async function to_geojson(
49
54
  cot: CoT,
50
55
  opts: ToGeoJSONOptions = {}
51
56
  ): Promise<Static<typeof Feature>> {
52
- const raw: Static<typeof JSONCoT> = JSON.parse(JSON.stringify(cot.raw));
53
- if (!raw.event.detail) raw.event.detail = {};
54
- if (!raw.event.detail.contact) raw.event.detail.contact = { _attributes: { callsign: 'UNKNOWN' } };
55
- if (!raw.event.detail.contact._attributes) raw.event.detail.contact._attributes = { callsign: 'UNKNOWN' };
57
+ const raw: Static<typeof JSONCoT> = jsonClone(cot.raw);
58
+ raw.event.detail ??= {};
59
+ raw.event.detail.contact ??= { _attributes: { callsign: 'UNKNOWN' } };
60
+ raw.event.detail.contact._attributes ??= { callsign: 'UNKNOWN' };
56
61
 
57
62
  const feat: Static<typeof Feature> = {
58
63
  id: raw.event._attributes.uid,
59
64
  type: 'Feature',
60
65
  properties: {
61
66
  callsign: raw.event.detail.contact._attributes.callsign || 'UNKNOWN',
62
- center: [ Number(raw.event.point._attributes.lon), Number(raw.event.point._attributes.lat), Number(raw.event.point._attributes.hae) ],
67
+ center: [ toNumber(raw.event.point._attributes.lon), toNumber(raw.event.point._attributes.lat), toNumber(raw.event.point._attributes.hae) ],
63
68
  type: raw.event._attributes.type,
64
69
  how: raw.event._attributes.how || '',
65
70
  time: raw.event._attributes.time,
@@ -68,13 +73,13 @@ export async function to_geojson(
68
73
  },
69
74
  geometry: {
70
75
  type: 'Point',
71
- coordinates: [ Number(raw.event.point._attributes.lon), Number(raw.event.point._attributes.lat), Number(raw.event.point._attributes.hae) ]
76
+ coordinates: [ toNumber(raw.event.point._attributes.lon), toNumber(raw.event.point._attributes.lat), toNumber(raw.event.point._attributes.hae) ]
72
77
  }
73
78
  };
74
79
 
75
- const contact = JSON.parse(JSON.stringify(raw.event.detail.contact._attributes));
80
+ const contact = jsonClone(raw.event.detail.contact._attributes);
76
81
  delete contact.callsign;
77
- if (Object.keys(contact).length) {
82
+ if (Object.keys(contact).length > 0) {
78
83
  feat.properties.contact = contact;
79
84
  }
80
85
 
@@ -89,7 +94,7 @@ export async function to_geojson(
89
94
  if (raw.event.detail.fileshare) {
90
95
  feat.properties.fileshare = raw.event.detail.fileshare._attributes;
91
96
  if (typeof feat.properties.fileshare?.sizeInBytes === 'string') {
92
- feat.properties.fileshare.sizeInBytes = parseInt(feat.properties.fileshare.sizeInBytes)
97
+ feat.properties.fileshare.sizeInBytes = Math.trunc(Number(feat.properties.fileshare.sizeInBytes))
93
98
  }
94
99
  }
95
100
 
@@ -128,12 +133,12 @@ export async function to_geojson(
128
133
 
129
134
  if (rad.radmeasurement) {
130
135
  const measurements = Array.isArray(rad.radmeasurement) ? rad.radmeasurement : [rad.radmeasurement];
131
- feat.properties.radsensordetail.radmeasurement = measurements.map((m: Static<typeof RadMeasurement>) => ({ ...m._attributes }));
136
+ feat.properties.radsensordetail.radmeasurement = measurements.map((m: Static<typeof RadMeasurement>) => m._attributes);
132
137
  }
133
138
 
134
139
  if (rad.physical_module) {
135
140
  const modules = Array.isArray(rad.physical_module) ? rad.physical_module : [rad.physical_module];
136
- feat.properties.radsensordetail.physical_module = modules.map((m: Static<typeof RadPhysicalModule>) => ({ ...m._attributes }));
141
+ feat.properties.radsensordetail.physical_module = modules.map((m: Static<typeof RadPhysicalModule>) => m._attributes);
137
142
  }
138
143
 
139
144
  if (rad.search_algorithm) {
@@ -142,12 +147,12 @@ export async function to_geojson(
142
147
 
143
148
  if (rad.spectrum) {
144
149
  const spectra = Array.isArray(rad.spectrum) ? rad.spectrum : [rad.spectrum];
145
- feat.properties.radsensordetail.spectrum = spectra.map((s: Static<typeof RadSpectrum>) => ({ ...s._attributes }));
150
+ feat.properties.radsensordetail.spectrum = spectra.map((s: Static<typeof RadSpectrum>) => s._attributes);
146
151
  }
147
152
 
148
153
  if (rad.isotope) {
149
154
  const isotopes = Array.isArray(rad.isotope) ? rad.isotope : [rad.isotope];
150
- feat.properties.radsensordetail.isotope = isotopes.map((i: Static<typeof RadIsotope>) => ({ ...i._attributes }));
155
+ feat.properties.radsensordetail.isotope = isotopes.map((i: Static<typeof RadIsotope>) => i._attributes);
151
156
  }
152
157
 
153
158
  if (rad.data_permissions) {
@@ -167,7 +172,7 @@ export async function to_geojson(
167
172
 
168
173
  if (chem.detection) {
169
174
  const detections = Array.isArray(chem.detection) ? chem.detection : [chem.detection];
170
- feat.properties.chemsensordetail.detection = detections.map((d: Static<typeof ChemDetection>) => ({ ...d._attributes }));
175
+ feat.properties.chemsensordetail.detection = detections.map((d: Static<typeof ChemDetection>) => d._attributes);
171
176
  }
172
177
  }
173
178
 
@@ -180,10 +185,10 @@ export async function to_geojson(
180
185
  if (bio.measurement) {
181
186
  const measurements = Array.isArray(bio.measurement) ? bio.measurement : [bio.measurement];
182
187
  feat.properties.biosensordetail.measurement = measurements.map((m: Static<typeof BioMeasurement>) => {
183
- const measurement: { level?: Array<Static<typeof BioMeasurementLevelAttributes>> } & typeof m._attributes = { ...m._attributes };
188
+ const measurement: { level?: Array<Static<typeof BioMeasurementLevelAttributes>> } & typeof m._attributes = m._attributes;
184
189
  if (m.level) {
185
190
  const levels = Array.isArray(m.level) ? m.level : [m.level];
186
- measurement.level = levels.map((l: Static<typeof BioMeasurementLevel>) => ({ ...l._attributes }));
191
+ measurement.level = levels.map((l: Static<typeof BioMeasurementLevel>) => l._attributes);
187
192
  }
188
193
  return measurement;
189
194
  });
@@ -243,12 +248,13 @@ export async function to_geojson(
243
248
  }
244
249
 
245
250
  if (raw.event.detail.attachment_list) {
251
+ // oxlint-disable-next-line typescript/no-unsafe-assignment
246
252
  feat.properties.attachments = JSON.parse(raw.event.detail.attachment_list._attributes.hashes);
247
253
  }
248
254
 
249
255
  if (raw.event.detail.image) {
250
256
  feat.properties.image = {
251
- ...(raw.event.detail.image._attributes || {}),
257
+ ...raw.event.detail.image._attributes,
252
258
  data: raw.event.detail.image._text
253
259
  };
254
260
  }
@@ -271,7 +277,7 @@ export async function to_geojson(
271
277
  });
272
278
  }
273
279
 
274
- if (!feat.properties.links || !feat.properties.links.length) delete feat.properties.links;
280
+ if (feat.properties.links.length === 0) delete feat.properties.links;
275
281
  }
276
282
 
277
283
  if (raw.event.detail.archive) {
@@ -283,14 +289,16 @@ export async function to_geojson(
283
289
  }
284
290
 
285
291
  if (raw.event.detail.__chat) {
292
+ const chatgrp: unknown = raw.event.detail.__chat.chatgrp;
286
293
  feat.properties.chat = {
287
294
  ...raw.event.detail.__chat._attributes,
288
- chatgrp: raw.event.detail.__chat.chatgrp
295
+ chatgrp
289
296
  }
290
297
  } else if (raw.event.detail.__chatreceipt) {
298
+ const chatgrp: unknown = raw.event.detail.__chatreceipt.chatgrp;
291
299
  feat.properties.chat = {
292
300
  ...raw.event.detail.__chatreceipt._attributes,
293
- chatgrp: raw.event.detail.__chatreceipt.chatgrp
301
+ chatgrp
294
302
  }
295
303
  }
296
304
 
@@ -342,8 +350,9 @@ export async function to_geojson(
342
350
  feat.properties.group = raw.event.detail.__group._attributes;
343
351
  }
344
352
 
345
- if (raw.event.detail['_flow-tags_']?._attributes) {
346
- feat.properties.flow = raw.event.detail['_flow-tags_']._attributes;
353
+ const flow: unknown = raw.event.detail['_flow-tags_'];
354
+ if (hasFlowAttributes(flow)) {
355
+ feat.properties.flow = flow._attributes;
347
356
  }
348
357
 
349
358
  if (raw.event.detail.status?._attributes) {
@@ -356,30 +365,33 @@ export async function to_geojson(
356
365
  };
357
366
 
358
367
  if (raw.event.detail.mission.MissionChanges) {
359
- const changes =
368
+ const missionChanges: Array<Static<typeof MissionChanges>> =
360
369
  Array.isArray(raw.event.detail.mission.MissionChanges)
361
370
  ? raw.event.detail.mission.MissionChanges
362
371
  : [ raw.event.detail.mission.MissionChanges ]
363
372
 
373
+ const changes = missionChanges.flatMap((missionChange) => missionChange.MissionChange);
374
+
364
375
  mission.missionChanges = []
365
376
  for (const change of changes) {
366
377
  const mc: Static<typeof FeaturePropertyMissionChange> = {
367
- contentUid: change.MissionChange.contentUid ? change.MissionChange.contentUid._text : undefined,
368
- creatorUid: change.MissionChange.creatorUid._text,
369
- isFederatedChange: change.MissionChange.isFederatedChange._text,
370
- missionName: change.MissionChange.missionName._text,
371
- timestamp: change.MissionChange.timestamp._text,
372
- type: change.MissionChange.type._text,
378
+ contentUid: change.contentUid ? change.contentUid._text : undefined,
379
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion
380
+ creatorUid: change.creatorUid._text as string,
381
+ isFederatedChange: change.isFederatedChange._text,
382
+ missionName: change.missionName._text,
383
+ timestamp: change.timestamp._text,
384
+ type: change.type._text,
373
385
  };
374
386
 
375
- if (change.MissionChange.contentResource) {
376
- const cr = change.MissionChange.contentResource;
387
+ if (change.contentResource) {
388
+ const cr = change.contentResource;
377
389
  mc.contentResource = {
378
390
  uid: cr.uid._text,
379
391
  expiration: cr.expiration._text,
380
392
  hash: cr.hash._text,
381
393
  name: cr.name._text,
382
- size: parseInt(cr.size._text),
394
+ size: Math.trunc(toNumber(cr.size._text)),
383
395
  submissionTime: cr.submissionTime._text,
384
396
  submitter: cr.submitter._text,
385
397
  filename: cr.filename ? cr.filename._text : undefined,
@@ -387,21 +399,22 @@ export async function to_geojson(
387
399
  };
388
400
  }
389
401
 
390
- if (change.MissionChange.content?.MissionProperty) {
391
- const mp = change.MissionChange.content.MissionProperty;
402
+ if (change.content?.MissionProperty) {
403
+ const mp = change.content.MissionProperty;
392
404
  mc.missionProperty = {
393
405
  key: mp.key._text,
394
406
  value: mp.value?._text ?? '',
395
407
  };
396
408
  }
397
409
 
398
- if (change.MissionChange.details) {
410
+ if (change.details) {
411
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion
399
412
  mc.details = {
400
- ...change.MissionChange.details._attributes,
401
- ...change.MissionChange.details.location
402
- ? change.MissionChange.details.location._attributes
413
+ ...change.details._attributes,
414
+ ...change.details.location
415
+ ? change.details.location._attributes
403
416
  : {}
404
- };
417
+ } as Static<typeof FeaturePropertyMissionChange>['details'];
405
418
  }
406
419
 
407
420
  mission.missionChanges.push(mc)
@@ -440,13 +453,14 @@ export async function to_geojson(
440
453
  }
441
454
 
442
455
  if (raw.event.detail.strokeColor?._attributes?.value !== undefined) {
443
- const stroke = new Color(Number(raw.event.detail.strokeColor._attributes.value));
456
+ const stroke = new Color(toNumber(raw.event.detail.strokeColor._attributes.value));
444
457
  feat.properties.stroke = stroke.as_hex();
445
458
  feat.properties['stroke-opacity'] = stroke.as_opacity() / 255;
446
459
  }
447
460
 
448
- if (raw.event.detail.strokeWeight?._attributes?.value) {
449
- feat.properties['stroke-width'] = Number(raw.event.detail.strokeWeight._attributes.value);
461
+ const strokeWeight = raw.event.detail.strokeWeight?._attributes?.value ?? 0;
462
+ if (strokeWeight) {
463
+ feat.properties['stroke-width'] = toNumber(strokeWeight);
450
464
  }
451
465
 
452
466
  if (raw.event.detail.strokeStyle?._attributes?.value) {
@@ -458,7 +472,7 @@ export async function to_geojson(
458
472
 
459
473
  if (Array.isArray(raw.event.detail.color) && raw.event.detail.color.length > 1) {
460
474
  color = raw.event.detail.color[0];
461
- if (!color._attributes) color._attributes = {};
475
+ color._attributes ??= {};
462
476
 
463
477
  for (let i = raw.event.detail.color.length - 1; i >= 1; i--) {
464
478
  if (raw.event.detail.color[i]._attributes) {
@@ -471,8 +485,9 @@ export async function to_geojson(
471
485
  color = raw.event.detail.color;
472
486
  }
473
487
 
474
- if (color?._attributes?.argb) {
475
- const parsedColor = new Color(Number(color._attributes.argb));
488
+ const argb = color?._attributes?.argb ?? 0;
489
+ if (argb) {
490
+ const parsedColor = new Color(toNumber(argb));
476
491
  feat.properties['marker-color'] = parsedColor.as_hex();
477
492
  feat.properties['marker-opacity'] = parsedColor.as_opacity() / 255;
478
493
  }
@@ -484,12 +499,12 @@ export async function to_geojson(
484
499
 
485
500
  for (const l of raw.event.detail.link) {
486
501
  if (!l._attributes.point) continue;
487
- coordinates.push(
488
- l._attributes.point.split(',')
489
- .map((p: string) => { return Number(p.trim()) })
490
- .splice(0, 2)
491
- .reverse()
492
- );
502
+ const coord = l._attributes.point.split(',')
503
+ .map((p: string) => { return Number(p.trim()) })
504
+ .splice(0, 2);
505
+ coord.reverse();
506
+
507
+ coordinates.push(coord);
493
508
  }
494
509
 
495
510
  // Range & Bearing Line
@@ -499,7 +514,7 @@ export async function to_geojson(
499
514
  if (!detail.range) throw new Error('Range value not provided')
500
515
  if (!detail.bearing) throw new Error('Bearing value not provided')
501
516
 
502
- // TODO Support inclination
517
+ // Inclination is not yet supported
503
518
  const dest = destination(
504
519
  cot.position(),
505
520
  detail.range._attributes.value / 1000,
@@ -510,7 +525,7 @@ export async function to_geojson(
510
525
  type: 'LineString',
511
526
  coordinates: [cot.position(), dest]
512
527
  };
513
- } 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])) {
528
+ } else if (raw.event._attributes.type === 'u-d-r' || (coordinates[0][0] === coordinates.at(-1)?.[0] && coordinates[0][1] === coordinates.at(-1)?.[1])) {
514
529
  if (raw.event._attributes.type === 'u-d-r') {
515
530
  // CoT rectangles are only 4 points - GeoJSON needs to be closed
516
531
  coordinates.push(coordinates[0])
@@ -522,7 +537,7 @@ export async function to_geojson(
522
537
  }
523
538
 
524
539
  if (raw.event.detail.fillColor?._attributes?.value !== undefined) {
525
- const fill = new Color(Number(raw.event.detail.fillColor._attributes.value));
540
+ const fill = new Color(toNumber(raw.event.detail.fillColor._attributes.value));
526
541
  feat.properties['fill-opacity'] = fill.as_opacity() / 255;
527
542
  feat.properties['fill'] = fill.as_hex();
528
543
  }
@@ -535,10 +550,7 @@ export async function to_geojson(
535
550
  } else if (ELLIPSE_TYPE_PREFIXES.some((prefix) => raw.event._attributes.type.startsWith(prefix))) {
536
551
  if (!raw.event.detail.shape) throw new Err(400, null, `${raw.event._attributes.type} (Circle) must define shape value`)
537
552
 
538
- if (
539
- !raw.event.detail.shape.ellipse
540
- || !raw.event.detail.shape.ellipse._attributes
541
- ) throw new Err(400, null, `${raw.event._attributes.type} (Circle) must define ellipse shape value`)
553
+ if (!raw.event.detail.shape.ellipse?._attributes) throw new Err(400, null, `${raw.event._attributes.type} (Circle) must define ellipse shape value`)
542
554
 
543
555
  const ellipse: {
544
556
  major: number;
@@ -546,18 +558,18 @@ export async function to_geojson(
546
558
  angle: number;
547
559
  swapAxis?: boolean;
548
560
  } = {
549
- major: Number(raw.event.detail.shape.ellipse._attributes.major),
550
- minor: Number(raw.event.detail.shape.ellipse._attributes.minor),
551
- angle: Number(raw.event.detail.shape.ellipse._attributes.angle)
561
+ major: toNumber(raw.event.detail.shape.ellipse._attributes.major),
562
+ minor: toNumber(raw.event.detail.shape.ellipse._attributes.minor),
563
+ angle: toNumber(raw.event.detail.shape.ellipse._attributes.angle)
552
564
  }
553
565
 
554
566
  if (raw.event.detail.shape.ellipse._attributes.swapAxis !== undefined) {
555
- ellipse.swapAxis = Boolean(raw.event.detail.shape.ellipse._attributes.swapAxis);
567
+ ellipse.swapAxis = raw.event.detail.shape.ellipse._attributes.swapAxis;
556
568
  }
557
569
 
558
570
  // ATAK puts circle fill in detail.fillColor; KML PolyStyle (below) may override.
559
571
  if (raw.event.detail.fillColor?._attributes?.value !== undefined) {
560
- const fill = new Color(Number(raw.event.detail.fillColor._attributes.value));
572
+ const fill = new Color(toNumber(raw.event.detail.fillColor._attributes.value));
561
573
  feat.properties['fill-opacity'] = fill.as_opacity() / 255;
562
574
  feat.properties['fill'] = fill.as_hex();
563
575
  }
@@ -568,18 +580,18 @@ export async function to_geojson(
568
580
  : [raw.event.detail.shape.link])
569
581
  : [];
570
582
  const styleLink = styleLinks.find((link) => (
571
- link?._attributes?.type === 'b-x-KmlStyle' && link.Style
583
+ link?._attributes?.type === 'b-x-KmlStyle' && link.Style !== undefined
572
584
  ));
573
585
 
574
586
  if (styleLink?.Style) {
575
587
  if (styleLink.Style.LineStyle?.color) {
576
588
  let rawColor = styleLink.Style.LineStyle.color._text;
577
- if (rawColor.startsWith('#')) rawColor = rawColor.substring(1);
589
+ if (rawColor.startsWith('#')) rawColor = rawColor.slice(1);
578
590
 
579
- const a = parseInt(rawColor.substring(0, 2), 16);
580
- const r = parseInt(rawColor.substring(2, 4), 16);
581
- const g = parseInt(rawColor.substring(4, 6), 16);
582
- const b = parseInt(rawColor.substring(6, 8), 16);
591
+ const a = parseInt(rawColor.slice(0, 2), 16);
592
+ const r = parseInt(rawColor.slice(2, 4), 16);
593
+ const g = parseInt(rawColor.slice(4, 6), 16);
594
+ const b = parseInt(rawColor.slice(6, 8), 16);
583
595
 
584
596
  const strokeColor = new Color([a, r, g, b]);
585
597
  feat.properties.stroke = strokeColor.as_hex();
@@ -588,17 +600,17 @@ export async function to_geojson(
588
600
  }
589
601
 
590
602
  if (styleLink.Style.LineStyle?.width) {
591
- feat.properties['stroke-width'] = Number(styleLink.Style.LineStyle.width._text);
603
+ feat.properties['stroke-width'] = toNumber(styleLink.Style.LineStyle.width._text);
592
604
  }
593
605
 
594
606
  if (styleLink.Style.PolyStyle?.color) {
595
607
  let rawColor = styleLink.Style.PolyStyle.color._text;
596
- if (rawColor.startsWith('#')) rawColor = rawColor.substring(1);
608
+ if (rawColor.startsWith('#')) rawColor = rawColor.slice(1);
597
609
 
598
- const a = parseInt(rawColor.substring(0, 2), 16);
599
- const r = parseInt(rawColor.substring(2, 4), 16);
600
- const g = parseInt(rawColor.substring(4, 6), 16);
601
- const b = parseInt(rawColor.substring(6, 8), 16);
610
+ const a = parseInt(rawColor.slice(0, 2), 16);
611
+ const r = parseInt(rawColor.slice(2, 4), 16);
612
+ const g = parseInt(rawColor.slice(4, 6), 16);
613
+ const b = parseInt(rawColor.slice(6, 8), 16);
602
614
 
603
615
  const fillColor = new Color([a, r, g, b]);
604
616
  feat.properties['fill-opacity'] = fillColor.as_opacity() / 255;
@@ -607,9 +619,9 @@ export async function to_geojson(
607
619
  }
608
620
 
609
621
  feat.geometry = Truncate(Ellipse(
610
- feat.geometry.coordinates as number[],
611
- Number(ellipse.major) / 1000,
612
- Number(ellipse.minor) / 1000,
622
+ feat.properties.center,
623
+ ellipse.major / 1000,
624
+ ellipse.minor / 1000,
613
625
  {
614
626
  angle: 90 - ellipse.angle
615
627
  }
@@ -617,7 +629,7 @@ export async function to_geojson(
617
629
  precision: COORDINATE_PRECISION,
618
630
  coordinates: 3,
619
631
  mutate: true
620
- }).geometry as Static<typeof Polygon>;
632
+ }).geometry;
621
633
 
622
634
  feat.properties.shape = {};
623
635
  feat.properties.shape.ellipse = ellipse;
@@ -627,8 +639,8 @@ export async function to_geojson(
627
639
 
628
640
  const vertices = Array.isArray(raw.event.detail.shape.polyline.vertex) ? raw.event.detail.shape.polyline.vertex : [raw.event.detail.shape.polyline.vertex];
629
641
  for (const v of vertices) {
630
- const coord = [Number(v._attributes.lon), Number(v._attributes.lat)];
631
- if (v._attributes.hae !== undefined) coord.push(Number(v._attributes.hae));
642
+ const coord = [toNumber(v._attributes.lon), toNumber(v._attributes.lat)];
643
+ if (v._attributes.hae !== undefined) coord.push(toNumber(v._attributes.hae));
632
644
  coordinates.push(coord);
633
645
  }
634
646
 
@@ -636,8 +648,8 @@ export async function to_geojson(
636
648
  feat.geometry = { type: 'Point', coordinates: coordinates[0] }
637
649
  } else if (raw.event.detail.shape.polyline._attributes?.closed === true) {
638
650
  const first = coordinates[0];
639
- const last = coordinates[coordinates.length - 1];
640
- if (first[0] !== last[0] || first[1] !== last[1]) coordinates.push(first);
651
+ const last = coordinates.at(-1);
652
+ if (first[0] !== last?.[0] || first[1] !== last[1]) coordinates.push(first);
641
653
 
642
654
  feat.geometry = { type: 'Polygon', coordinates: [coordinates] }
643
655
  } else {
@@ -657,16 +669,15 @@ export async function to_geojson(
657
669
  feat.properties['stroke-opacity'] = stroke.as_opacity() / 255;
658
670
  }
659
671
  }
660
- } else if (raw.event._attributes.type === 'b-m-p-s-m') {
661
- if (
662
- raw.event.detail.usericon?._attributes?.iconsetpath
663
- && raw.event.detail.usericon._attributes.iconsetpath.startsWith('COT_MAPPING_SPOTMAP/b-m-p-s-m/')
664
- ) {
665
- const spot = new Color(Number(raw.event.detail.usericon._attributes.iconsetpath.split('/')[2]));
666
- feat.properties['marker-color'] = spot.as_hex();
667
- feat.properties['marker-opacity'] = spot.as_opacity() / 255;
668
- delete feat.properties.icon;
669
- }
672
+ } else if (
673
+ raw.event._attributes.type === 'b-m-p-s-m'
674
+ && raw.event.detail.usericon?._attributes?.iconsetpath
675
+ && raw.event.detail.usericon._attributes.iconsetpath.startsWith('COT_MAPPING_SPOTMAP/b-m-p-s-m/')
676
+ ) {
677
+ const spot = new Color(Number(raw.event.detail.usericon._attributes.iconsetpath.split('/')[2]));
678
+ feat.properties['marker-color'] = spot.as_hex();
679
+ feat.properties['marker-opacity'] = spot.as_opacity() / 255;
680
+ delete feat.properties.icon;
670
681
  }
671
682
 
672
683
  feat.properties.metadata = cot.metadata;
@@ -5,6 +5,10 @@ export type ParsedAttributeVisitor = (
5
5
  path: string[]
6
6
  ) => void;
7
7
 
8
+ function isRecord(value: unknown): value is Record<string, unknown> {
9
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
10
+ }
11
+
8
12
  export function visitElementAttributes(
9
13
  input: unknown,
10
14
  visitor: ParsedAttributeVisitor,
@@ -18,18 +22,15 @@ export function visitElementAttributes(
18
22
  return;
19
23
  }
20
24
 
21
- if (!input || typeof input !== 'object') {
25
+ if (!isRecord(input)) {
22
26
  return;
23
27
  }
24
28
 
25
- const record = input as Record<string, unknown>;
26
- for (const [key, value] of Object.entries(record)) {
27
- if (key === '_attributes' && value && typeof value === 'object' && !Array.isArray(value)) {
28
- const attributes = value as Record<string, unknown>;
29
-
30
- for (const [attributeKey, attributeValue] of Object.entries(attributes)) {
31
- visitor(attributeKey, attributeValue, attributes, [...path, key]);
32
- visitElementAttributes(attributes[attributeKey], visitor, [...path, key, attributeKey]);
29
+ for (const [key, value] of Object.entries(input)) {
30
+ if (key === '_attributes' && isRecord(value)) {
31
+ for (const [attributeKey, attributeValue] of Object.entries(value)) {
32
+ visitor(attributeKey, attributeValue, value, [...path, key]);
33
+ visitElementAttributes(value[attributeKey], visitor, [...path, key, attributeKey]);
33
34
  }
34
35
 
35
36
  continue;
@@ -53,3 +54,23 @@ export function normalizeBooleanAttributeValues(input: unknown): void {
53
54
  }
54
55
  });
55
56
  }
57
+
58
+ /**
59
+ * xml-js leaves a circular `_parent` reference on every element that is still
60
+ * open when the input ends
61
+ */
62
+ export function hasUnclosedElements(input: unknown): boolean {
63
+ if (Array.isArray(input)) {
64
+ return input.some((value) => hasUnclosedElements(value));
65
+ }
66
+
67
+ if (!isRecord(input)) {
68
+ return false;
69
+ }
70
+
71
+ if ('_parent' in input) {
72
+ return true;
73
+ }
74
+
75
+ return Object.values(input).some((value) => hasUnclosedElements(value));
76
+ }