@duoquote/geotini 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.
- package/CHANGELOG.md +88 -0
- package/LICENSE +21 -0
- package/README.md +331 -2
- package/package.json +126 -4
- package/src/deckgl.js +120 -0
- package/src/error.js +16 -0
- package/src/geoindex.js +373 -0
- package/src/geotini.js +3169 -0
- package/src/google.js +113 -0
- package/src/leaflet.js +82 -0
- package/src/mapbox.js +8 -0
- package/src/maplibre.js +74 -0
- package/src/mongodb.js +179 -0
- package/src/openlayers.js +69 -0
- package/src/redis.js +217 -0
- package/src/reference.js +1463 -0
- package/src/turf.js +79 -0
- package/types/deckgl.d.ts +53 -0
- package/types/google.d.ts +57 -0
- package/types/index.d.ts +453 -0
- package/types/leaflet.d.ts +56 -0
- package/types/mapbox.d.ts +29 -0
- package/types/maplibre.d.ts +53 -0
- package/types/mongodb.d.ts +108 -0
- package/types/openlayers.d.ts +39 -0
- package/types/redis.d.ts +117 -0
- package/types/reference.d.ts +133 -0
- package/types/turf.d.ts +59 -0
package/src/turf.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// GeoJSON Features and FeatureCollections from geotini strings, in the shape
|
|
2
|
+
// Turf.js builds them (type, id, bbox, properties, geometry), and strings
|
|
3
|
+
// from the Features Turf hands back. No Turf code is imported. The MapLibre,
|
|
4
|
+
// Mapbox and deck.gl modules build their data with these too.
|
|
5
|
+
//
|
|
6
|
+
// import { toFeature, fromFeature } from '@duoquote/geotini/turf';
|
|
7
|
+
// const area = turf.area(toFeature(str));
|
|
8
|
+
// const smaller = fromFeature(turf.simplify(toFeature(str), { tolerance: 0.0001 }), 6);
|
|
9
|
+
/* @ts-self-types="../types/turf.d.ts" */
|
|
10
|
+
import { encode, decode } from './geotini.js';
|
|
11
|
+
|
|
12
|
+
// [west, south, east, north] over every position, or undefined when there
|
|
13
|
+
// are none.
|
|
14
|
+
function bbox(g) {
|
|
15
|
+
const b = [Infinity, Infinity, -Infinity, -Infinity];
|
|
16
|
+
const walk = (c) => {
|
|
17
|
+
if (typeof c[0] == 'number') {
|
|
18
|
+
if (c[0] < b[0]) b[0] = c[0];
|
|
19
|
+
if (c[1] < b[1]) b[1] = c[1];
|
|
20
|
+
if (c[0] > b[2]) b[2] = c[0];
|
|
21
|
+
if (c[1] > b[3]) b[3] = c[1];
|
|
22
|
+
} else c.forEach(walk);
|
|
23
|
+
};
|
|
24
|
+
const geom = (g) => (g.type == 'GeometryCollection' ? g.geometries.forEach(geom) : walk(g.coordinates));
|
|
25
|
+
geom(g);
|
|
26
|
+
return b[0] <= b[2] ? b : undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function feature(geometry, o, i) {
|
|
30
|
+
const f = { type: 'Feature' }, p = o?.properties;
|
|
31
|
+
if (o?.id != null) f.id = Array.isArray(o.id) ? o.id[i] : o.id;
|
|
32
|
+
if (o?.bbox) {
|
|
33
|
+
const b = bbox(geometry);
|
|
34
|
+
if (b) f.bbox = b;
|
|
35
|
+
}
|
|
36
|
+
f.properties = (Array.isArray(p) ? p[i] : p) ?? {};
|
|
37
|
+
f.geometry = geometry;
|
|
38
|
+
return f;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const list = (encoded) => {
|
|
42
|
+
if (typeof encoded == 'string') return [encoded];
|
|
43
|
+
if (!Array.isArray(encoded)) throw new TypeError('Expected a geotini string or an array of them');
|
|
44
|
+
return encoded;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// The options go to decode as they are. It reads checksum, maxLength and
|
|
48
|
+
// maxPositions and ignores the rest, so one object serves both.
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A Feature with the decoded geometry. Options: `properties`, `id` and
|
|
52
|
+
* `bbox` (true to add one), and decode's `checksum`, `maxLength` and
|
|
53
|
+
* `maxPositions`.
|
|
54
|
+
*/
|
|
55
|
+
export const toFeature = (str, options) => feature(decode(str, options), options, 0);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A FeatureCollection, one Feature per string. `properties` and `id` may be
|
|
59
|
+
* arrays, one entry per string. The decoder options hold for each string.
|
|
60
|
+
*/
|
|
61
|
+
export const toFeatureCollection = (encoded, options) => ({ type: 'FeatureCollection', features: list(encoded).map((s, i) => feature(decode(s, options), options, i)) });
|
|
62
|
+
|
|
63
|
+
const geometryOf = (g) => {
|
|
64
|
+
if (g?.type == 'Feature') {
|
|
65
|
+
if (!g.geometry) throw new TypeError('The Feature has no geometry');
|
|
66
|
+
return g.geometry;
|
|
67
|
+
}
|
|
68
|
+
return g;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** Encodes a Feature's geometry, or a bare geometry. Properties aren't kept. Options are encode's. */
|
|
72
|
+
export const fromFeature = (feature, options) => encode(geometryOf(feature), options);
|
|
73
|
+
|
|
74
|
+
/** Encodes each Feature of a FeatureCollection: one string per Feature, in order. */
|
|
75
|
+
export function fromFeatureCollection(fc, options) {
|
|
76
|
+
if (fc?.type != 'FeatureCollection' || !Array.isArray(fc.features)) throw new TypeError('Expected a FeatureCollection');
|
|
77
|
+
return fc.features.map((f) => fromFeature(f, options));
|
|
78
|
+
}
|
|
79
|
+
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Types for @duoquote/geotini/deckgl. Structural, so they don't depend on
|
|
2
|
+
// deck.gl's types and work for JSR too.
|
|
3
|
+
/**
|
|
4
|
+
* deck.gl layer data from geotini strings: GeoJSON for GeoJsonLayer, binary
|
|
5
|
+
* paths for PathLayer, and flat polygons with holes for SolidPolygonLayer
|
|
6
|
+
* and PolygonLayer. No deck.gl code is imported.
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { binaryPaths } from '@duoquote/geotini/deckgl';
|
|
10
|
+
* new PathLayer({ id: 'lines', data: binaryPaths(strings), _pathType: 'open' });
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import type { Geometry, DecodeOptions } from './index.d.ts';
|
|
16
|
+
import type { FeatureOptions, GeoFeatureCollection } from './turf.d.ts';
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/** `z`: three values per vertex (`[lng, lat, z]`, 0 where a position has none). `float32`: Float32Array instead of Float64Array. And decode's options, for each string. */
|
|
20
|
+
export interface ArrayOptions extends DecodeOptions {
|
|
21
|
+
/** Include the third value of each position as z. Default false: x and y only. */
|
|
22
|
+
z?: boolean;
|
|
23
|
+
/** Use a Float32Array instead of a Float64Array. */
|
|
24
|
+
float32?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** deck.gl's binary data for PathLayer, plus the string each path came from. */
|
|
28
|
+
export interface BinaryPaths {
|
|
29
|
+
/** The number of paths. */
|
|
30
|
+
length: number;
|
|
31
|
+
/** Where each path starts, in vertices, with one more entry for the end of the last. */
|
|
32
|
+
startIndices: Uint32Array;
|
|
33
|
+
/** Every vertex, `size` numbers each: deck.gl reads it as the getPath attribute. */
|
|
34
|
+
attributes: { getPath: { value: Float64Array | Float32Array; size: 2 | 3 } };
|
|
35
|
+
/** For each path, the index of the string it came from. */
|
|
36
|
+
featureIds: Uint32Array;
|
|
37
|
+
}
|
|
38
|
+
/** deck.gl's flat polygon, plus the string it came from. */
|
|
39
|
+
export interface FlatPolygon {
|
|
40
|
+
/** Every vertex of the outer ring and then of each hole, x and y each. */
|
|
41
|
+
positions: Float64Array | Float32Array;
|
|
42
|
+
/** Where each hole starts in `positions`, in numbers, not vertices. Absent when there are no holes. */
|
|
43
|
+
holeIndices?: number[];
|
|
44
|
+
/** The index of the string this polygon came from. */
|
|
45
|
+
featureId: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A FeatureCollection for GeoJsonLayer, one Feature per string. */
|
|
49
|
+
export function geotiniData<P = Record<string, unknown>>(encoded: string | readonly string[], options?: FeatureOptions<P>): GeoFeatureCollection<Geometry, P>;
|
|
50
|
+
/** One path per line or ring of every string, for PathLayer. Points are skipped. */
|
|
51
|
+
export function binaryPaths(encoded: string | readonly string[], options?: ArrayOptions): BinaryPaths;
|
|
52
|
+
/** Each polygon of every string, with holes, for SolidPolygonLayer or PolygonLayer with `getPolygon: (d) => d`. */
|
|
53
|
+
export function flatPolygons(encoded: string | readonly string[], options?: ArrayOptions): FlatPolygon[];
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Types for @duoquote/geotini/google. Structural, so they don't depend on
|
|
2
|
+
// @types/google.maps and work for JSR too.
|
|
3
|
+
/**
|
|
4
|
+
* Google Maps JavaScript API: geotini strings to paths for google.maps
|
|
5
|
+
* Polyline and Polygon, to google.maps.Data features, and back. The Maps API
|
|
6
|
+
* isn't loaded or imported.
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { toPaths, encodePolygon } from '@duoquote/geotini/google';
|
|
10
|
+
* const polygon = new google.maps.Polygon({ paths: toPaths(str), map });
|
|
11
|
+
* const back = encodePolygon(polygon.getPaths(), 6);
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
import type { EncodeOptions, DecodeOptions } from './index.d.ts';
|
|
17
|
+
import type { FeatureOptions } from './turf.d.ts';
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/** A google.maps.LatLngLiteral. */
|
|
21
|
+
export interface LatLngLiteral {
|
|
22
|
+
/** Latitude in degrees. */
|
|
23
|
+
lat: number;
|
|
24
|
+
/** Longitude in degrees. */
|
|
25
|
+
lng: number;
|
|
26
|
+
}
|
|
27
|
+
/** A google.maps.LatLng, or anything with lat() and lng(). */
|
|
28
|
+
export interface LatLngObject {
|
|
29
|
+
/** Latitude in degrees. */
|
|
30
|
+
lat(): number;
|
|
31
|
+
/** Longitude in degrees. */
|
|
32
|
+
lng(): number;
|
|
33
|
+
}
|
|
34
|
+
/** A point the encoders take: a LatLng object, a literal or a [lat, lng] tuple. */
|
|
35
|
+
export type PathPoint = LatLngObject | LatLngLiteral | readonly number[];
|
|
36
|
+
/** An array, or an MVCArray (anything with getArray()). */
|
|
37
|
+
export type ArrayLike<T> = readonly T[] | { getArray(): T[] };
|
|
38
|
+
/** With `LatLng` (google.maps.LatLng), the decoders make LatLng objects instead of literals. Decode's options go here too. */
|
|
39
|
+
export interface PathOptions<T = LatLngLiteral> extends DecodeOptions {
|
|
40
|
+
/** A LatLng class, such as `google.maps.LatLng`, to get LatLng objects instead of literals. */
|
|
41
|
+
LatLng?: new (lat: number, lng: number) => T;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Paths for a LineString (one), MultiLineString (one per line) or Polygon (one per ring, without the closing point). */
|
|
45
|
+
export function toPaths<T = LatLngLiteral>(str: string, options?: PathOptions<T>): T[][];
|
|
46
|
+
/** The path of a LineString string, like google.maps.geometry.encoding.decodePath. */
|
|
47
|
+
export function decodePath<T = LatLngLiteral>(str: string, options?: PathOptions<T>): T[];
|
|
48
|
+
/** One `paths` array per polygon of a Polygon or MultiPolygon string. */
|
|
49
|
+
export function toPolygons<T = LatLngLiteral>(str: string, options?: PathOptions<T>): T[][][];
|
|
50
|
+
/** Encodes a path as a LineString, like google.maps.geometry.encoding.encodePath. */
|
|
51
|
+
export function encodePath(path: ArrayLike<PathPoint>, options?: number | EncodeOptions): string;
|
|
52
|
+
/** Encodes a polygon's paths as a Polygon: the first path is the outer ring, the others holes. */
|
|
53
|
+
export function encodePolygon(paths: ArrayLike<ArrayLike<PathPoint>>, options?: number | EncodeOptions): string;
|
|
54
|
+
/** Adds one Feature per string to a google.maps.Data layer and returns the Data.Features. */
|
|
55
|
+
export function addGeoJson(data: { addGeoJson(geoJson: object, options?: object): any[] }, encoded: string | readonly string[], options?: FeatureOptions & { idPropertyName?: string }): any[];
|
|
56
|
+
/** Encodes every feature of a google.maps.Data layer, one string per feature. */
|
|
57
|
+
export function encodeData(data: { toGeoJson(callback: (geoJson: any) => void): void }, options?: number | EncodeOptions): Promise<string[]>;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
// Types for @duoquote/geotini, the default codec.
|
|
2
|
+
/**
|
|
3
|
+
* Encode and decode whole GeoJSON geometries as short, URL-safe strings:
|
|
4
|
+
* points, lines, polygons with holes, the multi-geometries and collections,
|
|
5
|
+
* with optional Z and M. Lossless at the precision you pick (5 decimals by
|
|
6
|
+
* default), and the format is in SPEC.md. `canonical` gives the string that
|
|
7
|
+
* stands for a geometry at settings you fix, for cache keys and comparisons.
|
|
8
|
+
*
|
|
9
|
+
* ```js
|
|
10
|
+
* import { encode, decode } from '@duoquote/geotini';
|
|
11
|
+
* encode({ type: 'Point', coordinates: [13.40495, 52.52001] }); // 'DZqSC6hPP'
|
|
12
|
+
* decode('DZqSC6hPP'); // { type: 'Point', coordinates: [13.40495, 52.52001] }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Where bytes are stored as they are, `encodeValue` and `decodeValue` keep
|
|
16
|
+
* the same bits in a `Uint8Array` three quarters as long as the string, and
|
|
17
|
+
* `toValue` and `fromValue` turn one form into the other.
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** A position: `[lng, lat]`, then `z` and `m` when the geometry has them. In `encodeLine` and `decodeLine`, `[lat, lng]`. */
|
|
23
|
+
export type Position = number[];
|
|
24
|
+
|
|
25
|
+
/** A GeoJSON Point. `coordinates` is `[]` for an empty Point. */
|
|
26
|
+
export interface Point {
|
|
27
|
+
/** Always "Point". */
|
|
28
|
+
type: 'Point';
|
|
29
|
+
/** One position. Empty for an empty Point. */
|
|
30
|
+
coordinates: Position;
|
|
31
|
+
}
|
|
32
|
+
/** A GeoJSON MultiPoint. */
|
|
33
|
+
export interface MultiPoint {
|
|
34
|
+
/** Always "MultiPoint". */
|
|
35
|
+
type: 'MultiPoint';
|
|
36
|
+
/** The points. */
|
|
37
|
+
coordinates: Position[];
|
|
38
|
+
}
|
|
39
|
+
/** A GeoJSON LineString. */
|
|
40
|
+
export interface LineString {
|
|
41
|
+
/** Always "LineString". */
|
|
42
|
+
type: 'LineString';
|
|
43
|
+
/** The points of the line, in order. */
|
|
44
|
+
coordinates: Position[];
|
|
45
|
+
}
|
|
46
|
+
/** A GeoJSON MultiLineString. */
|
|
47
|
+
export interface MultiLineString {
|
|
48
|
+
/** Always "MultiLineString". */
|
|
49
|
+
type: 'MultiLineString';
|
|
50
|
+
/** The lines. */
|
|
51
|
+
coordinates: Position[][];
|
|
52
|
+
}
|
|
53
|
+
/** A GeoJSON Polygon: rings, the first one the outside, the others holes. Winding order is kept as written. */
|
|
54
|
+
export interface Polygon {
|
|
55
|
+
/** Always "Polygon". */
|
|
56
|
+
type: 'Polygon';
|
|
57
|
+
/** The outer ring, then the holes. Each ring ends with its first position. */
|
|
58
|
+
coordinates: Position[][];
|
|
59
|
+
}
|
|
60
|
+
/** A GeoJSON MultiPolygon. */
|
|
61
|
+
export interface MultiPolygon {
|
|
62
|
+
/** Always "MultiPolygon". */
|
|
63
|
+
type: 'MultiPolygon';
|
|
64
|
+
/** The polygons, each as rings. */
|
|
65
|
+
coordinates: Position[][][];
|
|
66
|
+
}
|
|
67
|
+
/** A GeoJSON GeometryCollection, nested at most 32 deep. */
|
|
68
|
+
export interface GeometryCollection {
|
|
69
|
+
/** Always "GeometryCollection". */
|
|
70
|
+
type: 'GeometryCollection';
|
|
71
|
+
/** The geometries, which can be collections themselves. */
|
|
72
|
+
geometries: Geometry[];
|
|
73
|
+
}
|
|
74
|
+
/** Any of the seven GeoJSON geometry types. */
|
|
75
|
+
export type Geometry = Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon | GeometryCollection;
|
|
76
|
+
|
|
77
|
+
/** A GeoJSON Feature. `encode` takes one and encodes its geometry; properties aren't kept. */
|
|
78
|
+
export interface Feature {
|
|
79
|
+
/** Always "Feature". */
|
|
80
|
+
type: 'Feature';
|
|
81
|
+
/** The geometry that gets encoded. */
|
|
82
|
+
geometry: Geometry;
|
|
83
|
+
/** Properties and anything else; encode ignores them. */
|
|
84
|
+
[key: string]: unknown;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Options for `encode` and `encodeLine`. A number on its own is the
|
|
89
|
+
* precision. An option that's left out, `undefined` or `null` takes its
|
|
90
|
+
* default. One that's given must be one of its values, or the call throws a
|
|
91
|
+
* RangeError: `zPrecision: -1`, `checksum: 'none'` and `favor: 'fast'` are
|
|
92
|
+
* refused, not read as a default.
|
|
93
|
+
*/
|
|
94
|
+
export interface EncodeOptions {
|
|
95
|
+
/** Decimal places kept for longitude and latitude, an integer from 0 to 15. Default 5 (about 1 m). Above 7 the string uses wide integers. */
|
|
96
|
+
precision?: number;
|
|
97
|
+
/** Decimal places for the third value of each position, an integer from 0 to 15. Default: `precision`. */
|
|
98
|
+
zPrecision?: number;
|
|
99
|
+
/** Decimal places for the fourth value, an integer from 0 to 15. Default: `precision`. */
|
|
100
|
+
mPrecision?: number;
|
|
101
|
+
/**
|
|
102
|
+
* Add a check of the string, so that decoding refuses a damaged copy with
|
|
103
|
+
* code `CHECKSUM`. `true` adds the full CRC-32C, 6 characters, which
|
|
104
|
+
* catches every single wrong character. `'short'` adds 12 bits of it in
|
|
105
|
+
* 2 characters. That catches every single wrong character in a string of
|
|
106
|
+
* up to 24 characters, and in longer ones it let about 1 damaged string
|
|
107
|
+
* in 25,000 through (SPEC.md section 12 has the rates by kind of damage).
|
|
108
|
+
*
|
|
109
|
+
* A string with a check takes the 3-character header. So at precision 5
|
|
110
|
+
* to 7 with no other option the short check costs 4 or 5 characters in
|
|
111
|
+
* all and the full one 8 or 9, now and then one less. Any other string
|
|
112
|
+
* gets exactly 2 or 6 longer.
|
|
113
|
+
*
|
|
114
|
+
* A check only protects when the reader requires it: decode with
|
|
115
|
+
* `{ checksum: true }`. Default false.
|
|
116
|
+
*
|
|
117
|
+
* In a binary value (`encodeValue`) the check is of the bytes: `true`
|
|
118
|
+
* adds the whole CRC-32C in 4 bytes and `'short'` 16 bits of it in 2,
|
|
119
|
+
* which catches every single wrong byte in a value of up to 1,139 bytes.
|
|
120
|
+
* With the 17-bit header that comes with a check, the short one costs 3
|
|
121
|
+
* or 4 bytes in all at precision 5 to 7 and the full one 5 or 6.
|
|
122
|
+
*/
|
|
123
|
+
checksum?: boolean | 'short';
|
|
124
|
+
/**
|
|
125
|
+
* Which encoder writes the string. All of them write valid strings that
|
|
126
|
+
* decode to the same numbers.
|
|
127
|
+
*
|
|
128
|
+
* `'size'` is the canonical encoder of the format (SPEC.md section 16.4):
|
|
129
|
+
* it costs every predictor and every parameter exactly, and for the same
|
|
130
|
+
* geometry and options it writes the same string in every implementation
|
|
131
|
+
* and every release. Use it when the string is a key or gets compared.
|
|
132
|
+
* Its strings are about 1% smaller than the default's, and it takes 5 to
|
|
133
|
+
* 17 times as long.
|
|
134
|
+
*
|
|
135
|
+
* The default picks predictors from samples and parameters from estimates.
|
|
136
|
+
* `'speed'` skips the costly predictors. How much larger its strings get
|
|
137
|
+
* depends on the data: nothing on points, up to 2.5% on GPS tracks, 2 to
|
|
138
|
+
* 4% on OpenStreetMap buildings and streets, and 11 to 23% on driving
|
|
139
|
+
* routes from a routing engine. What these two write can change between
|
|
140
|
+
* releases.
|
|
141
|
+
*
|
|
142
|
+
* Wide strings (precision above 7, coordinates off the globe or past 32
|
|
143
|
+
* bits) and strings with Z or M are always written by the canonical
|
|
144
|
+
* encoder.
|
|
145
|
+
*/
|
|
146
|
+
favor?: 'size' | 'speed';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Options for `decode`, `decodeLine` and `canonical`, and for whatever
|
|
151
|
+
* reads a binary value (`decodeValue`, `decodeLineValue`, `canonicalValue`,
|
|
152
|
+
* `fromValue`) or a string to convert (`toValue`): an object, or nothing.
|
|
153
|
+
* An option that's given must be one of its values, or the call throws a
|
|
154
|
+
* RangeError. Options of `encode` in the same object are ignored, so one
|
|
155
|
+
* object can go to both.
|
|
156
|
+
*/
|
|
157
|
+
export interface DecodeOptions {
|
|
158
|
+
/**
|
|
159
|
+
* Require a check. A string that carries none, short or full, is then
|
|
160
|
+
* refused with code `CHECKSUM`. Set it when every string you decode was
|
|
161
|
+
* written with `checksum`: the header says whether a check follows, so
|
|
162
|
+
* damage to the first characters can otherwise turn a checked string into
|
|
163
|
+
* a valid one without a check. `'short'` means the same as `true`: either
|
|
164
|
+
* kind of check satisfies the requirement. Default false.
|
|
165
|
+
*/
|
|
166
|
+
checksum?: boolean | 'short';
|
|
167
|
+
/**
|
|
168
|
+
* The most characters a string may have, counted as its UTF-8 bytes
|
|
169
|
+
* (SPEC.md section 14): a character outside ASCII, which no string has,
|
|
170
|
+
* counts 2 to 4. A longer one is refused with code `LIMIT_EXCEEDED`
|
|
171
|
+
* before anything of it is read. An integer from 0
|
|
172
|
+
* up, or `Infinity`. Default 16,777,216 (2^24). No string of more than
|
|
173
|
+
* 2^28 characters is decoded, whatever this says.
|
|
174
|
+
*
|
|
175
|
+
* For a binary value it's the most bytes, with the same default, and no
|
|
176
|
+
* value of more than 201,326,592 bytes (3 * 2^26, the same number of
|
|
177
|
+
* bits) is decoded.
|
|
178
|
+
*/
|
|
179
|
+
maxLength?: number;
|
|
180
|
+
/**
|
|
181
|
+
* The most a string may hold: its positions, plus one for every line,
|
|
182
|
+
* ring, polygon and geometry in it, which take memory even when they're
|
|
183
|
+
* empty. A string that holds more is refused with code `LIMIT_EXCEEDED`
|
|
184
|
+
* as soon as a count in it says so, before the positions are read. An
|
|
185
|
+
* integer from 0 up, or `Infinity` for no limit. Default 1,048,576
|
|
186
|
+
* (2^20).
|
|
187
|
+
*
|
|
188
|
+
* A string can hold 24 positions per character and a binary value 32 per
|
|
189
|
+
* byte, so without this limit a megabyte of text decodes to 1.8 GB of
|
|
190
|
+
* arrays. With the default one call
|
|
191
|
+
* builds about 95 MB at most on Node and Deno, and 115 MB on Bun. Lower
|
|
192
|
+
* it to what your application expects when strings come from elsewhere,
|
|
193
|
+
* and raise it for larger geometries of your own.
|
|
194
|
+
*/
|
|
195
|
+
maxPositions?: number;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** What `canonical` writes its string at: the options of `encode`, without `favor`. */
|
|
199
|
+
export interface CanonicalTarget {
|
|
200
|
+
/** Decimal places for longitude and latitude, an integer from 0 to 15. Default 5. */
|
|
201
|
+
precision?: number;
|
|
202
|
+
/** Decimal places for the third value, an integer from 0 to 15. Default: `precision`. */
|
|
203
|
+
zPrecision?: number;
|
|
204
|
+
/** Decimal places for the fourth value, an integer from 0 to 15. Default: `precision`. */
|
|
205
|
+
mPrecision?: number;
|
|
206
|
+
/** The check the string ends in: `true` for the full one, `'short'`, or none. Default false. */
|
|
207
|
+
checksum?: boolean | 'short';
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Options for `canonical`: those of `decode`, for the string that comes in,
|
|
212
|
+
* and `to`, for the string that goes out.
|
|
213
|
+
*/
|
|
214
|
+
export interface CanonicalOptions extends DecodeOptions {
|
|
215
|
+
/**
|
|
216
|
+
* The settings to write the canonical string at: a precision, or the
|
|
217
|
+
* options of `encode`. Anything left out takes `encode`'s default, so
|
|
218
|
+
* nothing but the geometry comes from the string. Left out altogether,
|
|
219
|
+
* the string keeps its own precisions and its own kind of check.
|
|
220
|
+
*/
|
|
221
|
+
to?: number | CanonicalTarget;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Encodes a GeoJSON geometry (or a Feature's geometry) as a string.
|
|
226
|
+
* Coordinates are `[lng, lat]`, then optional `z` and `m`; every position
|
|
227
|
+
* must have the same number of values. Each one is rounded half away from
|
|
228
|
+
* zero at its precision, like Google's polyline algorithm.
|
|
229
|
+
*
|
|
230
|
+
* Throws a TypeError for input that isn't a geometry, and a RangeError for
|
|
231
|
+
* an option that isn't one of its values (a precision that isn't an integer
|
|
232
|
+
* from 0 to 15, an unknown `checksum` or `favor`), a coordinate that isn't
|
|
233
|
+
* finite, a rounded coordinate past 2^50, or collections nested deeper than
|
|
234
|
+
* 32.
|
|
235
|
+
*
|
|
236
|
+
* ```js
|
|
237
|
+
* encode({ type: 'LineString', coordinates: [[-120.2, 38.5], [-120.95, 40.7], [-126.453, 43.252]] });
|
|
238
|
+
* // 'JXO9tgHjZgAJOtsCSe-KJgFAo4'
|
|
239
|
+
* encode(geometry, { precision: 6, checksum: true });
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
export function encode(geometry: Geometry | Feature, options?: number | EncodeOptions): string;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Decodes a string from `encode` back to a GeoJSON geometry, `[lng, lat]`.
|
|
246
|
+
* The numbers are exactly what rounding the input gave: the integer in the
|
|
247
|
+
* string divided by 10^precision. The precision and the Z/M options travel
|
|
248
|
+
* in the string, so `decode` takes none.
|
|
249
|
+
*
|
|
250
|
+
* Throws a GeotiniError with a `code` for any string that isn't valid, and
|
|
251
|
+
* a TypeError if `str` isn't a string. A string that's longer than
|
|
252
|
+
* `maxLength` or holds more than `maxPositions` is refused with code
|
|
253
|
+
* `LIMIT_EXCEEDED`, valid or not. Both have defaults, 2^24 characters and
|
|
254
|
+
* 2^20 positions, so a geometry larger than that needs the option:
|
|
255
|
+
*
|
|
256
|
+
* ```js
|
|
257
|
+
* decode(str, { maxPositions: 5_000_000 }); // a large geometry of your own
|
|
258
|
+
* decode(str, { maxLength: 4096, maxPositions: 1000 }); // a string from a URL
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
export function decode(str: string, options?: DecodeOptions): Geometry;
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Encodes a line given as `[lat, lng]` points, polyline order, as a
|
|
265
|
+
* LineString. `[lat, lng, z]` and `[lat, lng, z, m]` work too, with
|
|
266
|
+
* `zPrecision` and `mPrecision` as in `encode`.
|
|
267
|
+
*
|
|
268
|
+
* ```js
|
|
269
|
+
* encodeLine([[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]); // 'JXO9tgHjZgAJOtsCSe-KJgFAo4'
|
|
270
|
+
* ```
|
|
271
|
+
*
|
|
272
|
+
* It writes the same string as `encode` of the LineString with the same
|
|
273
|
+
* positions, at every setting.
|
|
274
|
+
*/
|
|
275
|
+
export function encodeLine(points: readonly (readonly number[])[], options?: number | EncodeOptions): string;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Decodes a LineString string to `[lat, lng]` points, polyline order.
|
|
279
|
+
* Throws a GeotiniError with code `WRONG_TYPE` for any other geometry type.
|
|
280
|
+
* It takes the options of `decode`, and a line counts against
|
|
281
|
+
* `maxPositions` as it does there: its positions, plus 2.
|
|
282
|
+
*/
|
|
283
|
+
export function decodeLine(str: string, options?: DecodeOptions): Position[];
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The canonical string for the geometry a string decodes to: what
|
|
287
|
+
* `favor: 'size'` writes for it.
|
|
288
|
+
*
|
|
289
|
+
* With `to`, the string is written at those settings and takes nothing
|
|
290
|
+
* from `str` but its geometry: it's `encode(decode(str, options), { ...to,
|
|
291
|
+
* favor: 'size' })`. At settings your application fixes, one geometry has
|
|
292
|
+
* one string, whatever precision, check and coding choices it arrived
|
|
293
|
+
* with. That's the key to use for strings from elsewhere, in a cache, a
|
|
294
|
+
* set or an allow-list.
|
|
295
|
+
*
|
|
296
|
+
* Without `to`, the string keeps its own precisions and its own kind of
|
|
297
|
+
* check, and `canonical(str) === str` says whether `str` is canonical
|
|
298
|
+
* already. That isn't one string per geometry: the same Point has a
|
|
299
|
+
* canonical string at each of the 16 precisions and with each of the 3
|
|
300
|
+
* kinds of check, 48 in all, and whoever wrote the string picked one.
|
|
301
|
+
*
|
|
302
|
+
* ```js
|
|
303
|
+
* const key = canonical(str, { to: 6 }); // precision 6, no check
|
|
304
|
+
* canonical(str, { checksum: true, to: { precision: 6 } }); // and the string must come with a check
|
|
305
|
+
* canonical(str) === str; // whether str is canonical at its own settings
|
|
306
|
+
* ```
|
|
307
|
+
*
|
|
308
|
+
* It takes the options of `decode` for the string that comes in, and
|
|
309
|
+
* throws what `decode` throws for it. A target that isn't one (a precision
|
|
310
|
+
* of 16, `favor: 'speed'`) is a RangeError before the string is looked at,
|
|
311
|
+
* and a geometry that doesn't fit the target is the RangeError of `encode`.
|
|
312
|
+
* Canonical encoding takes 5 to 17 times as long as the default encoder,
|
|
313
|
+
* so for strings from elsewhere keep `maxPositions` low.
|
|
314
|
+
*/
|
|
315
|
+
export function canonical(str: string, options?: CanonicalOptions): string;
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Encodes a geometry as a binary value: the bits `encode` writes for the
|
|
319
|
+
* same geometry and options, 8 to a byte in a `Uint8Array` where a string
|
|
320
|
+
* has 6 to a character (SPEC.md section 23). It takes what `encode` takes
|
|
321
|
+
* and throws what it throws. The value is three quarters as long as the
|
|
322
|
+
* string, or a little more for a short one: a Point is 7 bytes for 9
|
|
323
|
+
* characters.
|
|
324
|
+
*
|
|
325
|
+
* Use it where bytes are stored as they are: a `bytea` or `BLOB` column, a
|
|
326
|
+
* Redis value, a field of a binary message. Compressed, the two forms come
|
|
327
|
+
* out about the same size (within 2% under gzip on our data), so over HTTP
|
|
328
|
+
* or in JSON a string is the better fit.
|
|
329
|
+
*
|
|
330
|
+
* ```js
|
|
331
|
+
* encodeValue({ type: 'Point', coordinates: [13.40495, 52.52001] });
|
|
332
|
+
* // Uint8Array [0x0d, 0x9a, 0x92, 0x0b, 0xa8, 0x4f, 0x3c]
|
|
333
|
+
* ```
|
|
334
|
+
*
|
|
335
|
+
* A value doesn't say that it's a geotini value, and the characters of a
|
|
336
|
+
* string given as bytes can decode as one. Keep the two forms in fields of
|
|
337
|
+
* their own, and where a value must not be mistaken write it with
|
|
338
|
+
* `checksum` and read it with `{ checksum: true }`.
|
|
339
|
+
*
|
|
340
|
+
* Each call returns a new array with a buffer of its own.
|
|
341
|
+
*/
|
|
342
|
+
export function encodeValue(geometry: Geometry | Feature, options?: number | EncodeOptions): Uint8Array;
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Decodes a binary value to a GeoJSON geometry: exactly what `decode`
|
|
346
|
+
* returns for the value's string. Any `Uint8Array` works, a Node `Buffer`
|
|
347
|
+
* and a view into a larger buffer included, and it isn't changed or kept.
|
|
348
|
+
*
|
|
349
|
+
* Throws a GeotiniError with a `code` for bytes that aren't a valid value,
|
|
350
|
+
* with the codes of `decode` except `BAD_CHARACTER`, and a TypeError if
|
|
351
|
+
* `bytes` isn't a `Uint8Array`. It takes the options of `decode`, with
|
|
352
|
+
* `maxLength` in bytes.
|
|
353
|
+
*
|
|
354
|
+
* ```js
|
|
355
|
+
* decodeValue(row.geom, { checksum: true, maxPositions: 100_000 });
|
|
356
|
+
* ```
|
|
357
|
+
*/
|
|
358
|
+
export function decodeValue(bytes: Uint8Array, options?: DecodeOptions): Geometry;
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* `encodeLine` for a binary value: `[lat, lng]` points as a LineString. It
|
|
362
|
+
* writes the same value as `encodeValue` of the LineString with the same
|
|
363
|
+
* positions.
|
|
364
|
+
*/
|
|
365
|
+
export function encodeLineValue(points: readonly (readonly number[])[], options?: number | EncodeOptions): Uint8Array;
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* `decodeLine` for a binary value: a LineString value to `[lat, lng]`
|
|
369
|
+
* points. Throws a GeotiniError with code `WRONG_TYPE` for any other
|
|
370
|
+
* geometry type.
|
|
371
|
+
*/
|
|
372
|
+
export function decodeLineValue(bytes: Uint8Array, options?: DecodeOptions): Position[];
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* `canonical` for a binary value: the canonical value for the geometry
|
|
376
|
+
* `bytes` decodes to, at its own settings or at the ones in `to`. It's the
|
|
377
|
+
* value of the string `canonical` gives for the value's string, so a
|
|
378
|
+
* canonical value works as a key the way a canonical string does.
|
|
379
|
+
*/
|
|
380
|
+
export function canonicalValue(bytes: Uint8Array, options?: CanonicalOptions): Uint8Array;
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* The binary value for a string: the same geometry, settings, kind of
|
|
384
|
+
* check and coding choices. `fromValue` gives the string back, character
|
|
385
|
+
* for character.
|
|
386
|
+
*
|
|
387
|
+
* It has to decode the string to find where its geometry ends, so it costs
|
|
388
|
+
* about a `decode`, takes the options of `decode` (`maxLength` in
|
|
389
|
+
* characters) and throws what `decode` throws for a string that isn't
|
|
390
|
+
* valid. The check isn't copied: a value's check is of its bytes, so a
|
|
391
|
+
* string with a check gets a new one of the same kind.
|
|
392
|
+
*
|
|
393
|
+
* ```js
|
|
394
|
+
* toValue('DZqSC6hPP'); // Uint8Array [0x0d, 0x9a, 0x92, 0x0b, 0xa8, 0x4f, 0x3c]
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
export function toValue(str: string, options?: DecodeOptions): Uint8Array;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* The string for a binary value: the same geometry, settings, kind of
|
|
401
|
+
* check and coding choices. `toValue` gives the value back, byte for byte.
|
|
402
|
+
* It takes the options of `decodeValue` (`maxLength` in bytes) and throws
|
|
403
|
+
* what `decodeValue` throws for bytes that aren't a valid value. The
|
|
404
|
+
* string is about a third longer than the value, and `maxLength` doesn't
|
|
405
|
+
* limit it.
|
|
406
|
+
*
|
|
407
|
+
* ```js
|
|
408
|
+
* fromValue(Uint8Array.of(0x0d, 0x9a, 0x92, 0x0b, 0xa8, 0x4f, 0x3c)); // 'DZqSC6hPP'
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
411
|
+
export function fromValue(bytes: Uint8Array, options?: DecodeOptions): string;
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Why a string was refused. The codes are stable, the same in every
|
|
415
|
+
* implementation of the format (SPEC.md section 14).
|
|
416
|
+
*/
|
|
417
|
+
export type GeotiniErrorCode =
|
|
418
|
+
/** The string is empty or ends early, or a count in it is larger than the rest of it could hold. */
|
|
419
|
+
| 'TRUNCATED'
|
|
420
|
+
/** A character outside base64url (`A-Z a-z 0-9 - _`). Never for a binary value, where every byte can occur. */
|
|
421
|
+
| 'BAD_CHARACTER'
|
|
422
|
+
/** A header reserved for a later version of the format. */
|
|
423
|
+
| 'UNSUPPORTED_VERSION'
|
|
424
|
+
/** A precision above 7 in a string that isn't wide. */
|
|
425
|
+
| 'BAD_HEADER'
|
|
426
|
+
/** A second spelling of something that has one: a longer form than needed, an option the string doesn't use, or an open path that ends on its first point. */
|
|
427
|
+
| 'NON_CANONICAL'
|
|
428
|
+
/** The check doesn't match: the string was changed. Or a check was required and the string has none. */
|
|
429
|
+
| 'CHECKSUM'
|
|
430
|
+
/** A count larger than the format can count. */
|
|
431
|
+
| 'COUNT_TOO_LARGE'
|
|
432
|
+
/** A value outside its allowed range. */
|
|
433
|
+
| 'OUT_OF_RANGE'
|
|
434
|
+
/** A closed path with no points. */
|
|
435
|
+
| 'EMPTY_CLOSED_PATH'
|
|
436
|
+
/** Collections nested more than 32 deep. */
|
|
437
|
+
| 'TOO_DEEP'
|
|
438
|
+
/** Data after the end of the geometry. */
|
|
439
|
+
| 'TRAILING_DATA'
|
|
440
|
+
/** `decodeLine` was given another geometry type. */
|
|
441
|
+
| 'WRONG_TYPE'
|
|
442
|
+
/** The string is longer than `maxLength` or holds more than `maxPositions`. The one code that isn't about the string itself: with other limits the same string may decode. */
|
|
443
|
+
| 'LIMIT_EXCEEDED';
|
|
444
|
+
|
|
445
|
+
/** What `decode` and `decodeLine` throw for a string they refuse, and `decodeValue` and the others for a binary value. Both entry points, and `/reference`, throw this same class. */
|
|
446
|
+
export class GeotiniError extends Error {
|
|
447
|
+
/** A GeotiniError for a code from SPEC.md section 14, with an optional message. */
|
|
448
|
+
constructor(code: GeotiniErrorCode, message?: string);
|
|
449
|
+
/** Why the string was refused. */
|
|
450
|
+
readonly code: GeotiniErrorCode;
|
|
451
|
+
/** Always "GeotiniError". */
|
|
452
|
+
readonly name: 'GeotiniError';
|
|
453
|
+
}
|