@math.gl/geoarrow 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 +140 -0
- package/README.md +311 -0
- package/dist/builder.d.ts +90 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +428 -0
- package/dist/builder.js.map +1 -0
- package/dist/codecs.d.ts +10 -0
- package/dist/codecs.d.ts.map +1 -0
- package/dist/codecs.js +122 -0
- package/dist/codecs.js.map +1 -0
- package/dist/index.cjs +1651 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/kernels.d.ts +51 -0
- package/dist/kernels.d.ts.map +1 -0
- package/dist/kernels.js +359 -0
- package/dist/kernels.js.map +1 -0
- package/dist/layout.d.ts +52 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +669 -0
- package/dist/layout.js.map +1 -0
- package/dist/tessellate.d.ts +31 -0
- package/dist/tessellate.d.ts.map +1 -0
- package/dist/tessellate.js +102 -0
- package/dist/tessellate.js.map +1 -0
- package/dist/types.d.ts +113 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +41 -0
- package/dist/types.js.map +1 -0
- package/dist/worker.cjs +73 -0
- package/dist/worker.cjs.map +6 -0
- package/dist/worker.d.ts +11 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +10 -0
- package/dist/worker.js.map +1 -0
- package/package.json +48 -0
- package/src/builder.ts +569 -0
- package/src/codecs.ts +155 -0
- package/src/index.ts +93 -0
- package/src/kernels.ts +463 -0
- package/src/layout.ts +833 -0
- package/src/tessellate.ts +133 -0
- package/src/types.ts +200 -0
- package/src/worker.ts +20 -0
package/src/codecs.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {formatWKT, parseWKB, parseWKT, writeWKB} from '@math.gl/wkb';
|
|
6
|
+
import type {
|
|
7
|
+
GeoArrowColumn,
|
|
8
|
+
GeoArrowDimension,
|
|
9
|
+
GeoArrowGeometryValue,
|
|
10
|
+
GeoArrowSerialized
|
|
11
|
+
} from './types';
|
|
12
|
+
import {getGeoArrowDimensionSize} from './types';
|
|
13
|
+
import {makeGeoArrowColumnFromGeometryRows} from './builder';
|
|
14
|
+
import {getGeoArrowOffset, isGeoArrowValueValid, materializeGeoArrowRows} from './layout';
|
|
15
|
+
|
|
16
|
+
const textEncoder = new TextEncoder();
|
|
17
|
+
const textDecoder = new TextDecoder();
|
|
18
|
+
|
|
19
|
+
/** Decodes a GeoArrow WKB column into native physical geometry buffers. */
|
|
20
|
+
export function decodeGeoArrowWKB(column: GeoArrowColumn): GeoArrowColumn {
|
|
21
|
+
if (column.encoding !== 'geoarrow.wkb') throw new Error('Expected a geoarrow.wkb column');
|
|
22
|
+
const rows = normalizeRowsToDimension(
|
|
23
|
+
decodeSerializedRows(column, bytes => parseWKB(bytes).geometry),
|
|
24
|
+
column.dimension
|
|
25
|
+
);
|
|
26
|
+
return copyMetadata(
|
|
27
|
+
column,
|
|
28
|
+
makeGeoArrowColumnFromGeometryRows(rows, {dimension: column.dimension})
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Encodes a native GeoArrow column as variable-width WKB bytes. */
|
|
33
|
+
export function encodeGeoArrowWKB(column: GeoArrowColumn): GeoArrowColumn {
|
|
34
|
+
if (column.encoding === 'geoarrow.wkb') return column;
|
|
35
|
+
const rows = materializeGeoArrowRows(column);
|
|
36
|
+
const values = rows.map(row => (row ? writeWKB(row, column.dimension) : null));
|
|
37
|
+
return copyMetadata(column, makeSerializedColumn(values, 'geoarrow.wkb', column.dimension));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Decodes a GeoArrow WKT column into native physical geometry buffers. */
|
|
41
|
+
export function decodeGeoArrowWKT(column: GeoArrowColumn): GeoArrowColumn {
|
|
42
|
+
if (column.encoding !== 'geoarrow.wkt') throw new Error('Expected a geoarrow.wkt column');
|
|
43
|
+
const rows = normalizeRowsToDimension(
|
|
44
|
+
decodeSerializedRows(column, bytes => parseWKT(textDecoder.decode(bytes))),
|
|
45
|
+
column.dimension
|
|
46
|
+
);
|
|
47
|
+
return copyMetadata(
|
|
48
|
+
column,
|
|
49
|
+
makeGeoArrowColumnFromGeometryRows(rows, {dimension: column.dimension})
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Encodes a native GeoArrow column as variable-width UTF-8 WKT. */
|
|
54
|
+
export function encodeGeoArrowWKT(column: GeoArrowColumn): GeoArrowColumn {
|
|
55
|
+
if (column.encoding === 'geoarrow.wkt') return column;
|
|
56
|
+
const rows = materializeGeoArrowRows(column);
|
|
57
|
+
const values = rows.map(row =>
|
|
58
|
+
row ? textEncoder.encode(formatWKT(row, column.dimension)) : null
|
|
59
|
+
);
|
|
60
|
+
return copyMetadata(column, makeSerializedColumn(values, 'geoarrow.wkt', column.dimension));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function normalizeRowsToDimension(
|
|
64
|
+
rows: readonly (GeoArrowGeometryValue | null)[],
|
|
65
|
+
dimension: GeoArrowDimension
|
|
66
|
+
): Array<GeoArrowGeometryValue | null> {
|
|
67
|
+
const size = getGeoArrowDimensionSize(dimension);
|
|
68
|
+
const normalizeGeometry = (geometry: GeoArrowGeometryValue): GeoArrowGeometryValue => {
|
|
69
|
+
if (geometry.type === 'GeometryCollection') {
|
|
70
|
+
return {...geometry, geometries: geometry.geometries.map(normalizeGeometry)};
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
...geometry,
|
|
74
|
+
coordinates: normalizeCoordinateNesting(geometry.coordinates, size)
|
|
75
|
+
} as GeoArrowGeometryValue;
|
|
76
|
+
};
|
|
77
|
+
return rows.map(row => (row ? normalizeGeometry(row) : null));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function normalizeCoordinateNesting(value: readonly unknown[], size: number): unknown {
|
|
81
|
+
if (value.length === 0) return [];
|
|
82
|
+
if (typeof value[0] === 'number') {
|
|
83
|
+
const coordinate = (value as readonly number[]).slice(0, size);
|
|
84
|
+
while (coordinate.length < size) coordinate.push(0);
|
|
85
|
+
return coordinate;
|
|
86
|
+
}
|
|
87
|
+
return value.map(child => normalizeCoordinateNesting(child as readonly unknown[], size));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function decodeSerializedRows(
|
|
91
|
+
column: GeoArrowColumn,
|
|
92
|
+
decoder: (bytes: Uint8Array) => GeoArrowGeometryValue
|
|
93
|
+
): Array<GeoArrowGeometryValue | null> {
|
|
94
|
+
const rows: Array<GeoArrowGeometryValue | null> = [];
|
|
95
|
+
for (const chunk of column.chunks) {
|
|
96
|
+
if (chunk.kind !== 'serialized') throw new Error('Serialized column contains native storage');
|
|
97
|
+
for (let rowIndex = 0; rowIndex < chunk.length; rowIndex++) {
|
|
98
|
+
if (!isGeoArrowValueValid(chunk.validity, rowIndex)) {
|
|
99
|
+
rows.push(null);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
rows.push(decoder(getSerializedBytes(chunk, rowIndex)));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return rows;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function getSerializedBytes(array: GeoArrowSerialized, rowIndex: number): Uint8Array {
|
|
109
|
+
const offsetIndex = (array.offset || 0) + rowIndex;
|
|
110
|
+
const baseValue = array.offsetBase ?? 0;
|
|
111
|
+
const base = typeof baseValue === 'bigint' ? Number(baseValue) : baseValue;
|
|
112
|
+
const first = getGeoArrowOffset(array.offsets, offsetIndex) - base;
|
|
113
|
+
const last = getGeoArrowOffset(array.offsets, offsetIndex + 1) - base;
|
|
114
|
+
return array.values.subarray(first, last);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function makeSerializedColumn(
|
|
118
|
+
rows: readonly (Uint8Array | null)[],
|
|
119
|
+
encoding: 'geoarrow.wkb' | 'geoarrow.wkt',
|
|
120
|
+
dimension: GeoArrowDimension
|
|
121
|
+
): GeoArrowColumn {
|
|
122
|
+
let byteLength = 0;
|
|
123
|
+
for (const row of rows) byteLength += row?.byteLength || 0;
|
|
124
|
+
const values = new Uint8Array(byteLength);
|
|
125
|
+
const offsets = new Int32Array(rows.length + 1);
|
|
126
|
+
const validity = new Uint8Array(Math.ceil(rows.length / 8));
|
|
127
|
+
let byteOffset = 0;
|
|
128
|
+
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
|
|
129
|
+
const row = rows[rowIndex];
|
|
130
|
+
if (row) {
|
|
131
|
+
values.set(row, byteOffset);
|
|
132
|
+
byteOffset += row.byteLength;
|
|
133
|
+
validity[rowIndex >> 3] |= 1 << (rowIndex & 7);
|
|
134
|
+
}
|
|
135
|
+
offsets[rowIndex + 1] = byteOffset;
|
|
136
|
+
}
|
|
137
|
+
const chunk: GeoArrowSerialized = {
|
|
138
|
+
kind: 'serialized',
|
|
139
|
+
encoding: encoding === 'geoarrow.wkb' ? 'binary' : 'utf8',
|
|
140
|
+
length: rows.length,
|
|
141
|
+
offsets,
|
|
142
|
+
values,
|
|
143
|
+
validity: {values: validity}
|
|
144
|
+
};
|
|
145
|
+
return {encoding, dimension, coordinateLayout: null, chunks: [chunk]};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function copyMetadata(source: GeoArrowColumn, target: GeoArrowColumn): GeoArrowColumn {
|
|
149
|
+
return {
|
|
150
|
+
...target,
|
|
151
|
+
spatialReference: source.spatialReference,
|
|
152
|
+
edges: source.edges,
|
|
153
|
+
metadata: source.metadata
|
|
154
|
+
};
|
|
155
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
export type {
|
|
6
|
+
GeoArrowArray,
|
|
7
|
+
GeoArrowArrayBase,
|
|
8
|
+
GeoArrowBox,
|
|
9
|
+
GeoArrowBounds,
|
|
10
|
+
GeoArrowColumn,
|
|
11
|
+
GeoArrowCoordinateLayout,
|
|
12
|
+
GeoArrowCoordinateMapper,
|
|
13
|
+
GeoArrowDenseUnion,
|
|
14
|
+
GeoArrowDenseUnionChild,
|
|
15
|
+
GeoArrowDimension,
|
|
16
|
+
GeoArrowEncoding,
|
|
17
|
+
GeoArrowFixedSizeList,
|
|
18
|
+
GeoArrowGeometryValue,
|
|
19
|
+
GeoArrowList,
|
|
20
|
+
GeoArrowNumericArray,
|
|
21
|
+
GeoArrowOffsets,
|
|
22
|
+
GeoArrowPrimitive,
|
|
23
|
+
GeoArrowSerialized,
|
|
24
|
+
GeoArrowStruct,
|
|
25
|
+
GeoArrowValidity
|
|
26
|
+
} from './types';
|
|
27
|
+
export {
|
|
28
|
+
getGeoArrowDimensionSize,
|
|
29
|
+
getGeoArrowEncodingForGeometry,
|
|
30
|
+
getGeoArrowGeometryType
|
|
31
|
+
} from './types';
|
|
32
|
+
|
|
33
|
+
export type {
|
|
34
|
+
GeoArrowColumnInspection,
|
|
35
|
+
GeoArrowValidationIssue,
|
|
36
|
+
GeoArrowValidationResult
|
|
37
|
+
} from './layout';
|
|
38
|
+
export {
|
|
39
|
+
getGeoArrowRowCount,
|
|
40
|
+
getGeoArrowTransferList,
|
|
41
|
+
getGeoArrowVertexCount,
|
|
42
|
+
inspectGeoArrowColumn,
|
|
43
|
+
isGeoArrowValueValid,
|
|
44
|
+
sliceGeoArrowArray,
|
|
45
|
+
sliceGeoArrowColumn,
|
|
46
|
+
validateGeoArrowColumn,
|
|
47
|
+
visitGeoArrowCoordinates
|
|
48
|
+
} from './layout';
|
|
49
|
+
|
|
50
|
+
export type {
|
|
51
|
+
GeoArrowBuilderEncoding,
|
|
52
|
+
GeoArrowBuilderMeasurement,
|
|
53
|
+
GeoArrowBuilderModeOptions,
|
|
54
|
+
GeoArrowBuilderOptions,
|
|
55
|
+
GeoArrowColumnFromRowsOptions,
|
|
56
|
+
GeoArrowBuilderTarget
|
|
57
|
+
} from './builder';
|
|
58
|
+
export {
|
|
59
|
+
allocateGeoArrowBuilderTarget,
|
|
60
|
+
GeoArrowBuilder,
|
|
61
|
+
makeGeoArrowColumnFromGeometryRows
|
|
62
|
+
} from './builder';
|
|
63
|
+
|
|
64
|
+
export type {
|
|
65
|
+
ConvertGeoArrowColumnOptions,
|
|
66
|
+
GeoArrowResourceLimitOptions,
|
|
67
|
+
GeoArrowRingOrientation,
|
|
68
|
+
MapGeoArrowCoordinatesOptions,
|
|
69
|
+
RewindGeoArrowOptions
|
|
70
|
+
} from './kernels';
|
|
71
|
+
export {
|
|
72
|
+
assertGeoArrowResourceLimits,
|
|
73
|
+
convertGeoArrowColumn,
|
|
74
|
+
getGeoArrowBounds,
|
|
75
|
+
interleaveGeoArrowCoordinates,
|
|
76
|
+
mapGeoArrowCoordinates,
|
|
77
|
+
mapGeoArrowCoordinatesInto,
|
|
78
|
+
normalizeGeoArrowUnion,
|
|
79
|
+
rewindGeoArrow
|
|
80
|
+
} from './kernels';
|
|
81
|
+
|
|
82
|
+
export {
|
|
83
|
+
decodeGeoArrowWKB,
|
|
84
|
+
decodeGeoArrowWKT,
|
|
85
|
+
encodeGeoArrowWKB,
|
|
86
|
+
encodeGeoArrowWKT
|
|
87
|
+
} from './codecs';
|
|
88
|
+
|
|
89
|
+
export type {
|
|
90
|
+
GeoArrowTessellation,
|
|
91
|
+
TessellateGeoArrowPolygonsOptions
|
|
92
|
+
} from './tessellate';
|
|
93
|
+
export {tessellateGeoArrowPolygons} from './tessellate';
|