@duoquote/polytini 0.1.0 → 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 (68) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +120 -120
  3. package/dist/types/cli.d.cts +2 -2
  4. package/dist/types/deckgl.d.cts +72 -60
  5. package/dist/types/flexible.d.cts +92 -80
  6. package/dist/types/geojson.jsr.d.cts +111 -0
  7. package/dist/types/google.d.cts +48 -48
  8. package/dist/types/google.jsr.d.cts +72 -60
  9. package/dist/types/index.d.cts +109 -87
  10. package/dist/types/leaflet-auto.d.cts +4 -4
  11. package/dist/types/leaflet-auto.jsr.d.cts +2 -2
  12. package/dist/types/leaflet.d.cts +39 -39
  13. package/dist/types/leaflet.jsr.d.cts +15 -13
  14. package/dist/types/mapbox.d.cts +50 -50
  15. package/dist/types/mapbox.jsr.d.cts +5 -0
  16. package/dist/types/maplibre.d.cts +54 -54
  17. package/dist/types/maplibre.jsr.d.cts +92 -0
  18. package/dist/types/openlayers.d.cts +23 -23
  19. package/dist/types/openlayers.jsr.d.cts +42 -0
  20. package/dist/types/presets.d.cts +70 -66
  21. package/dist/types/turf.d.cts +33 -33
  22. package/dist/types/turf.jsr.d.cts +46 -0
  23. package/dist/types/wasm.d.cts +42 -33
  24. package/package.json +200 -200
  25. package/src/browser.js +15 -15
  26. package/src/bun.js +4 -4
  27. package/src/cli-lib.js +160 -160
  28. package/src/cli.js +7 -7
  29. package/src/core.js +268 -268
  30. package/src/deckgl.js +176 -176
  31. package/src/deno.js +12 -12
  32. package/src/error.js +15 -15
  33. package/src/fast.js +179 -179
  34. package/src/flexible.js +283 -283
  35. package/src/google.js +70 -70
  36. package/src/leaflet-auto.js +11 -11
  37. package/src/leaflet.js +44 -44
  38. package/src/mapbox.js +10 -10
  39. package/src/maplibre.js +57 -57
  40. package/src/node.js +17 -17
  41. package/src/openlayers.js +172 -172
  42. package/src/presets.js +242 -242
  43. package/src/text.js +111 -111
  44. package/src/turf.js +80 -80
  45. package/src/wasm-bytes.js +2 -2
  46. package/src/wasm.js +235 -235
  47. package/src/web.js +9 -9
  48. package/types/cli.d.ts +2 -2
  49. package/types/deckgl.d.ts +72 -60
  50. package/types/flexible.d.ts +92 -80
  51. package/types/geojson.jsr.d.ts +111 -0
  52. package/types/google.d.ts +48 -48
  53. package/types/google.jsr.d.ts +72 -60
  54. package/types/index.d.ts +109 -87
  55. package/types/leaflet-auto.d.ts +4 -4
  56. package/types/leaflet-auto.jsr.d.ts +2 -2
  57. package/types/leaflet.d.ts +39 -39
  58. package/types/leaflet.jsr.d.ts +15 -13
  59. package/types/mapbox.d.ts +50 -50
  60. package/types/mapbox.jsr.d.ts +5 -0
  61. package/types/maplibre.d.ts +54 -54
  62. package/types/maplibre.jsr.d.ts +92 -0
  63. package/types/openlayers.d.ts +23 -23
  64. package/types/openlayers.jsr.d.ts +42 -0
  65. package/types/presets.d.ts +70 -66
  66. package/types/turf.d.ts +33 -33
  67. package/types/turf.jsr.d.ts +46 -0
  68. package/types/wasm.d.ts +42 -33
package/src/wasm.js CHANGED
@@ -1,235 +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);
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 CHANGED
@@ -1,9 +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);
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 CHANGED
@@ -1,2 +1,2 @@
1
- // The polytini command. Running it is the whole API.
2
- export {};
1
+ // The polytini command. Running it is the whole API.
2
+ export {};
package/types/deckgl.d.ts CHANGED
@@ -1,60 +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
- export interface BinaryOptions extends Options {
7
- /** Float32Array instead of Float64Array. Smaller, and enough for most maps; deck.gl uses 64-bit positions only when given them. Default false. */
8
- float32?: boolean;
9
- }
10
-
11
- export interface PathOptions extends BinaryOptions {
12
- /**
13
- * For TripsLayer: the last value of each point (with dimensions 3 or 4) goes
14
- * to attributes.getTimestamps instead of the positions. The values are
15
- * divided by zFactor, like any value after the second, and always come out
16
- * as a Float32Array, because TripsLayer's timestamp attribute is 32-bit
17
- * (deck.gl 9.4 drew nothing given Float64Array timestamps). Default false.
18
- */
19
- timestamps?: boolean;
20
- }
21
-
22
- /** A deck.gl binary attribute. */
23
- export interface BinaryAttribute<T extends Float32Array | Float64Array = Float32Array | Float64Array, S extends number = number> {
24
- value: T;
25
- size: S;
26
- }
27
-
28
- export interface BinaryPaths<T extends Float32Array | Float64Array = Float32Array | Float64Array> {
29
- /** The number of paths, one per string. */
30
- length: number;
31
- /** The first vertex of each path, plus the vertex count at the end (length + 1 entries). */
32
- startIndices: Uint32Array;
33
- attributes: {
34
- /** [lng, lat] (size 2) or [lng, lat, z] (size 3) per vertex. */
35
- getPath: BinaryAttribute<T, 2 | 3>;
36
- /** One value per vertex, with the timestamps option. Always 32-bit. */
37
- getTimestamps?: BinaryAttribute<Float32Array, 1>;
38
- };
39
- }
40
-
41
- export interface BinaryPolygons<T extends Float32Array | Float64Array = Float32Array | Float64Array> {
42
- /** The number of polygons, one ring per string. */
43
- length: number;
44
- /** The first vertex of each ring, plus the vertex count at the end (length + 1 entries). */
45
- startIndices: Uint32Array;
46
- attributes: {
47
- /** [lng, lat] (size 2) or [lng, lat, z] (size 3) per vertex, every ring closed. */
48
- getPolygon: BinaryAttribute<T, 2 | 3>;
49
- };
50
- }
51
-
52
- /** Binary data for PathLayer and TripsLayer, one path per string. Pass _pathType: 'open' to skip deck.gl's normalization. */
53
- export function binaryPaths(encoded: string | string[], options: PathOptions & { float32: true }): BinaryPaths<Float32Array>;
54
- export function binaryPaths(encoded: string | string[], options?: number | (PathOptions & { float32?: false })): BinaryPaths<Float64Array>;
55
- export function binaryPaths(encoded: string | string[], options?: number | PathOptions): BinaryPaths;
56
-
57
- /** Binary data for SolidPolygonLayer, one closed ring per string (no holes). */
58
- export function binaryPolygons(encoded: string | string[], options: BinaryOptions & { float32: true }): BinaryPolygons<Float32Array>;
59
- export function binaryPolygons(encoded: string | string[], options?: number | (BinaryOptions & { float32?: false })): BinaryPolygons<Float64Array>;
60
- export function binaryPolygons(encoded: string | string[], options?: number | BinaryOptions): BinaryPolygons;
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;