@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
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Types for @duoquote/geotini/leaflet. Structural, so they don't depend on
|
|
2
|
+
// @types/leaflet and work for JSR too.
|
|
3
|
+
/**
|
|
4
|
+
* Leaflet: geotini strings to layers and back. Leaflet isn't imported;
|
|
5
|
+
* `install(L)` and the functions take it from you. Leaflet 1.9 and 2.
|
|
6
|
+
*
|
|
7
|
+
* ```js
|
|
8
|
+
* import L from 'leaflet';
|
|
9
|
+
* import { install } from '@duoquote/geotini/leaflet';
|
|
10
|
+
* install(L);
|
|
11
|
+
* L.GeoJSON.fromGeotini(str).addTo(map);
|
|
12
|
+
* const back = layer.toGeotini(6);
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
import type { EncodeOptions, DecodeOptions } from './index.d.ts';
|
|
18
|
+
import type { FeatureOptions } from './turf.d.ts';
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
/** [lat, lng] or [lat, lng, alt], as Leaflet takes them. */
|
|
22
|
+
export type LatLngTuple = [number, number] | [number, number, number];
|
|
23
|
+
/** Latlngs for L.Polyline (one level, or two for a MultiLineString) or L.Polygon (rings, or polygons of rings). */
|
|
24
|
+
export type LatLngs = LatLngTuple[] | LatLngTuple[][] | LatLngTuple[][][];
|
|
25
|
+
|
|
26
|
+
/** The parts of Leaflet that install() and toLayer use. */
|
|
27
|
+
export interface LeafletLike {
|
|
28
|
+
/** The L.Polyline class. */
|
|
29
|
+
Polyline: any;
|
|
30
|
+
/** The L.Polygon class. */
|
|
31
|
+
Polygon: any;
|
|
32
|
+
/** The L.GeoJSON class. */
|
|
33
|
+
GeoJSON: any;
|
|
34
|
+
/** The L.Layer class. */
|
|
35
|
+
Layer: any;
|
|
36
|
+
/** The rest of Leaflet. */
|
|
37
|
+
[key: string]: any;
|
|
38
|
+
}
|
|
39
|
+
/** Anything with Leaflet's toGeoJSON(precision). */
|
|
40
|
+
export interface LayerLike {
|
|
41
|
+
/** Leaflet's toGeoJSON(precision). */
|
|
42
|
+
toGeoJSON(precision?: number | false): any;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Latlngs for a LineString, MultiLineString, Polygon or MultiPolygon string; throws GeotiniError WRONG_TYPE for other types. Options are decode's. */
|
|
46
|
+
export function toLatLngs(str: string, options?: DecodeOptions): LatLngs;
|
|
47
|
+
/** An L.GeoJSON layer for one string or many. Options are L.GeoJSON's, plus `properties` and `id` for the Features and decode's options. */
|
|
48
|
+
export function toLayer<L extends LeafletLike>(L: L, encoded: string | readonly string[], options?: FeatureOptions & Record<string, unknown>): any;
|
|
49
|
+
/** Encodes a layer through its toGeoJSON(false); a group of several geometries becomes one GeometryCollection string. */
|
|
50
|
+
export function fromLayer(layer: LayerLike, options?: number | EncodeOptions): string;
|
|
51
|
+
/**
|
|
52
|
+
* Adds L.Polyline.fromGeotini, L.Polygon.fromGeotini, L.GeoJSON.fromGeotini,
|
|
53
|
+
* layer.toGeotini() and, where L can take it, the factory L.geotini. Returns L.
|
|
54
|
+
* Each fromGeotini(str, options) takes the layer's options plus decode's.
|
|
55
|
+
*/
|
|
56
|
+
export function install<L extends LeafletLike>(L: L): L;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Types for @duoquote/geotini/mapbox: the same as /maplibre.
|
|
2
|
+
/**
|
|
3
|
+
* Geotini strings as GeoJSON sources and layers in Mapbox GL JS 3: the same
|
|
4
|
+
* functions as `@duoquote/geotini/maplibre`, since the calls they make take
|
|
5
|
+
* the same arguments in both libraries.
|
|
6
|
+
*
|
|
7
|
+
* ```js
|
|
8
|
+
* import { addGeotiniLayer } from '@duoquote/geotini/mapbox';
|
|
9
|
+
* map.on('load', () => addGeotiniLayer(map, 'route', str));
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
import type * as maplibre from './maplibre.d.ts';
|
|
15
|
+
|
|
16
|
+
export type { SourceOptions, LayerOptions, MapLike } from './maplibre.d.ts';
|
|
17
|
+
|
|
18
|
+
/** The FeatureCollection for one string or many; see `@duoquote/geotini/maplibre`. */
|
|
19
|
+
export declare const geotiniData: typeof maplibre.geotiniData;
|
|
20
|
+
/** A GeoJSON source specification for one string or many; see `@duoquote/geotini/maplibre`. */
|
|
21
|
+
export declare const geotiniSource: typeof maplibre.geotiniSource;
|
|
22
|
+
/** Adds a GeoJSON source and a fill, line or circle layer; see `@duoquote/geotini/maplibre`. */
|
|
23
|
+
export declare const addGeotiniLayer: typeof maplibre.addGeotiniLayer;
|
|
24
|
+
/** Replaces a source's data with new strings; see `@duoquote/geotini/maplibre`. */
|
|
25
|
+
export declare const setGeotiniData: typeof maplibre.setGeotiniData;
|
|
26
|
+
/** Encodes a Feature's geometry; see `@duoquote/geotini/turf`. */
|
|
27
|
+
export declare const fromFeature: typeof maplibre.fromFeature;
|
|
28
|
+
/** Encodes every Feature of a FeatureCollection; see `@duoquote/geotini/turf`. */
|
|
29
|
+
export declare const fromFeatureCollection: typeof maplibre.fromFeatureCollection;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Types for @duoquote/geotini/maplibre. Structural, so they don't depend on
|
|
2
|
+
// maplibre-gl's types and work for JSR too.
|
|
3
|
+
/**
|
|
4
|
+
* Geotini strings as GeoJSON sources and layers in MapLibre GL JS. No map
|
|
5
|
+
* library code is imported; the functions call addSource, addLayer and
|
|
6
|
+
* getSource(id).setData on the map you pass.
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { addGeotiniLayer } from '@duoquote/geotini/maplibre';
|
|
10
|
+
* map.on('load', () => addGeotiniLayer(map, 'parcels', strings, { layer: { paint: { 'fill-color': '#0a7' } } }));
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import type { Geometry } from './index.d.ts';
|
|
16
|
+
import type { FeatureOptions, GeoFeatureCollection } from './turf.d.ts';
|
|
17
|
+
import type * as turf from './turf.d.ts';
|
|
18
|
+
|
|
19
|
+
/** Encodes a Feature's geometry, the same as `fromFeature` in `@duoquote/geotini/turf`: for features you get back from the map. */
|
|
20
|
+
export declare const fromFeature: typeof turf.fromFeature;
|
|
21
|
+
/** Encodes every Feature of a FeatureCollection, the same as `fromFeatureCollection` in `@duoquote/geotini/turf`. */
|
|
22
|
+
export declare const fromFeatureCollection: typeof turf.fromFeatureCollection;
|
|
23
|
+
|
|
24
|
+
/** Options for the data, plus `source` options (promoteId, tolerance, ...) for the source specification. */
|
|
25
|
+
export interface SourceOptions<P = Record<string, unknown>> extends FeatureOptions<P> {
|
|
26
|
+
/** Other options for the source specification: promoteId, tolerance, cluster, and so on. */
|
|
27
|
+
source?: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
/** Source options plus the layer: `layer` is merged into the layer specification, `beforeId` goes to addLayer. */
|
|
30
|
+
export interface LayerOptions<P = Record<string, unknown>> extends SourceOptions<P> {
|
|
31
|
+
/** Merged into the layer specification: paint, layout, filter, minzoom, or a `type` other than the inferred one. */
|
|
32
|
+
layer?: { type?: 'fill' | 'line' | 'circle' | 'fill-extrusion' | 'symbol' | 'heatmap'; [key: string]: unknown };
|
|
33
|
+
/** The id of an existing layer to insert the new layer before. */
|
|
34
|
+
beforeId?: string;
|
|
35
|
+
}
|
|
36
|
+
/** The parts of a map these functions use. */
|
|
37
|
+
export interface MapLike {
|
|
38
|
+
/** The map's addSource. */
|
|
39
|
+
addSource(id: string, source: any): unknown;
|
|
40
|
+
/** The map's addLayer. */
|
|
41
|
+
addLayer(layer: any, beforeId?: string): unknown;
|
|
42
|
+
/** The map's getSource. */
|
|
43
|
+
getSource(id: string): any;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A FeatureCollection, one Feature per string. */
|
|
47
|
+
export function geotiniData<P = Record<string, unknown>>(encoded: string | readonly string[], options?: FeatureOptions<P>): GeoFeatureCollection<Geometry, P>;
|
|
48
|
+
/** A GeoJSON source specification: `{ type: 'geojson', data, ...options.source }`. */
|
|
49
|
+
export function geotiniSource<P = Record<string, unknown>>(encoded: string | readonly string[], options?: SourceOptions<P>): { type: 'geojson'; data: GeoFeatureCollection<Geometry, P>; [key: string]: unknown };
|
|
50
|
+
/** Adds a GeoJSON source and a fill, line or circle layer (from the first non-empty geometry), both named `id`. Returns the map. */
|
|
51
|
+
export function addGeotiniLayer<M extends MapLike>(map: M, id: string, encoded: string | readonly string[], options?: LayerOptions): M;
|
|
52
|
+
/** Replaces the data of the GeoJSON source `id`; returns what its setData returns. */
|
|
53
|
+
export function setGeotiniData(map: MapLike, id: string, encoded: string | readonly string[], options?: FeatureOptions): unknown;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Types for @duoquote/geotini/mongodb.
|
|
2
|
+
/**
|
|
3
|
+
* Geometries in MongoDB as geotini strings. A document keeps the canonical
|
|
4
|
+
* string next to a small GeoJSON geometry for the 2dsphere index: the
|
|
5
|
+
* geometry's box as a polygon, its centroid, or the whole geometry. No
|
|
6
|
+
* MongoDB code is imported: documents, index specifications and filters are
|
|
7
|
+
* plain objects for any driver.
|
|
8
|
+
*
|
|
9
|
+
* ```js
|
|
10
|
+
* import { toDocument, fromDocument, indexSpec, geoIntersects } from '@duoquote/geotini/mongodb';
|
|
11
|
+
* await parcels.createIndex(indexSpec());
|
|
12
|
+
* await parcels.insertOne({ name, ...toDocument(geometry, { precision: 6 }) });
|
|
13
|
+
* for await (const doc of parcels.find(geoIntersects(viewport))) draw(fromDocument(doc));
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* With `binary: true` the field holds the binary value (SPEC.md section 23)
|
|
17
|
+
* instead of the string, which the driver stores as BSON Binary.
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
import type { Geometry, Point, Polygon, MultiPolygon, Feature, CanonicalTarget, DecodeOptions } from './index.d.ts';
|
|
22
|
+
|
|
23
|
+
/** What goes in the index field. */
|
|
24
|
+
export type IndexMode = 'bbox' | 'centroid' | 'geometry';
|
|
25
|
+
|
|
26
|
+
/** Where the two fields are. */
|
|
27
|
+
export interface FieldOptions {
|
|
28
|
+
/** The field of the geotini string. 'geom' by default. fromDocument also takes a dotted path. */
|
|
29
|
+
field?: string;
|
|
30
|
+
/** The field of the index geometry. field + '_index' by default. */
|
|
31
|
+
indexField?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The settings strings are written at: encode's, without favor (it's always 'size'), and decode's for a string given. */
|
|
35
|
+
export interface KeyOptions extends CanonicalTarget {
|
|
36
|
+
/** decode's options, for reading a geotini string or a binary value given as input. */
|
|
37
|
+
decode?: DecodeOptions;
|
|
38
|
+
/** Write the canonical binary value, a Uint8Array the driver stores as BSON Binary, instead of the string. */
|
|
39
|
+
binary?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The options of toDocument. */
|
|
43
|
+
export interface DocumentOptions extends KeyOptions, FieldOptions {
|
|
44
|
+
/** 'bbox' (the default), 'centroid', 'geometry', or false for no index field. */
|
|
45
|
+
index?: IndexMode | false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The two fields toDocument gives: the string (or the binary value), and the index geometry or null. */
|
|
49
|
+
export type GeotiniFields = Record<string, string | Uint8Array | Geometry | null>;
|
|
50
|
+
|
|
51
|
+
/** Options of the query filters. */
|
|
52
|
+
export interface QueryOptions extends FieldOptions {
|
|
53
|
+
/** decode's options, for a geotini string given as the query geometry. */
|
|
54
|
+
decode?: DecodeOptions;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Options of geoWithin and geoIntersects. */
|
|
58
|
+
export interface AreaOptions extends QueryOptions {
|
|
59
|
+
/** Read a Polygon by its winding, for one larger than a hemisphere. */
|
|
60
|
+
strict?: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Options of near. */
|
|
64
|
+
export interface NearOptions extends QueryOptions {
|
|
65
|
+
/** The farthest a document may be, in metres. */
|
|
66
|
+
maxDistance?: number;
|
|
67
|
+
/** The nearest a document may be, in metres. */
|
|
68
|
+
minDistance?: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A BSON Binary as the driver gives one back, from the `bson` package. Only
|
|
73
|
+
* these fields are read; the generic subtype, 0, is a binary value.
|
|
74
|
+
*/
|
|
75
|
+
export interface BsonBinary {
|
|
76
|
+
/** The `bson` package's tag for this type. */
|
|
77
|
+
readonly _bsontype: 'Binary';
|
|
78
|
+
/** The bytes; only the first `position` of them are the value. */
|
|
79
|
+
readonly buffer: Uint8Array;
|
|
80
|
+
/** How many bytes of `buffer` are in use. */
|
|
81
|
+
readonly position: number;
|
|
82
|
+
/** The BSON binary subtype; 0, generic, holds a geotini value. */
|
|
83
|
+
readonly sub_type: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A geometry, a Feature, a geotini string, or a binary value. */
|
|
87
|
+
export type GeometryInput = Geometry | Feature | string | Uint8Array | BsonBinary;
|
|
88
|
+
|
|
89
|
+
/** The canonical string toDocument writes for an input, at the settings in options. */
|
|
90
|
+
export function key(input: GeometryInput, options?: KeyOptions & { binary?: false }): string;
|
|
91
|
+
/** The canonical binary value toDocument writes for an input with `binary: true`. */
|
|
92
|
+
export function key(input: GeometryInput, options: KeyOptions & { binary: true }): Uint8Array;
|
|
93
|
+
/** The canonical string, or with `binary: true` the binary value. */
|
|
94
|
+
export function key(input: GeometryInput, options?: KeyOptions): string | Uint8Array;
|
|
95
|
+
/** The index geometry for a geometry: its box on the sphere as a polygon, its centroid, or the geometry in 2D. null when it has no positions. */
|
|
96
|
+
export function indexGeometry(geometry: Geometry | Feature, index?: IndexMode): Geometry | null;
|
|
97
|
+
/** The string field and the index field to put in a document. */
|
|
98
|
+
export function toDocument(input: GeometryInput, options?: DocumentOptions): GeotiniFields;
|
|
99
|
+
/** The geometry of a document, or null when it has none: a string field as a string, a binary one as a value. Options are decode's, and field. */
|
|
100
|
+
export function fromDocument(doc: unknown, options?: DecodeOptions & Pick<FieldOptions, 'field'>): Geometry | null;
|
|
101
|
+
/** The 2dsphere index on the index field, for createIndex. */
|
|
102
|
+
export function indexSpec(options?: FieldOptions): Record<string, '2dsphere'>;
|
|
103
|
+
/** A filter for documents whose index geometry lies inside an area. */
|
|
104
|
+
export function geoWithin(area: Polygon | MultiPolygon | Feature | string | Uint8Array | BsonBinary, options?: AreaOptions): Record<string, { $geoWithin: { $geometry: Geometry } }>;
|
|
105
|
+
/** A filter for documents whose index geometry meets a geometry. */
|
|
106
|
+
export function geoIntersects(geometry: GeometryInput, options?: AreaOptions): Record<string, { $geoIntersects: { $geometry: Geometry } }>;
|
|
107
|
+
/** A filter that sorts documents by distance from a point, nearest first. Needs the 2dsphere index. */
|
|
108
|
+
export function near(point: readonly number[] | Point | Feature | string | Uint8Array | BsonBinary, options?: NearOptions): Record<string, { $near: { $geometry: Point; $maxDistance?: number; $minDistance?: number } }>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Types for @duoquote/geotini/openlayers. Structural, so they don't depend
|
|
2
|
+
// on ol's types and work for JSR too.
|
|
3
|
+
/**
|
|
4
|
+
* OpenLayers: a feature format for geotini strings, a subclass of the
|
|
5
|
+
* ol/format/GeoJSON class you pass. ol isn't imported.
|
|
6
|
+
*
|
|
7
|
+
* ```js
|
|
8
|
+
* import GeoJSON from 'ol/format/GeoJSON.js';
|
|
9
|
+
* import { createGeotiniFormat } from '@duoquote/geotini/openlayers';
|
|
10
|
+
* const format = new (createGeotiniFormat({ GeoJSON }))({ precision: 6, featureProjection: 'EPSG:3857' });
|
|
11
|
+
* const feature = format.readFeature(str);
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
import type { EncodeOptions, DecodeOptions } from './index.d.ts';
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/** ol GeoJSON's constructor options plus encode's, and `decode` for the strings it reads. */
|
|
20
|
+
export type GeotiniFormatOptions = EncodeOptions & {
|
|
21
|
+
/** decode's options (`checksum`, `maxLength`, `maxPositions`) for every string the format reads. They're apart from encode's, since `checksum` means something else there. */
|
|
22
|
+
decode?: DecodeOptions;
|
|
23
|
+
} & Record<string, unknown>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Returns a subclass of `GeoJSON` that reads geotini strings where GeoJSON
|
|
27
|
+
* text or objects go (readGeometry, readFeature, readFeatures, which also
|
|
28
|
+
* takes an array of strings) and writes strings (writeGeometry,
|
|
29
|
+
* writeFeature, and writeFeatures as one GeometryCollection string).
|
|
30
|
+
*/
|
|
31
|
+
export function createGeotiniFormat<C extends new (options?: any) => any>(classes: { GeoJSON: C }): new (options?: GeotiniFormatOptions) => InstanceType<C> & {
|
|
32
|
+
/** The encode options this format writes with. */
|
|
33
|
+
readonly encodeOptions: EncodeOptions;
|
|
34
|
+
/** The decode options this format reads with, if any. */
|
|
35
|
+
readonly decodeOptions: DecodeOptions | undefined;
|
|
36
|
+
writeGeometry(geometry: any, options?: object): string;
|
|
37
|
+
writeFeature(feature: any, options?: object): string;
|
|
38
|
+
writeFeatures(features: any[], options?: object): string;
|
|
39
|
+
};
|
package/types/redis.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Types for @duoquote/geotini/redis.
|
|
2
|
+
/**
|
|
3
|
+
* Geometries in Redis as geotini strings, one key or one hash field each,
|
|
4
|
+
* with the centroid in a GEO set for GEOSEARCH, and a cache keyed by the
|
|
5
|
+
* canonical string. You pass a node-redis (4 and newer) or ioredis (5 and
|
|
6
|
+
* newer) client; no Redis code is imported.
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { createStore, cached } from '@duoquote/geotini/redis';
|
|
10
|
+
* const parcels = createStore(client, { prefix: 'parcel:', geoKey: 'parcels:geo', precision: 6 });
|
|
11
|
+
* await parcels.set('p1', geometry);
|
|
12
|
+
* const near = await parcels.search({ lng: 14.42, lat: 50.08, radius: 500, withGeometry: true });
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
import type { Geometry, Feature, CanonicalTarget, DecodeOptions } from './index.d.ts';
|
|
18
|
+
|
|
19
|
+
/** A node-redis client (it has sendCommand and multi) or an ioredis one (it has call and multi). */
|
|
20
|
+
export type RedisClient =
|
|
21
|
+
| { sendCommand(args: string[]): Promise<unknown>; multi(): unknown }
|
|
22
|
+
| { call(...args: string[]): Promise<unknown>; multi(commands?: unknown[]): unknown };
|
|
23
|
+
|
|
24
|
+
/** A geometry, a Feature, or a geotini string. */
|
|
25
|
+
export type GeometryInput = Geometry | Feature | string;
|
|
26
|
+
|
|
27
|
+
/** The options of createStore. */
|
|
28
|
+
export interface StoreOptions extends CanonicalTarget {
|
|
29
|
+
/** The prefix of each geometry's key. 'geotini:' by default. */
|
|
30
|
+
prefix?: string;
|
|
31
|
+
/** Keep the strings as fields of this one hash, not in keys of their own. */
|
|
32
|
+
hash?: string;
|
|
33
|
+
/** The GEO set of the points: prefix + 'geo' by default, false for none. */
|
|
34
|
+
geoKey?: string | false;
|
|
35
|
+
/** decode's options, for every string the store reads. */
|
|
36
|
+
decode?: DecodeOptions;
|
|
37
|
+
/** Keep each geometry's binary value, read back with buffer replies, instead of its string. */
|
|
38
|
+
binary?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** What GEOSEARCH looks for. */
|
|
42
|
+
export interface SearchQuery {
|
|
43
|
+
/** Longitude of the centre, with lat. */
|
|
44
|
+
lng?: number;
|
|
45
|
+
/** Latitude of the centre, with lng. */
|
|
46
|
+
lat?: number;
|
|
47
|
+
/** An id in the store, as the centre, in place of lng and lat. */
|
|
48
|
+
member?: string | number;
|
|
49
|
+
/** Search within this distance. */
|
|
50
|
+
radius?: number;
|
|
51
|
+
/** Or within a box this wide, with height. */
|
|
52
|
+
width?: number;
|
|
53
|
+
/** The box's height, with width. */
|
|
54
|
+
height?: number;
|
|
55
|
+
/** The unit of the distances: 'm' (the default), 'km', 'mi' or 'ft'. */
|
|
56
|
+
unit?: 'm' | 'km' | 'mi' | 'ft';
|
|
57
|
+
/** At most this many results. */
|
|
58
|
+
count?: number;
|
|
59
|
+
/** With count, stop at the first count found, not the nearest count. */
|
|
60
|
+
any?: boolean;
|
|
61
|
+
/** 'ASC', nearest first (the default), or 'DESC'. */
|
|
62
|
+
order?: 'ASC' | 'DESC';
|
|
63
|
+
/** Fetch each geometry too. */
|
|
64
|
+
withGeometry?: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** One result of a search. */
|
|
68
|
+
export interface SearchResult {
|
|
69
|
+
/** The id. */
|
|
70
|
+
id: string;
|
|
71
|
+
/** Its distance from the centre, in the query's unit. */
|
|
72
|
+
distance: number;
|
|
73
|
+
/** The geometry, with withGeometry. */
|
|
74
|
+
geometry?: Geometry | null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The store createStore returns. `W` is what it writes: a string, or with
|
|
79
|
+
* `binary: true` a Uint8Array holding the binary value.
|
|
80
|
+
*/
|
|
81
|
+
export interface GeotiniStore<W extends string | Uint8Array = string> {
|
|
82
|
+
/** Writes a geometry, a geotini string or a binary value under id in one MULTI with its point, and returns what it wrote. */
|
|
83
|
+
set(id: string | number, input: GeometryInput | Uint8Array, options?: { point?: readonly [number, number] }): Promise<W>;
|
|
84
|
+
/** The geometry under id, or null. */
|
|
85
|
+
get(id: string | number): Promise<Geometry | null>;
|
|
86
|
+
/** The string under id, or null. A binary store converts its value. */
|
|
87
|
+
getString(id: string | number): Promise<string | null>;
|
|
88
|
+
/** The geometries under several ids, in order, null where there's none. */
|
|
89
|
+
getMany(ids: readonly (string | number)[]): Promise<(Geometry | null)[]>;
|
|
90
|
+
/** Removes the geometry under id and its point. */
|
|
91
|
+
delete(id: string | number): Promise<void>;
|
|
92
|
+
/** GEOSEARCH on the GEO set. Redis 6.2 and newer. */
|
|
93
|
+
search(query: SearchQuery): Promise<SearchResult[]>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The options of cacheKey and cached. */
|
|
97
|
+
export interface CacheOptions extends DecodeOptions {
|
|
98
|
+
/** The settings every key is written at: a precision, or encode's options without favor. Required. */
|
|
99
|
+
to: number | CanonicalTarget;
|
|
100
|
+
/** The prefix of the keys. 'geotini:cache:' by default. */
|
|
101
|
+
prefix?: string;
|
|
102
|
+
/** Seconds until a cached value expires. */
|
|
103
|
+
ttl?: number;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The point a geometry goes into a GEO set at: its centroid, latitude within ±85.05112878. null without positions. */
|
|
107
|
+
export function geoPoint(geometry: Geometry | Feature): [number, number] | null;
|
|
108
|
+
/** A store of geometries under ids, in keys or one hash, with their points in a GEO set. */
|
|
109
|
+
export function createStore(client: RedisClient, options?: StoreOptions & { binary?: false }): GeotiniStore<string>;
|
|
110
|
+
/** A store that keeps each geometry's binary value, read back with buffer replies. */
|
|
111
|
+
export function createStore(client: RedisClient, options: StoreOptions & { binary: true }): GeotiniStore<Uint8Array>;
|
|
112
|
+
/** A store of strings, or of binary values with `binary: true`. */
|
|
113
|
+
export function createStore(client: RedisClient, options?: StoreOptions): GeotiniStore<string> | GeotiniStore<Uint8Array>;
|
|
114
|
+
/** The cache key: prefix and the canonical string at options.to. */
|
|
115
|
+
export function cacheKey(input: GeometryInput, options: CacheOptions): string;
|
|
116
|
+
/** The cached value for a geometry, or compute(geometry) stored under its key. */
|
|
117
|
+
export function cached(client: RedisClient, input: GeometryInput, compute: (geometry: Geometry) => string | Promise<string>, options: CacheOptions): Promise<string>;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Types for @duoquote/geotini/reference.
|
|
2
|
+
/**
|
|
3
|
+
* The reference codec: SPEC.md turned into code one bit at a time, written
|
|
4
|
+
* to be easy to check, not to be fast. It decodes exactly what the default
|
|
5
|
+
* codec decodes and refuses the same strings with the same codes, and the
|
|
6
|
+
* same binary values. Its
|
|
7
|
+
* encoder is the canonical encoder of SPEC.md section 16.4, the one the
|
|
8
|
+
* main entry calls `favor: 'size'`, and the two write the same strings.
|
|
9
|
+
* About 11 KB gzipped against 32.
|
|
10
|
+
*
|
|
11
|
+
* ```js
|
|
12
|
+
* import { decode } from '@duoquote/geotini/reference';
|
|
13
|
+
* decode('DZqSC6hPP'); // { type: 'Point', coordinates: [13.40495, 52.52001] }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
import type { Geometry, Feature, Position, EncodeOptions, DecodeOptions, CanonicalOptions } from './index.d.ts';
|
|
19
|
+
|
|
20
|
+
export type { Geometry, Feature, Position, EncodeOptions, DecodeOptions, CanonicalOptions, CanonicalTarget, GeotiniErrorCode, Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection } from './index.d.ts';
|
|
21
|
+
import type * as core from './index.d.ts';
|
|
22
|
+
|
|
23
|
+
/** The same error class as the main entry's: both codecs throw it, with the same codes. */
|
|
24
|
+
export declare const GeotiniError: typeof core.GeotiniError;
|
|
25
|
+
/** What decoding throws, with a `code` from SPEC.md section 14. */
|
|
26
|
+
export type GeotiniError = core.GeotiniError;
|
|
27
|
+
|
|
28
|
+
/** The coding choices of a string (SPEC.md section 16.3), in the order a decoder reads them. */
|
|
29
|
+
export interface Choices {
|
|
30
|
+
/** The predictor bit of Z, then of M, when the string has them. */
|
|
31
|
+
pred: number[];
|
|
32
|
+
/** The mode of each geometry that has one. */
|
|
33
|
+
modes: number[];
|
|
34
|
+
/** Every Rice parameter: `k` of a paired block, `k1` and `k2` of a split one, then the block's Z and M parameters. */
|
|
35
|
+
k: number[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Options for the reference encoder. It has no `favor`: it's the canonical
|
|
40
|
+
* encoder. The others are for building test strings and worked examples;
|
|
41
|
+
* with any of them the string isn't canonical any more.
|
|
42
|
+
*/
|
|
43
|
+
export interface ReferenceEncodeOptions extends Omit<EncodeOptions, 'favor'> {
|
|
44
|
+
/** Only `'size'`, which is what this encoder always is. `'speed'` throws a RangeError. */
|
|
45
|
+
favor?: 'size';
|
|
46
|
+
/** The predictor mode for every geometry that has a step: 0 delta, 1 d2, 2 slope-fold, 3 ortho, 4 ema, 5 slope-lin. Throws a RangeError when the mode can't write the geometry. */
|
|
47
|
+
mode?: 0 | 1 | 2 | 3 | 4 | 5;
|
|
48
|
+
/** The coding choices to write with, as `inspect` returns them. They must fit the geometry. */
|
|
49
|
+
choices?: Choices;
|
|
50
|
+
/** Forms no encoder may write and every decoder refuses unless the string needs them: a wide string, the openRings bit, or paths left open although they end on their first point. */
|
|
51
|
+
force?: { wide?: boolean; openRings?: boolean; open?: boolean };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** What `inspect` returns: a string's header fields, its coding choices and its geometry. */
|
|
55
|
+
export interface Inspection {
|
|
56
|
+
/** The geometry type's name. */
|
|
57
|
+
type: string;
|
|
58
|
+
/** The precision in the header. */
|
|
59
|
+
precision: number;
|
|
60
|
+
/** The precision of Z, when the string has Z. */
|
|
61
|
+
zPrecision?: number;
|
|
62
|
+
/** The precision of M, when the string has M. */
|
|
63
|
+
mPrecision?: number;
|
|
64
|
+
/** The kind of check: `true` for the full one, `'short'`, or `false`. */
|
|
65
|
+
checksum: boolean | 'short';
|
|
66
|
+
/** Whether the string is wide. */
|
|
67
|
+
wide: boolean;
|
|
68
|
+
/** Whether rings carry a closed bit. */
|
|
69
|
+
openRings: boolean;
|
|
70
|
+
/** The number of payload bits: from the first bit of the header to the last bit of the geometry, without padding or check (SPEC.md section 23.5). The same for a string and its binary value. */
|
|
71
|
+
bits: number;
|
|
72
|
+
/** The coding choices. */
|
|
73
|
+
choices: Choices;
|
|
74
|
+
/** What the string decodes to. */
|
|
75
|
+
geometry: Geometry;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Called for each field the encoder writes, with a label and its bits as a '0'/'1' string. */
|
|
79
|
+
export type Trace = (label: string, bits: string) => void;
|
|
80
|
+
|
|
81
|
+
/** Encodes a geometry with the canonical encoder. `trace` sees each field as it's written. */
|
|
82
|
+
export function encode(geometry: Geometry | Feature, options?: number | ReferenceEncodeOptions, trace?: Trace): string;
|
|
83
|
+
/** Decodes a string to a GeoJSON geometry, reading one bit at a time. It has the limits of the main entry's `decode`, `maxLength` and `maxPositions`, with the same defaults. */
|
|
84
|
+
export function decode(str: string, options?: DecodeOptions): Geometry;
|
|
85
|
+
/** Encodes `[lat, lng]` points as a LineString: the same string as `encode` of that LineString. */
|
|
86
|
+
export function encodeLine(points: readonly (readonly number[])[], options?: number | ReferenceEncodeOptions): string;
|
|
87
|
+
/** Decodes a LineString string to `[lat, lng]` points. */
|
|
88
|
+
export function decodeLine(str: string, options?: DecodeOptions): Position[];
|
|
89
|
+
/** Decodes a string and returns its header fields and coding choices with its geometry. Takes `decode`'s options. */
|
|
90
|
+
export function inspect(str: string, options?: DecodeOptions): Inspection;
|
|
91
|
+
/**
|
|
92
|
+
* The canonical string for the geometry a string decodes to. With `to`, at
|
|
93
|
+
* those settings, which is `encode(decode(str, options), to)`. Without it,
|
|
94
|
+
* at the string's own precisions and with its kind of check. Takes
|
|
95
|
+
* `decode`'s options for the string that comes in.
|
|
96
|
+
*/
|
|
97
|
+
export function canonical(str: string, options?: CanonicalOptions): string;
|
|
98
|
+
|
|
99
|
+
/** Encodes a geometry as a binary value (SPEC.md section 23): the bits of `encode`'s string for the same options, 8 to a byte, with the check of a value. `trace` sees each field as it's written. */
|
|
100
|
+
export function encodeValue(geometry: Geometry | Feature, options?: number | ReferenceEncodeOptions, trace?: Trace): Uint8Array;
|
|
101
|
+
/** Decodes a binary value to a GeoJSON geometry, reading one bit at a time. Takes `decode`'s options, with `maxLength` in bytes. */
|
|
102
|
+
export function decodeValue(bytes: Uint8Array, options?: DecodeOptions): Geometry;
|
|
103
|
+
/** Encodes `[lat, lng]` points as a LineString value: the same bytes as `encodeValue` of that LineString. */
|
|
104
|
+
export function encodeLineValue(points: readonly (readonly number[])[], options?: number | ReferenceEncodeOptions): Uint8Array;
|
|
105
|
+
/** Decodes a LineString value to `[lat, lng]` points. */
|
|
106
|
+
export function decodeLineValue(bytes: Uint8Array, options?: DecodeOptions): Position[];
|
|
107
|
+
/** `inspect` for a binary value: its header fields, coding choices and geometry. They're those of its string. */
|
|
108
|
+
export function inspectValue(bytes: Uint8Array, options?: DecodeOptions): Inspection;
|
|
109
|
+
/** The canonical value for the geometry a value decodes to, at its own settings or at `to`: the value of the canonical string of its string. */
|
|
110
|
+
export function canonicalValue(bytes: Uint8Array, options?: CanonicalOptions): Uint8Array;
|
|
111
|
+
/** The binary value for a string (SPEC.md section 23.6). Decodes the string with `decode`'s options, and throws what `decode` throws for one that isn't valid. */
|
|
112
|
+
export function toValue(str: string, options?: DecodeOptions): Uint8Array;
|
|
113
|
+
/** The string for a binary value, the inverse of `toValue`. Decodes the value with `decodeValue`'s options, and throws what `decodeValue` throws for one that isn't valid. */
|
|
114
|
+
export function fromValue(bytes: Uint8Array, options?: DecodeOptions): string;
|
|
115
|
+
|
|
116
|
+
/** The 64 characters, in value order. */
|
|
117
|
+
export const ALPHABET: string;
|
|
118
|
+
/** The geometry types by number, as in the extended header. */
|
|
119
|
+
export const TYPES: readonly string[];
|
|
120
|
+
/** The first Rice parameter for each precision, 0 to 15. */
|
|
121
|
+
export const KI: readonly number[];
|
|
122
|
+
/** The 17 scale values of the ortho predictor (SPEC.md section 9.4). */
|
|
123
|
+
export const COS2_17: readonly number[];
|
|
124
|
+
/** The reciprocal table of divSlope, for divisors 1 to 511. */
|
|
125
|
+
export const INV: Int32Array;
|
|
126
|
+
/** The first-position code parameters for precisions 0 to 7 (SPEC.md section 7). */
|
|
127
|
+
export const ANCHOR: readonly { NA: number; NB: number; bA: number; bB: number; TA: number; TB: number; offA: number; offB: number; maxA: number; maxB: number }[];
|
|
128
|
+
/** The integer slope a / b in 1/4096 units (SPEC.md section 9.2). */
|
|
129
|
+
export function divSlope(a: number, b: number): number;
|
|
130
|
+
/** The ortho predictor's scale for a path's first latitude integer at a precision: 4096 beyond 90 degrees. */
|
|
131
|
+
export function cos2Int(lat0: number, prec: number): number;
|
|
132
|
+
/** CRC-32C of a string's character codes or of bytes, or of the first `length` of them. */
|
|
133
|
+
export function crc32c(data: string | Uint8Array, length?: number): number;
|
package/types/turf.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Types for @duoquote/geotini/turf.
|
|
2
|
+
/**
|
|
3
|
+
* GeoJSON Features from geotini strings, in the shape Turf.js builds them,
|
|
4
|
+
* and strings from the Features Turf returns. No Turf code is imported.
|
|
5
|
+
*
|
|
6
|
+
* ```js
|
|
7
|
+
* import { toFeature, fromFeature } from '@duoquote/geotini/turf';
|
|
8
|
+
* turf.area(toFeature(str));
|
|
9
|
+
* fromFeature(turf.simplify(toFeature(str), { tolerance: 0.0001 }), 6);
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
import type { Geometry, EncodeOptions, DecodeOptions } from './index.d.ts';
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
/** A GeoJSON Feature as Turf makes it. */
|
|
18
|
+
export interface GeoFeature<G extends Geometry = Geometry, P = Record<string, unknown>> {
|
|
19
|
+
/** Always "Feature". */
|
|
20
|
+
type: 'Feature';
|
|
21
|
+
/** The id, when given. */
|
|
22
|
+
id?: string | number;
|
|
23
|
+
/** [west, south, east, north], when asked for with `bbox: true`. */
|
|
24
|
+
bbox?: [number, number, number, number];
|
|
25
|
+
/** The properties, {} by default. */
|
|
26
|
+
properties: P;
|
|
27
|
+
/** The decoded geometry. */
|
|
28
|
+
geometry: G;
|
|
29
|
+
}
|
|
30
|
+
/** A GeoJSON FeatureCollection. */
|
|
31
|
+
export interface GeoFeatureCollection<G extends Geometry = Geometry, P = Record<string, unknown>> {
|
|
32
|
+
/** Always "FeatureCollection". */
|
|
33
|
+
type: 'FeatureCollection';
|
|
34
|
+
/** One Feature per string. */
|
|
35
|
+
features: GeoFeature<G, P>[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* What goes on each Feature, with an array entry i on the Feature of string
|
|
40
|
+
* i, and decode's options (`checksum`, `maxLength`, `maxPositions`) for
|
|
41
|
+
* every string.
|
|
42
|
+
*/
|
|
43
|
+
export interface FeatureOptions<P = Record<string, unknown>> extends DecodeOptions {
|
|
44
|
+
/** Properties for every Feature, or an array with one object per string. */
|
|
45
|
+
properties?: P | P[];
|
|
46
|
+
/** An id for every Feature, or an array with one per string. */
|
|
47
|
+
id?: string | number | (string | number)[];
|
|
48
|
+
/** Add a [west, south, east, north] bbox to each Feature that has positions. */
|
|
49
|
+
bbox?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A Feature with the decoded geometry. */
|
|
53
|
+
export function toFeature<P = Record<string, unknown>>(str: string, options?: FeatureOptions<P>): GeoFeature<Geometry, P>;
|
|
54
|
+
/** A FeatureCollection, one Feature per string. */
|
|
55
|
+
export function toFeatureCollection<P = Record<string, unknown>>(encoded: string | readonly string[], options?: FeatureOptions<P>): GeoFeatureCollection<Geometry, P>;
|
|
56
|
+
/** Encodes a Feature's geometry, or a bare geometry. Properties aren't kept. */
|
|
57
|
+
export function fromFeature(feature: { type: 'Feature'; geometry: Geometry | null } | Geometry, options?: number | EncodeOptions): string;
|
|
58
|
+
/** Encodes each Feature of a FeatureCollection: one string per Feature, in order. Throws a TypeError for a Feature without a geometry. */
|
|
59
|
+
export function fromFeatureCollection(fc: { type: 'FeatureCollection'; features: { type: 'Feature'; geometry: Geometry | null }[] }, options?: number | EncodeOptions): string[];
|