@duoquote/polytini 0.0.0-stage → 0.1.0

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 (70) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +120 -3
  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 +60 -0
  15. package/dist/types/flexible.d.cts +80 -0
  16. package/dist/types/google.d.cts +48 -0
  17. package/dist/types/google.jsr.d.cts +60 -0
  18. package/dist/types/index.d.cts +87 -0
  19. package/dist/types/leaflet-auto.d.cts +4 -0
  20. package/dist/types/leaflet-auto.jsr.d.cts +2 -0
  21. package/dist/types/leaflet.d.cts +39 -0
  22. package/dist/types/leaflet.jsr.d.cts +13 -0
  23. package/dist/types/mapbox.d.cts +50 -0
  24. package/dist/types/maplibre.d.cts +54 -0
  25. package/dist/types/openlayers.d.cts +23 -0
  26. package/dist/types/presets.d.cts +66 -0
  27. package/dist/types/turf.d.cts +33 -0
  28. package/dist/types/wasm.d.cts +33 -0
  29. package/dist/web.cjs +231 -0
  30. package/package.json +200 -6
  31. package/src/browser.js +15 -0
  32. package/src/bun.js +4 -0
  33. package/src/cli-lib.js +160 -0
  34. package/src/cli.js +7 -0
  35. package/src/core.js +268 -0
  36. package/src/deckgl.js +176 -0
  37. package/src/deno.js +12 -0
  38. package/src/error.js +15 -0
  39. package/src/fast.js +179 -0
  40. package/src/flexible.js +283 -0
  41. package/src/google.js +70 -0
  42. package/src/leaflet-auto.js +11 -0
  43. package/src/leaflet.js +44 -0
  44. package/src/mapbox.js +10 -0
  45. package/src/maplibre.js +57 -0
  46. package/src/node.js +17 -0
  47. package/src/openlayers.js +172 -0
  48. package/src/presets.js +242 -0
  49. package/src/text.js +111 -0
  50. package/src/turf.js +80 -0
  51. package/src/wasm-bytes.js +2 -0
  52. package/src/wasm.js +235 -0
  53. package/src/web.js +9 -0
  54. package/types/cli.d.ts +2 -0
  55. package/types/deckgl.d.ts +60 -0
  56. package/types/flexible.d.ts +80 -0
  57. package/types/google.d.ts +48 -0
  58. package/types/google.jsr.d.ts +60 -0
  59. package/types/index.d.ts +87 -0
  60. package/types/leaflet-auto.d.ts +4 -0
  61. package/types/leaflet-auto.jsr.d.ts +2 -0
  62. package/types/leaflet.d.ts +39 -0
  63. package/types/leaflet.jsr.d.ts +13 -0
  64. package/types/mapbox.d.ts +50 -0
  65. package/types/maplibre.d.ts +54 -0
  66. package/types/openlayers.d.ts +23 -0
  67. package/types/presets.d.ts +66 -0
  68. package/types/turf.d.ts +33 -0
  69. package/types/wasm.d.ts +33 -0
  70. package/wasm/polytini.wasm +0 -0
@@ -0,0 +1,39 @@
1
+ // Types for @duoquote/polytini/leaflet. Leaflet's own types come from
2
+ // @types/leaflet (1.9) or leaflet's bundled types (2.x); this only adds the
3
+ // plugin's methods.
4
+ import type * as P from './index.d.ts';
5
+ import type { OptionsArg } from './index.d.ts';
6
+ import type { Polyline as LeafletPolyline, Polygon as LeafletPolygon } from 'leaflet';
7
+
8
+ /** One encoded line or ring, or nested arrays of them (multi-polylines, holes, multi-polygons). */
9
+ export type Encoded = string | Encoded[];
10
+
11
+ /** Leaflet path options plus polytini's precision, factor, strict and dimensions. */
12
+ export type EncodedOptions = Record<string, unknown> & Exclude<OptionsArg, number>;
13
+
14
+ /** Adds fromEncoded, toEncoded, encodePath and the polylineEncoded/polygonEncoded factories. Returns L. */
15
+ export function install<T>(L: T): T;
16
+
17
+ export declare const encode: typeof P.encode;
18
+ export declare const decode: typeof P.decode;
19
+
20
+ declare module 'leaflet' {
21
+ namespace Polyline {
22
+ /** A polyline from an encoded string, or a multi-polyline from an array of strings. */
23
+ // Generic with a default so Polygon's static side still extends Polyline's.
24
+ function fromEncoded<T = LeafletPolyline>(encoded: Encoded, options?: EncodedOptions): T;
25
+ }
26
+ namespace Polygon {
27
+ /** A polygon from one string, rings (outer, then holes) from string[], a multi-polygon from string[][]. */
28
+ function fromEncoded<T = LeafletPolygon>(encoded: Encoded, options?: EncodedOptions): T;
29
+ }
30
+ interface Polyline {
31
+ /** A string for a simple line, string[] for a multi-polyline. Polygons give string[] (rings) or string[][]. */
32
+ toEncoded(options?: OptionsArg): Encoded;
33
+ /** Alias of toEncoded, the Polyline.encoded.js name. */
34
+ encodePath(options?: OptionsArg): Encoded;
35
+ }
36
+ function polylineEncoded(encoded: Encoded, options?: EncodedOptions): LeafletPolyline;
37
+ function polygonEncoded(encoded: Encoded, options?: EncodedOptions): LeafletPolygon;
38
+ const PolylineUtil: { encode: typeof P.encode; decode: typeof P.decode };
39
+ }
@@ -0,0 +1,13 @@
1
+ // Types for @duoquote/polytini/leaflet on JSR. JSR doesn't allow module
2
+ // augmentation, so this has the plugin's own exports but not the methods it
3
+ // adds to Leaflet's classes. The npm package's leaflet.d.ts has those.
4
+ import type * as P from './index.d.ts';
5
+
6
+ /** One encoded line or ring, or nested arrays of them (multi-polylines, holes, multi-polygons). */
7
+ export type Encoded = string | Encoded[];
8
+
9
+ /** Adds fromEncoded, toEncoded, encodePath and the polylineEncoded/polygonEncoded factories. Returns L. */
10
+ export function install<T>(L: T): T;
11
+
12
+ export declare const encode: typeof P.encode;
13
+ export declare const decode: typeof P.decode;
@@ -0,0 +1,50 @@
1
+ // Types for @duoquote/polytini/mapbox: the same functions as the maplibre
2
+ // module, typed with mapbox-gl's own types (type-only imports). mapbox-gl's
3
+ // types use the GeoJSON namespace from @types/geojson.
4
+ import type { Map, GeoJSONSource, GeoJSONSourceSpecification, LineLayerSpecification } from 'mapbox-gl';
5
+ import type { FeatureCollection, LineString, GeoJsonProperties } from 'geojson';
6
+ import type { Options } from './index.d.ts';
7
+
8
+ export type { GeoJSONSourceSpecification, LineLayerSpecification };
9
+
10
+ // Repeated from maplibre.d.ts so this file does not need maplibre-gl installed.
11
+ export type FeatureId = string | number;
12
+
13
+ export interface DataOptions<P = GeoJsonProperties> extends Options {
14
+ /** Properties for every line, or an array with one object per line. Default {}. */
15
+ properties?: P | P[];
16
+ /** Feature id for every line, or an array with one per line. Feature state needs numeric ids or promoteId. */
17
+ id?: FeatureId | FeatureId[];
18
+ /** Add a [west, south, east, north] bbox to each feature. Default false. */
19
+ bbox?: boolean;
20
+ }
21
+
22
+ export interface SourceOptions<P = GeoJsonProperties> extends DataOptions<P> {
23
+ /** Other source options: lineMetrics, tolerance, promoteId, generateId, attribution, and so on. */
24
+ source?: Partial<Omit<GeoJSONSourceSpecification, 'type' | 'data'>>;
25
+ }
26
+
27
+ export interface LayerOptions<P = GeoJsonProperties> extends SourceOptions<P> {
28
+ /** Merged into the line layer: paint, layout, filter, slot, minzoom, maxzoom, or another id. */
29
+ layer?: Partial<Omit<LineLayerSpecification, 'type' | 'source'>>;
30
+ /** The id of an existing layer to insert the new layer before. */
31
+ beforeId?: string;
32
+ }
33
+
34
+ /** A GeoJSON source specification whose data is a FeatureCollection of LineStrings. */
35
+ export type PolylineSource<P = GeoJsonProperties> = GeoJSONSourceSpecification & {
36
+ type: 'geojson';
37
+ data: FeatureCollection<LineString, P>;
38
+ };
39
+
40
+ /** A FeatureCollection<LineString> ([lng, lat]) with one Feature per encoded string. */
41
+ export function polylineData<P = GeoJsonProperties>(encoded: string | string[], options?: number | DataOptions<P>): FeatureCollection<LineString, P>;
42
+
43
+ /** `{ type: 'geojson', data }` for map.addSource or a style's sources. */
44
+ export function polylineSource<P = GeoJsonProperties>(encoded: string | string[], options?: number | SourceOptions<P>): PolylineSource<P>;
45
+
46
+ /** Adds a GeoJSON source and a line layer, both named `id`. Returns the map. */
47
+ export function addPolylineLayer<M extends Map, P = GeoJsonProperties>(map: M, id: string, encoded: string | string[], options?: number | LayerOptions<P>): M;
48
+
49
+ /** Replaces the data of the GeoJSON source `id`. Returns what setData returns (the source). Throws a PolylineError if there is no such GeoJSON source. */
50
+ export function setPolylineData<P = GeoJsonProperties>(map: Map, id: string, encoded: string | string[], options?: number | DataOptions<P>): ReturnType<GeoJSONSource['setData']>;
@@ -0,0 +1,54 @@
1
+ // Types for @duoquote/polytini/maplibre, built from maplibre-gl's own types
2
+ // (type-only imports, so nothing is loaded at runtime). The source and layer
3
+ // types are taken from Map#addSource and Map#addLayer, so they follow the
4
+ // installed maplibre-gl version.
5
+ import type { Map, GeoJSONSource } from 'maplibre-gl';
6
+ import type { FeatureCollection, LineString, GeoJsonProperties } from 'geojson';
7
+ import type { Options } from './index.d.ts';
8
+
9
+ /** MapLibre's GeoJSON source specification. */
10
+ export type GeoJSONSourceSpecification = Extract<Parameters<Map['addSource']>[1], { type: 'geojson' }>;
11
+
12
+ /** MapLibre's line layer specification. */
13
+ export type LineLayerSpecification = Extract<Parameters<Map['addLayer']>[0], { type: 'line' }>;
14
+
15
+ export type FeatureId = string | number;
16
+
17
+ export interface DataOptions<P = GeoJsonProperties> extends Options {
18
+ /** Properties for every line, or an array with one object per line. Default {}. */
19
+ properties?: P | P[];
20
+ /** Feature id for every line, or an array with one per line. MapLibre's feature state needs numeric ids or promoteId. */
21
+ id?: FeatureId | FeatureId[];
22
+ /** Add a [west, south, east, north] bbox to each feature. Default false. */
23
+ bbox?: boolean;
24
+ }
25
+
26
+ export interface SourceOptions<P = GeoJsonProperties> extends DataOptions<P> {
27
+ /** Other source options: lineMetrics, tolerance, promoteId, generateId, attribution, and so on. */
28
+ source?: Partial<Omit<GeoJSONSourceSpecification, 'type' | 'data'>>;
29
+ }
30
+
31
+ export interface LayerOptions<P = GeoJsonProperties> extends SourceOptions<P> {
32
+ /** Merged into the line layer: paint, layout, filter, minzoom, maxzoom, metadata, or another id. */
33
+ layer?: Partial<Omit<LineLayerSpecification, 'type' | 'source'>>;
34
+ /** The id of an existing layer to insert the new layer before. */
35
+ beforeId?: string;
36
+ }
37
+
38
+ /** A GeoJSON source specification whose data is a FeatureCollection of LineStrings. */
39
+ export type PolylineSource<P = GeoJsonProperties> = GeoJSONSourceSpecification & {
40
+ type: 'geojson';
41
+ data: FeatureCollection<LineString, P>;
42
+ };
43
+
44
+ /** A FeatureCollection<LineString> ([lng, lat]) with one Feature per encoded string. */
45
+ export function polylineData<P = GeoJsonProperties>(encoded: string | string[], options?: number | DataOptions<P>): FeatureCollection<LineString, P>;
46
+
47
+ /** `{ type: 'geojson', data }` for map.addSource or a style's sources. */
48
+ export function polylineSource<P = GeoJsonProperties>(encoded: string | string[], options?: number | SourceOptions<P>): PolylineSource<P>;
49
+
50
+ /** Adds a GeoJSON source and a line layer, both named `id`. Returns the map. */
51
+ export function addPolylineLayer<M extends Map, P = GeoJsonProperties>(map: M, id: string, encoded: string | string[], options?: number | LayerOptions<P>): M;
52
+
53
+ /** Replaces the data of the GeoJSON source `id`. Returns setData's Promise. Throws a PolylineError if there is no such GeoJSON source. */
54
+ export function setPolylineData<P = GeoJsonProperties>(map: Map, id: string, encoded: string | string[], options?: number | DataOptions<P>): ReturnType<GeoJSONSource['setData']>;
@@ -0,0 +1,23 @@
1
+ // Types for @duoquote/polytini/openlayers. Type-only imports: nothing from
2
+ // ol is loaded at runtime, you pass its classes to createPolylineFormat.
3
+ import type OlPolyline from 'ol/format/Polyline.js';
4
+ import type { Options } from 'ol/format/Polyline.js';
5
+ import type OlLineString from 'ol/geom/LineString.js';
6
+
7
+ export type { Options };
8
+
9
+ /** ol's classes, as imported from ol/format/Polyline.js and ol/geom/LineString.js. */
10
+ export interface OpenLayersClasses {
11
+ Polyline: typeof OlPolyline;
12
+ LineString: typeof OlLineString;
13
+ }
14
+
15
+ /**
16
+ * Makes a drop-in for ol/format/Polyline: the same constructor options
17
+ * (factor, geometryLayout), methods and output, faster. Unlike ol's,
18
+ * writeGeometry never changes the geometry it is given. Also takes the `ol`
19
+ * namespace of the full build ({ format: { Polyline }, geom: { LineString } }).
20
+ */
21
+ export function createPolylineFormat(
22
+ ol: OpenLayersClasses | { format: { Polyline: typeof OlPolyline }; geom: { LineString: typeof OlLineString } },
23
+ ): typeof OlPolyline;
@@ -0,0 +1,66 @@
1
+ // Option presets for routing engines that use the Google encoded polyline
2
+ // format. Each one is a plain options object for encode, decode,
3
+ // encodeFlat, decodeFlat, toGeoJSON and fromGeoJSON. See src/presets.js for
4
+ // the sources behind every value.
5
+
6
+ /** A 2D preset: precision and the order of the tuples you pass and get back. */
7
+ export interface Preset2D<P extends number = number, O extends 'latlng' | 'lnglat' = 'latlng' | 'lnglat'> {
8
+ readonly precision: P;
9
+ readonly order: O;
10
+ }
11
+
12
+ /** A preset with a third value per point, scaled by `zFactor`. */
13
+ export interface Preset3D<P extends number = number, O extends 'latlng' | 'lnglat' = 'latlng' | 'lnglat'> extends Preset2D<P, O> {
14
+ readonly dimensions: 3;
15
+ readonly zFactor: number;
16
+ }
17
+
18
+ /** Google Maps Platform: precision 5, [lat, lng]. */
19
+ export declare const google: Preset2D<5, 'latlng'>;
20
+ /** OSRM geometries=polyline: precision 5, [lng, lat] like its GeoJSON. */
21
+ export declare const osrm: Preset2D<5, 'lnglat'>;
22
+ /** OSRM geometries=polyline6: precision 6, [lng, lat]. */
23
+ export declare const osrm6: Preset2D<6, 'lnglat'>;
24
+ /** Valhalla shapes: precision 6, [lng, lat] like its format=osrm GeoJSON. */
25
+ export declare const valhalla: Preset2D<6, 'lnglat'>;
26
+ /** Stadia Maps routing (Valhalla): precision 6, [lng, lat]. */
27
+ export declare const stadia: Preset2D<6, 'lnglat'>;
28
+ /**
29
+ * GraphHopper points_encoded=true: precision 5 (the default
30
+ * points_encoded_multiplier), [lng, lat] like points_encoded=false.
31
+ */
32
+ export declare const graphhopper: Preset2D<5, 'lnglat'>;
33
+ /** GraphHopper with elevation=true: [lng, lat, elevation in meters], elevation stored in centimeters. */
34
+ export declare const graphhopper3d: Preset3D<5, 'lnglat'>;
35
+ /** openrouteservice JSON geometry: precision 5, [lng, lat] like its GeoJSON. */
36
+ export declare const openrouteservice: Preset2D<5, 'lnglat'>;
37
+ /** openrouteservice with elevation=true: [lng, lat, elevation in meters], elevation stored in centimeters. */
38
+ export declare const openrouteservice3d: Preset3D<5, 'lnglat'>;
39
+ /** Mapbox Directions geometries=polyline: precision 5, [lng, lat]. */
40
+ export declare const mapbox: Preset2D<5, 'lnglat'>;
41
+ /** Mapbox Directions geometries=polyline6: precision 6, [lng, lat]. */
42
+ export declare const mapbox6: Preset2D<6, 'lnglat'>;
43
+ /** Strava map.polyline and summary_polyline: precision 5, [lat, lng] like its latlng stream. */
44
+ export declare const strava: Preset2D<5, 'latlng'>;
45
+ /** OpenTripPlanner legGeometry.points: precision 5, [lat, lng]. */
46
+ export declare const otp: Preset2D<5, 'latlng'>;
47
+
48
+ /** Every preset, keyed by its export name. */
49
+ export declare const presets: {
50
+ readonly google: typeof google;
51
+ readonly osrm: typeof osrm;
52
+ readonly osrm6: typeof osrm6;
53
+ readonly valhalla: typeof valhalla;
54
+ readonly stadia: typeof stadia;
55
+ readonly graphhopper: typeof graphhopper;
56
+ readonly graphhopper3d: typeof graphhopper3d;
57
+ readonly openrouteservice: typeof openrouteservice;
58
+ readonly openrouteservice3d: typeof openrouteservice3d;
59
+ readonly mapbox: typeof mapbox;
60
+ readonly mapbox6: typeof mapbox6;
61
+ readonly strava: typeof strava;
62
+ readonly otp: typeof otp;
63
+ };
64
+
65
+ /** The name of a preset in `presets`. */
66
+ export type PresetName = keyof typeof presets;
@@ -0,0 +1,33 @@
1
+ // Types for @duoquote/polytini/turf, using Turf's and GeoJSON's own types.
2
+ import type { Feature, FeatureCollection, LineString, Polygon, GeoJsonProperties } from 'geojson';
3
+ import type { AllGeoJSON, Id } from '@turf/helpers';
4
+ import type { Options } from './index.d.ts';
5
+
6
+ export interface FeatureOptions<P = GeoJsonProperties> extends Options {
7
+ /** Feature properties. Default {}. */
8
+ properties?: P;
9
+ /** Feature id. */
10
+ id?: Id;
11
+ /** Add a [west, south, east, north] bbox. Default false. */
12
+ bbox?: boolean;
13
+ }
14
+
15
+ export interface CollectionOptions<P = GeoJsonProperties> extends Options {
16
+ /** Shared properties, or one object per line. */
17
+ properties?: P | P[];
18
+ /** One id per line, or the same for all. */
19
+ id?: Id | Id[];
20
+ bbox?: boolean;
21
+ }
22
+
23
+ /** A Feature<LineString> ([lng, lat], like Turf) from one encoded line. */
24
+ export function toFeature<P = GeoJsonProperties>(encoded: string, options?: number | FeatureOptions<P>): Feature<LineString, P>;
25
+
26
+ /** A FeatureCollection<LineString>, one Feature per string. */
27
+ export function toFeatureCollection<P = GeoJsonProperties>(encoded: string[], options?: number | CollectionOptions<P>): FeatureCollection<LineString, P>;
28
+
29
+ /** A Feature<Polygon> from encoded rings: outer first, then holes. */
30
+ export function toPolygonFeature<P = GeoJsonProperties>(rings: string[], options?: number | FeatureOptions<P>): Feature<Polygon, P>;
31
+
32
+ /** Every line in any GeoJSON: LineStrings, MultiLineString parts and polygon rings, in document order. Points are skipped. */
33
+ export function fromGeoJSON(geojson: AllGeoJSON, options?: number | Options): string[];
@@ -0,0 +1,33 @@
1
+ // The /wasm entry: the same API as the main entry, after init.
2
+ import type * as P from './index.d.ts';
3
+
4
+ export type { Point, LatLngLike, Options, OptionsArg, ErrorCode, LineString, LineStringFeature } from './index.d.ts';
5
+ export declare const encode: typeof P.encode;
6
+ export declare const decode: typeof P.decode;
7
+ export declare const encodeFlat: typeof P.encodeFlat;
8
+ export declare const decodeFlat: typeof P.decodeFlat;
9
+ export declare const decodeLatLng: typeof P.decodeLatLng;
10
+ export declare const toGeoJSON: typeof P.toGeoJSON;
11
+ export declare const fromGeoJSON: typeof P.fromGeoJSON;
12
+ export declare const PolylineError: typeof P.PolylineError;
13
+ export type PolylineError = P.PolylineError;
14
+
15
+ /** Compiles and instantiates the WASM module. Call once before anything else; repeat calls are free. */
16
+ export function init(): Promise<void>;
17
+
18
+ /** The same, synchronously. Fine here because the module is under 1 KB. */
19
+ export function initSync(): void;
20
+
21
+ /** Many lines decoded in one call: all points in one array, plus where each line starts. */
22
+ export interface ManyLines {
23
+ /** lat, lng pairs (lng, lat with order 'lnglat') of every line, back to back. */
24
+ coords: Float64Array;
25
+ /** The first point index of each line, then the total point count (length + 1 entries), as deck.gl's binary paths expect. */
26
+ starts: Uint32Array;
27
+ }
28
+
29
+ /** Decodes many lines in one WASM call. Strict mode throws on the first bad line. */
30
+ export function decodeMany(lines: readonly string[], options?: P.OptionsArg): ManyLines;
31
+
32
+ /** Encodes many lines in one WASM call: arrays of points, or the { coords, starts } shape decodeMany returns. */
33
+ export function encodeMany(lines: readonly (readonly P.Point[])[] | ManyLines, options?: P.OptionsArg): string[];
package/dist/web.cjs ADDED
@@ -0,0 +1,231 @@
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/web.js
20
+ var web_exports = {};
21
+ __export(web_exports, {
22
+ PolylineError: () => PolylineError,
23
+ decode: () => decode,
24
+ decodeFlat: () => decodeFlat,
25
+ decodeLatLng: () => decodeLatLng,
26
+ encode: () => encode,
27
+ encodeFlat: () => encodeFlat,
28
+ fromGeoJSON: () => fromGeoJSON,
29
+ toGeoJSON: () => toGeoJSON
30
+ });
31
+ module.exports = __toCommonJS(web_exports);
32
+
33
+ // src/error.js
34
+ var PolylineError = class extends Error {
35
+ constructor(code, message, index) {
36
+ super(index == null ? message : message + " at " + index);
37
+ this.name = "PolylineError";
38
+ this.code = code;
39
+ this.index = index;
40
+ }
41
+ };
42
+ var fail = (code, message, index) => {
43
+ throw new PolylineError(code, message, index);
44
+ };
45
+
46
+ // src/core.js
47
+ var st = { swap: false, strict: true, dims: 2, zf: 1e5 };
48
+ function opts(o) {
49
+ st.swap = false;
50
+ st.strict = true;
51
+ st.dims = 2;
52
+ return o && typeof o == "object" ? objOpts(o) : st.zf = o == null ? 1e5 : pow(o);
53
+ }
54
+ function objOpts(o) {
55
+ const { order, dimensions: d } = o;
56
+ if (order == "lnglat") st.swap = true;
57
+ else if (order != null && order != "latlng") bad("order");
58
+ if (o.strict === false) st.strict = false;
59
+ if (d != null) st.dims = d >= 1 && d <= 16 && d % 1 == 0 ? d : bad("dimensions");
60
+ const f = o.factor != null ? num(o.factor) : pow(o.precision ?? 5);
61
+ st.zf = o.zFactor != null ? num(o.zFactor) : f;
62
+ return f;
63
+ }
64
+ var bad = (name) => fail("option", "Invalid " + name);
65
+ var num = (f) => f > 0 && f < 1 / 0 ? f : bad("factor");
66
+ var pow = (p) => typeof p == "number" && p >= 0 && p <= 15 && p % 1 == 0 ? 10 ** p : bad("precision");
67
+ var LIMIT = 2 ** 29;
68
+ function slowDecode(str, f, flat) {
69
+ if (typeof str != "string") fail("input", "Expected a string");
70
+ const { dims: d, strict, zf } = st, sw = st.swap && d > 1, values = [];
71
+ let v = 0, scale = 1, i = 0, c;
72
+ for (; i < str.length; i++) {
73
+ c = str.charCodeAt(i) - 63;
74
+ if (c < 0 || c > 63) {
75
+ if (strict) fail("char", "Invalid character", i);
76
+ continue;
77
+ }
78
+ v += (c & 31) * scale;
79
+ scale *= 32;
80
+ if (v > 2 ** 53 && strict) fail("overflow", "Value too large", i);
81
+ if (c < 32) {
82
+ values.push(v % 2 ? -(v + 1) / 2 : v / 2);
83
+ v = 0;
84
+ scale = 1;
85
+ }
86
+ }
87
+ if (strict && (scale > 1 || values.length % d)) fail("truncated", "Truncated", i);
88
+ const n = values.length / d | 0, sum = Array(d).fill(0), out = flat ? new Float64Array(n * d) : Array(n);
89
+ for (let j = 0, k = 0; j < n; j++) {
90
+ const p = flat ? out : Array(d);
91
+ for (let a = 0; a < d; a++, k++) {
92
+ sum[a] += values[k];
93
+ p[(flat ? k - a : 0) + (sw && a < 2 ? 1 - a : a)] = sum[a] / (a < 2 ? f : zf);
94
+ }
95
+ if (!flat) out[j] = p;
96
+ }
97
+ return out;
98
+ }
99
+ function slowEncode(points, f, flat) {
100
+ if (points == null || !(points.length >= 0)) fail("input", "Expected an array");
101
+ const { dims: d, strict, zf } = st, sw = st.swap && d > 1, last = Array(d).fill(0), n = flat ? points.length / d : points.length;
102
+ if (n % 1 && strict) fail("input", "Length not a multiple of " + d);
103
+ let s = "";
104
+ for (let i = 0; i < (n | 0); i++) {
105
+ const p = flat ? 0 : points[i];
106
+ for (let a = 0; a < d; a++) {
107
+ const b = sw && a < 2 ? 1 - a : a;
108
+ let x = flat ? points[i * d + b] : p == null ? p : p.length >= 0 ? p[b] : a ? a > 1 ? p.alt ?? p.altitude : p.lng ?? p.lon ?? p.longitude : p.lat ?? p.latitude;
109
+ x = x == null ? NaN : +x * (a < 2 ? f : zf);
110
+ if (!(x - x == 0)) {
111
+ if (strict) fail("coordinate", "Invalid coordinate", i);
112
+ x = 0;
113
+ }
114
+ x = Math.sign(x) * Math.round(Math.abs(x));
115
+ let z = x - last[a];
116
+ last[a] = x;
117
+ z = z < 0 ? -z * 2 - 1 : z * 2;
118
+ if (z > 2 ** 53 && strict) fail("overflow", "Value too large", i);
119
+ for (; z >= 32; z = Math.floor(z / 32)) s += String.fromCharCode(z % 32 + 95);
120
+ s += String.fromCharCode(z + 63);
121
+ }
122
+ }
123
+ return s;
124
+ }
125
+ var decodeWith = (str, o, flat) => {
126
+ const f = opts(o), out = st.dims == 2 && typeof str == "string" && decodeLoop(str, f, st.swap, flat);
127
+ return out ? st.swap && !flat ? flip(out) : out : slowDecode(str, f, flat);
128
+ };
129
+ var flip = (out) => {
130
+ for (const p of out) {
131
+ const t = p[0];
132
+ p[0] = p[1];
133
+ p[1] = t;
134
+ }
135
+ return out;
136
+ };
137
+ var decode = (str, o) => decodeWith(str, o, 0);
138
+ var decodeFlat = (str, o) => decodeWith(str, o, 1);
139
+ function decodeLoop(str, f, sw, flat) {
140
+ const len = str.length, a = +sw;
141
+ let i = 0, n = 0, lat = 0, lng = 0, bad2 = 0, c, v, s, out;
142
+ if (flat) {
143
+ for (; i < len; i++) n += str.charCodeAt(i) - 95 >>> 31;
144
+ out = new Float64Array(n & ~1);
145
+ i = n = 0;
146
+ } else out = len < 64 ? [] : Array(len >> 1);
147
+ while (i < len) {
148
+ c = str.charCodeAt(i++) - 63;
149
+ bad2 |= c;
150
+ v = c & 31;
151
+ for (s = 5; c >= 32; s += 5) {
152
+ c = str.charCodeAt(i++) - 63;
153
+ bad2 |= c | 25 - s;
154
+ v |= (c & 31) << s;
155
+ }
156
+ lat += v >>> 1 ^ -(v & 1);
157
+ c = str.charCodeAt(i++) - 63;
158
+ bad2 |= c;
159
+ v = c & 31;
160
+ for (s = 5; c >= 32; s += 5) {
161
+ c = str.charCodeAt(i++) - 63;
162
+ bad2 |= c | 25 - s;
163
+ v |= (c & 31) << s;
164
+ }
165
+ lng += v >>> 1 ^ -(v & 1);
166
+ if (flat) {
167
+ out[n + a] = lat / f;
168
+ out[n + 1 - a] = lng / f;
169
+ n += 2;
170
+ } else out[n++] = [lat / f, lng / f];
171
+ }
172
+ if (bad2 & ~63 || i != len) return null;
173
+ if (out.length != n) out.length = n;
174
+ return out;
175
+ }
176
+ var CHARS = /* @__PURE__ */ Array.from({ length: 1024 }, (_, z) => chars(z));
177
+ function chars(z) {
178
+ let s = "";
179
+ for (; z >= 32; z >>>= 5) s += String.fromCharCode((z & 31) + 95);
180
+ return s + String.fromCharCode(z + 63);
181
+ }
182
+ var encodeWith = (src, o, flat) => {
183
+ const f = opts(o);
184
+ return (st.dims == 2 && f <= 1e6 && (flat ? src != null && !(src.length % 2) : Array.isArray(src)) ? encodeLoop(src, f, +st.swap, flat) : null) ?? slowEncode(src, f, flat);
185
+ };
186
+ var encode = (points, o) => encodeWith(points, o, 0);
187
+ var encodeFlat = (values, o) => encodeWith(values, o, 1);
188
+ function encodeLoop(src, f, a, flat) {
189
+ const n = flat ? src.length >> 1 : src.length;
190
+ let s = "", i = 0, la = 0, lb = 0, x, y, p, q, z, pt;
191
+ try {
192
+ for (; i < n; i++) {
193
+ if (flat) {
194
+ x = src[i * 2 + a];
195
+ y = src[i * 2 + 1 - a];
196
+ } else {
197
+ pt = src[i];
198
+ x = pt[a];
199
+ y = pt[1 - a];
200
+ }
201
+ if (typeof x != "number" || typeof y != "number" || !((x *= f) < LIMIT && x > -LIMIT && (y *= f) < LIMIT && y > -LIMIT)) return null;
202
+ p = x < 0 ? -(0.5 - x | 0) : x + 0.5 | 0;
203
+ q = y < 0 ? -(0.5 - y | 0) : y + 0.5 | 0;
204
+ if (x < 0.5 && x > -0.5) p = 0;
205
+ if (y < 0.5 && y > -0.5) q = 0;
206
+ z = p - la;
207
+ z = z << 1 ^ z >> 31;
208
+ s += z < 1024 ? CHARS[z] : chars(z);
209
+ z = q - lb;
210
+ z = z << 1 ^ z >> 31;
211
+ s += z < 1024 ? CHARS[z] : chars(z);
212
+ la = p;
213
+ lb = q;
214
+ }
215
+ } catch {
216
+ return null;
217
+ }
218
+ return s;
219
+ }
220
+ var lngLat = (o) => ({ ...typeof o == "number" ? { precision: o } : o, order: "lnglat" });
221
+ var latLngs = (decode2) => (str, o) => decode2(str, o).map(([lat, lng]) => ({ lat, lng }));
222
+ var toGeo = (decode2) => (str, o) => ({ type: "LineString", coordinates: decode2(str, lngLat(o)) });
223
+ var fromGeo = (encode2) => (g, o) => {
224
+ if (g?.type == "Feature") g = g.geometry;
225
+ return g?.type == "LineString" ? encode2(g.coordinates, lngLat(o)) : fail("input", "Expected a LineString");
226
+ };
227
+
228
+ // src/web.js
229
+ var decodeLatLng = /* @__PURE__ */ latLngs(decode);
230
+ var toGeoJSON = /* @__PURE__ */ toGeo(decode);
231
+ var fromGeoJSON = /* @__PURE__ */ fromGeo(encode);