@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/google.js CHANGED
@@ -1,70 +1,70 @@
1
- // Google Maps JavaScript API helpers: decodePath and encodePath with the
2
- // signatures of google.maps.geometry.encoding, without loading the geometry
3
- // library.
4
- //
5
- // import { decodePath, encodePath } from '@duoquote/polytini/google';
6
- // const path = decodePath(route.polyline.encodedPolyline);
7
- // new google.maps.Polyline({ path, map });
8
- // encodePath(polyline.getPath()); // an MVCArray<LatLng>
9
- //
10
- // Differences from google.maps.geometry.encoding:
11
- // - Both take an optional second argument: polytini's precision (a number)
12
- // or options object. Precision defaults to 5, Google's fixed value.
13
- // - decodePath returns google.maps.LatLng objects when a LatLng constructor
14
- // is available (the LatLng option, else globalThis.google.maps.LatLng),
15
- // and plain { lat, lng } literals otherwise, so it also works in Node or
16
- // before the Maps API has loaded. Pass `LatLng: false` for literals even
17
- // when the API is loaded. Polyline, Polygon and most Maps API methods
18
- // take literals as well as LatLng, and literals are cheaper to create.
19
- // - Strict by default: malformed strings and invalid coordinates throw a
20
- // PolylineError. Google documents no error behavior for decodePath or
21
- // encodePath, so what they return for such input can't be matched; a
22
- // thrown error is easier to find than a wrong line on the map, and valid
23
- // input gives the same result either way. Pass `strict: false` to never
24
- // throw on bad characters or truncated strings (invalid characters are
25
- // skipped and an incomplete last point is dropped) or on invalid
26
- // coordinates (encoded as 0).
27
- // - encodePath also takes polytini's [lat, lng] tuples and encodes
28
- // LatLngLiteral values as given, without clamping or wrapping them.
29
- /* @ts-self-types="../types/google.jsr.d.ts" */
30
- import { decode, encode } from './web.js';
31
-
32
- export { PolylineError } from './web.js';
33
-
34
- // decodePath reads the tuples as [lat, lng], so it ignores order 'lnglat'.
35
- const latFirst = (o) => (o?.order == 'lnglat' ? { ...o, order: 'latlng' } : o);
36
-
37
- /**
38
- * Decodes an encoded path to google.maps.LatLng objects, or to LatLngLiteral
39
- * objects when no LatLng constructor is available. Options: polytini's
40
- * (precision, strict, factor, dimensions), plus `LatLng` (a constructor, or
41
- * false for literals) and `noClampNoWrap` (passed to the LatLng constructor;
42
- * without it, Google's LatLng clamps latitude and wraps longitude 180 to -180).
43
- */
44
- export function decodePath(encodedPath, o) {
45
- // o is a number, an options object or undefined; o?.x reads all three.
46
- const C = o?.LatLng !== undefined ? o.LatLng : globalThis.google?.maps?.LatLng;
47
- const pts = decode(encodedPath, latFirst(o)), nw = o?.noClampNoWrap;
48
- for (let i = 0; i < pts.length; i++) {
49
- const [lat, lng] = pts[i];
50
- pts[i] = C ? new C(lat, lng, nw) : { lat, lng };
51
- }
52
- return pts;
53
- }
54
-
55
- /**
56
- * Encodes an array or MVCArray of google.maps.LatLng objects, LatLngLiteral
57
- * objects or [lat, lng] tuples (mixed is fine). Options: polytini's
58
- * (precision, strict, factor, dimensions, and order for tuples and LatLng
59
- * objects; literals are read by name).
60
- */
61
- export function encodePath(path, o) {
62
- if (path && typeof path.getArray == 'function') path = path.getArray();
63
- if (Array.isArray(path)) {
64
- const sw = o?.order == 'lnglat';
65
- // LatLng objects have lat() and lng() methods; the core reads literals
66
- // and tuples directly.
67
- path = path.map((p) => (p && typeof p.lat == 'function' ? (sw ? [p.lng(), p.lat()] : [p.lat(), p.lng()]) : p));
68
- }
69
- return encode(path, o);
70
- }
1
+ // Google Maps JavaScript API helpers: decodePath and encodePath with the
2
+ // signatures of google.maps.geometry.encoding, without loading the geometry
3
+ // library.
4
+ //
5
+ // import { decodePath, encodePath } from '@duoquote/polytini/google';
6
+ // const path = decodePath(route.polyline.encodedPolyline);
7
+ // new google.maps.Polyline({ path, map });
8
+ // encodePath(polyline.getPath()); // an MVCArray<LatLng>
9
+ //
10
+ // Differences from google.maps.geometry.encoding:
11
+ // - Both take an optional second argument: polytini's precision (a number)
12
+ // or options object. Precision defaults to 5, Google's fixed value.
13
+ // - decodePath returns google.maps.LatLng objects when a LatLng constructor
14
+ // is available (the LatLng option, else globalThis.google.maps.LatLng),
15
+ // and plain { lat, lng } literals otherwise, so it also works in Node or
16
+ // before the Maps API has loaded. Pass `LatLng: false` for literals even
17
+ // when the API is loaded. Polyline, Polygon and most Maps API methods
18
+ // take literals as well as LatLng, and literals are cheaper to create.
19
+ // - Strict by default: malformed strings and invalid coordinates throw a
20
+ // PolylineError. Google documents no error behavior for decodePath or
21
+ // encodePath, so what they return for such input can't be matched; a
22
+ // thrown error is easier to find than a wrong line on the map, and valid
23
+ // input gives the same result either way. Pass `strict: false` to never
24
+ // throw on bad characters or truncated strings (invalid characters are
25
+ // skipped and an incomplete last point is dropped) or on invalid
26
+ // coordinates (encoded as 0).
27
+ // - encodePath also takes polytini's [lat, lng] tuples and encodes
28
+ // LatLngLiteral values as given, without clamping or wrapping them.
29
+ /* @ts-self-types="../types/google.jsr.d.ts" */
30
+ import { decode, encode } from './web.js';
31
+
32
+ export { PolylineError } from './web.js';
33
+
34
+ // decodePath reads the tuples as [lat, lng], so it ignores order 'lnglat'.
35
+ const latFirst = (o) => (o?.order == 'lnglat' ? { ...o, order: 'latlng' } : o);
36
+
37
+ /**
38
+ * Decodes an encoded path to google.maps.LatLng objects, or to LatLngLiteral
39
+ * objects when no LatLng constructor is available. Options: polytini's
40
+ * (precision, strict, factor, dimensions), plus `LatLng` (a constructor, or
41
+ * false for literals) and `noClampNoWrap` (passed to the LatLng constructor;
42
+ * without it, Google's LatLng clamps latitude and wraps longitude 180 to -180).
43
+ */
44
+ export function decodePath(encodedPath, o) {
45
+ // o is a number, an options object or undefined; o?.x reads all three.
46
+ const C = o?.LatLng !== undefined ? o.LatLng : globalThis.google?.maps?.LatLng;
47
+ const pts = decode(encodedPath, latFirst(o)), nw = o?.noClampNoWrap;
48
+ for (let i = 0; i < pts.length; i++) {
49
+ const [lat, lng] = pts[i];
50
+ pts[i] = C ? new C(lat, lng, nw) : { lat, lng };
51
+ }
52
+ return pts;
53
+ }
54
+
55
+ /**
56
+ * Encodes an array or MVCArray of google.maps.LatLng objects, LatLngLiteral
57
+ * objects or [lat, lng] tuples (mixed is fine). Options: polytini's
58
+ * (precision, strict, factor, dimensions, and order for tuples and LatLng
59
+ * objects; literals are read by name).
60
+ */
61
+ export function encodePath(path, o) {
62
+ if (path && typeof path.getArray == 'function') path = path.getArray();
63
+ if (Array.isArray(path)) {
64
+ const sw = o?.order == 'lnglat';
65
+ // LatLng objects have lat() and lng() methods; the core reads literals
66
+ // and tuples directly.
67
+ path = path.map((p) => (p && typeof p.lat == 'function' ? (sw ? [p.lng(), p.lat()] : [p.lat(), p.lng()]) : p));
68
+ }
69
+ return encode(path, o);
70
+ }
@@ -1,11 +1,11 @@
1
- // Side-effect import: patches the global L (Leaflet's UMD build sets it).
2
- // import 'leaflet';
3
- // import '@duoquote/polytini/leaflet/auto';
4
- // For ES module imports of Leaflet, and Leaflet 2, call install(L) from
5
- // @duoquote/polytini/leaflet instead.
6
- /* @ts-self-types="../types/leaflet-auto.jsr.d.ts" */
7
- import { install } from './leaflet.js';
8
-
9
- const L = globalThis.L;
10
- if (!L?.Polyline) throw new Error('@duoquote/polytini/leaflet/auto: load Leaflet first, or call install(L) from @duoquote/polytini/leaflet');
11
- install(L);
1
+ // Side-effect import: patches the global L (Leaflet's UMD build sets it).
2
+ // import 'leaflet';
3
+ // import '@duoquote/polytini/leaflet/auto';
4
+ // For ES module imports of Leaflet, and Leaflet 2, call install(L) from
5
+ // @duoquote/polytini/leaflet instead.
6
+ /* @ts-self-types="../types/leaflet-auto.jsr.d.ts" */
7
+ import { install } from './leaflet.js';
8
+
9
+ const L = globalThis.L;
10
+ if (!L?.Polyline) throw new Error('@duoquote/polytini/leaflet/auto: load Leaflet first, or call install(L) from @duoquote/polytini/leaflet');
11
+ install(L);
package/src/leaflet.js CHANGED
@@ -1,44 +1,44 @@
1
- // Leaflet plugin: encoded polylines in and out of L.Polyline and L.Polygon.
2
- //
3
- // import L from 'leaflet';
4
- // import { install } from '@duoquote/polytini/leaflet';
5
- // install(L);
6
- // L.Polyline.fromEncoded('_p~iF~ps|U_ulLnnqC_mqNvxq`@', { color: 'red' }).addTo(map);
7
- //
8
- // Works with Leaflet 1.9 (the global L, the UMD build or ESM imports) and the
9
- // Leaflet 2 betas, where L is a frozen module namespace of ES classes: there
10
- // the static and prototype methods are added, and the lower-case factories
11
- // are skipped because the namespace can't take new properties.
12
- //
13
- // It also covers the API of the old Polyline.encoded.js plugin (npm
14
- // polyline-encoded): L.Polyline.fromEncoded, L.Polygon.fromEncoded,
15
- // encodePath() and L.PolylineUtil, so it can replace it.
16
- /* @ts-self-types="../types/leaflet.jsr.d.ts" */
17
- import { encode, decode } from './web.js';
18
-
19
- // One string decodes to one line or ring, arrays nest: string[] is a
20
- // multi-polyline or a polygon with holes, string[][] a multi-polygon.
21
- const toLatLngs = (s, o) => (typeof s == 'string' ? decode(s, o) : s.map((x) => toLatLngs(x, o)));
22
-
23
- // The reverse, from getLatLngs(): LatLng[] encodes to a string, nested arrays
24
- // to nested arrays of strings.
25
- const toStrings = (l, o) => (Array.isArray(l[0]) ? l.map((x) => toStrings(x, o)) : encode(l.map((p) => [p.lat, p.lng]), o));
26
-
27
- export function install(L) {
28
- const { Polyline, Polygon } = L;
29
- const add = (C, name) => {
30
- C.fromEncoded = (s, o) => new C(toLatLngs(s, o), o);
31
- C.prototype.toEncoded = function (o) {
32
- return toStrings(this.getLatLngs(), o);
33
- };
34
- // Polyline.encoded.js name.
35
- C.prototype.encodePath = C.prototype.toEncoded;
36
- if (Object.isExtensible(L)) L[name] = C.fromEncoded;
37
- };
38
- add(Polyline, 'polylineEncoded');
39
- add(Polygon, 'polygonEncoded');
40
- if (Object.isExtensible(L)) L.PolylineUtil ??= { encode, decode };
41
- return L;
42
- }
43
-
44
- export { encode, decode };
1
+ // Leaflet plugin: encoded polylines in and out of L.Polyline and L.Polygon.
2
+ //
3
+ // import L from 'leaflet';
4
+ // import { install } from '@duoquote/polytini/leaflet';
5
+ // install(L);
6
+ // L.Polyline.fromEncoded('_p~iF~ps|U_ulLnnqC_mqNvxq`@', { color: 'red' }).addTo(map);
7
+ //
8
+ // Works with Leaflet 1.9 (the global L, the UMD build or ESM imports) and the
9
+ // Leaflet 2 betas, where L is a frozen module namespace of ES classes: there
10
+ // the static and prototype methods are added, and the lower-case factories
11
+ // are skipped because the namespace can't take new properties.
12
+ //
13
+ // It also covers the API of the old Polyline.encoded.js plugin (npm
14
+ // polyline-encoded): L.Polyline.fromEncoded, L.Polygon.fromEncoded,
15
+ // encodePath() and L.PolylineUtil, so it can replace it.
16
+ /* @ts-self-types="../types/leaflet.jsr.d.ts" */
17
+ import { encode, decode } from './web.js';
18
+
19
+ // One string decodes to one line or ring, arrays nest: string[] is a
20
+ // multi-polyline or a polygon with holes, string[][] a multi-polygon.
21
+ const toLatLngs = (s, o) => (typeof s == 'string' ? decode(s, o) : s.map((x) => toLatLngs(x, o)));
22
+
23
+ // The reverse, from getLatLngs(): LatLng[] encodes to a string, nested arrays
24
+ // to nested arrays of strings.
25
+ const toStrings = (l, o) => (Array.isArray(l[0]) ? l.map((x) => toStrings(x, o)) : encode(l.map((p) => [p.lat, p.lng]), o));
26
+
27
+ export function install(L) {
28
+ const { Polyline, Polygon } = L;
29
+ const add = (C, name) => {
30
+ C.fromEncoded = (s, o) => new C(toLatLngs(s, o), o);
31
+ C.prototype.toEncoded = function (o) {
32
+ return toStrings(this.getLatLngs(), o);
33
+ };
34
+ // Polyline.encoded.js name.
35
+ C.prototype.encodePath = C.prototype.toEncoded;
36
+ if (Object.isExtensible(L)) L[name] = C.fromEncoded;
37
+ };
38
+ add(Polyline, 'polylineEncoded');
39
+ add(Polygon, 'polygonEncoded');
40
+ if (Object.isExtensible(L)) L.PolylineUtil ??= { encode, decode };
41
+ return L;
42
+ }
43
+
44
+ export { encode, decode };
package/src/mapbox.js CHANGED
@@ -1,10 +1,10 @@
1
- // Mapbox GL JS 3: the same functions as the MapLibre module. The calls they
2
- // make (addSource with a GeoJSON source specification, addLayer with a line
3
- // layer and a beforeId, getSource(id).setData) take the same arguments in
4
- // both libraries, so only the types differ: types/mapbox.d.ts uses
5
- // mapbox-gl's.
6
- //
7
- // import { addPolylineLayer } from '@duoquote/polytini/mapbox';
8
- // map.on('load', () => addPolylineLayer(map, 'route', encoded, { layer: { paint: { 'line-color': '#c00' } } }));
9
- /* @ts-self-types="../types/mapbox.d.ts" */
10
- export { polylineData, polylineSource, addPolylineLayer, setPolylineData } from './maplibre.js';
1
+ // Mapbox GL JS 3: the same functions as the MapLibre module. The calls they
2
+ // make (addSource with a GeoJSON source specification, addLayer with a line
3
+ // layer and a beforeId, getSource(id).setData) take the same arguments in
4
+ // both libraries, so only the types differ: types/mapbox.d.ts uses
5
+ // mapbox-gl's.
6
+ //
7
+ // import { addPolylineLayer } from '@duoquote/polytini/mapbox';
8
+ // map.on('load', () => addPolylineLayer(map, 'route', encoded, { layer: { paint: { 'line-color': '#c00' } } }));
9
+ /* @ts-self-types="../types/mapbox.jsr.d.ts" */
10
+ export { polylineData, polylineSource, addPolylineLayer, setPolylineData } from './maplibre.js';
package/src/maplibre.js CHANGED
@@ -1,57 +1,57 @@
1
- // MapLibre GL JS (and Mapbox GL JS, see mapbox.js): encoded polylines as
2
- // GeoJSON sources and line layers.
3
- //
4
- // import { addPolylineLayer, setPolylineData } from '@duoquote/polytini/maplibre';
5
- // map.on('load', () => {
6
- // addPolylineLayer(map, 'route', route.geometry, { precision: 6, layer: { paint: { 'line-width': 4 } } });
7
- // });
8
- // setPolylineData(map, 'route', newRoute.geometry, { precision: 6 });
9
- //
10
- // No map library code is imported: the functions only call addSource,
11
- // addLayer and getSource(id).setData on the map they are given, which
12
- // MapLibre GL 4 to 6 and Mapbox GL 3 all have with the same arguments.
13
- // Coordinates come out as [lng, lat] like all GeoJSON, whatever `order` the
14
- // options or a preset say.
15
- /* @ts-self-types="../types/maplibre.d.ts" */
16
- import { toFeatureCollection } from './turf.js';
17
- import { fail } from './core.js';
18
-
19
- const list = (encoded) => (typeof encoded == 'string' ? [encoded] : encoded);
20
-
21
- /**
22
- * The FeatureCollection<LineString> for one encoded string or many.
23
- * Options: polytini options or a preset, plus `properties` and `id` (one
24
- * value for all lines or an array with one per line) and `bbox`.
25
- */
26
- export const polylineData = (encoded, options) => toFeatureCollection(list(encoded), options);
27
-
28
- /**
29
- * A GeoJSON source specification, `{ type: 'geojson', data }`, for addSource
30
- * or a style's `sources`. `options.source` adds source options such as
31
- * lineMetrics, tolerance or promoteId.
32
- */
33
- export const polylineSource = (encoded, options) => ({ ...options?.source, type: 'geojson', data: polylineData(encoded, options) });
34
-
35
- /**
36
- * Adds a GeoJSON source and a line layer that draws it, both named `id`.
37
- * `options.layer` is merged into the layer (paint, layout, filter, minzoom,
38
- * a different id, and so on), and `options.beforeId` is passed to addLayer.
39
- * Returns the map.
40
- */
41
- export function addPolylineLayer(map, id, encoded, options) {
42
- map.addSource(id, polylineSource(encoded, options));
43
- map.addLayer({ id, type: 'line', source: id, ...options?.layer }, options?.beforeId);
44
- return map;
45
- }
46
-
47
- /**
48
- * Replaces the data of the GeoJSON source `id` with new lines and returns
49
- * what the source's setData returns (a Promise in MapLibre, the source in
50
- * Mapbox). Throws a PolylineError with code 'input' if there is no such
51
- * GeoJSON source.
52
- */
53
- export function setPolylineData(map, id, encoded, options) {
54
- const source = map.getSource(id);
55
- if (typeof source?.setData != 'function') fail('input', 'No GeoJSON source named ' + id);
56
- return source.setData(polylineData(encoded, options));
57
- }
1
+ // MapLibre GL JS (and Mapbox GL JS, see mapbox.js): encoded polylines as
2
+ // GeoJSON sources and line layers.
3
+ //
4
+ // import { addPolylineLayer, setPolylineData } from '@duoquote/polytini/maplibre';
5
+ // map.on('load', () => {
6
+ // addPolylineLayer(map, 'route', route.geometry, { precision: 6, layer: { paint: { 'line-width': 4 } } });
7
+ // });
8
+ // setPolylineData(map, 'route', newRoute.geometry, { precision: 6 });
9
+ //
10
+ // No map library code is imported: the functions only call addSource,
11
+ // addLayer and getSource(id).setData on the map they are given, which
12
+ // MapLibre GL 4 to 6 and Mapbox GL 3 all have with the same arguments.
13
+ // Coordinates come out as [lng, lat] like all GeoJSON, whatever `order` the
14
+ // options or a preset say.
15
+ /* @ts-self-types="../types/maplibre.jsr.d.ts" */
16
+ import { toFeatureCollection } from './turf.js';
17
+ import { fail } from './core.js';
18
+
19
+ const list = (encoded) => (typeof encoded == 'string' ? [encoded] : encoded);
20
+
21
+ /**
22
+ * The FeatureCollection<LineString> for one encoded string or many.
23
+ * Options: polytini options or a preset, plus `properties` and `id` (one
24
+ * value for all lines or an array with one per line) and `bbox`.
25
+ */
26
+ export const polylineData = (encoded, options) => toFeatureCollection(list(encoded), options);
27
+
28
+ /**
29
+ * A GeoJSON source specification, `{ type: 'geojson', data }`, for addSource
30
+ * or a style's `sources`. `options.source` adds source options such as
31
+ * lineMetrics, tolerance or promoteId.
32
+ */
33
+ export const polylineSource = (encoded, options) => ({ ...options?.source, type: 'geojson', data: polylineData(encoded, options) });
34
+
35
+ /**
36
+ * Adds a GeoJSON source and a line layer that draws it, both named `id`.
37
+ * `options.layer` is merged into the layer (paint, layout, filter, minzoom,
38
+ * a different id, and so on), and `options.beforeId` is passed to addLayer.
39
+ * Returns the map.
40
+ */
41
+ export function addPolylineLayer(map, id, encoded, options) {
42
+ map.addSource(id, polylineSource(encoded, options));
43
+ map.addLayer({ id, type: 'line', source: id, ...options?.layer }, options?.beforeId);
44
+ return map;
45
+ }
46
+
47
+ /**
48
+ * Replaces the data of the GeoJSON source `id` with new lines and returns
49
+ * what the source's setData returns (a Promise in MapLibre, the source in
50
+ * Mapbox). Throws a PolylineError with code 'input' if there is no such
51
+ * GeoJSON source.
52
+ */
53
+ export function setPolylineData(map, id, encoded, options) {
54
+ const source = map.getSource(id);
55
+ if (typeof source?.setData != 'function') fail('input', 'No GeoJSON source named ' + id);
56
+ return source.setData(polylineData(encoded, options));
57
+ }
package/src/node.js CHANGED
@@ -1,17 +1,17 @@
1
- // Node entry: the core plus the Buffer fast paths in fast.js.
2
- /* @ts-self-types="../types/index.d.ts" */
3
- import { Buffer } from 'node:buffer';
4
- import { fast } from './fast.js';
5
- import { latLngs, toGeo, fromGeo } from './core.js';
6
-
7
- export { PolylineError } from './core.js';
8
- // On V8 12 (Node 22 and 23), a pre-sized array of 100,000 points made decode
9
- // about 40% slower than appending. Probably because the big array lands in
10
- // old space and every young point stored into it goes through the write
11
- // barrier, but I haven't profiled the GC to confirm it. V8 13
12
- // and 14 (Node 24 on, and what Bun reports) were 25% faster pre-sized, so
13
- // only V8 12 appends, from 32,768 points.
14
- export const { decode, decodeFlat, encode, encodeFlat } = fast(Buffer, process.versions.v8?.startsWith('12.') ? 32768 : Infinity);
15
- export const decodeLatLng = latLngs(decode);
16
- export const toGeoJSON = toGeo(decode);
17
- export const fromGeoJSON = fromGeo(encode);
1
+ // Node entry: the core plus the Buffer fast paths in fast.js.
2
+ /* @ts-self-types="../types/index.d.ts" */
3
+ import { Buffer } from 'node:buffer';
4
+ import { fast } from './fast.js';
5
+ import { latLngs, toGeo, fromGeo } from './core.js';
6
+
7
+ export { PolylineError } from './core.js';
8
+ // On V8 12 (Node 22 and 23), a pre-sized array of 100,000 points made decode
9
+ // about 40% slower than appending. Probably because the big array lands in
10
+ // old space and every young point stored into it goes through the write
11
+ // barrier, but I haven't profiled the GC to confirm it. V8 13
12
+ // and 14 (Node 24 on, and what Bun reports) were 25% faster pre-sized, so
13
+ // only V8 12 appends, from 32,768 points.
14
+ export const { decode, decodeFlat, encode, encodeFlat } = fast(Buffer, process.versions.v8?.startsWith('12.') ? 32768 : Infinity);
15
+ export const decodeLatLng = latLngs(decode);
16
+ export const toGeoJSON = toGeo(decode);
17
+ export const fromGeoJSON = fromGeo(encode);