@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/openlayers.js CHANGED
@@ -1,172 +1,172 @@
1
- // OpenLayers: a faster drop-in for ol/format/Polyline.
2
- //
3
- // import Polyline from 'ol/format/Polyline.js';
4
- // import LineString from 'ol/geom/LineString.js';
5
- // import { createPolylineFormat } from '@duoquote/polytini/openlayers';
6
- // const FastPolyline = createPolylineFormat({ Polyline, LineString });
7
- // const format = new FastPolyline({ factor: 1e6 });
8
- //
9
- // The factory takes ol's classes from you instead of importing ol, so
10
- // polytini has no runtime dependency on it. The class it returns extends
11
- // the Polyline class you pass and replaces only the two text-level methods,
12
- // readGeometryFromText and writeGeometryText. Everything else is ol's: the
13
- // constructor options (factor, geometryLayout), projections, readFeature,
14
- // readFeatures, writeFeature, writeFeatures and the `decimals` write option.
15
- //
16
- // The output is the same as ol's for every input, with one exception: ol's
17
- // writeGeometry flips XY and writes the deltas into the geometry's own flat
18
- // coordinates when there is no projection transform (and no `decimals`), so
19
- // a second writeGeometry of the same geometry gives a wrong string. This
20
- // class never changes the geometry.
21
- //
22
- // What "the same" means in detail:
23
- // - Coordinates come out as [x, y] = [lng, lat], then Z and M, as plain
24
- // arrays, and every value, M included, is divided by the factor. A factor
25
- // of 0 or no factor means 1e5, like ol.
26
- // - Rounding is ol's, not Google's. ol rounds with
27
- // `value < 0 ? Math.ceil(value - 0.5) : Math.round(value)`. That is half
28
- // away from zero for every value except -0.49999999999999994 (the largest
29
- // double below 0.5, negated), where `value - 0.5` rounds to -1 in floating
30
- // point, so ol writes -1 where Google's algorithm, and polytini's own
31
- // encode, write 0. (Values of 2^52 and more differ too, but those are past
32
- // ol's 32-bit limit anyway.) A drop-in should give the same strings as the
33
- // format it replaces, so this class rounds like ol.
34
- // - ol never throws on bad input. Strings that are not plain valid polylines
35
- // (characters outside '?' to '~', a value cut off at the end, a point with
36
- // fewer values than the layout has, or a value of more than 30 bits) and
37
- // geometries with values that do not fit 32-bit deltas (NaN, Infinity, or
38
- // deltas of 2^29 units or more) are handed to ol's own code, so they give
39
- // exactly what ol gives, odd results included: a NaN for a missing value,
40
- // and 32-bit wrap-around for very large values.
41
- /* @ts-self-types="../types/openlayers.d.ts" */
42
- import { chars, LIMIT } from './core.js';
43
-
44
- const STRIDE = { XY: 2, XYZ: 3, XYM: 3, XYZM: 4 };
45
-
46
- // Encoded characters for zigzag values below 1024.
47
- const CHARS = [];
48
- for (let z = 0; z < 1024; z++) CHARS[z] = chars(z);
49
-
50
- /**
51
- * Decodes to ol's flat coordinates ([lng, lat, ...rest] per point), or
52
- * returns null when the string is anything but a plain valid polyline with
53
- * a whole number of points and values of at most 6 characters. On those
54
- * strings ol's 32-bit math is exact, so the result is identical to ol's.
55
- */
56
- function decodeFlat(str, stride, f) {
57
- if (!(stride >= 2 && stride <= 4)) return null;
58
- const len = str.length, sum = [0, 0, 0, 0];
59
- let i = 0, n = 0, bad = 0, d, c, v, s;
60
- // Long lines are counted first (every value ends with exactly one
61
- // character below code 95) and get one array of the right size: growing
62
- // an array of a few hundred thousand doubles was slower than the extra
63
- // pass. Short ones grow by appending, which was faster for them.
64
- if (len > 50000) for (; i < len; i++) n += (str.charCodeAt(i) - 95) >>> 31;
65
- const out = n ? new Array(n) : [];
66
- i = n = 0;
67
- while (i < len) {
68
- for (d = 0; d < stride; d++) {
69
- // Past the end, charCodeAt gives NaN: the value ends and i passes len.
70
- c = str.charCodeAt(i++) - 63;
71
- bad |= c;
72
- v = c & 31;
73
- for (s = 5; c >= 32; s += 5) {
74
- c = str.charCodeAt(i++) - 63;
75
- bad |= c;
76
- v |= (c & 31) << s;
77
- }
78
- bad |= 30 - s;
79
- sum[d] += (v >>> 1) ^ -(v & 1);
80
- }
81
- out[n++] = sum[1] / f;
82
- out[n++] = sum[0] / f;
83
- if (stride > 2) out[n++] = sum[2] / f;
84
- if (stride > 3) out[n++] = sum[3] / f;
85
- }
86
- return bad & ~63 || i != len ? null : out;
87
- }
88
-
89
- /**
90
- * Encodes ol flat coordinates without changing them, rounding like ol.
91
- * Returns null when a delta is not finite or too large for 32-bit math, or
92
- * the length is not a whole number of points.
93
- */
94
- function encodeFlat(flat, stride, f) {
95
- const n = flat.length, last = [0, 0, 0, 0];
96
- if (n % stride) return null;
97
- let s = '', k = 0, d, v, z;
98
- for (; k < n; k += stride) {
99
- for (d = 0; d < stride; d++) {
100
- v = flat[k + (d < 2 ? 1 - d : d)] * f;
101
- v = v < 0 ? Math.ceil(v - 0.5) : Math.round(v);
102
- z = v - last[d];
103
- last[d] = v;
104
- if (!(z < LIMIT && z > -LIMIT)) return null;
105
- z = (z << 1) ^ (z >> 31);
106
- s += z < 1024 ? CHARS[z] : chars(z);
107
- }
108
- }
109
- return s;
110
- }
111
-
112
- // The projection step of ol's format, done with the geometry's own
113
- // transform() so nothing from ol has to be imported. Returns null when ol
114
- // has to do it itself: tile-pixel projections, which ol handles specially.
115
- const projections = (options, write) => {
116
- const fp = options?.featureProjection, dp = options?.dataProjection;
117
- const tile = (p) => typeof p == 'object' && p?.getUnits?.() == 'tile-pixels';
118
- if (tile(fp) || tile(dp)) return null;
119
- // Equal codes need nothing. Different but equivalent projections
120
- // (EPSG:3857 and EPSG:900913) go through transform(), which copies the
121
- // values unchanged, so the numbers are the same as ol's.
122
- return fp && dp && fp !== dp ? (write ? [fp, dp] : [dp, fp]) : [];
123
- };
124
-
125
- /**
126
- * Makes the drop-in class from ol's own. Pass `{ Polyline, LineString }`
127
- * (ol/format/Polyline.js and ol/geom/LineString.js), or the `ol` namespace
128
- * of the full build.
129
- */
130
- export function createPolylineFormat(ol) {
131
- const Base = ol?.Polyline ?? ol?.format?.Polyline, LineString = ol?.LineString ?? ol?.geom?.LineString;
132
- if (typeof Base != 'function' || typeof LineString != 'function') {
133
- throw new TypeError('createPolylineFormat needs { Polyline, LineString } from ol');
134
- }
135
- /** Drop-in for ol/format/Polyline: same options and output, faster, and it never changes the geometry it writes. */
136
- return class Polyline extends Base {
137
- constructor(options) {
138
- super(options);
139
- options = options || {};
140
- // ol keeps these as private fields; keep our own copies.
141
- this.polyFactor_ = options.factor ? options.factor : 1e5;
142
- this.polyLayout_ = options.geometryLayout ? options.geometryLayout : 'XY';
143
- }
144
-
145
- readGeometryFromText(text, options) {
146
- options = this.adaptOptions(options);
147
- const layout = this.polyLayout_, p = projections(options, false);
148
- const flat = p && decodeFlat(text, STRIDE[layout], this.polyFactor_);
149
- if (!flat) return super.readGeometryFromText(text, options);
150
- const geometry = new LineString(flat, layout);
151
- return p.length ? geometry.transform(p[0], p[1]) : geometry;
152
- }
153
-
154
- writeGeometryText(geometry, options) {
155
- options = this.adaptOptions(options);
156
- const p = projections(options, true);
157
- let flat = geometry.getFlatCoordinates();
158
- const stride = geometry.getStride(), f = this.polyFactor_;
159
- if (p) {
160
- if (p.length) flat = geometry.clone().transform(p[0], p[1]).getFlatCoordinates();
161
- // ol's `decimals`: round every value to that many places first.
162
- if (options?.decimals !== undefined) {
163
- const power = Math.pow(10, options.decimals);
164
- flat = flat.map((c) => Math.round(c * power) / power);
165
- }
166
- }
167
- // Anything else goes to ol's own writer, on a copy: it writes the
168
- // deltas into the geometry it encodes.
169
- return (p && encodeFlat(flat, stride, f)) ?? super.writeGeometryText(geometry.clone(), options);
170
- }
171
- };
172
- }
1
+ // OpenLayers: a faster drop-in for ol/format/Polyline.
2
+ //
3
+ // import Polyline from 'ol/format/Polyline.js';
4
+ // import LineString from 'ol/geom/LineString.js';
5
+ // import { createPolylineFormat } from '@duoquote/polytini/openlayers';
6
+ // const FastPolyline = createPolylineFormat({ Polyline, LineString });
7
+ // const format = new FastPolyline({ factor: 1e6 });
8
+ //
9
+ // The factory takes ol's classes from you instead of importing ol, so
10
+ // polytini has no runtime dependency on it. The class it returns extends
11
+ // the Polyline class you pass and replaces only the two text-level methods,
12
+ // readGeometryFromText and writeGeometryText. Everything else is ol's: the
13
+ // constructor options (factor, geometryLayout), projections, readFeature,
14
+ // readFeatures, writeFeature, writeFeatures and the `decimals` write option.
15
+ //
16
+ // The output is the same as ol's for every input, with one exception: ol's
17
+ // writeGeometry flips XY and writes the deltas into the geometry's own flat
18
+ // coordinates when there is no projection transform (and no `decimals`), so
19
+ // a second writeGeometry of the same geometry gives a wrong string. This
20
+ // class never changes the geometry.
21
+ //
22
+ // What "the same" means in detail:
23
+ // - Coordinates come out as [x, y] = [lng, lat], then Z and M, as plain
24
+ // arrays, and every value, M included, is divided by the factor. A factor
25
+ // of 0 or no factor means 1e5, like ol.
26
+ // - Rounding is ol's, not Google's. ol rounds with
27
+ // `value < 0 ? Math.ceil(value - 0.5) : Math.round(value)`. That is half
28
+ // away from zero for every value except -0.49999999999999994 (the largest
29
+ // double below 0.5, negated), where `value - 0.5` rounds to -1 in floating
30
+ // point, so ol writes -1 where Google's algorithm, and polytini's own
31
+ // encode, write 0. (Values of 2^52 and more differ too, but those are past
32
+ // ol's 32-bit limit anyway.) A drop-in should give the same strings as the
33
+ // format it replaces, so this class rounds like ol.
34
+ // - ol never throws on bad input. Strings that are not plain valid polylines
35
+ // (characters outside '?' to '~', a value cut off at the end, a point with
36
+ // fewer values than the layout has, or a value of more than 30 bits) and
37
+ // geometries with values that do not fit 32-bit deltas (NaN, Infinity, or
38
+ // deltas of 2^29 units or more) are handed to ol's own code, so they give
39
+ // exactly what ol gives, odd results included: a NaN for a missing value,
40
+ // and 32-bit wrap-around for very large values.
41
+ /* @ts-self-types="../types/openlayers.jsr.d.ts" */
42
+ import { chars, LIMIT } from './core.js';
43
+
44
+ const STRIDE = { XY: 2, XYZ: 3, XYM: 3, XYZM: 4 };
45
+
46
+ // Encoded characters for zigzag values below 1024.
47
+ const CHARS = [];
48
+ for (let z = 0; z < 1024; z++) CHARS[z] = chars(z);
49
+
50
+ /**
51
+ * Decodes to ol's flat coordinates ([lng, lat, ...rest] per point), or
52
+ * returns null when the string is anything but a plain valid polyline with
53
+ * a whole number of points and values of at most 6 characters. On those
54
+ * strings ol's 32-bit math is exact, so the result is identical to ol's.
55
+ */
56
+ function decodeFlat(str, stride, f) {
57
+ if (!(stride >= 2 && stride <= 4)) return null;
58
+ const len = str.length, sum = [0, 0, 0, 0];
59
+ let i = 0, n = 0, bad = 0, d, c, v, s;
60
+ // Long lines are counted first (every value ends with exactly one
61
+ // character below code 95) and get one array of the right size: growing
62
+ // an array of a few hundred thousand doubles was slower than the extra
63
+ // pass. Short ones grow by appending, which was faster for them.
64
+ if (len > 50000) for (; i < len; i++) n += (str.charCodeAt(i) - 95) >>> 31;
65
+ const out = n ? new Array(n) : [];
66
+ i = n = 0;
67
+ while (i < len) {
68
+ for (d = 0; d < stride; d++) {
69
+ // Past the end, charCodeAt gives NaN: the value ends and i passes len.
70
+ c = str.charCodeAt(i++) - 63;
71
+ bad |= c;
72
+ v = c & 31;
73
+ for (s = 5; c >= 32; s += 5) {
74
+ c = str.charCodeAt(i++) - 63;
75
+ bad |= c;
76
+ v |= (c & 31) << s;
77
+ }
78
+ bad |= 30 - s;
79
+ sum[d] += (v >>> 1) ^ -(v & 1);
80
+ }
81
+ out[n++] = sum[1] / f;
82
+ out[n++] = sum[0] / f;
83
+ if (stride > 2) out[n++] = sum[2] / f;
84
+ if (stride > 3) out[n++] = sum[3] / f;
85
+ }
86
+ return bad & ~63 || i != len ? null : out;
87
+ }
88
+
89
+ /**
90
+ * Encodes ol flat coordinates without changing them, rounding like ol.
91
+ * Returns null when a delta is not finite or too large for 32-bit math, or
92
+ * the length is not a whole number of points.
93
+ */
94
+ function encodeFlat(flat, stride, f) {
95
+ const n = flat.length, last = [0, 0, 0, 0];
96
+ if (n % stride) return null;
97
+ let s = '', k = 0, d, v, z;
98
+ for (; k < n; k += stride) {
99
+ for (d = 0; d < stride; d++) {
100
+ v = flat[k + (d < 2 ? 1 - d : d)] * f;
101
+ v = v < 0 ? Math.ceil(v - 0.5) : Math.round(v);
102
+ z = v - last[d];
103
+ last[d] = v;
104
+ if (!(z < LIMIT && z > -LIMIT)) return null;
105
+ z = (z << 1) ^ (z >> 31);
106
+ s += z < 1024 ? CHARS[z] : chars(z);
107
+ }
108
+ }
109
+ return s;
110
+ }
111
+
112
+ // The projection step of ol's format, done with the geometry's own
113
+ // transform() so nothing from ol has to be imported. Returns null when ol
114
+ // has to do it itself: tile-pixel projections, which ol handles specially.
115
+ const projections = (options, write) => {
116
+ const fp = options?.featureProjection, dp = options?.dataProjection;
117
+ const tile = (p) => typeof p == 'object' && p?.getUnits?.() == 'tile-pixels';
118
+ if (tile(fp) || tile(dp)) return null;
119
+ // Equal codes need nothing. Different but equivalent projections
120
+ // (EPSG:3857 and EPSG:900913) go through transform(), which copies the
121
+ // values unchanged, so the numbers are the same as ol's.
122
+ return fp && dp && fp !== dp ? (write ? [fp, dp] : [dp, fp]) : [];
123
+ };
124
+
125
+ /**
126
+ * Makes the drop-in class from ol's own. Pass `{ Polyline, LineString }`
127
+ * (ol/format/Polyline.js and ol/geom/LineString.js), or the `ol` namespace
128
+ * of the full build.
129
+ */
130
+ export function createPolylineFormat(ol) {
131
+ const Base = ol?.Polyline ?? ol?.format?.Polyline, LineString = ol?.LineString ?? ol?.geom?.LineString;
132
+ if (typeof Base != 'function' || typeof LineString != 'function') {
133
+ throw new TypeError('createPolylineFormat needs { Polyline, LineString } from ol');
134
+ }
135
+ /** Drop-in for ol/format/Polyline: same options and output, faster, and it never changes the geometry it writes. */
136
+ return class Polyline extends Base {
137
+ constructor(options) {
138
+ super(options);
139
+ options = options || {};
140
+ // ol keeps these as private fields; keep our own copies.
141
+ this.polyFactor_ = options.factor ? options.factor : 1e5;
142
+ this.polyLayout_ = options.geometryLayout ? options.geometryLayout : 'XY';
143
+ }
144
+
145
+ readGeometryFromText(text, options) {
146
+ options = this.adaptOptions(options);
147
+ const layout = this.polyLayout_, p = projections(options, false);
148
+ const flat = p && decodeFlat(text, STRIDE[layout], this.polyFactor_);
149
+ if (!flat) return super.readGeometryFromText(text, options);
150
+ const geometry = new LineString(flat, layout);
151
+ return p.length ? geometry.transform(p[0], p[1]) : geometry;
152
+ }
153
+
154
+ writeGeometryText(geometry, options) {
155
+ options = this.adaptOptions(options);
156
+ const p = projections(options, true);
157
+ let flat = geometry.getFlatCoordinates();
158
+ const stride = geometry.getStride(), f = this.polyFactor_;
159
+ if (p) {
160
+ if (p.length) flat = geometry.clone().transform(p[0], p[1]).getFlatCoordinates();
161
+ // ol's `decimals`: round every value to that many places first.
162
+ if (options?.decimals !== undefined) {
163
+ const power = Math.pow(10, options.decimals);
164
+ flat = flat.map((c) => Math.round(c * power) / power);
165
+ }
166
+ }
167
+ // Anything else goes to ol's own writer, on a copy: it writes the
168
+ // deltas into the geometry it encodes.
169
+ return (p && encodeFlat(flat, stride, f)) ?? super.writeGeometryText(geometry.clone(), options);
170
+ }
171
+ };
172
+ }