@math.gl/wkb 5.0.0-alpha.2

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/LICENSE ADDED
@@ -0,0 +1,140 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Uber Technologies, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
23
+ ---
24
+
25
+ Some primitive tessellation approaches were originally inspired by the TDL WebGL samples under
26
+ the New BSD License:
27
+
28
+ Copyright (c) 2011, Google Inc. All rights reserved.
29
+
30
+ Redistribution and use in source and binary forms, with or without modification, are permitted
31
+ provided that the following conditions are met:
32
+
33
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions
34
+ and the following disclaimer.
35
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of
36
+ conditions and the following disclaimer in the documentation and/or other materials provided
37
+ with the distribution.
38
+ 3. Neither the name of Google Inc. nor the names of its contributors may be used to endorse or
39
+ promote products derived from this software without specific prior written permission.
40
+
41
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
42
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
43
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
44
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
46
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
47
+ IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49
+
50
+ ---
51
+
52
+ math.gl builds on docs and code from "gl-matrix", which is MIT licensed as follows:
53
+
54
+ Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
55
+
56
+ Permission is hereby granted, free of charge, to any person obtaining a copy
57
+ of this software and associated documentation files (the "Software"), to deal
58
+ in the Software without restriction, including without limitation the rights
59
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
60
+ copies of the Software, and to permit persons to whom the Software is
61
+ furnished to do so, subject to the following conditions:
62
+
63
+ The above copyright notice and this permission notice shall be included in
64
+ all copies or substantial portions of the Software.
65
+
66
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
69
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
71
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
72
+ THE SOFTWARE.
73
+
74
+ --
75
+
76
+ math.gl builds on docs and code from THREE.js, which is MIT licensed as follows:
77
+
78
+ The MIT License
79
+
80
+ Copyright © 2010-2017 three.js authors
81
+
82
+ Permission is hereby granted, free of charge, to any person obtaining a copy
83
+ of this software and associated documentation files (the "Software"), to deal
84
+ in the Software without restriction, including without limitation the rights
85
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
86
+ copies of the Software, and to permit persons to whom the Software is
87
+ furnished to do so, subject to the following conditions:
88
+
89
+ The above copyright notice and this permission notice shall be included in
90
+ all copies or substantial portions of the Software.
91
+
92
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
93
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
94
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
95
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
96
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
97
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
98
+ THE SOFTWARE.
99
+
100
+ ---
101
+
102
+ math.gl includes certain files from Cesium (https://github.com/AnalyticalGraphicsInc/cesium) under the Apache 2 License:
103
+
104
+ Copyright 2011-2018 CesiumJS Contributors
105
+
106
+ Licensed under the Apache License, Version 2.0 (the "License");
107
+ you may not use this file except in compliance with the License.
108
+ You may obtain a copy of the License at
109
+ http://www.apache.org/licenses/LICENSE-2.0
110
+
111
+ Unless required by applicable law or agreed to in writing, software
112
+ distributed under the License is distributed on an "AS IS" BASIS,
113
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114
+ See the License for the specific language governing permissions and limitations under the License.
115
+
116
+ Cesium-derived code can be found in the submodule: modules/3d-tiles
117
+
118
+ ---
119
+
120
+ The @math.gl/geometry primitive tessellators are adapted from luma.gl under the MIT License:
121
+
122
+ Copyright (c) 2020 vis.gl contributors
123
+
124
+ Permission is hereby granted, free of charge, to any person obtaining a copy
125
+ of this software and associated documentation files (the "Software"), to deal
126
+ in the Software without restriction, including without limitation the rights
127
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
128
+ copies of the Software, and to permit persons to whom the Software is
129
+ furnished to do so, subject to the following conditions:
130
+
131
+ The above copyright notice and this permission notice shall be included in
132
+ all copies or substantial portions of the Software.
133
+
134
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
135
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
136
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
137
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
138
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
139
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
140
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,232 @@
1
+ # @math.gl/wkb
2
+
3
+ Dependency-free, synchronous codecs for Well-Known Binary (WKB), Extended WKB (EWKB), and
4
+ Well-Known Text (WKT) geometry.
5
+
6
+ `@math.gl/wkb` is the format layer beneath `@math.gl/geoarrow`. It parses and writes individual
7
+ geometry values without importing Arrow, GeoArrow, loaders.gl, or a geometry class hierarchy.
8
+ Applications can therefore use the codecs for row-oriented data, database values, network
9
+ messages, or as a building block for their own columnar adapters.
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ npm install @math.gl/wkb
15
+ ```
16
+
17
+ The package has no runtime, peer, or optional dependencies.
18
+
19
+ ## Quick start
20
+
21
+ ```typescript
22
+ import {parseWKB, writeWKB, parseWKT, formatWKT} from '@math.gl/wkb';
23
+
24
+ const geometry = parseWKT('LINESTRING Z (0 0 5, 10 20 6)');
25
+ const bytes = writeWKB(geometry, 'xyz');
26
+
27
+ const parsed = parseWKB(bytes);
28
+ console.log(parsed.geometry);
29
+ console.log(parsed.dimension); // 'xyz'
30
+ console.log(formatWKT(parsed.geometry, parsed.dimension));
31
+ ```
32
+
33
+ All APIs are synchronous. Inputs are borrowed and never detached or mutated. Returned geometry
34
+ values are plain immutable-by-contract objects and arrays.
35
+
36
+ ## Geometry values
37
+
38
+ Both formats use the same `WellKnownGeometry` discriminated union:
39
+
40
+ ```typescript
41
+ type WellKnownGeometry =
42
+ | {type: 'Point'; coordinates: readonly number[]}
43
+ | {type: 'LineString'; coordinates: readonly (readonly number[])[]}
44
+ | {type: 'Polygon'; coordinates: readonly (readonly (readonly number[])[])[]}
45
+ | {type: 'MultiPoint'; coordinates: readonly (readonly number[])[]}
46
+ | {type: 'MultiLineString'; coordinates: readonly (readonly (readonly number[])[])[]}
47
+ | {type: 'MultiPolygon'; coordinates: readonly (readonly (readonly (readonly number[])[])[])[]}
48
+ | {type: 'GeometryCollection'; geometries: readonly WellKnownGeometry[]};
49
+ ```
50
+
51
+ Coordinates use semantic dimensions `xy`, `xyz`, `xym`, or `xyzm`. Three-number tuples are
52
+ inferred as XYZ because tuple width alone cannot distinguish Z from M; pass `xym` explicitly when
53
+ writing measured geometry.
54
+
55
+ ## WKB
56
+
57
+ ### Parse
58
+
59
+ ```typescript
60
+ const result = parseWKB(bytes);
61
+
62
+ result.geometry; // WellKnownGeometry
63
+ result.byteLength; // exact bytes consumed
64
+ result.dimension; // 'xy' | 'xyz' | 'xym' | 'xyzm'
65
+ result.srid; // EWKB root SRID, when present
66
+ ```
67
+
68
+ `parseWKB` accepts:
69
+
70
+ - little- and big-endian WKB;
71
+ - ISO SQL/MM Z, M, and ZM type offsets;
72
+ - EWKB Z/M/SRID header flags;
73
+ - Point, LineString, Polygon, all Multi geometries, and nested GeometryCollection values.
74
+
75
+ The parser requires exactly one complete geometry. It rejects trailing bytes, truncated buffers,
76
+ invalid byte order, wrong child families inside Multi geometries, excessive nesting, and excessive
77
+ declared element counts.
78
+
79
+ ```typescript
80
+ const result = parseWKB(untrustedBytes, {
81
+ maximumDepth: 32,
82
+ maximumElements: 1_000_000
83
+ });
84
+ ```
85
+
86
+ Limits are checked before the corresponding geometry arrays are allocated. Defaults are 64 levels
87
+ and 100 million total declared child elements.
88
+
89
+ ### Write
90
+
91
+ ```typescript
92
+ const xy = writeWKB({type: 'Point', coordinates: [1, 2]});
93
+ const measured = writeWKB({type: 'Point', coordinates: [1, 2, 9]}, 'xym');
94
+ ```
95
+
96
+ `writeWKB` emits deterministic little-endian ISO WKB. Missing ordinates are written as zero. It
97
+ does not emit an EWKB SRID; carry CRS/SRID metadata outside the neutral geometry value or in a
98
+ higher-level container.
99
+
100
+ ### Inspect and scan without decoding
101
+
102
+ Use `inspectWKBHeader` when routing or classifying data. It reads only the endian flag, type code,
103
+ dimension flags, and optional EWKB SRID; the coordinate payload does not need to be present.
104
+
105
+ ```typescript
106
+ import {inspectWKBHeader, scanWKB} from '@math.gl/wkb';
107
+
108
+ const header = inspectWKBHeader(bytes);
109
+ console.log(header.geometryType, header.dimension, header.srid);
110
+
111
+ const statistics = scanWKB(bytes, {maximumElements: 1_000_000});
112
+ console.log(statistics.coordinateCount);
113
+ console.log(statistics.geometryCounts);
114
+ console.log(statistics.bounds); // XYZM-aware finite bounds
115
+ ```
116
+
117
+ `scanWKB` validates exactly one complete value and reports its byte length, geometry families,
118
+ per-family counts, coordinate and ring counts, maximum nesting depth, and finite coordinate bounds.
119
+ It accepts sliced typed-array views and mixed-endian nested geometries.
120
+
121
+ ### Visit coordinates directly
122
+
123
+ `visitWKB` exposes the same validated traversal through callbacks. Coordinate values are passed as
124
+ individual numbers rather than temporary coordinate arrays.
125
+
126
+ ```typescript
127
+ import {visitWKB} from '@math.gl/wkb';
128
+
129
+ visitWKB(bytes, {
130
+ geometry: (header, count, depth) => {
131
+ console.log(header.geometryType, count, depth);
132
+ },
133
+ ring: (pointCount, ringIndex) => {
134
+ console.log(pointCount, ringIndex);
135
+ },
136
+ coordinate: (x, y, z, m, dimension, byteOffset) => {
137
+ // Inspect, copy, transform, or aggregate directly from the source buffer.
138
+ }
139
+ });
140
+ ```
141
+
142
+ The visitor does not detach or mutate the input and does not create `WellKnownGeometry` rows.
143
+
144
+ ### Two-pass caller-buffer writing
145
+
146
+ `WKBBuilder` supports the same geometry event sequence in measurement and write modes. Write mode
147
+ accepts an existing buffer, a sliced typed-array view, and an optional destination offset.
148
+
149
+ ```typescript
150
+ import {WKBBuilder} from '@math.gl/wkb';
151
+
152
+ function emitLine(builder: WKBBuilder): void {
153
+ builder.beginLineString(2);
154
+ builder.writeCoordinate(0, 0, 5);
155
+ builder.writeCoordinate(10, 20, 6);
156
+ }
157
+
158
+ const measure = new WKBBuilder({mode: 'measure', dimension: 'xyz'});
159
+ emitLine(measure);
160
+
161
+ const target = new Uint8Array(measure.finishGeometry());
162
+ const writer = new WKBBuilder({mode: 'write', target, dimension: 'xyz'});
163
+ emitLine(writer);
164
+ ```
165
+
166
+ Builder options include little- or big-endian output, XY/XYZ/XYM/XYZM dimensions, EWKB SRID, and
167
+ coordinate transformation. `WKBBuilder.buildGeometryArray()` additionally returns plain Int32
168
+ offsets, contiguous bytes, and an optional validity bitmap for columnar adapters. It never creates
169
+ Arrow objects.
170
+
171
+ ## WKT
172
+
173
+ ```typescript
174
+ const polygon = parseWKT('POLYGON ((0 0, 4 0, 0 4, 0 0))');
175
+ const text = formatWKT(polygon);
176
+ ```
177
+
178
+ The parser supports:
179
+
180
+ - all seven geometry families;
181
+ - Z, M, and ZM dimension tokens, inherited through nested collections;
182
+ - both `MULTIPOINT (1 2, 3 4)` and `MULTIPOINT ((1 2), (3 4))`;
183
+ - `EMPTY`, including dimension-sized empty points;
184
+ - decimal and exponent notation;
185
+ - arbitrary whitespace while rejecting every unrecognized non-whitespace character.
186
+
187
+ `formatWKT` accepts an optional explicit dimension. As with WKB, pass `xym` when a three-value
188
+ tuple represents a measure rather than elevation.
189
+
190
+ ## Empty geometry
191
+
192
+ Empty non-point geometry uses an empty coordinate array. Empty points use a tuple of `NaN` values
193
+ with the declared dimension. Formatting treats an all-non-finite coordinate tuple as `POINT EMPTY`.
194
+
195
+ ## GeoArrow integration
196
+
197
+ `@math.gl/geoarrow` owns column descriptors, validity, offsets, chunks, and column metadata. Its
198
+ `decodeGeoArrowWKB`, `encodeGeoArrowWKB`, `decodeGeoArrowWKT`, and `encodeGeoArrowWKT` functions
199
+ delegate individual values to this package:
200
+
201
+ ```typescript
202
+ import {parseWKB} from '@math.gl/wkb';
203
+ import {decodeGeoArrowWKB} from '@math.gl/geoarrow';
204
+
205
+ const oneGeometry = parseWKB(bytes);
206
+ const nativeColumn = decodeGeoArrowWKB(serializedColumn);
207
+ ```
208
+
209
+ This dependency points one way: GeoArrow may depend on WKB, while WKB is prevented by a package
210
+ boundary check from referencing GeoArrow or Apache Arrow.
211
+
212
+ ## API
213
+
214
+ - `parseWKB(bytes, options?)`
215
+ - `writeWKB(geometry, dimension?)`
216
+ - `inspectWKBHeader(bytes, byteOffset?)`
217
+ - `visitWKB(bytes, visitor, options?)`
218
+ - `scanWKB(bytes, options?)`
219
+ - `WKBBuilder`
220
+ - `parseWKT(text)`
221
+ - `formatWKT(geometry, dimension?)`
222
+ - `getWellKnownDimensionSize(dimension)`
223
+ - `inferWellKnownGeometryDimension(geometry)`
224
+ - `WellKnownGeometry`
225
+ - `WellKnownDimension`
226
+ - `WKBParseOptions`
227
+ - `WKBParseResult`
228
+ - `WKBHeader`
229
+ - `WKBVisitor`
230
+ - `WKBScanResult`
231
+ - `WKBBuilderOptions`
232
+ - `WKBGeometryArray`