@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/presets.js CHANGED
@@ -1,242 +1,242 @@
1
- // Option presets for routing engines and APIs that use the Google encoded
2
- // polyline format. Each preset is a plain options object for encode, decode,
3
- // encodeFlat, decodeFlat, toGeoJSON and fromGeoJSON:
4
- //
5
- // import { decode } from '@duoquote/polytini';
6
- // import { osrm6 } from '@duoquote/polytini/presets';
7
- // decode(route.geometry, osrm6); // [[lng, lat], ...]
8
- //
9
- // This module imports nothing, so it adds no bytes to the codec, and a
10
- // bundler keeps only the presets you import.
11
- //
12
- // How the values were chosen:
13
- //
14
- // - precision: the engine's documented precision, checked against its
15
- // encoder source where the source is public. Presets use `precision`
16
- // rather than `factor`, so spreading a preset and adding `factor` (for
17
- // example GraphHopper's points_encoded_multiplier) still works, because
18
- // factor takes priority over precision.
19
- // - order: the order of the engine's own unencoded coordinates (GeoJSON or
20
- // coordinate arrays), so decoded output can be compared with, or used in
21
- // place of, what the engine returns when you ask it for plain coordinates.
22
- // The encoded string always stores latitude first; order only changes the
23
- // tuples you pass in and get back. Override it with a spread, for example
24
- // { ...valhalla, order: 'latlng' }.
25
- // - dimensions and zFactor: only for engines that put a third value in the
26
- // string. Those engines only add it when asked, so each has a 2D preset
27
- // for the default response and a "3d" preset for the elevation response.
28
- // A 3D preset on a 2D string throws in strict mode when the value count is
29
- // not a multiple of 3, and gives wrong points when it is, so pick the
30
- // preset that matches the request.
31
- //
32
- // Rounding: polytini rounds half away from zero, like Google's description
33
- // and like OSRM and Valhalla (C++ std::round / round). Java's Math.round,
34
- // used by GraphHopper and openrouteservice, rounds halves toward positive
35
- // infinity, and some engines floor. This only matters when encoding:
36
- // decoding gives the engine's exact values for every preset. The core has
37
- // no rounding option, so where an engine rounds differently, encoding a
38
- // point with its preset can give a string that differs from the engine's
39
- // own output by one unit in the last place for some inputs. Each preset
40
- // below says when that happens.
41
- //
42
- // Not covered here, because they are not the Google algorithm:
43
- // - HERE flexible polyline (a header with precision and a third dimension
44
- // type, and a different character table). Use '@duoquote/polytini/flexible'.
45
- // - Bing Maps point compression (each latitude and longitude pair is
46
- // combined into one value, and it uses a different character table):
47
- // https://learn.microsoft.com/en-us/bingmaps/rest-services/elevations/point-compression-algorithm
48
- //
49
- // Sources were read on 2026-11-26. Commits are the default branch heads on
50
- // that date.
51
- /* @ts-self-types="../types/presets.d.ts" */
52
-
53
- /**
54
- * Google Maps Platform: the format's own definition, precision 5.
55
- * https://developers.google.com/maps/documentation/utilities/polylinealgorithm
56
- *
57
- * order 'latlng': Google's decoders return latitude first:
58
- * google.maps.geometry.encoding.decodePath returns Array<LatLng>
59
- * (https://developers.google.com/maps/documentation/javascript/reference/geometry),
60
- * and Directions API locations are {lat, lng}. The Routes API can also
61
- * return GeoJSON ([lng, lat]) with polylineEncoding: GEO_JSON_LINESTRING
62
- * (https://developers.google.com/maps/documentation/routes/traffic_on_polylines);
63
- * use { ...google, order: 'lnglat' } to compare with that.
64
- */
65
- export const google = { precision: 5, order: 'latlng' };
66
-
67
- /**
68
- * OSRM, geometries=polyline (the default), precision 5.
69
- * Docs: https://github.com/Project-OSRM/osrm-backend/blob/578a12065917e02bf14bb4568f12a2d3c388ba15/docs/http.md
70
- * ("polyline with precision 5 in [latitude,longitude] encoding"; also
71
- * http://project-osrm.org/docs/v5.24.0/api/).
72
- * Encoder: include/engine/polyline_compressor.hpp at the same commit.
73
- * OSRM stores coordinates as integers at 1e6 and encodes
74
- * std::round(fixed * 0.1), which rounds half away from zero, as polytini
75
- * does.
76
- *
77
- * order 'lnglat': geometries=geojson returns a GeoJSON LineString, and
78
- * waypoint, maneuver and intersection locations are [longitude, latitude].
79
- * The tests check the public demo server's polyline against its GeoJSON.
80
- */
81
- export const osrm = { precision: 5, order: 'lnglat' };
82
-
83
- /** OSRM, geometries=polyline6: as `osrm`, precision 6. */
84
- export const osrm6 = { precision: 6, order: 'lnglat' };
85
-
86
- /**
87
- * Valhalla, precision 6. The native JSON response has trip.legs[].shape;
88
- * trace_attributes, height and linear_cost_factors also use precision 6.
89
- * Docs: https://github.com/valhalla/valhalla/blob/4d8b2061b566412ce61eb8a2001b7cfa2392356a/docs/docs/api/decoding.md
90
- * ("Valhalla APIs use six digits of decimal precision") and
91
- * docs/docs/api/route/api-reference.md at the same commit.
92
- * Encoder: valhalla/midgard/encoded.h at the same commit,
93
- * round(value * 1e6), half away from zero, latitude first.
94
- *
95
- * order 'lnglat': Valhalla's only plain coordinate line output is
96
- * format=osrm with shape_format=geojson, which is [lng, lat]. Request
97
- * coordinates are {lat, lon} objects, and the JavaScript example in
98
- * decoding.md returns [lat, lng]; use { ...valhalla, order: 'latlng' } to
99
- * match that example.
100
- *
101
- * With format=osrm the geometry is polyline6 by default (use this preset or
102
- * osrm6), or polyline5 with shape_format=polyline5 (use `osrm`).
103
- */
104
- export const valhalla = { precision: 6, order: 'lnglat' };
105
-
106
- /**
107
- * Stadia Maps routing, which runs Valhalla: precision 6, legs[].shape.
108
- * https://docs.stadiamaps.com/tutorials/getting-directions-in-javascript-from-geospatial-apis-maplibre
109
- * ("Our polylines have 6 digits of precision, not 5"; the example decodes
110
- * leg.shape with polyline.toGeoJSON(shape, 6)). Same values as `valhalla`.
111
- */
112
- export const stadia = { precision: 6, order: 'lnglat' };
113
-
114
- /**
115
- * GraphHopper, points_encoded=true (the default) and elevation=false (the
116
- * default): paths[].points and paths[].snapped_waypoints.
117
- * Docs: https://github.com/graphhopper/graphhopper/blob/ce72e96197eb16fcc3ab4f18fdd543fa270be9d5/docs/web/api-doc.md
118
- * Encoder: ResponsePathSerializer.encodePolyline in
119
- * web-api/src/main/java/com/graphhopper/jackson/ResponsePathSerializer.java
120
- * at the same commit.
121
- *
122
- * Latitude and longitude use the points_encoded_multiplier parameter,
123
- * default 1e5 (web-bundle/.../RouteResource.java), which any value of 1 or
124
- * more can change. The response echoes it as paths[].points_encoded_multiplier
125
- * since GraphHopper 9.0 (commit 322be2aa43, "polyline multiplier"). For a
126
- * response with another multiplier use
127
- * { ...graphhopper, factor: path.points_encoded_multiplier }.
128
- * GraphHopper's /navigate endpoint (Mapbox compatible) always uses
129
- * polyline6; use `mapbox6` there.
130
- *
131
- * Rounding: Math.round since GraphHopper 9.0 (commit 4a5dbeb3ff, "encoded
132
- * polyline algorithm: closer to Google spec"); 8.0 and earlier used
133
- * Math.floor. Math.round rounds halves toward positive infinity, so a
134
- * scaled value of exactly -2.5 becomes -2 in GraphHopper and -3 in
135
- * polytini. Only negative values exactly halfway between two steps differ.
136
- * The floor of older versions differs from rounding about half the time.
137
- * Decoding is exact for all versions.
138
- *
139
- * order 'lnglat': with points_encoded=false, points is a GeoJSON
140
- * LineString with [lon, lat, elevation] order.
141
- */
142
- export const graphhopper = { precision: 5, order: 'lnglat' };
143
-
144
- /**
145
- * GraphHopper with elevation=true: a third value per point, elevation in
146
- * meters times 100 (hard coded, it does not follow
147
- * points_encoded_multiplier), so centimeters. Decodes to
148
- * [lng, lat, elevation]. Same encoder and rounding notes as `graphhopper`:
149
- * a negative elevation exactly halfway between two centimeters (such as
150
- * -0.125 m) encodes one centimeter higher in GraphHopper than in polytini.
151
- */
152
- export const graphhopper3d = { precision: 5, dimensions: 3, zFactor: 100, order: 'lnglat' };
153
-
154
- /**
155
- * openrouteservice, /v2/directions/{profile}/json: routes[].geometry,
156
- * precision 5. ORS uses its own encoder, not GraphHopper's, and the
157
- * multiplier is fixed.
158
- * Docs: https://github.com/GIScience/openrouteservice/blob/65fb57b1d6716ef5147af5d62909402eb51a85f4/docs/api-reference/endpoints/directions/geometry-decoding.md
159
- * and requests-and-return-types.md in the same folder.
160
- * Encoder: ors-engine/src/main/java/org/heigit/ors/util/PolylineEncoder.java
161
- * at the same commit: Math.round(lat * 1e5) and Math.round(lng * 1e5), with
162
- * the same negative halfway difference as `graphhopper`.
163
- *
164
- * order 'lnglat': the geojson endpoint returns a LineString, and request
165
- * coordinates are longitude/latitude pairs.
166
- */
167
- export const openrouteservice = { precision: 5, order: 'lnglat' };
168
-
169
- /**
170
- * openrouteservice with elevation=true ("elevation also gets encoded for
171
- * json response encoded polyline", RouteRequest.java): a third value,
172
- * elevation in meters times 100. Decodes to [lng, lat, elevation].
173
- *
174
- * Rounding: ORS floors the elevation, (long) Math.floor(z * 100), where
175
- * polytini rounds, so polytini can encode one centimeter more than ORS.
176
- * That includes some whole-centimeter values, because the multiplication
177
- * is not exact: 1027.1 * 100 is 102709.99999999999, which ORS stores as
178
- * 1027.09 m. Latitude and longitude follow the `openrouteservice` notes.
179
- * Decoding is exact.
180
- */
181
- export const openrouteservice3d = { precision: 5, dimensions: 3, zFactor: 100, order: 'lnglat' };
182
-
183
- /**
184
- * Mapbox Directions, geometries=polyline (the default), precision 5.
185
- * Docs: https://docs.mapbox.com/api/navigation/directions/
186
- * ("polyline (default, a polyline with a precision of five decimal
187
- * places)"). The encoder is not public; the tests check the response
188
- * fixtures in mapbox/mapbox-java (MIT).
189
- *
190
- * order 'lnglat': geometries=geojson returns a LineString, and waypoint,
191
- * maneuver and intersection locations are [longitude, latitude].
192
- */
193
- export const mapbox = { precision: 5, order: 'lnglat' };
194
-
195
- /** Mapbox Directions, geometries=polyline6: as `mapbox`, precision 6. */
196
- export const mapbox6 = { precision: 6, order: 'lnglat' };
197
-
198
- /**
199
- * Strava API v3: map.polyline and map.summary_polyline on activities,
200
- * routes and segments, precision 5.
201
- * Docs: https://developers.strava.com/docs/reference/ (PolylineMap). The
202
- * docs do not state the precision; the example summary_polyline there
203
- * starts at 37.82822, -122.26348 at precision 5, next to the example's
204
- * start_latlng [37.83, -122.26]. The encoder is not public.
205
- *
206
- * order 'latlng': Strava's own coordinates (LatLng, start_latlng, the
207
- * latlng stream) are [latitude, longitude] arrays.
208
- */
209
- export const strava = { precision: 5, order: 'latlng' };
210
-
211
- /**
212
- * OpenTripPlanner 2: legGeometry.points in the GraphQL APIs, precision 5.
213
- * Encoder: street/src/main/java/org/opentripplanner/street/geometry/PolylineEncoder.java
214
- * and application/src/main/java/org/opentripplanner/api/model/geometry/EncodedPolyline.java
215
- * in opentripplanner/OpenTripPlanner at 14bcee2a04d0c4a012f524b84b0814595130bf99.
216
- *
217
- * Rounding: OTP floors, (int) Math.floor(coordinate * 1e5), so encoding
218
- * with this preset can give a value one step (1e-5 degrees) higher than
219
- * OTP's for any point that is not on the 1e-5 grid. Decoding is exact.
220
- *
221
- * order 'latlng': OTP has no plain coordinate line output; places and
222
- * steps give lat and lon as separate fields. The preset keeps the string's
223
- * own latitude first order.
224
- */
225
- export const otp = { precision: 5, order: 'latlng' };
226
-
227
- /** Every preset, keyed by its export name. */
228
- export const presets = {
229
- google,
230
- osrm,
231
- osrm6,
232
- valhalla,
233
- stadia,
234
- graphhopper,
235
- graphhopper3d,
236
- openrouteservice,
237
- openrouteservice3d,
238
- mapbox,
239
- mapbox6,
240
- strava,
241
- otp,
242
- };
1
+ // Option presets for routing engines and APIs that use the Google encoded
2
+ // polyline format. Each preset is a plain options object for encode, decode,
3
+ // encodeFlat, decodeFlat, toGeoJSON and fromGeoJSON:
4
+ //
5
+ // import { decode } from '@duoquote/polytini';
6
+ // import { osrm6 } from '@duoquote/polytini/presets';
7
+ // decode(route.geometry, osrm6); // [[lng, lat], ...]
8
+ //
9
+ // This module imports nothing, so it adds no bytes to the codec, and a
10
+ // bundler keeps only the presets you import.
11
+ //
12
+ // How the values were chosen:
13
+ //
14
+ // - precision: the engine's documented precision, checked against its
15
+ // encoder source where the source is public. Presets use `precision`
16
+ // rather than `factor`, so spreading a preset and adding `factor` (for
17
+ // example GraphHopper's points_encoded_multiplier) still works, because
18
+ // factor takes priority over precision.
19
+ // - order: the order of the engine's own unencoded coordinates (GeoJSON or
20
+ // coordinate arrays), so decoded output can be compared with, or used in
21
+ // place of, what the engine returns when you ask it for plain coordinates.
22
+ // The encoded string always stores latitude first; order only changes the
23
+ // tuples you pass in and get back. Override it with a spread, for example
24
+ // { ...valhalla, order: 'latlng' }.
25
+ // - dimensions and zFactor: only for engines that put a third value in the
26
+ // string. Those engines only add it when asked, so each has a 2D preset
27
+ // for the default response and a "3d" preset for the elevation response.
28
+ // A 3D preset on a 2D string throws in strict mode when the value count is
29
+ // not a multiple of 3, and gives wrong points when it is, so pick the
30
+ // preset that matches the request.
31
+ //
32
+ // Rounding: polytini rounds half away from zero, like Google's description
33
+ // and like OSRM and Valhalla (C++ std::round / round). Java's Math.round,
34
+ // used by GraphHopper and openrouteservice, rounds halves toward positive
35
+ // infinity, and some engines floor. This only matters when encoding:
36
+ // decoding gives the engine's exact values for every preset. The core has
37
+ // no rounding option, so where an engine rounds differently, encoding a
38
+ // point with its preset can give a string that differs from the engine's
39
+ // own output by one unit in the last place for some inputs. Each preset
40
+ // below says when that happens.
41
+ //
42
+ // Not covered here, because they are not the Google algorithm:
43
+ // - HERE flexible polyline (a header with precision and a third dimension
44
+ // type, and a different character table). Use '@duoquote/polytini/flexible'.
45
+ // - Bing Maps point compression (each latitude and longitude pair is
46
+ // combined into one value, and it uses a different character table):
47
+ // https://learn.microsoft.com/en-us/bingmaps/rest-services/elevations/point-compression-algorithm
48
+ //
49
+ // Sources were read on 2026-11-26. Commits are the default branch heads on
50
+ // that date.
51
+ /* @ts-self-types="../types/presets.d.ts" */
52
+
53
+ /**
54
+ * Google Maps Platform: the format's own definition, precision 5.
55
+ * https://developers.google.com/maps/documentation/utilities/polylinealgorithm
56
+ *
57
+ * order 'latlng': Google's decoders return latitude first:
58
+ * google.maps.geometry.encoding.decodePath returns Array<LatLng>
59
+ * (https://developers.google.com/maps/documentation/javascript/reference/geometry),
60
+ * and Directions API locations are {lat, lng}. The Routes API can also
61
+ * return GeoJSON ([lng, lat]) with polylineEncoding: GEO_JSON_LINESTRING
62
+ * (https://developers.google.com/maps/documentation/routes/traffic_on_polylines);
63
+ * use { ...google, order: 'lnglat' } to compare with that.
64
+ */
65
+ export const google = { precision: 5, order: 'latlng' };
66
+
67
+ /**
68
+ * OSRM, geometries=polyline (the default), precision 5.
69
+ * Docs: https://github.com/Project-OSRM/osrm-backend/blob/578a12065917e02bf14bb4568f12a2d3c388ba15/docs/http.md
70
+ * ("polyline with precision 5 in [latitude,longitude] encoding"; also
71
+ * http://project-osrm.org/docs/v5.24.0/api/).
72
+ * Encoder: include/engine/polyline_compressor.hpp at the same commit.
73
+ * OSRM stores coordinates as integers at 1e6 and encodes
74
+ * std::round(fixed * 0.1), which rounds half away from zero, as polytini
75
+ * does.
76
+ *
77
+ * order 'lnglat': geometries=geojson returns a GeoJSON LineString, and
78
+ * waypoint, maneuver and intersection locations are [longitude, latitude].
79
+ * The tests check the public demo server's polyline against its GeoJSON.
80
+ */
81
+ export const osrm = { precision: 5, order: 'lnglat' };
82
+
83
+ /** OSRM, geometries=polyline6: as `osrm`, precision 6. */
84
+ export const osrm6 = { precision: 6, order: 'lnglat' };
85
+
86
+ /**
87
+ * Valhalla, precision 6. The native JSON response has trip.legs[].shape;
88
+ * trace_attributes, height and linear_cost_factors also use precision 6.
89
+ * Docs: https://github.com/valhalla/valhalla/blob/4d8b2061b566412ce61eb8a2001b7cfa2392356a/docs/docs/api/decoding.md
90
+ * ("Valhalla APIs use six digits of decimal precision") and
91
+ * docs/docs/api/route/api-reference.md at the same commit.
92
+ * Encoder: valhalla/midgard/encoded.h at the same commit,
93
+ * round(value * 1e6), half away from zero, latitude first.
94
+ *
95
+ * order 'lnglat': Valhalla's only plain coordinate line output is
96
+ * format=osrm with shape_format=geojson, which is [lng, lat]. Request
97
+ * coordinates are {lat, lon} objects, and the JavaScript example in
98
+ * decoding.md returns [lat, lng]; use { ...valhalla, order: 'latlng' } to
99
+ * match that example.
100
+ *
101
+ * With format=osrm the geometry is polyline6 by default (use this preset or
102
+ * osrm6), or polyline5 with shape_format=polyline5 (use `osrm`).
103
+ */
104
+ export const valhalla = { precision: 6, order: 'lnglat' };
105
+
106
+ /**
107
+ * Stadia Maps routing, which runs Valhalla: precision 6, legs[].shape.
108
+ * https://docs.stadiamaps.com/tutorials/getting-directions-in-javascript-from-geospatial-apis-maplibre
109
+ * ("Our polylines have 6 digits of precision, not 5"; the example decodes
110
+ * leg.shape with polyline.toGeoJSON(shape, 6)). Same values as `valhalla`.
111
+ */
112
+ export const stadia = { precision: 6, order: 'lnglat' };
113
+
114
+ /**
115
+ * GraphHopper, points_encoded=true (the default) and elevation=false (the
116
+ * default): paths[].points and paths[].snapped_waypoints.
117
+ * Docs: https://github.com/graphhopper/graphhopper/blob/ce72e96197eb16fcc3ab4f18fdd543fa270be9d5/docs/web/api-doc.md
118
+ * Encoder: ResponsePathSerializer.encodePolyline in
119
+ * web-api/src/main/java/com/graphhopper/jackson/ResponsePathSerializer.java
120
+ * at the same commit.
121
+ *
122
+ * Latitude and longitude use the points_encoded_multiplier parameter,
123
+ * default 1e5 (web-bundle/.../RouteResource.java), which any value of 1 or
124
+ * more can change. The response echoes it as paths[].points_encoded_multiplier
125
+ * since GraphHopper 9.0 (commit 322be2aa43, "polyline multiplier"). For a
126
+ * response with another multiplier use
127
+ * { ...graphhopper, factor: path.points_encoded_multiplier }.
128
+ * GraphHopper's /navigate endpoint (Mapbox compatible) always uses
129
+ * polyline6; use `mapbox6` there.
130
+ *
131
+ * Rounding: Math.round since GraphHopper 9.0 (commit 4a5dbeb3ff, "encoded
132
+ * polyline algorithm: closer to Google spec"); 8.0 and earlier used
133
+ * Math.floor. Math.round rounds halves toward positive infinity, so a
134
+ * scaled value of exactly -2.5 becomes -2 in GraphHopper and -3 in
135
+ * polytini. Only negative values exactly halfway between two steps differ.
136
+ * The floor of older versions differs from rounding about half the time.
137
+ * Decoding is exact for all versions.
138
+ *
139
+ * order 'lnglat': with points_encoded=false, points is a GeoJSON
140
+ * LineString with [lon, lat, elevation] order.
141
+ */
142
+ export const graphhopper = { precision: 5, order: 'lnglat' };
143
+
144
+ /**
145
+ * GraphHopper with elevation=true: a third value per point, elevation in
146
+ * meters times 100 (hard coded, it does not follow
147
+ * points_encoded_multiplier), so centimeters. Decodes to
148
+ * [lng, lat, elevation]. Same encoder and rounding notes as `graphhopper`:
149
+ * a negative elevation exactly halfway between two centimeters (such as
150
+ * -0.125 m) encodes one centimeter higher in GraphHopper than in polytini.
151
+ */
152
+ export const graphhopper3d = { precision: 5, dimensions: 3, zFactor: 100, order: 'lnglat' };
153
+
154
+ /**
155
+ * openrouteservice, /v2/directions/{profile}/json: routes[].geometry,
156
+ * precision 5. ORS uses its own encoder, not GraphHopper's, and the
157
+ * multiplier is fixed.
158
+ * Docs: https://github.com/GIScience/openrouteservice/blob/65fb57b1d6716ef5147af5d62909402eb51a85f4/docs/api-reference/endpoints/directions/geometry-decoding.md
159
+ * and requests-and-return-types.md in the same folder.
160
+ * Encoder: ors-engine/src/main/java/org/heigit/ors/util/PolylineEncoder.java
161
+ * at the same commit: Math.round(lat * 1e5) and Math.round(lng * 1e5), with
162
+ * the same negative halfway difference as `graphhopper`.
163
+ *
164
+ * order 'lnglat': the geojson endpoint returns a LineString, and request
165
+ * coordinates are longitude/latitude pairs.
166
+ */
167
+ export const openrouteservice = { precision: 5, order: 'lnglat' };
168
+
169
+ /**
170
+ * openrouteservice with elevation=true ("elevation also gets encoded for
171
+ * json response encoded polyline", RouteRequest.java): a third value,
172
+ * elevation in meters times 100. Decodes to [lng, lat, elevation].
173
+ *
174
+ * Rounding: ORS floors the elevation, (long) Math.floor(z * 100), where
175
+ * polytini rounds, so polytini can encode one centimeter more than ORS.
176
+ * That includes some whole-centimeter values, because the multiplication
177
+ * is not exact: 1027.1 * 100 is 102709.99999999999, which ORS stores as
178
+ * 1027.09 m. Latitude and longitude follow the `openrouteservice` notes.
179
+ * Decoding is exact.
180
+ */
181
+ export const openrouteservice3d = { precision: 5, dimensions: 3, zFactor: 100, order: 'lnglat' };
182
+
183
+ /**
184
+ * Mapbox Directions, geometries=polyline (the default), precision 5.
185
+ * Docs: https://docs.mapbox.com/api/navigation/directions/
186
+ * ("polyline (default, a polyline with a precision of five decimal
187
+ * places)"). The encoder is not public; the tests check the response
188
+ * fixtures in mapbox/mapbox-java (MIT).
189
+ *
190
+ * order 'lnglat': geometries=geojson returns a LineString, and waypoint,
191
+ * maneuver and intersection locations are [longitude, latitude].
192
+ */
193
+ export const mapbox = { precision: 5, order: 'lnglat' };
194
+
195
+ /** Mapbox Directions, geometries=polyline6: as `mapbox`, precision 6. */
196
+ export const mapbox6 = { precision: 6, order: 'lnglat' };
197
+
198
+ /**
199
+ * Strava API v3: map.polyline and map.summary_polyline on activities,
200
+ * routes and segments, precision 5.
201
+ * Docs: https://developers.strava.com/docs/reference/ (PolylineMap). The
202
+ * docs do not state the precision; the example summary_polyline there
203
+ * starts at 37.82822, -122.26348 at precision 5, next to the example's
204
+ * start_latlng [37.83, -122.26]. The encoder is not public.
205
+ *
206
+ * order 'latlng': Strava's own coordinates (LatLng, start_latlng, the
207
+ * latlng stream) are [latitude, longitude] arrays.
208
+ */
209
+ export const strava = { precision: 5, order: 'latlng' };
210
+
211
+ /**
212
+ * OpenTripPlanner 2: legGeometry.points in the GraphQL APIs, precision 5.
213
+ * Encoder: street/src/main/java/org/opentripplanner/street/geometry/PolylineEncoder.java
214
+ * and application/src/main/java/org/opentripplanner/api/model/geometry/EncodedPolyline.java
215
+ * in opentripplanner/OpenTripPlanner at 14bcee2a04d0c4a012f524b84b0814595130bf99.
216
+ *
217
+ * Rounding: OTP floors, (int) Math.floor(coordinate * 1e5), so encoding
218
+ * with this preset can give a value one step (1e-5 degrees) higher than
219
+ * OTP's for any point that is not on the 1e-5 grid. Decoding is exact.
220
+ *
221
+ * order 'latlng': OTP has no plain coordinate line output; places and
222
+ * steps give lat and lon as separate fields. The preset keeps the string's
223
+ * own latitude first order.
224
+ */
225
+ export const otp = { precision: 5, order: 'latlng' };
226
+
227
+ /** Every preset, keyed by its export name. */
228
+ export const presets = {
229
+ google,
230
+ osrm,
231
+ osrm6,
232
+ valhalla,
233
+ stadia,
234
+ graphhopper,
235
+ graphhopper3d,
236
+ openrouteservice,
237
+ openrouteservice3d,
238
+ mapbox,
239
+ mapbox6,
240
+ strava,
241
+ otp,
242
+ };