@duoquote/polytini 0.0.0-stage → 0.1.1

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 (80) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +119 -2
  3. package/dist/deckgl.cjs +250 -0
  4. package/dist/flexible.cjs +229 -0
  5. package/dist/google.cjs +232 -0
  6. package/dist/leaflet-auto.cjs +209 -0
  7. package/dist/leaflet.cjs +231 -0
  8. package/dist/mapbox.cjs +185 -0
  9. package/dist/maplibre.cjs +185 -0
  10. package/dist/node.cjs +322 -0
  11. package/dist/presets.cjs +65 -0
  12. package/dist/turf.cjs +267 -0
  13. package/dist/types/cli.d.cts +2 -0
  14. package/dist/types/deckgl.d.cts +72 -0
  15. package/dist/types/flexible.d.cts +92 -0
  16. package/dist/types/geojson.jsr.d.cts +111 -0
  17. package/dist/types/google.d.cts +48 -0
  18. package/dist/types/google.jsr.d.cts +72 -0
  19. package/dist/types/index.d.cts +109 -0
  20. package/dist/types/leaflet-auto.d.cts +4 -0
  21. package/dist/types/leaflet-auto.jsr.d.cts +2 -0
  22. package/dist/types/leaflet.d.cts +39 -0
  23. package/dist/types/leaflet.jsr.d.cts +15 -0
  24. package/dist/types/mapbox.d.cts +50 -0
  25. package/dist/types/mapbox.jsr.d.cts +5 -0
  26. package/dist/types/maplibre.d.cts +54 -0
  27. package/dist/types/maplibre.jsr.d.cts +92 -0
  28. package/dist/types/openlayers.d.cts +23 -0
  29. package/dist/types/openlayers.jsr.d.cts +42 -0
  30. package/dist/types/presets.d.cts +70 -0
  31. package/dist/types/turf.d.cts +33 -0
  32. package/dist/types/turf.jsr.d.cts +46 -0
  33. package/dist/types/wasm.d.cts +42 -0
  34. package/dist/web.cjs +231 -0
  35. package/package.json +198 -4
  36. package/src/browser.js +15 -0
  37. package/src/bun.js +4 -0
  38. package/src/cli-lib.js +160 -0
  39. package/src/cli.js +7 -0
  40. package/src/core.js +268 -0
  41. package/src/deckgl.js +176 -0
  42. package/src/deno.js +12 -0
  43. package/src/error.js +15 -0
  44. package/src/fast.js +179 -0
  45. package/src/flexible.js +283 -0
  46. package/src/google.js +70 -0
  47. package/src/leaflet-auto.js +11 -0
  48. package/src/leaflet.js +44 -0
  49. package/src/mapbox.js +10 -0
  50. package/src/maplibre.js +57 -0
  51. package/src/node.js +17 -0
  52. package/src/openlayers.js +172 -0
  53. package/src/presets.js +242 -0
  54. package/src/text.js +111 -0
  55. package/src/turf.js +80 -0
  56. package/src/wasm-bytes.js +2 -0
  57. package/src/wasm.js +235 -0
  58. package/src/web.js +9 -0
  59. package/types/cli.d.ts +2 -0
  60. package/types/deckgl.d.ts +72 -0
  61. package/types/flexible.d.ts +92 -0
  62. package/types/geojson.jsr.d.ts +111 -0
  63. package/types/google.d.ts +48 -0
  64. package/types/google.jsr.d.ts +72 -0
  65. package/types/index.d.ts +109 -0
  66. package/types/leaflet-auto.d.ts +4 -0
  67. package/types/leaflet-auto.jsr.d.ts +2 -0
  68. package/types/leaflet.d.ts +39 -0
  69. package/types/leaflet.jsr.d.ts +15 -0
  70. package/types/mapbox.d.ts +50 -0
  71. package/types/mapbox.jsr.d.ts +5 -0
  72. package/types/maplibre.d.ts +54 -0
  73. package/types/maplibre.jsr.d.ts +92 -0
  74. package/types/openlayers.d.ts +23 -0
  75. package/types/openlayers.jsr.d.ts +42 -0
  76. package/types/presets.d.ts +70 -0
  77. package/types/turf.d.ts +33 -0
  78. package/types/turf.jsr.d.ts +46 -0
  79. package/types/wasm.d.ts +42 -0
  80. package/wasm/polytini.wasm +0 -0
package/src/wasm.js ADDED
@@ -0,0 +1,235 @@
1
+ // Opt-in WebAssembly build. Call `await init()` once (or `initSync()`), then
2
+ // the functions are synchronous and take the same arguments as the main
3
+ // entry, plus two batch functions for many lines in one call.
4
+ //
5
+ // The module (wasm/polytini.wat, under 1 KB) does the parsing and
6
+ // formatting. The glue keeps one instance and one memory for the whole
7
+ // program, grows it when an input doesn't fit, and moves data with
8
+ // TextEncoder.encodeInto and Buffer#latin1Slice (or TextDecoder). Anything the module doesn't handle
9
+ // (objects, other dimensions, bad input) goes to the JS core, so results and
10
+ // errors are the same as the main entry's.
11
+ /* @ts-self-types="../types/wasm.d.ts" */
12
+ import bytes from './wasm-bytes.js';
13
+ import { opts, st, slowDecode, slowEncode, fail, latLngs, toGeo, fromGeo, decode as jsDecode, decodeFlat as jsFlat, encode as jsEncode, encodeFlat as jsEncodeFlat } from './core.js';
14
+
15
+ export { PolylineError } from './core.js';
16
+
17
+ // The module keeps nothing in its memory, so the glue uses all of it.
18
+ const base = 0;
19
+ let ex, mem, u8, f64, u32, input, size = 0;
20
+ // Deno has a global Buffer too, but its latin1Slice is a JS polyfill that
21
+ // made encoding 2 times slower there than TextDecoder.
22
+ const latin1Slice = !globalThis.Deno && globalThis.Buffer?.prototype.latin1Slice;
23
+ const te = new TextEncoder(), td = new TextDecoder('latin1');
24
+
25
+ const bin = () => Uint8Array.from(atob(bytes), (c) => c.charCodeAt(0));
26
+ const ready = (instance) => {
27
+ ex = instance.exports;
28
+ mem = ex.memory;
29
+ views();
30
+ };
31
+ const views = () => {
32
+ u8 = new Uint8Array(mem.buffer);
33
+ f64 = new Float64Array(mem.buffer);
34
+ u32 = new Uint32Array(mem.buffer);
35
+ size = u8.length;
36
+ // encodeInto writes at the start of its target, so keep one view at base.
37
+ input = u8.subarray(base);
38
+ };
39
+
40
+ /** Compiles and instantiates the module. Safe to call more than once. */
41
+ export async function init() {
42
+ if (!ex) ready((await WebAssembly.instantiate(bin())).instance);
43
+ }
44
+
45
+ /** Synchronous init. Browsers allow it here because the module is small. */
46
+ export function initSync() {
47
+ if (!ex) ready(new WebAssembly.Instance(new WebAssembly.Module(bin())));
48
+ }
49
+
50
+ // Makes sure `n` bytes past base exist. The memory only grows, so a big
51
+ // line costs its memory once, not on every call.
52
+ function room(n) {
53
+ if (!ex) fail('init', 'Call init() before using polytini/wasm');
54
+ const need = base + n - size;
55
+ if (need > 0) {
56
+ mem.grow(Math.ceil(need / 65536));
57
+ views();
58
+ }
59
+ }
60
+
61
+ const align = (n) => (n + 15) & ~7;
62
+ const text = (a, b) => (latin1Slice ? latin1Slice.call(u8, a, b) : td.decode(u8.subarray(a, b)));
63
+
64
+ // Copies an ASCII string to base. False when it isn't ASCII, which the JS
65
+ // core then reports.
66
+ const put = (str, len) => te.encodeInto(str, input).written == len;
67
+
68
+ // Decodes str to f64 pairs in memory. Returns [offset in f64s, count], or
69
+ // null when the JS core has to handle it.
70
+ function run(str, f) {
71
+ const len = str.length, out = base + align(len);
72
+ room(align(len) + len * 8 + 16);
73
+ if (!put(str, len)) return null;
74
+ const k = ex.decode(base, len, out, f, +st.swap);
75
+ return k < 0 ? null : [out >> 3, k];
76
+ }
77
+
78
+ // Lines this short are faster in JS on every runtime measured: the call and
79
+ // moving the data in and out cost more than the work. About 30 points.
80
+ const SHORT = 128;
81
+
82
+ export function decodeFlat(str, o) {
83
+ if (typeof str == 'string' && str.length < SHORT) return jsFlat(str, o);
84
+ const f = opts(o);
85
+ if (typeof str != 'string') fail('input', 'Expected a string');
86
+ const r = st.dims == 2 && run(str, f);
87
+ return r ? f64.slice(r[0], r[0] + r[1]) : slowDecode(str, f, 1);
88
+ }
89
+
90
+ // [lat, lng] tuples come from the JS decoder. Building one array per point
91
+ // is most of the work, and reading the numbers back out of WASM memory only
92
+ // added to it: the WASM version was 15 to 40% slower at every size.
93
+ export const decode = (str, o) => jsDecode(str, o);
94
+
95
+ // f64 values at base to a string.
96
+ function finish(values, f) {
97
+ const out = base + values * 8;
98
+ const k = ex.encode(base, values, f, out);
99
+ return k < 0 ? null : text(out, out + k);
100
+ }
101
+
102
+ export function encode(points, o) {
103
+ if (points?.length < SHORT / 4) return jsEncode(points, o);
104
+ const f = opts(o);
105
+ if (st.dims != 2 || !Array.isArray(points)) return slowEncode(points, f);
106
+ const n = points.length, a = +st.swap, b = 1 - a;
107
+ room(n * 16 + n * 14 + 16);
108
+ const m = f64;
109
+ try {
110
+ for (let i = 0, j = 0; i < n; i++, j += 2) {
111
+ const p = points[i], x = p[a], y = p[b];
112
+ // A Float64Array store would turn null into 0 and '1' into 1.
113
+ if (typeof x != 'number' || typeof y != 'number') return slowEncode(points, f);
114
+ m[j] = x;
115
+ m[j + 1] = y;
116
+ }
117
+ } catch {
118
+ // A null or missing point.
119
+ return slowEncode(points, f);
120
+ }
121
+ return finish(n * 2, f) ?? slowEncode(points, f);
122
+ }
123
+
124
+ export function encodeFlat(values, o) {
125
+ if (values?.length < SHORT / 2) return jsEncodeFlat(values, o);
126
+ const f = opts(o);
127
+ if (st.dims != 2 || values == null || values.length % 2) return slowEncode(values, f, 1);
128
+ const n = values.length;
129
+ room(n * 8 + n * 7 + 16);
130
+ if (!(values instanceof Float64Array || values instanceof Float32Array)) {
131
+ for (let i = 0; i < n; i++) if (typeof values[i] != 'number') return slowEncode(values, f, 1);
132
+ }
133
+ const at = base >> 3;
134
+ if (st.swap) for (let i = 0; i < n; i += 2) (f64[at + i] = values[i + 1]), (f64[at + i + 1] = values[i]);
135
+ else f64.set(values, at);
136
+ return finish(n, f) ?? slowEncode(values, f, 1);
137
+ }
138
+
139
+ /**
140
+ * Decodes many lines in one call into one Float64Array of lat/lng pairs,
141
+ * plus `starts`: the first point of each line, and the total point count
142
+ * at the end, as deck.gl's binary paths expect.
143
+ */
144
+ export function decodeMany(lines, o) {
145
+ const f = opts(o);
146
+ if (!Array.isArray(lines)) fail('input', 'Expected an array of strings');
147
+ const count = lines.length;
148
+ let len = 0;
149
+ for (let i = 0; i < count; i++) {
150
+ if (typeof lines[i] != 'string') return many(lines, o);
151
+ len += lines[i].length;
152
+ }
153
+ if (st.dims != 2) return many(lines, o);
154
+ const lens = base + align(len), out = lens + align(count * 4), starts = out + len * 8 + 8;
155
+ room(starts - base + count * 4 + 16);
156
+ if (!put(lines.join(''), len)) return many(lines, o);
157
+ const L = u32, l0 = lens >> 2;
158
+ for (let i = 0; i < count; i++) L[l0 + i] = lines[i].length;
159
+ const k = ex.decode_many(base, lens, count, out, starts, f, +st.swap);
160
+ if (k < 0) return many(lines, o);
161
+ return { coords: f64.slice(out >> 3, (out >> 3) + k), starts: u32.slice(starts >> 2, (starts >> 2) + count + 1) };
162
+ }
163
+
164
+ // The JS path for decodeMany, for bad lines (strict mode throws on the
165
+ // first one) and anything the module doesn't take.
166
+ function many(lines, o) {
167
+ const parts = lines.map((s) => jsFlat(s, o)), starts = new Uint32Array(parts.length + 1);
168
+ let n = 0;
169
+ parts.forEach((p, i) => (starts[i + 1] = n += p.length / st.dims));
170
+ const coords = new Float64Array(n * st.dims);
171
+ let at = 0;
172
+ for (const p of parts) coords.set(p, (at += p.length) - p.length);
173
+ return { coords, starts };
174
+ }
175
+
176
+ /**
177
+ * Encodes many lines in one call. Takes an array of point arrays, or the
178
+ * { coords, starts } shape decodeMany returns. Returns one string per line.
179
+ */
180
+ export function encodeMany(lines, o) {
181
+ const f = opts(o);
182
+ if (st.dims != 2) return toLines(lines).map((l) => slowEncode(l, f, ArrayBuffer.isView(l) ? 1 : 0));
183
+ let coords, starts, count;
184
+ if (Array.isArray(lines)) {
185
+ count = lines.length;
186
+ let n = 0;
187
+ for (const l of lines) {
188
+ if (!Array.isArray(l)) return lines.map((x) => jsEncode(x, o));
189
+ n += l.length;
190
+ }
191
+ room(n * 16 + count * 8 + n * 14 + 64);
192
+ const a = +st.swap, b = 1 - a, m = f64, S = u32, s0 = (base + n * 16) >> 2;
193
+ let j = base >> 3, k = 0;
194
+ S[s0] = 0;
195
+ try {
196
+ for (let i = 0; i < count; i++) {
197
+ const l = lines[i], len = l.length;
198
+ for (let q = 0; q < len; q++) {
199
+ const p = l[q], x = p[a], y = p[b];
200
+ if (typeof x != 'number' || typeof y != 'number') return lines.map((x) => jsEncode(x, o));
201
+ m[j++] = x;
202
+ m[j++] = y;
203
+ }
204
+ S[s0 + i + 1] = k += len;
205
+ }
206
+ } catch {
207
+ return lines.map((x) => jsEncode(x, o));
208
+ }
209
+ return manyOut(base + n * 16, count, n, f) ?? lines.map((x) => jsEncode(x, o));
210
+ }
211
+ ({ coords, starts } = lines ?? {});
212
+ if (!coords || !starts) fail('input', 'Expected an array of lines or { coords, starts }');
213
+ count = starts.length - 1;
214
+ const n = coords.length >> 1;
215
+ room(n * 16 + count * 8 + n * 14 + 64);
216
+ const at = base >> 3;
217
+ if (st.swap) for (let i = 0; i < n * 2; i += 2) (f64[at + i] = coords[i + 1]), (f64[at + i + 1] = coords[i]);
218
+ else f64.set(coords, at);
219
+ u32.set(starts, (base + n * 16) >> 2);
220
+ return manyOut(base + n * 16, count, n, f) ?? toLines(lines).map((l) => jsEncodeFlat(l, o));
221
+ }
222
+ const toLines = (l) => (Array.isArray(l) ? l : Array.from({ length: l.starts.length - 1 }, (_, i) => l.coords.subarray(l.starts[i] * 2, l.starts[i + 1] * 2)));
223
+
224
+ function manyOut(startsAt, count, n, f) {
225
+ const ends = startsAt + align((count + 1) * 4), out = ends + align(count * 4);
226
+ const k = ex.encode_many(base, startsAt, count, f, out, ends);
227
+ if (k < 0) return null;
228
+ const r = Array(count), e = u32, e0 = ends >> 2;
229
+ for (let i = 0, a = out; i < count; i++) r[i] = text(a, (a = out + e[e0 + i]));
230
+ return r;
231
+ }
232
+
233
+ export const decodeLatLng = /* @__PURE__ */ latLngs(decode);
234
+ export const toGeoJSON = /* @__PURE__ */ toGeo(decode);
235
+ export const fromGeoJSON = /* @__PURE__ */ fromGeo(encode);
package/src/web.js ADDED
@@ -0,0 +1,9 @@
1
+ // Default and browser entry: the portable core and nothing else, so it has
2
+ // no Node, Bun or Deno globals and bundles small.
3
+ /* @ts-self-types="../types/index.d.ts" */
4
+ import { decode, encode, latLngs, toGeo, fromGeo } from './core.js';
5
+
6
+ export { decode, encode, decodeFlat, encodeFlat, PolylineError } from './core.js';
7
+ export const decodeLatLng = /* @__PURE__ */ latLngs(decode);
8
+ export const toGeoJSON = /* @__PURE__ */ toGeo(decode);
9
+ export const fromGeoJSON = /* @__PURE__ */ fromGeo(encode);
package/types/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ // The polytini command. Running it is the whole API.
2
+ export {};
@@ -0,0 +1,72 @@
1
+ // Types for @duoquote/polytini/deckgl. The results are plain objects in
2
+ // deck.gl's binary data shape, so they need no deck.gl types: they can be
3
+ // passed as `data` to PathLayer, TripsLayer and SolidPolygonLayer.
4
+ import type { Options } from './index.d.ts';
5
+
6
+ /** Options for binaryPolygons: polytini's options plus float32. */
7
+ export interface BinaryOptions extends Options {
8
+ /** Float32Array instead of Float64Array. Smaller, and enough for most maps; deck.gl uses 64-bit positions only when given them. Default false. */
9
+ float32?: boolean;
10
+ }
11
+
12
+ /** Options for binaryPaths: BinaryOptions plus timestamps. */
13
+ export interface PathOptions extends BinaryOptions {
14
+ /**
15
+ * For TripsLayer: the last value of each point (with dimensions 3 or 4) goes
16
+ * to attributes.getTimestamps instead of the positions. The values are
17
+ * divided by zFactor, like any value after the second, and always come out
18
+ * as a Float32Array, because TripsLayer's timestamp attribute is 32-bit
19
+ * (deck.gl 9.4 drew nothing given Float64Array timestamps). Default false.
20
+ */
21
+ timestamps?: boolean;
22
+ }
23
+
24
+ /** A deck.gl binary attribute. */
25
+ export interface BinaryAttribute<T extends Float32Array | Float64Array = Float32Array | Float64Array, S extends number = number> {
26
+ /** The values, size per vertex. */
27
+ value: T;
28
+ /** Values per vertex. */
29
+ size: S;
30
+ }
31
+
32
+ /** What binaryPaths returns: `data` for PathLayer or TripsLayer. */
33
+ export interface BinaryPaths<T extends Float32Array | Float64Array = Float32Array | Float64Array> {
34
+ /** The number of paths, one per string. */
35
+ length: number;
36
+ /** The first vertex of each path, plus the vertex count at the end (length + 1 entries). */
37
+ startIndices: Uint32Array;
38
+ /** The vertex attributes. */
39
+ attributes: {
40
+ /** [lng, lat] (size 2) or [lng, lat, z] (size 3) per vertex. */
41
+ getPath: BinaryAttribute<T, 2 | 3>;
42
+ /** One value per vertex, with the timestamps option. Always 32-bit. */
43
+ getTimestamps?: BinaryAttribute<Float32Array, 1>;
44
+ };
45
+ }
46
+
47
+ /** What binaryPolygons returns: `data` for SolidPolygonLayer. */
48
+ export interface BinaryPolygons<T extends Float32Array | Float64Array = Float32Array | Float64Array> {
49
+ /** The number of polygons, one ring per string. */
50
+ length: number;
51
+ /** The first vertex of each ring, plus the vertex count at the end (length + 1 entries). */
52
+ startIndices: Uint32Array;
53
+ /** The vertex attributes. */
54
+ attributes: {
55
+ /** [lng, lat] (size 2) or [lng, lat, z] (size 3) per vertex, every ring closed. */
56
+ getPolygon: BinaryAttribute<T, 2 | 3>;
57
+ };
58
+ }
59
+
60
+ /** Binary data for PathLayer and TripsLayer, one path per string. Pass _pathType: 'open' to skip deck.gl's normalization. */
61
+ export function binaryPaths(encoded: string | string[], options: PathOptions & { float32: true }): BinaryPaths<Float32Array>;
62
+ /** binaryPaths with Float64Array positions, the default. */
63
+ export function binaryPaths(encoded: string | string[], options?: number | (PathOptions & { float32?: false })): BinaryPaths<Float64Array>;
64
+ /** binaryPaths when float32 isn't known until runtime. */
65
+ export function binaryPaths(encoded: string | string[], options?: number | PathOptions): BinaryPaths;
66
+
67
+ /** Binary data for SolidPolygonLayer, one closed ring per string (no holes). */
68
+ export function binaryPolygons(encoded: string | string[], options: BinaryOptions & { float32: true }): BinaryPolygons<Float32Array>;
69
+ /** binaryPolygons with Float64Array positions, the default. */
70
+ export function binaryPolygons(encoded: string | string[], options?: number | (BinaryOptions & { float32?: false })): BinaryPolygons<Float64Array>;
71
+ /** binaryPolygons when float32 isn't known until runtime. */
72
+ export function binaryPolygons(encoded: string | string[], options?: number | BinaryOptions): BinaryPolygons;
@@ -0,0 +1,92 @@
1
+ // Types for @duoquote/polytini/flexible, HERE's Flexible Polyline format
2
+ // (version 1). Separate from the Google-format entry points.
3
+ import type { ErrorCode } from './index.d.ts';
4
+
5
+ /** Third dimension types, as stored in the header. */
6
+ /** No third value: a 2D line. */
7
+ export const ABSENT: 0;
8
+ /** The third value is a level, such as a floor. */
9
+ export const LEVEL: 1;
10
+ /** The third value is altitude. */
11
+ export const ALTITUDE: 2;
12
+ /** The third value is elevation. */
13
+ export const ELEVATION: 3;
14
+ /** Reserved by the spec: decode accepts it, encode refuses it. */
15
+ export const RESERVED1: 4;
16
+ /** Reserved by the spec: decode accepts it, encode refuses it. */
17
+ export const RESERVED2: 5;
18
+ /** A third value of your own meaning. */
19
+ export const CUSTOM1: 6;
20
+ /** A second type for a third value of your own meaning. */
21
+ export const CUSTOM2: 7;
22
+
23
+ /** Any type a header can hold. */
24
+ export type ThirdDimension = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
25
+ /** The types encode accepts. */
26
+ export type EncodableThirdDimension = 0 | 1 | 2 | 3 | 6 | 7;
27
+
28
+ /** The header fields of an encoded line. */
29
+ export interface Header {
30
+ /** Decimal places of latitude and longitude, 0 to 15. */
31
+ precision: number;
32
+ /** What the third value means. ABSENT (0) for 2D lines. */
33
+ thirdDim: ThirdDimension;
34
+ /** Decimal places of the third value, 0 to 15. */
35
+ thirdDimPrecision: number;
36
+ }
37
+
38
+ /** What decode returns: the header and the points. */
39
+ export interface DecodedResult extends Header {
40
+ /** [lat, lng] points, or [lat, lng, z] when thirdDim is not ABSENT. */
41
+ polyline: number[][];
42
+ }
43
+
44
+ /** The header fields encode writes. */
45
+ export interface EncodeOptions {
46
+ /** Decimal places of latitude and longitude, an integer from 0 to 15. Default 5. */
47
+ precision?: number;
48
+ /** Type of the third value. Default ABSENT, which ignores any third value. */
49
+ thirdDim?: EncodableThirdDimension;
50
+ /** Decimal places of the third value, an integer from 0 to 15. Default 0. */
51
+ thirdDimPrecision?: number;
52
+ }
53
+
54
+ /** [lat, lng] or [lat, lng, z] points, or one flat array of numbers: [lat0, lng0, lat1, lng1, ...]. */
55
+ export type Points = ArrayLike<ArrayLike<number>> | ArrayLike<number>;
56
+
57
+ /** The argument of @here/flexpolyline's encode. */
58
+ export interface EncodeInput extends EncodeOptions {
59
+ /** The points. */
60
+ polyline: Points;
61
+ }
62
+
63
+ /**
64
+ * Codes of a PolylineError from this entry: the main entry's, plus 'version'
65
+ * for a format version other than 1 or a header with bits set above bit 10.
66
+ */
67
+ export type FlexibleErrorCode = Exclude<ErrorCode, 'init'> | 'version';
68
+
69
+ /** The same class as the main entry's PolylineError. */
70
+ export class PolylineError extends Error {
71
+ /** Always "PolylineError". */
72
+ readonly name: 'PolylineError';
73
+ /** What went wrong. */
74
+ readonly code: FlexibleErrorCode;
75
+ /** Character index (decode) or point index (encode), when there is one. */
76
+ readonly index: number | undefined;
77
+ /** Makes an error; the message gets " at <index>" appended when there's an index. */
78
+ constructor(code: FlexibleErrorCode, message: string, index?: number);
79
+ }
80
+
81
+ /**
82
+ * Encodes a line. Takes @here/flexpolyline's argument, or the points with
83
+ * the header fields in `options`, which win over the input object's.
84
+ * Rounds half away from zero.
85
+ */
86
+ export function encode(input: EncodeInput | Points, options?: EncodeOptions): string;
87
+
88
+ /** Decodes a line, with its header. The same result shape as @here/flexpolyline. */
89
+ export function decode(str: string): DecodedResult;
90
+
91
+ /** Reads only the header. */
92
+ export function header(str: string): Header;
@@ -0,0 +1,111 @@
1
+ // The GeoJSON types the JSR type files need, written out here because JSR
2
+ // builds its docs without node_modules, so `geojson` (@types/geojson) can't
3
+ // be imported. The npm type files use @types/geojson itself. These follow
4
+ // RFC 7946 and have the same shape as @types/geojson's, so values flow
5
+ // between the two.
6
+
7
+ /** [longitude, latitude], then an optional altitude and anything else. */
8
+ export type Position = number[];
9
+
10
+ /** [west, south, east, north], or with altitudes [west, south, low, east, north, high]. */
11
+ export type BBox = [number, number, number, number] | [number, number, number, number, number, number];
12
+
13
+ /** The properties of a Feature: any JSON object, or null. */
14
+ export type GeoJsonProperties = { [name: string]: unknown } | null;
15
+
16
+ /** A GeoJSON Point. */
17
+ export interface Point {
18
+ /** Always "Point". */
19
+ type: 'Point';
20
+ /** The position. */
21
+ coordinates: Position;
22
+ /** Optional bounding box. */
23
+ bbox?: BBox;
24
+ }
25
+
26
+ /** A GeoJSON MultiPoint. */
27
+ export interface MultiPoint {
28
+ /** Always "MultiPoint". */
29
+ type: 'MultiPoint';
30
+ /** The positions. */
31
+ coordinates: Position[];
32
+ /** Optional bounding box. */
33
+ bbox?: BBox;
34
+ }
35
+
36
+ /** A GeoJSON LineString: two or more positions. */
37
+ export interface LineString {
38
+ /** Always "LineString". */
39
+ type: 'LineString';
40
+ /** The positions, [lng, lat]. */
41
+ coordinates: Position[];
42
+ /** Optional bounding box. */
43
+ bbox?: BBox;
44
+ }
45
+
46
+ /** A GeoJSON MultiLineString. */
47
+ export interface MultiLineString {
48
+ /** Always "MultiLineString". */
49
+ type: 'MultiLineString';
50
+ /** One position array per line. */
51
+ coordinates: Position[][];
52
+ /** Optional bounding box. */
53
+ bbox?: BBox;
54
+ }
55
+
56
+ /** A GeoJSON Polygon: the outer ring, then holes. */
57
+ export interface Polygon {
58
+ /** Always "Polygon". */
59
+ type: 'Polygon';
60
+ /** One closed position array per ring. */
61
+ coordinates: Position[][];
62
+ /** Optional bounding box. */
63
+ bbox?: BBox;
64
+ }
65
+
66
+ /** A GeoJSON MultiPolygon. */
67
+ export interface MultiPolygon {
68
+ /** Always "MultiPolygon". */
69
+ type: 'MultiPolygon';
70
+ /** One ring array per polygon. */
71
+ coordinates: Position[][][];
72
+ /** Optional bounding box. */
73
+ bbox?: BBox;
74
+ }
75
+
76
+ /** A GeoJSON GeometryCollection. */
77
+ export interface GeometryCollection {
78
+ /** Always "GeometryCollection". */
79
+ type: 'GeometryCollection';
80
+ /** The geometries. */
81
+ geometries: Geometry[];
82
+ /** Optional bounding box. */
83
+ bbox?: BBox;
84
+ }
85
+
86
+ /** Any GeoJSON geometry. */
87
+ export type Geometry = Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon | GeometryCollection;
88
+
89
+ /** A GeoJSON Feature. */
90
+ export interface Feature<G extends Geometry | null = Geometry, P = GeoJsonProperties> {
91
+ /** Always "Feature". */
92
+ type: 'Feature';
93
+ /** The geometry, or null. */
94
+ geometry: G;
95
+ /** The properties. */
96
+ properties: P;
97
+ /** Optional id. */
98
+ id?: string | number;
99
+ /** Optional bounding box. */
100
+ bbox?: BBox;
101
+ }
102
+
103
+ /** A GeoJSON FeatureCollection. */
104
+ export interface FeatureCollection<G extends Geometry | null = Geometry, P = GeoJsonProperties> {
105
+ /** Always "FeatureCollection". */
106
+ type: 'FeatureCollection';
107
+ /** The features. */
108
+ features: Feature<G, P>[];
109
+ /** Optional bounding box. */
110
+ bbox?: BBox;
111
+ }
@@ -0,0 +1,48 @@
1
+ // Types for @duoquote/polytini/google, using the google.maps types from
2
+ // @types/google.maps. Those are global declarations, not a module, so they
3
+ // are pulled in with a reference directive: types only, nothing at runtime.
4
+ /// <reference types="google.maps" />
5
+ import type * as P from './index.d.ts';
6
+ import type { Options, Point } from './index.d.ts';
7
+
8
+ export declare const PolylineError: typeof P.PolylineError;
9
+ export type PolylineError = P.PolylineError;
10
+
11
+ /** A LatLng constructor: google.maps.LatLng or anything built the same way. */
12
+ export type LatLngConstructor = new (lat: number, lng: number, noClampNoWrap?: boolean) => google.maps.LatLng;
13
+
14
+ export interface DecodePathOptions extends Omit<Options, 'order'> {
15
+ /**
16
+ * The class to build points with. Default: globalThis.google.maps.LatLng
17
+ * when it exists. false or null returns LatLngLiteral objects.
18
+ */
19
+ LatLng?: LatLngConstructor | false | null;
20
+ /** Passed to the LatLng constructor. Without it, LatLng clamps latitude and wraps longitude into [-180, 180). */
21
+ noClampNoWrap?: boolean;
22
+ }
23
+
24
+ /** A point encodePath takes: google.maps.LatLng, LatLngLiteral or a [lat, lng] tuple. */
25
+ export type PathPoint = google.maps.LatLng | google.maps.LatLngLiteral | Point;
26
+
27
+ /**
28
+ * Decodes an encoded path to LatLngLiteral objects (`LatLng: false`).
29
+ */
30
+ export function decodePath(encodedPath: string, options: DecodePathOptions & { LatLng: false | null }): google.maps.LatLngLiteral[];
31
+ /**
32
+ * Decodes an encoded path, like google.maps.geometry.encoding.decodePath,
33
+ * to google.maps.LatLng objects. Without a `LatLng` option this uses
34
+ * globalThis.google.maps.LatLng; when the Maps JavaScript API isn't loaded
35
+ * the result is LatLngLiteral objects at runtime, so pass `LatLng: false`
36
+ * there to get that type. Strict by default: a malformed string throws a
37
+ * PolylineError unless `strict: false`.
38
+ */
39
+ export function decodePath(encodedPath: string, options?: number | (DecodePathOptions & { LatLng?: LatLngConstructor })): google.maps.LatLng[];
40
+ export function decodePath(encodedPath: string, options?: number | DecodePathOptions): google.maps.LatLng[] | google.maps.LatLngLiteral[];
41
+
42
+ /**
43
+ * Encodes a path, like google.maps.geometry.encoding.encodePath: an array or
44
+ * MVCArray of google.maps.LatLng, LatLngLiteral or [lat, lng] tuples, mixed
45
+ * or not. Precision 5 by default. `order: 'lnglat'` applies to tuples and
46
+ * LatLng objects; literals are read by name. Strict by default.
47
+ */
48
+ export function encodePath(path: readonly PathPoint[] | google.maps.MVCArray<PathPoint>, options?: number | Options): string;
@@ -0,0 +1,72 @@
1
+ // JSR copy of google.d.ts. JSR's type output can't carry the
2
+ // @types/google.maps reference, so the Maps shapes are described
3
+ // structurally here: anything with lat() and lng() is a LatLng. The npm
4
+ // package's google.d.ts uses the real google.maps types.
5
+ import type * as P from './index.d.ts';
6
+ import type { Options, Point } from './index.d.ts';
7
+
8
+ /** Anything with lat() and lng(), such as google.maps.LatLng. */
9
+ export interface LatLng {
10
+ /** Latitude in degrees. */
11
+ lat(): number;
12
+ /** Longitude in degrees. */
13
+ lng(): number;
14
+ }
15
+ /** A plain { lat, lng } object, like google.maps.LatLngLiteral. */
16
+ export interface LatLngLiteral {
17
+ /** Latitude in degrees. */
18
+ lat: number;
19
+ /** Longitude in degrees. */
20
+ lng: number;
21
+ }
22
+ /** Anything with getArray(), such as google.maps.MVCArray. */
23
+ export interface MVCArrayLike<T> {
24
+ /** The items as an array. */
25
+ getArray(): T[];
26
+ }
27
+
28
+ /** The main entry's PolylineError. */
29
+ export declare const PolylineError: typeof P.PolylineError;
30
+ /** The main entry's PolylineError type. */
31
+ export type PolylineError = P.PolylineError;
32
+
33
+ /** A LatLng constructor: LatLng or anything built the same way. */
34
+ export type LatLngConstructor = new (lat: number, lng: number, noClampNoWrap?: boolean) => LatLng;
35
+
36
+ /** Options for decodePath: polytini's options without order, plus the LatLng class. */
37
+ export interface DecodePathOptions extends Omit<Options, 'order'> {
38
+ /**
39
+ * The class to build points with. Default: globalThis.LatLng
40
+ * when it exists. false or null returns LatLngLiteral objects.
41
+ */
42
+ LatLng?: LatLngConstructor | false | null;
43
+ /** Passed to the LatLng constructor. Without it, LatLng clamps latitude and wraps longitude into [-180, 180). */
44
+ noClampNoWrap?: boolean;
45
+ }
46
+
47
+ /** A point encodePath takes: LatLng, LatLngLiteral or a [lat, lng] tuple. */
48
+ export type PathPoint = LatLng | LatLngLiteral | Point;
49
+
50
+ /**
51
+ * Decodes an encoded path to LatLngLiteral objects (`LatLng: false`).
52
+ */
53
+ export function decodePath(encodedPath: string, options: DecodePathOptions & { LatLng: false | null }): LatLngLiteral[];
54
+ /**
55
+ * Decodes an encoded path, like google.maps.geometry.encoding.decodePath,
56
+ * to LatLng objects. Without a `LatLng` option this uses
57
+ * globalThis.LatLng; when the Maps JavaScript API isn't loaded
58
+ * the result is LatLngLiteral objects at runtime, so pass `LatLng: false`
59
+ * there to get that type. Strict by default: a malformed string throws a
60
+ * PolylineError unless `strict: false`.
61
+ */
62
+ export function decodePath(encodedPath: string, options?: number | (DecodePathOptions & { LatLng?: LatLngConstructor })): LatLng[];
63
+ /** decodePath when the LatLng option isn't known until runtime. */
64
+ export function decodePath(encodedPath: string, options?: number | DecodePathOptions): LatLng[] | LatLngLiteral[];
65
+
66
+ /**
67
+ * Encodes a path, like google.maps.geometry.encoding.encodePath: an array or
68
+ * MVCArray of LatLng, LatLngLiteral or [lat, lng] tuples, mixed
69
+ * or not. Precision 5 by default. `order: 'lnglat'` applies to tuples and
70
+ * LatLng objects; literals are read by name. Strict by default.
71
+ */
72
+ export function encodePath(path: readonly PathPoint[] | MVCArrayLike<PathPoint>, options?: number | Options): string;