@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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Güven Değirmenci
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,120 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Duoquote/polytini/main/assets/polytini-dark.png">
4
+ <img src="https://raw.githubusercontent.com/Duoquote/polytini/main/assets/polytini-light.png" width="160" alt="polytini logo: a small bird sitting on a line">
5
+ </picture>
6
+ </p>
2
7
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
8
+ # polytini
9
+
10
+ <p align="center">
11
+ <a href="https://www.npmjs.com/package/@duoquote/polytini"><img alt="npm" src="https://img.shields.io/npm/v/@duoquote/polytini?style=flat"></a>
12
+ <a href="https://jsr.io/@duoquote/polytini"><img alt="JSR" src="https://img.shields.io/jsr/v/@duoquote/polytini?style=flat"></a>
13
+ <a href="https://github.com/Duoquote/polytini/actions/workflows/ci.yml"><img alt="CI" src="https://img.shields.io/github/actions/workflow/status/Duoquote/polytini/ci.yml?branch=main&style=flat&label=CI"></a>
14
+ <a href="docs/platforms.md#size"><img alt="gzipped size of the default entry" src="https://raw.githubusercontent.com/Duoquote/polytini/main/assets/badges/size.svg"></a>
15
+ <a href="scripts/check-deps.js"><img alt="dependencies: 0" src="https://img.shields.io/badge/dependencies-0-brightgreen?style=flat"></a>
16
+ <a href="LICENSE"><img alt="license: MIT" src="https://img.shields.io/github/license/Duoquote/polytini?style=flat"></a>
17
+ </p>
18
+
19
+ polytini encodes and decodes [Google encoded polylines](https://developers.google.com/maps/documentation/utilities/polylinealgorithm), the strings Google Maps, OSRM, Valhalla, GraphHopper and Mapbox return for route geometry. It's strict by default, so a cut-off or corrupted string throws instead of drawing a wrong line. Node, Bun, Deno and browsers each get the code path that measured fastest there, and it has no dependencies. I wrote it while rebuilding [polyline-benchmark](https://github.com/Duoquote/polyline-benchmark), where it's compared with every other JavaScript polyline library.
20
+
21
+ ## Install
22
+
23
+ ```sh
24
+ npm install @duoquote/polytini # or pnpm add, yarn add, bun add
25
+ deno add jsr:@duoquote/polytini
26
+ ```
27
+
28
+ ESM and CommonJS, with TypeScript types. Node 22 or newer. Not on npm or JSR yet; [RELEASING.md](RELEASING.md) covers the first publish.
29
+
30
+ ## Usage
31
+
32
+ ```js
33
+ import { encode, decode } from '@duoquote/polytini';
34
+
35
+ encode([[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]); // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
36
+ decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@'); // [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]
37
+ decode(osrmGeometry, { precision: 6, order: 'lnglat' }); // OSRM polyline6, as [lng, lat]
38
+ ```
39
+
40
+ <picture>
41
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-decode-dark.svg">
42
+ <img alt="Bun: decode speed of every polyline library relative to @mapbox/polyline on real-world lines; polytini is highlighted" src="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-decode-light.svg">
43
+ </picture>
44
+ <picture>
45
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-encode-dark.svg">
46
+ <img alt="Bun: encode speed of every polyline library relative to @mapbox/polyline on real-world lines; polytini is highlighted" src="https://raw.githubusercontent.com/Duoquote/polyline-benchmark/main/docs/charts/speed-bun-encode-light.svg">
47
+ </picture>
48
+
49
+ ## Best at what
50
+
51
+ <!-- best -->
52
+ From the [benchmark](https://github.com/Duoquote/polyline-benchmark)'s 27 November 2026 run, across Bun, Node 22 to 26, Deno and Chrome. Leads under 1.3x are marked, because cells moved about 30% between rounds.
53
+
54
+ | Situation | Best | Runner-up | Margin |
55
+ |---|---|---|--:|
56
+ | Decode short lines (10 and 100 points) | @googlemaps/polyline-codec | @mapbox/polyline | 1.05x faster, within the noise |
57
+ | Decode long lines (10,000 and 100,000 points) | **@duoquote/polytini** | @googlemaps/polyline-codec | 1.14x faster, within the noise |
58
+ | Decode real-world lines (median 82 points) | **@duoquote/polytini** | @googlemaps/polyline-codec | 1.22x faster, within the noise |
59
+ | Encode, any length | **@duoquote/polytini** | google-polyline | 2.05x faster |
60
+ | Decode to typed arrays | **@duoquote/polytini (wasm, Float64Array)** | @duoquote/polytini (Float64Array) | 1.08x faster, within the noise |
61
+ | Encode from typed arrays | **@duoquote/polytini (wasm, Float64Array)** | @duoquote/polytini (Float64Array) | 1.05x faster, within the noise |
62
+ | Decode 1000 lines in one call | **@duoquote/polytini (wasm decodeMany/encodeMany)** | @duoquote/polytini (wasm, Float64Array) | 1.61x faster |
63
+ | Least memory per decode (10,000 points, [lat, lng] arrays) | **@duoquote/polytini (765.4 KB)** | @googlemaps/polyline-codec (766.2 KB) | 0.1% less |
64
+ | Smallest bundle (gzipped) | google-polyline (551 B) | @googlemaps/polyline-codec (568 B) | 17 B smaller |
65
+ | Strict validation (throws on bad input) | **@duoquote/polytini** | @lp-labs/polyline-codec | 15x faster |
66
+ | HERE Flexible Polyline (encode and decode) | **@duoquote/polytini (flexible)** | @here/flexpolyline | 24x faster |
67
+ <!-- /best -->
68
+
69
+ ## API
70
+
71
+ | Function | Does |
72
+ |---|---|
73
+ | `decode(str, options?)` | String to `[[lat, lng], ...]` |
74
+ | `encode(points, options?)` | `[lat, lng]` tuples or `{ lat, lng }` objects to a string |
75
+ | `decodeFlat` / `encodeFlat` | The same with one `Float64Array`: `[lat0, lng0, lat1, lng1, ...]` |
76
+ | `decodeLatLng(str)` | String to `[{ lat, lng }, ...]` |
77
+ | `toGeoJSON(str)` / `fromGeoJSON(geo)` | GeoJSON LineString in and out |
78
+ | `PolylineError` | What strict mode throws, with a `code` and an `index` |
79
+
80
+ Options are a precision number or `{ precision, factor, order, strict, dimensions, zFactor }`. Details, errors and rounding: [docs/api.md](docs/api.md).
81
+
82
+ ## Integrations
83
+
84
+ | Import | For |
85
+ |---|---|
86
+ | `@duoquote/polytini/presets` | Options for OSRM, Valhalla, GraphHopper, Mapbox, Strava and others |
87
+ | `@duoquote/polytini/leaflet` | `L.Polyline.fromEncoded`, `toEncoded`, factories; Leaflet 1.9 and 2 |
88
+ | `@duoquote/polytini/turf` | Turf Features and FeatureCollections, any GeoJSON line encoded |
89
+ | `@duoquote/polytini/google` | Drop-in `decodePath` and `encodePath` for the Maps JavaScript API |
90
+ | `@duoquote/polytini/openlayers` | Drop-in `ol/format/Polyline` that doesn't overwrite your geometry |
91
+ | `@duoquote/polytini/maplibre`, `/mapbox` | GeoJSON sources, add a line layer, update it |
92
+ | `@duoquote/polytini/deckgl` | deck.gl binary paths and polygons, no array per point |
93
+ | `@duoquote/polytini/flexible` | HERE's Flexible Polyline, a drop-in for `@here/flexpolyline` |
94
+ | `@duoquote/polytini/wasm` | Opt-in WebAssembly build, with batch calls |
95
+
96
+ None of them import their map library; you pass it in or use what they return. Examples for each: [docs/integrations.md](docs/integrations.md).
97
+
98
+ ## Notes
99
+
100
+ - The precision isn't stored in the string. A `polyline6` string decoded at 5 gives coordinates ten times too big, with no error.
101
+ - Strict mode throws on invalid characters, cut-off strings and NaN coordinates. Pass `strict: false` to never throw.
102
+ - `/wasm` only pays off for typed arrays on long lines and for many lines in one call (`decodeMany`). For `[lat, lng]` arrays the default entry is as fast or faster. Numbers: [docs/platforms.md](docs/platforms.md#wasm).
103
+ - Each runtime gets its own entry through package.json conditions (`node`, `bun`, `deno`, `browser`). Why each one exists: [docs/platforms.md](docs/platforms.md).
104
+
105
+ ## CLI
106
+
107
+ ```sh
108
+ npx @duoquote/polytini decode '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
109
+ echo '[[38.5,-120.2],[40.7,-120.95]]' | npx @duoquote/polytini encode
110
+ ```
111
+
112
+ JSON, GeoJSON, CSV and plain lines; works with `bunx`, `pnpm dlx`, `yarn dlx` and `deno run jsr:@duoquote/polytini/cli`. All options: [docs/cli.md](docs/cli.md).
113
+
114
+ ## More
115
+
116
+ [Tests and coverage](docs/testing.md), [releasing](RELEASING.md), and the [benchmark](https://github.com/Duoquote/polyline-benchmark) with every table and chart.
117
+
118
+ ## License
119
+
120
+ MIT, see [LICENSE](LICENSE).
@@ -0,0 +1,250 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/deckgl.js
20
+ var deckgl_exports = {};
21
+ __export(deckgl_exports, {
22
+ binaryPaths: () => binaryPaths,
23
+ binaryPolygons: () => binaryPolygons
24
+ });
25
+ module.exports = __toCommonJS(deckgl_exports);
26
+
27
+ // src/error.js
28
+ var PolylineError = class extends Error {
29
+ constructor(code, message, index) {
30
+ super(index == null ? message : message + " at " + index);
31
+ this.name = "PolylineError";
32
+ this.code = code;
33
+ this.index = index;
34
+ }
35
+ };
36
+ var fail = (code, message, index) => {
37
+ throw new PolylineError(code, message, index);
38
+ };
39
+
40
+ // src/core.js
41
+ var st = { swap: false, strict: true, dims: 2, zf: 1e5 };
42
+ function opts(o) {
43
+ st.swap = false;
44
+ st.strict = true;
45
+ st.dims = 2;
46
+ return o && typeof o == "object" ? objOpts(o) : st.zf = o == null ? 1e5 : pow(o);
47
+ }
48
+ function objOpts(o) {
49
+ const { order, dimensions: d } = o;
50
+ if (order == "lnglat") st.swap = true;
51
+ else if (order != null && order != "latlng") bad("order");
52
+ if (o.strict === false) st.strict = false;
53
+ if (d != null) st.dims = d >= 1 && d <= 16 && d % 1 == 0 ? d : bad("dimensions");
54
+ const f = o.factor != null ? num(o.factor) : pow(o.precision ?? 5);
55
+ st.zf = o.zFactor != null ? num(o.zFactor) : f;
56
+ return f;
57
+ }
58
+ var bad = (name) => fail("option", "Invalid " + name);
59
+ var num = (f) => f > 0 && f < 1 / 0 ? f : bad("factor");
60
+ var pow = (p) => typeof p == "number" && p >= 0 && p <= 15 && p % 1 == 0 ? 10 ** p : bad("precision");
61
+ var LIMIT = 2 ** 29;
62
+ function slowDecode(str, f, flat) {
63
+ if (typeof str != "string") fail("input", "Expected a string");
64
+ const { dims: d, strict, zf } = st, sw = st.swap && d > 1, values = [];
65
+ let v = 0, scale = 1, i = 0, c;
66
+ for (; i < str.length; i++) {
67
+ c = str.charCodeAt(i) - 63;
68
+ if (c < 0 || c > 63) {
69
+ if (strict) fail("char", "Invalid character", i);
70
+ continue;
71
+ }
72
+ v += (c & 31) * scale;
73
+ scale *= 32;
74
+ if (v > 2 ** 53 && strict) fail("overflow", "Value too large", i);
75
+ if (c < 32) {
76
+ values.push(v % 2 ? -(v + 1) / 2 : v / 2);
77
+ v = 0;
78
+ scale = 1;
79
+ }
80
+ }
81
+ if (strict && (scale > 1 || values.length % d)) fail("truncated", "Truncated", i);
82
+ const n = values.length / d | 0, sum = Array(d).fill(0), out = flat ? new Float64Array(n * d) : Array(n);
83
+ for (let j = 0, k = 0; j < n; j++) {
84
+ const p = flat ? out : Array(d);
85
+ for (let a = 0; a < d; a++, k++) {
86
+ sum[a] += values[k];
87
+ p[(flat ? k - a : 0) + (sw && a < 2 ? 1 - a : a)] = sum[a] / (a < 2 ? f : zf);
88
+ }
89
+ if (!flat) out[j] = p;
90
+ }
91
+ return out;
92
+ }
93
+ var decodeWith = (str, o, flat) => {
94
+ const f = opts(o), out = st.dims == 2 && typeof str == "string" && decodeLoop(str, f, st.swap, flat);
95
+ return out ? st.swap && !flat ? flip(out) : out : slowDecode(str, f, flat);
96
+ };
97
+ var flip = (out) => {
98
+ for (const p of out) {
99
+ const t = p[0];
100
+ p[0] = p[1];
101
+ p[1] = t;
102
+ }
103
+ return out;
104
+ };
105
+ var decodeFlat = (str, o) => decodeWith(str, o, 1);
106
+ function decodeLoop(str, f, sw, flat) {
107
+ const len = str.length, a = +sw;
108
+ let i = 0, n = 0, lat = 0, lng = 0, bad2 = 0, c, v, s, out;
109
+ if (flat) {
110
+ for (; i < len; i++) n += str.charCodeAt(i) - 95 >>> 31;
111
+ out = new Float64Array(n & ~1);
112
+ i = n = 0;
113
+ } else out = len < 64 ? [] : Array(len >> 1);
114
+ while (i < len) {
115
+ c = str.charCodeAt(i++) - 63;
116
+ bad2 |= c;
117
+ v = c & 31;
118
+ for (s = 5; c >= 32; s += 5) {
119
+ c = str.charCodeAt(i++) - 63;
120
+ bad2 |= c | 25 - s;
121
+ v |= (c & 31) << s;
122
+ }
123
+ lat += v >>> 1 ^ -(v & 1);
124
+ c = str.charCodeAt(i++) - 63;
125
+ bad2 |= c;
126
+ v = c & 31;
127
+ for (s = 5; c >= 32; s += 5) {
128
+ c = str.charCodeAt(i++) - 63;
129
+ bad2 |= c | 25 - s;
130
+ v |= (c & 31) << s;
131
+ }
132
+ lng += v >>> 1 ^ -(v & 1);
133
+ if (flat) {
134
+ out[n + a] = lat / f;
135
+ out[n + 1 - a] = lng / f;
136
+ n += 2;
137
+ } else out[n++] = [lat / f, lng / f];
138
+ }
139
+ if (bad2 & ~63 || i != len) return null;
140
+ if (out.length != n) out.length = n;
141
+ return out;
142
+ }
143
+
144
+ // src/deckgl.js
145
+ function fill2(str, out, k, f) {
146
+ const len = str.length, start = k;
147
+ let i = 0, lat = 0, lng = 0, bad2 = 0, c, v, s;
148
+ k *= 2;
149
+ while (i < len) {
150
+ c = str.charCodeAt(i++) - 63;
151
+ bad2 |= c;
152
+ v = c & 31;
153
+ for (s = 5; c >= 32; s += 5) {
154
+ c = str.charCodeAt(i++) - 63;
155
+ bad2 |= c;
156
+ v |= (c & 31) << s;
157
+ }
158
+ bad2 |= 30 - s;
159
+ lat += v >>> 1 ^ -(v & 1);
160
+ c = str.charCodeAt(i++) - 63;
161
+ bad2 |= c;
162
+ v = c & 31;
163
+ for (s = 5; c >= 32; s += 5) {
164
+ c = str.charCodeAt(i++) - 63;
165
+ bad2 |= c;
166
+ v |= (c & 31) << s;
167
+ }
168
+ bad2 |= 30 - s;
169
+ lng += v >>> 1 ^ -(v & 1);
170
+ out[k++] = lng / f;
171
+ out[k++] = lat / f;
172
+ }
173
+ return bad2 & ~63 || i != len ? -1 : k / 2 - start;
174
+ }
175
+ function fillN(str, out, ts, k, d, size, f, zf) {
176
+ const len = str.length, start = k, sum = [0, 0, 0, 0];
177
+ let i = 0, bad2 = 0, a, c, v, s, j;
178
+ while (i < len) {
179
+ for (a = 0; a < d; a++) {
180
+ c = str.charCodeAt(i++) - 63;
181
+ bad2 |= c;
182
+ v = c & 31;
183
+ for (s = 5; c >= 32; s += 5) {
184
+ c = str.charCodeAt(i++) - 63;
185
+ bad2 |= c;
186
+ v |= (c & 31) << s;
187
+ }
188
+ bad2 |= 30 - s;
189
+ sum[a] += v >>> 1 ^ -(v & 1);
190
+ }
191
+ j = k * size;
192
+ out[j] = sum[1] / f;
193
+ out[j + 1] = sum[0] / f;
194
+ if (size == 3) out[j + 2] = sum[2] / zf;
195
+ if (ts) ts[k] = sum[d - 1] / zf;
196
+ k++;
197
+ }
198
+ return bad2 & ~63 || i != len ? -1 : k - start;
199
+ }
200
+ function slow(str, o, out, ts, k, d, size) {
201
+ const flat = decodeFlat(str, o), n = flat.length / d;
202
+ for (let p = 0; p < n; p++, k++) {
203
+ for (let a = 0; a < size; a++) out[k * size + a] = flat[p * d + a];
204
+ if (ts) ts[k] = flat[p * d + d - 1];
205
+ }
206
+ return n;
207
+ }
208
+ function build(encoded, o, polygons) {
209
+ const list = typeof encoded == "string" ? [encoded] : encoded;
210
+ if (!Array.isArray(list)) fail("input", "Expected a string or an array of strings");
211
+ const f = opts(o), { dims: d, zf } = st, time = !polygons && o != null && typeof o == "object" && !!o.timestamps;
212
+ const size = time ? d - 1 : d;
213
+ if (size < 2 || size > 3) fail("option", time ? "timestamps need dimensions 3 or 4" : "Invalid dimensions for deck.gl: use 2 or 3, or 4 with timestamps");
214
+ const Type = o?.float32 ? Float32Array : Float64Array;
215
+ const n = list.length;
216
+ let max = 0;
217
+ for (let i = 0; i < n; i++) {
218
+ const s = list[i];
219
+ if (typeof s != "string") fail("input", "Expected a string", i);
220
+ let values = 0;
221
+ for (let j = 0; j < s.length; j++) values += s.charCodeAt(j) - 95 >>> 31;
222
+ max += (values / d | 0) + (polygons ? 1 : 0);
223
+ }
224
+ const out = new Type(max * size), ts = time ? new Float32Array(max) : null, starts = new Uint32Array(n + 1);
225
+ const exact = { ...typeof o == "number" ? { precision: o } : o, order: "lnglat" };
226
+ let k = 0;
227
+ for (let i = 0; i < n; i++) {
228
+ const s = list[i];
229
+ starts[i] = k;
230
+ let m = d == 2 ? fill2(s, out, k, f) : fillN(s, out, ts, k, d, size, f, zf);
231
+ if (m < 0) m = slow(s, exact, out, ts, k, d, size);
232
+ if (polygons && m > 0) {
233
+ const a = k * size, b = (k + m - 1) * size;
234
+ let open = false;
235
+ for (let c = 0; c < size; c++) open ||= out[a + c] !== out[b + c];
236
+ if (open) {
237
+ for (let c = 0; c < size; c++) out[b + size + c] = out[a + c];
238
+ m++;
239
+ }
240
+ }
241
+ k += m;
242
+ }
243
+ starts[n] = k;
244
+ const value = k * size < out.length ? out.subarray(0, k * size) : out;
245
+ const attributes = polygons ? { getPolygon: { value, size } } : { getPath: { value, size } };
246
+ if (ts) attributes.getTimestamps = { value: k < ts.length ? ts.subarray(0, k) : ts, size: 1 };
247
+ return { length: n, startIndices: starts, attributes };
248
+ }
249
+ var binaryPaths = (encoded, options) => build(encoded, options, false);
250
+ var binaryPolygons = (encoded, options) => build(encoded, options, true);
@@ -0,0 +1,229 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/flexible.js
20
+ var flexible_exports = {};
21
+ __export(flexible_exports, {
22
+ ABSENT: () => ABSENT,
23
+ ALTITUDE: () => ALTITUDE,
24
+ CUSTOM1: () => CUSTOM1,
25
+ CUSTOM2: () => CUSTOM2,
26
+ ELEVATION: () => ELEVATION,
27
+ LEVEL: () => LEVEL,
28
+ PolylineError: () => PolylineError,
29
+ RESERVED1: () => RESERVED1,
30
+ RESERVED2: () => RESERVED2,
31
+ decode: () => decode,
32
+ encode: () => encode,
33
+ header: () => header
34
+ });
35
+ module.exports = __toCommonJS(flexible_exports);
36
+
37
+ // src/error.js
38
+ var PolylineError = class extends Error {
39
+ constructor(code, message, index) {
40
+ super(index == null ? message : message + " at " + index);
41
+ this.name = "PolylineError";
42
+ this.code = code;
43
+ this.index = index;
44
+ }
45
+ };
46
+ var fail = (code, message, index) => {
47
+ throw new PolylineError(code, message, index);
48
+ };
49
+
50
+ // src/flexible.js
51
+ var ABSENT = 0;
52
+ var LEVEL = 1;
53
+ var ALTITUDE = 2;
54
+ var ELEVATION = 3;
55
+ var RESERVED1 = 4;
56
+ var RESERVED2 = 5;
57
+ var CUSTOM1 = 6;
58
+ var CUSTOM2 = 7;
59
+ var TYPES = [0, 1, 2, 3, 6, 7];
60
+ var E = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
61
+ var T = new Uint8Array(128).fill(64);
62
+ for (let i = 0; i < 64; i++) T[E.charCodeAt(i)] = i;
63
+ var C = [];
64
+ for (let z = 0; z < 1024; z++) C[z] = z < 32 ? E[z] : E[z & 31 | 32] + E[z >> 5];
65
+ var P = [];
66
+ for (let p = 0; p < 16; p++) P[p] = +("1e" + p);
67
+ var chars = (z) => {
68
+ let s = "";
69
+ for (; z > 31; z = Math.floor(z / 32)) s += E[z % 32 | 32];
70
+ return s + E[z];
71
+ };
72
+ var range = (p, name) => typeof p == "number" && p >= 0 && p <= 15 && p % 1 == 0 ? p : fail("option", name + " must be an integer from 0 to 15");
73
+ function head(str) {
74
+ if (typeof str != "string") fail("input", "Expected a string");
75
+ let i = 0, k = 0, v, m, c;
76
+ for (; k < 2; k++) {
77
+ for (v = 0, m = 1, c = 32; c & 32; m *= 32, i++) {
78
+ if (i >= str.length) fail("truncated", "Polyline ends inside the header", i);
79
+ c = T[str.charCodeAt(i)];
80
+ if (!(c < 64)) fail("char", "Invalid character " + JSON.stringify(str[i]), i);
81
+ v += (c & 31) * m;
82
+ }
83
+ if (!k && v != 1) fail("version", "Unsupported format version " + v, 0);
84
+ }
85
+ return v < 2048 ? v + i * 2048 : fail("version", "Invalid header", i);
86
+ }
87
+ function header(str) {
88
+ const h = head(str) % 2048;
89
+ return { precision: h & 15, thirdDim: h >> 4 & 7, thirdDimPrecision: h >> 7 };
90
+ }
91
+ function decode(str) {
92
+ const r = head(str), h = r % 2048, i = (r - h) / 2048, t = h >> 4 & 7, f = P[h & 15], fz = P[h >> 7];
93
+ return { precision: h & 15, thirdDim: t, thirdDimPrecision: h >> 7, polyline: decodeLoop(str, i, f, fz, t) || slowDecode(str, i, f, fz, t ? 3 : 2) };
94
+ }
95
+ function decodeLoop(str, i, f, fz, three) {
96
+ const len = str.length, out = [];
97
+ let n = 0, a = 0, b = 0, z = 0, bad = 0, w = 0, k, c, v, s;
98
+ while (i < len) {
99
+ k = str.charCodeAt(i++);
100
+ bad |= c = T[k & 127] | k & -128;
101
+ for (v = c & 31, s = 5; c & 32; s += 5) {
102
+ k = str.charCodeAt(i++);
103
+ bad |= c = T[k & 127] | k & -128;
104
+ if (s < 30) v |= (c & 31) << s;
105
+ else if (s < 50) w += v += (c & 31) * 2 ** s;
106
+ else bad = 64;
107
+ }
108
+ a += s > 30 ? v % 2 ? -(v + 1) / 2 : v / 2 : v >>> 1 ^ -(v & 1);
109
+ k = str.charCodeAt(i++);
110
+ bad |= c = T[k & 127] | k & -128;
111
+ for (v = c & 31, s = 5; c & 32; s += 5) {
112
+ k = str.charCodeAt(i++);
113
+ bad |= c = T[k & 127] | k & -128;
114
+ if (s < 30) v |= (c & 31) << s;
115
+ else if (s < 50) w += v += (c & 31) * 2 ** s;
116
+ else bad = 64;
117
+ }
118
+ b += s > 30 ? v % 2 ? -(v + 1) / 2 : v / 2 : v >>> 1 ^ -(v & 1);
119
+ if (three) {
120
+ k = str.charCodeAt(i++);
121
+ bad |= c = T[k & 127] | k & -128;
122
+ for (v = c & 31, s = 5; c & 32; s += 5) {
123
+ k = str.charCodeAt(i++);
124
+ bad |= c = T[k & 127] | k & -128;
125
+ if (s < 30) v |= (c & 31) << s;
126
+ else if (s < 50) w += v += (c & 31) * 2 ** s;
127
+ else bad = 64;
128
+ }
129
+ z += s > 30 ? v % 2 ? -(v + 1) / 2 : v / 2 : v >>> 1 ^ -(v & 1);
130
+ out[n++] = [a / f, b / f, z / fz];
131
+ } else out[n++] = [a / f, b / f];
132
+ }
133
+ return bad > 63 || i > len || w > 2 ** 52 ? null : out;
134
+ }
135
+ function slowDecode(str, i, f, fz, d) {
136
+ const out = [], sum = [0n, 0n, 0n];
137
+ let pt = [], j = 0, v = 0n, s = 0n, k, c;
138
+ for (; i < str.length; i++) {
139
+ k = str.charCodeAt(i);
140
+ c = k < 128 ? T[k] : 64;
141
+ if (c > 63) fail("char", "Invalid character " + JSON.stringify(str[i]), i);
142
+ v |= BigInt(c & 31) << s;
143
+ if (c & 32) {
144
+ if ((s += 5n) > 60n) fail("overflow", "Value too large", i);
145
+ continue;
146
+ }
147
+ if (v >> 64n) fail("overflow", "Value too large", i);
148
+ sum[j] += v & 1n ? ~(v >> 1n) : v >> 1n;
149
+ pt[j] = Number(sum[j]) / (j < 2 ? f : fz);
150
+ if (++j == d) {
151
+ out.push(pt);
152
+ pt = [];
153
+ j = 0;
154
+ }
155
+ v = s = 0n;
156
+ }
157
+ if (s || j) fail("truncated", "Polyline ends inside a " + (s ? "value" : "point"), i);
158
+ return out;
159
+ }
160
+ function encode(input, options) {
161
+ const obj = input != null && typeof input == "object" && !(input.length >= 0), a = obj ? input : {}, o = options || {};
162
+ const pts = obj ? input.polyline : input;
163
+ const p = range(o.precision ?? a.precision ?? 5, "precision");
164
+ const q = range(o.thirdDimPrecision ?? a.thirdDimPrecision ?? 0, "thirdDimPrecision");
165
+ const t = o.thirdDim ?? a.thirdDim ?? 0;
166
+ if (!TYPES.includes(t)) fail("option", "thirdDim must be 0, 1, 2, 3, 6 or 7");
167
+ if (pts == null || !(pts.length >= 0)) fail("input", "Expected an array of points");
168
+ const s = "B" + chars(q << 7 | t << 4 | p), d = t ? 3 : 2, flat = typeof pts[0] == "number";
169
+ if (flat && pts.length % d) fail("input", "Flat array length is not a multiple of " + d);
170
+ return !flat && encodeLoop(pts, P[p], P[q], t, s) || slowEncode(pts, P[p], P[q], d, flat, s);
171
+ }
172
+ var LIMIT = 2 ** 29;
173
+ var WIDE = 2 ** 51;
174
+ function encodeLoop(pts, f, fz, three, s) {
175
+ const n = pts.length;
176
+ let i = 0, la = 0, lb = 0, lz = 0, x, y, w, p, q, r, z, pt;
177
+ try {
178
+ for (; i < n; i++) {
179
+ pt = pts[i];
180
+ x = pt[0] * f;
181
+ y = pt[1] * f;
182
+ w = three ? pt[2] * fz : 0;
183
+ if (!(x < LIMIT && x > -LIMIT && y < LIMIT && y > -LIMIT && w < LIMIT && w > -LIMIT)) return;
184
+ p = x < 0 ? -(0.5 - x | 0) : x + 0.5 | 0;
185
+ q = y < 0 ? -(0.5 - y | 0) : y + 0.5 | 0;
186
+ z = p - la;
187
+ z = z << 1 ^ z >> 31;
188
+ s += z < 1024 ? C[z] : chars(z);
189
+ z = q - lb;
190
+ z = z << 1 ^ z >> 31;
191
+ s += z < 1024 ? C[z] : chars(z);
192
+ la = p;
193
+ lb = q;
194
+ if (three) {
195
+ r = w < 0 ? -(0.5 - w | 0) : w + 0.5 | 0;
196
+ z = r - lz;
197
+ z = z << 1 ^ z >> 31;
198
+ s += z < 1024 ? C[z] : chars(z);
199
+ lz = r;
200
+ }
201
+ }
202
+ } catch {
203
+ return;
204
+ }
205
+ return s;
206
+ }
207
+ function slowEncode(pts, f, fz, d, flat, s) {
208
+ const n = flat ? pts.length / d : pts.length, last = [0, 0, 0];
209
+ for (let i = 0; i < n; i++) {
210
+ const pt = flat ? null : pts[i];
211
+ if (!flat && pt == null) fail("coordinate", "Invalid point", i);
212
+ for (let a = 0; a < d; a++) {
213
+ const x = (flat ? pts[i * d + a] : pt[a]) * (a < 2 ? f : fz);
214
+ if (!(x - x == 0)) fail("coordinate", "Invalid coordinate", i);
215
+ const r = x < 0 ? -Math.round(-x) : Math.round(x), l = last[a];
216
+ let z = r - l;
217
+ last[a] = r;
218
+ if (r < WIDE && r > -WIDE && l < WIDE && l > -WIDE) s += chars(z < 0 ? -2 * z - 1 : 2 * z);
219
+ else {
220
+ z = BigInt(r) - BigInt(l);
221
+ z = z < 0n ? ~(z << 1n) : z << 1n;
222
+ if (z >> 64n) fail("overflow", "Value too large", i);
223
+ for (; z > 31n; z >>= 5n) s += E[Number(z & 31n) | 32];
224
+ s += E[Number(z)];
225
+ }
226
+ }
227
+ }
228
+ return s;
229
+ }