drone-path-core 1.0.0-rc1 → 1.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/track-gpx.js CHANGED
@@ -2,15 +2,33 @@
2
2
  // and a <time> only where the vertex carries trustworthy UTC. No metadata
3
3
  // wall-clock or version strings, so identical input yields identical bytes.
4
4
  import { isoUtc } from './track.js';
5
+ function decimal(value) {
6
+ const text = String(value);
7
+ const scientific = /^(-?)(\d)(?:\.(\d+))?e([+-]?\d+)$/u.exec(text);
8
+ if (!scientific)
9
+ return text;
10
+ const [, sign, digit, fraction = '', exponent] = scientific;
11
+ const digits = digit + fraction;
12
+ const position = 1 + Number(exponent);
13
+ if (position <= 0)
14
+ return sign + '0.' + '0'.repeat(-position) + digits;
15
+ if (position >= digits.length)
16
+ return sign + digits + '0'.repeat(position - digits.length);
17
+ return sign + digits.slice(0, position) + '.' + digits.slice(position);
18
+ }
19
+ // GPX 1.1's longitude domain excludes the east edge of the antimeridian.
20
+ const GPX_EAST_EDGE = 179.99999999999997;
5
21
  function trkptLines(vertex) {
22
+ const lat = decimal(vertex.lat);
23
+ const lon = decimal(vertex.lon === 180 ? GPX_EAST_EDGE : vertex.lon);
6
24
  const children = [];
7
25
  if (vertex.altM !== null)
8
- children.push(`<ele>${vertex.altM}</ele>`);
26
+ children.push(`<ele>${decimal(vertex.altM)}</ele>`);
9
27
  if (vertex.timeMs !== null)
10
28
  children.push(`<time>${isoUtc(vertex.timeMs)}</time>`);
11
- const open = `<trkpt lat="${vertex.lat}" lon="${vertex.lon}">`;
29
+ const open = `<trkpt lat="${lat}" lon="${lon}">`;
12
30
  if (children.length === 0)
13
- return [` <trkpt lat="${vertex.lat}" lon="${vertex.lon}"/>`];
31
+ return [` <trkpt lat="${lat}" lon="${lon}"/>`];
14
32
  return [
15
33
  ` ${open}`,
16
34
  ...children.map((child) => ` ${child}`),
@@ -18,17 +36,23 @@ function trkptLines(vertex) {
18
36
  ];
19
37
  }
20
38
  export function serializeGpx(segments) {
39
+ return serializeGpxTracks([segments]);
40
+ }
41
+ export function serializeGpxTracks(tracks) {
21
42
  const lines = [
22
43
  '<?xml version="1.0" encoding="UTF-8"?>',
23
44
  '<gpx version="1.1" creator="drone-path" xmlns="http://www.topografix.com/GPX/1/1">',
24
- ' <trk>',
25
45
  ];
26
- for (const segment of segments) {
27
- lines.push(' <trkseg>');
28
- for (const vertex of segment)
29
- lines.push(...trkptLines(vertex));
30
- lines.push(' </trkseg>');
46
+ for (const segments of tracks) {
47
+ lines.push(' <trk>');
48
+ for (const segment of segments) {
49
+ lines.push(' <trkseg>');
50
+ for (const vertex of segment)
51
+ lines.push(...trkptLines(vertex));
52
+ lines.push(' </trkseg>');
53
+ }
54
+ lines.push(' </trk>');
31
55
  }
32
- lines.push(' </trk>', '</gpx>', '');
56
+ lines.push('</gpx>', '');
33
57
  return lines.join('\n');
34
58
  }
@@ -1,2 +1,3 @@
1
1
  import type { TrackSegment } from './track.js';
2
2
  export declare function serializeKml(segments: readonly TrackSegment[]): string;
3
+ export declare function serializeKmlTracks(tracks: readonly (readonly TrackSegment[])[]): string;
package/dist/track-kml.js CHANGED
@@ -35,13 +35,21 @@ function placemarkLines(segment) {
35
35
  return lines;
36
36
  }
37
37
  export function serializeKml(segments) {
38
+ return kmlDocument(segments.flatMap(placemarkLines));
39
+ }
40
+ export function serializeKmlTracks(tracks) {
41
+ return kmlDocument(tracks.flatMap((segments, index) => [
42
+ ' <Folder>', ` <name>Flight ${index + 1}</name>`,
43
+ ...segments.flatMap((segment) => placemarkLines(segment.length === 1 ? [segment[0], segment[0]] : segment))
44
+ .map((line) => ` ${line}`),
45
+ ' </Folder>',
46
+ ]));
47
+ }
48
+ function kmlDocument(placemarks) {
38
49
  const lines = [
39
50
  '<?xml version="1.0" encoding="UTF-8"?>',
40
51
  '<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">',
41
52
  ' <Document>',
42
53
  ];
43
- for (const segment of segments)
44
- lines.push(...placemarkLines(segment));
45
- lines.push(' </Document>', '</kml>', '');
46
- return lines.join('\n');
54
+ return lines.concat(placemarks, ' </Document>', '</kml>', '').join('\n');
47
55
  }
package/dist/track-kmz.js CHANGED
@@ -7,35 +7,29 @@ const ENTRY_NAME = 'doc.kml';
7
7
  // The zip epoch (1980-01-01 00:00:00) in MS-DOS date/time fields.
8
8
  const DOS_DATE_1980_01_01 = 0x0021;
9
9
  const DOS_TIME_MIDNIGHT = 0x0000;
10
- function localHeader(name, data) {
10
+ function writeCommonEntryFields(buf, offset, crc, dataLength, nameLength) {
11
+ buf.writeUInt16LE(20, offset); // version needed: 2.0
12
+ buf.writeUInt16LE(0, offset + 2); // flags
13
+ buf.writeUInt16LE(0, offset + 4); // method: stored
14
+ buf.writeUInt16LE(DOS_TIME_MIDNIGHT, offset + 6);
15
+ buf.writeUInt16LE(DOS_DATE_1980_01_01, offset + 8);
16
+ buf.writeUInt32LE(crc, offset + 10);
17
+ buf.writeUInt32LE(dataLength, offset + 14);
18
+ buf.writeUInt32LE(dataLength, offset + 18);
19
+ buf.writeUInt16LE(nameLength, offset + 22);
20
+ buf.writeUInt16LE(0, offset + 24); // extra field length
21
+ }
22
+ function localHeader(name, data, crc) {
11
23
  const header = Buffer.alloc(30);
12
24
  header.writeUInt32LE(0x04034b50, 0);
13
- header.writeUInt16LE(20, 4); // version needed: 2.0
14
- header.writeUInt16LE(0, 6); // flags
15
- header.writeUInt16LE(0, 8); // method: stored
16
- header.writeUInt16LE(DOS_TIME_MIDNIGHT, 10);
17
- header.writeUInt16LE(DOS_DATE_1980_01_01, 12);
18
- header.writeUInt32LE(crc32(data), 14);
19
- header.writeUInt32LE(data.length, 18);
20
- header.writeUInt32LE(data.length, 22);
21
- header.writeUInt16LE(name.length, 26);
22
- header.writeUInt16LE(0, 28); // extra field length
25
+ writeCommonEntryFields(header, 4, crc, data.length, name.length);
23
26
  return Buffer.concat([header, name, data]);
24
27
  }
25
- function centralDirectoryEntry(name, data, offset) {
28
+ function centralDirectoryEntry(name, dataLength, crc, offset) {
26
29
  const entry = Buffer.alloc(46);
27
30
  entry.writeUInt32LE(0x02014b50, 0);
28
31
  entry.writeUInt16LE(20, 4); // version made by: 2.0, MS-DOS
29
- entry.writeUInt16LE(20, 6); // version needed: 2.0
30
- entry.writeUInt16LE(0, 8); // flags
31
- entry.writeUInt16LE(0, 10); // method: stored
32
- entry.writeUInt16LE(DOS_TIME_MIDNIGHT, 12);
33
- entry.writeUInt16LE(DOS_DATE_1980_01_01, 14);
34
- entry.writeUInt32LE(crc32(data), 16);
35
- entry.writeUInt32LE(data.length, 20);
36
- entry.writeUInt32LE(data.length, 24);
37
- entry.writeUInt16LE(name.length, 28);
38
- entry.writeUInt16LE(0, 30); // extra field length
32
+ writeCommonEntryFields(entry, 6, crc, dataLength, name.length);
39
33
  entry.writeUInt16LE(0, 32); // comment length
40
34
  entry.writeUInt16LE(0, 34); // disk number start
41
35
  entry.writeUInt16LE(0, 36); // internal attributes
@@ -58,8 +52,9 @@ function endOfCentralDirectory(entryCount, cdSize, cdOffset) {
58
52
  export function kmzBytes(kml) {
59
53
  const name = Buffer.from(ENTRY_NAME, 'utf8');
60
54
  const data = Buffer.from(kml, 'utf8');
61
- const local = localHeader(name, data);
62
- const central = centralDirectoryEntry(name, data, 0);
55
+ const crc = crc32(data);
56
+ const local = localHeader(name, data, crc);
57
+ const central = centralDirectoryEntry(name, data.length, crc, 0);
63
58
  return Buffer.concat([
64
59
  local,
65
60
  central,
@@ -12,11 +12,7 @@ import { serializeKml } from './track-kml.js';
12
12
  import { kmzBytes } from './track-kmz.js';
13
13
  import { trackFormatOf } from './track-format.js';
14
14
  import { buildTrack, selectTrackRecords } from './track.js';
15
- export function trackFileBytes(file, input, range) {
16
- const format = trackFormatOf(file);
17
- if (format === 'json') {
18
- return serializeTelemetryDocument(buildTelemetryDocument(segmentTelemetryInput(input.document, range)));
19
- }
15
+ function serializeTrack(format, input, range) {
20
16
  const track = buildTrack(selectTrackRecords(input.track, range));
21
17
  switch (format) {
22
18
  case 'gpx':
@@ -29,6 +25,13 @@ export function trackFileBytes(file, input, range) {
29
25
  return kmzBytes(serializeKml(track));
30
26
  }
31
27
  }
28
+ export function trackFileBytes(file, input, range) {
29
+ const format = trackFormatOf(file);
30
+ if (format === 'json') {
31
+ return serializeTelemetryDocument(buildTelemetryDocument(segmentTelemetryInput(input.document, range)));
32
+ }
33
+ return serializeTrack(format, input, range);
34
+ }
32
35
  export function writeTrackFile(file, input, range) {
33
36
  const format = trackFormatOf(file);
34
37
  fs.mkdirSync(path.dirname(file), { recursive: true });
@@ -37,5 +40,5 @@ export function writeTrackFile(file, input, range) {
37
40
  writeTelemetryDocument(file, segmentTelemetryInput(input.document, range));
38
41
  return;
39
42
  }
40
- fs.writeFileSync(file, trackFileBytes(file, input, range));
43
+ fs.writeFileSync(file, serializeTrack(format, input, range));
41
44
  }
@@ -6,4 +6,6 @@ interface ShapeWording {
6
6
  }
7
7
  export declare function strictShape<T extends z.ZodRawShape>(shape: T, wording: ShapeWording): z.ZodObject<{ -readonly [P in keyof T]: T[P]; }, z.core.$strict>;
8
8
  export declare function numberValue(name: string): z.ZodType<number>;
9
+ export declare function isRecord(value: unknown): value is Record<string, unknown>;
10
+ export declare function asRecord(value: unknown): Record<string, unknown>;
9
11
  export {};
@@ -20,3 +20,9 @@ export function strictShape(shape, wording) {
20
20
  export function numberValue(name) {
21
21
  return z.number({ error: invalidNumber(name) });
22
22
  }
23
+ export function isRecord(value) {
24
+ return typeof value === 'object' && value !== null;
25
+ }
26
+ export function asRecord(value) {
27
+ return isRecord(value) ? (value) : {};
28
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drone-path-core",
3
- "version": "1.0.0-rc1",
3
+ "version": "1.0.0",
4
4
  "description": "Extract drone telemetry from DJI CAM-meta video streams and SRT sidecars.",
5
5
  "type": "module",
6
6
  "exports": {