@loaders.gl/gis 4.3.1 → 4.4.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2411 -469
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +31 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +38 -15
- package/dist/lib/binary-geometry-api/binary-geometry-info.d.ts +21 -0
- package/dist/lib/binary-geometry-api/binary-geometry-info.d.ts.map +1 -0
- package/dist/lib/binary-geometry-api/binary-geometry-info.js +48 -0
- package/dist/lib/binary-geometry-api/concat-binary-geometry.d.ts +5 -0
- package/dist/lib/binary-geometry-api/concat-binary-geometry.d.ts.map +1 -0
- package/dist/lib/binary-geometry-api/concat-binary-geometry.js +50 -0
- package/dist/lib/{binary-features/transform.d.ts → binary-geometry-api/transform-coordinates.d.ts} +1 -2
- package/dist/lib/binary-geometry-api/transform-coordinates.d.ts.map +1 -0
- package/dist/lib/{binary-features/transform.js → binary-geometry-api/transform-coordinates.js} +0 -1
- package/dist/lib/feature-collection-converters/convert-binary-features-to-geojson.d.ts +16 -0
- package/dist/lib/feature-collection-converters/convert-binary-features-to-geojson.d.ts.map +1 -0
- package/dist/lib/{binary-features/binary-to-geojson.js → feature-collection-converters/convert-binary-features-to-geojson.js} +3 -90
- package/dist/lib/{binary-features/flat-geojson-to-binary.d.ts → feature-collection-converters/convert-flat-geojson-to-binary-features.d.ts} +6 -9
- package/dist/lib/feature-collection-converters/convert-flat-geojson-to-binary-features.d.ts.map +1 -0
- package/dist/lib/{binary-features/flat-geojson-to-binary.js → feature-collection-converters/convert-flat-geojson-to-binary-features.js} +5 -5
- package/dist/lib/feature-collection-converters/convert-geoarrow-to-binary-features.d.ts +74 -0
- package/dist/lib/feature-collection-converters/convert-geoarrow-to-binary-features.d.ts.map +1 -0
- package/dist/lib/feature-collection-converters/convert-geoarrow-to-binary-features.js +377 -0
- package/dist/lib/{binary-features/geojson-to-binary.d.ts → feature-collection-converters/convert-geojson-to-binary-features.d.ts} +2 -2
- package/dist/lib/feature-collection-converters/convert-geojson-to-binary-features.d.ts.map +1 -0
- package/dist/lib/feature-collection-converters/convert-geojson-to-binary-features.js +24 -0
- package/dist/lib/{binary-features/geojson-to-flat-geojson.d.ts → feature-collection-converters/convert-geojson-to-flat-geojson.d.ts} +2 -2
- package/dist/lib/feature-collection-converters/convert-geojson-to-flat-geojson.d.ts.map +1 -0
- package/dist/lib/{binary-features/geojson-to-flat-geojson.js → feature-collection-converters/convert-geojson-to-flat-geojson.js} +4 -1
- package/dist/lib/feature-collection-converters/helpers/flat-geojson-to-binary-types.d.ts.map +1 -0
- package/dist/lib/feature-collection-converters/helpers/flat-geojson-to-binary-types.js +4 -0
- package/dist/lib/geometry-api/geometry-info.d.ts +21 -0
- package/dist/lib/geometry-api/geometry-info.d.ts.map +1 -0
- package/dist/lib/{binary-features/extract-geometry-info.js → geometry-api/geometry-info.js} +4 -1
- package/dist/lib/geometry-converters/convert-binary-geometry-to-geojson.d.ts +4 -0
- package/dist/lib/geometry-converters/convert-binary-geometry-to-geojson.d.ts.map +1 -0
- package/dist/lib/geometry-converters/convert-binary-geometry-to-geojson.js +91 -0
- package/dist/lib/geometry-converters/convert-geoarrow-to-geojson.d.ts +13 -0
- package/dist/lib/geometry-converters/convert-geoarrow-to-geojson.d.ts.map +1 -0
- package/dist/lib/geometry-converters/convert-geoarrow-to-geojson.js +164 -0
- package/dist/lib/geometry-converters/convert-to-geojson.d.ts +6 -0
- package/dist/lib/geometry-converters/convert-to-geojson.d.ts.map +1 -0
- package/dist/lib/geometry-converters/convert-to-geojson.js +39 -0
- package/dist/lib/geometry-converters/wkb/convert-geometry-to-twkb.d.ts +6 -0
- package/dist/lib/geometry-converters/wkb/convert-geometry-to-twkb.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/convert-geometry-to-twkb.js +195 -0
- package/dist/lib/geometry-converters/wkb/convert-geometry-to-wkb.d.ts +9 -0
- package/dist/lib/geometry-converters/wkb/convert-geometry-to-wkb.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/convert-geometry-to-wkb.js +273 -0
- package/dist/lib/geometry-converters/wkb/convert-geometry-to-wkt.d.ts +8 -0
- package/dist/lib/geometry-converters/wkb/convert-geometry-to-wkt.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/convert-geometry-to-wkt.js +44 -0
- package/dist/lib/geometry-converters/wkb/convert-twkb-to-geometry.d.ts +10 -0
- package/dist/lib/geometry-converters/wkb/convert-twkb-to-geometry.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/convert-twkb-to-geometry.js +254 -0
- package/dist/lib/geometry-converters/wkb/convert-wkb-to-binary-geometry.d.ts +3 -0
- package/dist/lib/geometry-converters/wkb/convert-wkb-to-binary-geometry.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/convert-wkb-to-binary-geometry.js +164 -0
- package/dist/lib/geometry-converters/wkb/convert-wkb-to-geometry.d.ts +11 -0
- package/dist/lib/geometry-converters/wkb/convert-wkb-to-geometry.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/convert-wkb-to-geometry.js +9 -0
- package/dist/lib/geometry-converters/wkb/convert-wkt-to-geometry.d.ts +16 -0
- package/dist/lib/geometry-converters/wkb/convert-wkt-to-geometry.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/convert-wkt-to-geometry.js +258 -0
- package/dist/lib/geometry-converters/wkb/helpers/parse-wkb-header.d.ts +43 -0
- package/dist/lib/geometry-converters/wkb/helpers/parse-wkb-header.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/helpers/parse-wkb-header.js +174 -0
- package/dist/lib/geometry-converters/wkb/helpers/wkb-types.d.ts +64 -0
- package/dist/lib/geometry-converters/wkb/helpers/wkb-types.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/helpers/wkb-types.js +46 -0
- package/dist/lib/geometry-converters/wkb/helpers/wkb-utils.d.ts +21 -0
- package/dist/lib/geometry-converters/wkb/helpers/wkb-utils.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/helpers/wkb-utils.js +64 -0
- package/dist/lib/geometry-converters/wkb/helpers/write-wkb-header.d.ts +8 -0
- package/dist/lib/geometry-converters/wkb/helpers/write-wkb-header.d.ts.map +1 -0
- package/dist/lib/geometry-converters/wkb/helpers/write-wkb-header.js +33 -0
- package/dist/lib/table-converters/convert-geoarrow-table.d.ts +27 -0
- package/dist/lib/table-converters/convert-geoarrow-table.d.ts.map +1 -0
- package/dist/lib/table-converters/convert-geoarrow-table.js +154 -0
- package/dist/lib/table-converters/convert-wkb-table-to-geojson.d.ts +4 -0
- package/dist/lib/table-converters/convert-wkb-table-to-geojson.d.ts.map +1 -0
- package/dist/lib/{tables/convert-table-to-geojson.js → table-converters/convert-wkb-table-to-geojson.js} +10 -16
- package/dist/lib/table-converters/make-arrow-batch-iterator.d.ts +6 -0
- package/dist/lib/table-converters/make-arrow-batch-iterator.d.ts.map +1 -0
- package/dist/lib/table-converters/make-arrow-batch-iterator.js +35 -0
- package/dist/lib/utils/base64-encoder.d.ts +5 -0
- package/dist/lib/utils/base64-encoder.d.ts.map +1 -0
- package/dist/lib/utils/base64-encoder.js +153 -0
- package/dist/lib/utils/binary-reader.d.ts +18 -0
- package/dist/lib/utils/binary-reader.d.ts.map +1 -0
- package/dist/lib/utils/binary-reader.js +69 -0
- package/dist/lib/utils/binary-writer.d.ts +30 -0
- package/dist/lib/utils/binary-writer.d.ts.map +1 -0
- package/dist/lib/utils/binary-writer.js +127 -0
- package/dist/lib/utils/concat-typed-arrays.d.ts +3 -0
- package/dist/lib/utils/concat-typed-arrays.d.ts.map +1 -0
- package/dist/lib/utils/concat-typed-arrays.js +18 -0
- package/dist/lib/utils/hex-encoder.d.ts +15 -0
- package/dist/lib/utils/hex-encoder.d.ts.map +1 -0
- package/dist/lib/utils/hex-encoder.js +54 -0
- package/dist/lib/utils/hex-transcoder.d.ts +15 -0
- package/dist/lib/utils/hex-transcoder.d.ts.map +1 -0
- package/dist/lib/utils/hex-transcoder.js +50 -0
- package/dist/lib/wkt-crs/encode-wkt-crs.d.ts +10 -0
- package/dist/lib/wkt-crs/encode-wkt-crs.d.ts.map +1 -0
- package/dist/lib/wkt-crs/encode-wkt-crs.js +35 -0
- package/dist/lib/wkt-crs/parse-wkt-crs.d.ts +15 -0
- package/dist/lib/wkt-crs/parse-wkt-crs.d.ts.map +1 -0
- package/dist/lib/wkt-crs/parse-wkt-crs.js +120 -0
- package/package.json +7 -5
- package/src/index.ts +87 -14
- package/src/lib/binary-geometry-api/binary-geometry-info.ts +75 -0
- package/src/lib/binary-geometry-api/concat-binary-geometry.ts +78 -0
- package/src/lib/{binary-features/transform.ts → binary-geometry-api/transform-coordinates.ts} +0 -1
- package/src/lib/{binary-features/binary-to-geojson.ts → feature-collection-converters/convert-binary-features-to-geojson.ts} +5 -142
- package/src/lib/{binary-features/flat-geojson-to-binary.ts → feature-collection-converters/convert-flat-geojson-to-binary-features.ts} +14 -9
- package/src/lib/feature-collection-converters/convert-geoarrow-to-binary-features.ts +496 -0
- package/src/lib/{binary-features/geojson-to-binary.ts → feature-collection-converters/convert-geojson-to-binary-features.ts} +11 -7
- package/src/lib/{binary-features/geojson-to-flat-geojson.ts → feature-collection-converters/convert-geojson-to-flat-geojson.ts} +5 -1
- package/src/lib/{binary-features → feature-collection-converters/helpers}/flat-geojson-to-binary-types.ts +4 -0
- package/src/lib/{binary-features/extract-geometry-info.ts → geometry-api/geometry-info.ts} +20 -2
- package/src/lib/geometry-converters/convert-binary-geometry-to-geojson.ts +148 -0
- package/src/lib/geometry-converters/convert-geoarrow-to-geojson.ts +193 -0
- package/src/lib/geometry-converters/convert-to-geojson.ts +52 -0
- package/src/lib/geometry-converters/wkb/convert-geometry-to-twkb.ts +308 -0
- package/src/lib/geometry-converters/wkb/convert-geometry-to-wkb.ts +365 -0
- package/src/lib/geometry-converters/wkb/convert-geometry-to-wkt.ts +54 -0
- package/src/lib/geometry-converters/wkb/convert-twkb-to-geometry.ts +366 -0
- package/src/lib/geometry-converters/wkb/convert-wkb-to-binary-geometry.ts +238 -0
- package/src/lib/geometry-converters/wkb/convert-wkb-to-geometry.ts +23 -0
- package/src/lib/geometry-converters/wkb/convert-wkt-to-geometry.ts +294 -0
- package/src/lib/geometry-converters/wkb/helpers/parse-wkb-header.ts +213 -0
- package/src/lib/geometry-converters/wkb/helpers/wkb-types.ts +82 -0
- package/src/lib/geometry-converters/wkb/helpers/wkb-utils.ts +85 -0
- package/src/lib/geometry-converters/wkb/helpers/write-wkb-header.ts +41 -0
- package/src/lib/table-converters/convert-geoarrow-table.ts +218 -0
- package/src/lib/{tables/convert-table-to-geojson.ts → table-converters/convert-wkb-table-to-geojson.ts} +10 -23
- package/src/lib/table-converters/make-arrow-batch-iterator.ts +53 -0
- package/src/lib/utils/base64-encoder.ts +157 -0
- package/src/lib/utils/binary-reader.ts +76 -0
- package/src/lib/utils/binary-writer.ts +136 -0
- package/src/lib/utils/concat-typed-arrays.ts +24 -0
- package/src/lib/utils/hex-encoder.ts +60 -0
- package/src/lib/utils/hex-transcoder.ts +54 -0
- package/src/lib/wkt-crs/encode-wkt-crs.ts +41 -0
- package/src/lib/wkt-crs/parse-wkt-crs.ts +149 -0
- package/dist/lib/binary-features/binary-to-geojson.d.ts +0 -18
- package/dist/lib/binary-features/binary-to-geojson.d.ts.map +0 -1
- package/dist/lib/binary-features/extract-geometry-info.d.ts +0 -8
- package/dist/lib/binary-features/extract-geometry-info.d.ts.map +0 -1
- package/dist/lib/binary-features/flat-geojson-to-binary-types.d.ts.map +0 -1
- package/dist/lib/binary-features/flat-geojson-to-binary-types.js +0 -1
- package/dist/lib/binary-features/flat-geojson-to-binary.d.ts.map +0 -1
- package/dist/lib/binary-features/geojson-to-binary.d.ts.map +0 -1
- package/dist/lib/binary-features/geojson-to-binary.js +0 -21
- package/dist/lib/binary-features/geojson-to-flat-geojson.d.ts.map +0 -1
- package/dist/lib/binary-features/transform.d.ts.map +0 -1
- package/dist/lib/geo/geoarrow-metadata.d.ts +0 -27
- package/dist/lib/geo/geoarrow-metadata.d.ts.map +0 -1
- package/dist/lib/geo/geoarrow-metadata.js +0 -70
- package/dist/lib/geo/geoparquet-metadata-schema.d.ts +0 -79
- package/dist/lib/geo/geoparquet-metadata-schema.d.ts.map +0 -1
- package/dist/lib/geo/geoparquet-metadata-schema.js +0 -69
- package/dist/lib/geo/geoparquet-metadata.d.ts +0 -45
- package/dist/lib/geo/geoparquet-metadata.d.ts.map +0 -1
- package/dist/lib/geo/geoparquet-metadata.js +0 -117
- package/dist/lib/tables/convert-table-to-geojson.d.ts +0 -5
- package/dist/lib/tables/convert-table-to-geojson.d.ts.map +0 -1
- package/src/lib/geo/geoarrow-metadata.ts +0 -100
- package/src/lib/geo/geoparquet-metadata-schema.json +0 -60
- package/src/lib/geo/geoparquet-metadata-schema.ts +0 -71
- package/src/lib/geo/geoparquet-metadata.ts +0 -191
- /package/dist/lib/{binary-features → feature-collection-converters/helpers}/flat-geojson-to-binary-types.d.ts +0 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
import { concatenateBinaryPointGeometries, concatenateBinaryLineGeometries, concatenateBinaryPolygonGeometries } from "../../binary-geometry-api/concat-binary-geometry.js";
|
|
5
|
+
import { concatTypedArrays } from "../../utils/concat-typed-arrays.js";
|
|
6
|
+
import { WKBGeometryType } from "./helpers/wkb-types.js";
|
|
7
|
+
import { parseWKBHeader } from "./helpers/parse-wkb-header.js";
|
|
8
|
+
export function convertWKBToBinaryGeometry(arrayBuffer) {
|
|
9
|
+
const dataView = new DataView(arrayBuffer);
|
|
10
|
+
const wkbHeader = parseWKBHeader(dataView);
|
|
11
|
+
const { geometryType, dimensions, littleEndian } = wkbHeader;
|
|
12
|
+
const offset = wkbHeader.byteOffset;
|
|
13
|
+
switch (geometryType) {
|
|
14
|
+
case WKBGeometryType.Point:
|
|
15
|
+
const point = parsePoint(dataView, offset, dimensions, littleEndian);
|
|
16
|
+
return point.geometry;
|
|
17
|
+
case WKBGeometryType.LineString:
|
|
18
|
+
const line = parseLineString(dataView, offset, dimensions, littleEndian);
|
|
19
|
+
return line.geometry;
|
|
20
|
+
case WKBGeometryType.Polygon:
|
|
21
|
+
const polygon = parsePolygon(dataView, offset, dimensions, littleEndian);
|
|
22
|
+
return polygon.geometry;
|
|
23
|
+
case WKBGeometryType.MultiPoint:
|
|
24
|
+
const multiPoint = parseMultiPoint(dataView, offset, dimensions, littleEndian);
|
|
25
|
+
multiPoint.type = 'Point';
|
|
26
|
+
return multiPoint;
|
|
27
|
+
case WKBGeometryType.MultiLineString:
|
|
28
|
+
const multiLine = parseMultiLineString(dataView, offset, dimensions, littleEndian);
|
|
29
|
+
multiLine.type = 'LineString';
|
|
30
|
+
return multiLine;
|
|
31
|
+
case WKBGeometryType.MultiPolygon:
|
|
32
|
+
const multiPolygon = parseMultiPolygon(dataView, offset, dimensions, littleEndian);
|
|
33
|
+
multiPolygon.type = 'Polygon';
|
|
34
|
+
return multiPolygon;
|
|
35
|
+
// case WKBGeometryType.GeometryCollection:
|
|
36
|
+
// TODO: handle GeometryCollections
|
|
37
|
+
// return parseGeometryCollection(dataView, offset, dimensions, littleEndian);
|
|
38
|
+
default:
|
|
39
|
+
throw new Error(`WKB: Unsupported geometry type: ${geometryType}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Primitives; parse point and linear ring
|
|
43
|
+
function parsePoint(dataView, offset, dimension, littleEndian) {
|
|
44
|
+
const positions = new Float64Array(dimension);
|
|
45
|
+
for (let i = 0; i < dimension; i++) {
|
|
46
|
+
positions[i] = dataView.getFloat64(offset, littleEndian);
|
|
47
|
+
offset += 8;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
geometry: { type: 'Point', positions: { value: positions, size: dimension } },
|
|
51
|
+
offset
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function parseLineString(dataView, offset, dimension, littleEndian) {
|
|
55
|
+
const nPoints = dataView.getUint32(offset, littleEndian);
|
|
56
|
+
offset += 4;
|
|
57
|
+
// Instantiate array
|
|
58
|
+
const positions = new Float64Array(nPoints * dimension);
|
|
59
|
+
for (let i = 0; i < nPoints * dimension; i++) {
|
|
60
|
+
positions[i] = dataView.getFloat64(offset, littleEndian);
|
|
61
|
+
offset += 8;
|
|
62
|
+
}
|
|
63
|
+
const pathIndices = [0];
|
|
64
|
+
if (nPoints > 0) {
|
|
65
|
+
pathIndices.push(nPoints);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
geometry: {
|
|
69
|
+
type: 'LineString',
|
|
70
|
+
positions: { value: positions, size: dimension },
|
|
71
|
+
pathIndices: { value: new Uint32Array(pathIndices), size: 1 }
|
|
72
|
+
},
|
|
73
|
+
offset
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
// https://stackoverflow.com/a/55261098
|
|
77
|
+
const cumulativeSum = (sum) => (value) => (sum += value);
|
|
78
|
+
function parsePolygon(dataView, offset, dimension, littleEndian) {
|
|
79
|
+
const nRings = dataView.getUint32(offset, littleEndian);
|
|
80
|
+
offset += 4;
|
|
81
|
+
const rings = [];
|
|
82
|
+
for (let i = 0; i < nRings; i++) {
|
|
83
|
+
const parsed = parseLineString(dataView, offset, dimension, littleEndian);
|
|
84
|
+
const { positions } = parsed.geometry;
|
|
85
|
+
offset = parsed.offset;
|
|
86
|
+
rings.push(positions.value);
|
|
87
|
+
}
|
|
88
|
+
const concatenatedPositions = new Float64Array(concatTypedArrays(rings).buffer);
|
|
89
|
+
const polygonIndices = [0];
|
|
90
|
+
if (concatenatedPositions.length > 0) {
|
|
91
|
+
polygonIndices.push(concatenatedPositions.length / dimension);
|
|
92
|
+
}
|
|
93
|
+
const primitivePolygonIndices = rings.map((l) => l.length / dimension).map(cumulativeSum(0));
|
|
94
|
+
primitivePolygonIndices.unshift(0);
|
|
95
|
+
return {
|
|
96
|
+
geometry: {
|
|
97
|
+
type: 'Polygon',
|
|
98
|
+
positions: { value: concatenatedPositions, size: dimension },
|
|
99
|
+
polygonIndices: {
|
|
100
|
+
value: new Uint32Array(polygonIndices),
|
|
101
|
+
size: 1
|
|
102
|
+
},
|
|
103
|
+
primitivePolygonIndices: { value: new Uint32Array(primitivePolygonIndices), size: 1 }
|
|
104
|
+
},
|
|
105
|
+
offset
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function parseMultiPoint(dataView, offset, dimension, littleEndian) {
|
|
109
|
+
const nPoints = dataView.getUint32(offset, littleEndian);
|
|
110
|
+
offset += 4;
|
|
111
|
+
const binaryPointGeometries = [];
|
|
112
|
+
for (let i = 0; i < nPoints; i++) {
|
|
113
|
+
// Byte order for point
|
|
114
|
+
const littleEndianPoint = dataView.getUint8(offset) === 1;
|
|
115
|
+
offset++;
|
|
116
|
+
// Assert point type
|
|
117
|
+
if (dataView.getUint32(offset, littleEndianPoint) % 1000 !== 1) {
|
|
118
|
+
throw new Error('WKB: Inner geometries of MultiPoint not of type Point');
|
|
119
|
+
}
|
|
120
|
+
offset += 4;
|
|
121
|
+
const parsed = parsePoint(dataView, offset, dimension, littleEndianPoint);
|
|
122
|
+
offset = parsed.offset;
|
|
123
|
+
binaryPointGeometries.push(parsed.geometry);
|
|
124
|
+
}
|
|
125
|
+
return concatenateBinaryPointGeometries(binaryPointGeometries, dimension);
|
|
126
|
+
}
|
|
127
|
+
function parseMultiLineString(dataView, offset, dimension, littleEndian) {
|
|
128
|
+
const nLines = dataView.getUint32(offset, littleEndian);
|
|
129
|
+
offset += 4;
|
|
130
|
+
const binaryLineGeometries = [];
|
|
131
|
+
for (let i = 0; i < nLines; i++) {
|
|
132
|
+
// Byte order for line
|
|
133
|
+
const littleEndianLine = dataView.getUint8(offset) === 1;
|
|
134
|
+
offset++;
|
|
135
|
+
// Assert type LineString
|
|
136
|
+
if (dataView.getUint32(offset, littleEndianLine) % 1000 !== 2) {
|
|
137
|
+
throw new Error('WKB: Inner geometries of MultiLineString not of type LineString');
|
|
138
|
+
}
|
|
139
|
+
offset += 4;
|
|
140
|
+
const parsed = parseLineString(dataView, offset, dimension, littleEndianLine);
|
|
141
|
+
offset = parsed.offset;
|
|
142
|
+
binaryLineGeometries.push(parsed.geometry);
|
|
143
|
+
}
|
|
144
|
+
return concatenateBinaryLineGeometries(binaryLineGeometries, dimension);
|
|
145
|
+
}
|
|
146
|
+
function parseMultiPolygon(dataView, offset, dimension, littleEndian) {
|
|
147
|
+
const nPolygons = dataView.getUint32(offset, littleEndian);
|
|
148
|
+
offset += 4;
|
|
149
|
+
const binaryPolygonGeometries = [];
|
|
150
|
+
for (let i = 0; i < nPolygons; i++) {
|
|
151
|
+
// Byte order for polygon
|
|
152
|
+
const littleEndianPolygon = dataView.getUint8(offset) === 1;
|
|
153
|
+
offset++;
|
|
154
|
+
// Assert type Polygon
|
|
155
|
+
if (dataView.getUint32(offset, littleEndianPolygon) % 1000 !== 3) {
|
|
156
|
+
throw new Error('WKB: Inner geometries of MultiPolygon not of type Polygon');
|
|
157
|
+
}
|
|
158
|
+
offset += 4;
|
|
159
|
+
const parsed = parsePolygon(dataView, offset, dimension, littleEndianPolygon);
|
|
160
|
+
offset = parsed.offset;
|
|
161
|
+
binaryPolygonGeometries.push(parsed.geometry);
|
|
162
|
+
}
|
|
163
|
+
return concatenateBinaryPolygonGeometries(binaryPolygonGeometries, dimension);
|
|
164
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Geometry } from '@loaders.gl/schema';
|
|
2
|
+
export type convertWKBOptions = {
|
|
3
|
+
/** Does the GeoJSON input have Z values? */
|
|
4
|
+
hasZ?: boolean;
|
|
5
|
+
/** Does the GeoJSON input have M values? */
|
|
6
|
+
hasM?: boolean;
|
|
7
|
+
/** Spatial reference for input GeoJSON */
|
|
8
|
+
srid?: any;
|
|
9
|
+
};
|
|
10
|
+
export declare function convertWKBToGeometry(arrayBuffer: ArrayBuffer): Geometry;
|
|
11
|
+
//# sourceMappingURL=convert-wkb-to-geometry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convert-wkb-to-geometry.d.ts","sourceRoot":"","sources":["../../../../src/lib/geometry-converters/wkb/convert-wkb-to-geometry.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,oBAAoB,CAAC;AAIjD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,4CAA4C;IAC5C,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf,4CAA4C;IAC5C,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf,0CAA0C;IAC1C,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ,CAAC;AAEF,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,WAAW,GAAG,QAAQ,CAGvE"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
import { convertBinaryGeometryToGeometry } from "../convert-binary-geometry-to-geojson.js";
|
|
5
|
+
import { convertWKBToBinaryGeometry } from "./convert-wkb-to-binary-geometry.js";
|
|
6
|
+
export function convertWKBToGeometry(arrayBuffer) {
|
|
7
|
+
const binaryGeometry = convertWKBToBinaryGeometry(arrayBuffer);
|
|
8
|
+
return convertBinaryGeometryToGeometry(binaryGeometry);
|
|
9
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Geometry } from '@loaders.gl/schema';
|
|
2
|
+
export type ParseWKTOptions = {
|
|
3
|
+
wkt?: {
|
|
4
|
+
/** Whether to add any CRS, if found, as undocumented CRS property on the return geometry */
|
|
5
|
+
crs?: boolean;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Parse WKT and return GeoJSON.
|
|
10
|
+
* @param input A WKT geometry string
|
|
11
|
+
* @return A GeoJSON geometry object
|
|
12
|
+
*
|
|
13
|
+
* @note We only support the "geojson" subset of the OGC simple features standard
|
|
14
|
+
**/
|
|
15
|
+
export declare function convertWKTToGeometry(input: string, options?: ParseWKTOptions): Geometry | null;
|
|
16
|
+
//# sourceMappingURL=convert-wkt-to-geometry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convert-wkt-to-geometry.d.ts","sourceRoot":"","sources":["../../../../src/lib/geometry-converters/wkb/convert-wkt-to-geometry.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,oBAAoB,CAAC;AASjD,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,CAAC,EAAE;QACJ,4FAA4F;QAC5F,GAAG,CAAC,EAAE,OAAO,CAAC;KACf,CAAC;CACH,CAAC;AASF;;;;;;IAMI;AACJ,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,QAAQ,GAAG,IAAI,CAU9F"}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
// Fork of https://github.com/mapbox/wellknown under ISC license (MIT/BSD-2-clause equivalent)
|
|
5
|
+
/* eslint-disable */
|
|
6
|
+
// @ts-nocheck
|
|
7
|
+
const numberRegexp = /[-+]?([0-9]*\.[0-9]+|[0-9]+)([eE][-+]?[0-9]+)?/;
|
|
8
|
+
// Matches sequences like '100 100' or '100 100 100'.
|
|
9
|
+
const tuples = new RegExp('^' + numberRegexp.source + '(\\s' + numberRegexp.source + '){1,}');
|
|
10
|
+
/**
|
|
11
|
+
* Parse WKT and return GeoJSON.
|
|
12
|
+
* @param input A WKT geometry string
|
|
13
|
+
* @return A GeoJSON geometry object
|
|
14
|
+
*
|
|
15
|
+
* @note We only support the "geojson" subset of the OGC simple features standard
|
|
16
|
+
**/
|
|
17
|
+
export function convertWKTToGeometry(input, options) {
|
|
18
|
+
const parts = input.split(';');
|
|
19
|
+
let _ = parts.pop();
|
|
20
|
+
const srid = (parts.shift() || '').split('=').pop();
|
|
21
|
+
const state = { parts, _, i: 0 };
|
|
22
|
+
const geometry = parseGeometry(state);
|
|
23
|
+
return options?.wkt?.crs ? addCRS(geometry, srid) : geometry;
|
|
24
|
+
}
|
|
25
|
+
function parseGeometry(state) {
|
|
26
|
+
return (parsePoint(state) ||
|
|
27
|
+
parseLineString(state) ||
|
|
28
|
+
parsePolygon(state) ||
|
|
29
|
+
parseMultiPoint(state) ||
|
|
30
|
+
parseMultiLineString(state) ||
|
|
31
|
+
parseMultiPolygon(state) ||
|
|
32
|
+
parseGeometryCollection(state));
|
|
33
|
+
}
|
|
34
|
+
/** Adds a coordinate reference system as an undocumented */
|
|
35
|
+
function addCRS(obj, srid) {
|
|
36
|
+
if (obj && srid?.match(/\d+/)) {
|
|
37
|
+
const crs = {
|
|
38
|
+
type: 'name',
|
|
39
|
+
properties: {
|
|
40
|
+
name: 'urn:ogc:def:crs:EPSG::' + srid
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
// @ts-expect-error we assign an undocumented property on the geometry
|
|
44
|
+
obj.crs = crs;
|
|
45
|
+
}
|
|
46
|
+
return obj;
|
|
47
|
+
}
|
|
48
|
+
// GEOMETRIES
|
|
49
|
+
function parsePoint(state) {
|
|
50
|
+
if (!$(/^(POINT(\sz)?)/i, state)) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
white(state);
|
|
54
|
+
if (!$(/^(\()/, state)) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
const c = coords(state);
|
|
58
|
+
if (!c) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
white(state);
|
|
62
|
+
if (!$(/^(\))/, state)) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
type: 'Point',
|
|
67
|
+
coordinates: c[0]
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function parseMultiPoint(state) {
|
|
71
|
+
if (!$(/^(MULTIPOINT)/i, state)) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
white(state);
|
|
75
|
+
const newCoordsFormat = state._?.substring(state._?.indexOf('(') + 1, state._.length - 1)
|
|
76
|
+
.replace(/\(/g, '')
|
|
77
|
+
.replace(/\)/g, '');
|
|
78
|
+
state._ = 'MULTIPOINT (' + newCoordsFormat + ')';
|
|
79
|
+
const c = multicoords(state);
|
|
80
|
+
if (!c) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
white(state);
|
|
84
|
+
return {
|
|
85
|
+
type: 'MultiPoint',
|
|
86
|
+
coordinates: c
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function parseLineString(state) {
|
|
90
|
+
if (!$(/^(LINESTRING(\sz)?)/i, state)) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
white(state);
|
|
94
|
+
if (!$(/^(\()/, state)) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const c = coords(state);
|
|
98
|
+
if (!c) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
if (!$(/^(\))/, state)) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
type: 'LineString',
|
|
106
|
+
coordinates: c
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function parseMultiLineString(state) {
|
|
110
|
+
if (!$(/^(MULTILINESTRING)/i, state))
|
|
111
|
+
return null;
|
|
112
|
+
white(state);
|
|
113
|
+
const c = multicoords(state);
|
|
114
|
+
if (!c) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
white(state);
|
|
118
|
+
return {
|
|
119
|
+
// @ts-ignore
|
|
120
|
+
type: 'MultiLineString',
|
|
121
|
+
// @ts-expect-error
|
|
122
|
+
coordinates: c
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function parsePolygon(state) {
|
|
126
|
+
if (!$(/^(POLYGON(\sz)?)/i, state)) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
white(state);
|
|
130
|
+
const c = multicoords(state);
|
|
131
|
+
if (!c) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
type: 'Polygon',
|
|
137
|
+
// @ts-expect-error
|
|
138
|
+
coordinates: c
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
function parseMultiPolygon(state) {
|
|
142
|
+
if (!$(/^(MULTIPOLYGON)/i, state)) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
white(state);
|
|
146
|
+
const c = multicoords(state);
|
|
147
|
+
if (!c) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
type: 'MultiPolygon',
|
|
152
|
+
// @ts-expect-error
|
|
153
|
+
coordinates: c
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function parseGeometryCollection(state) {
|
|
157
|
+
const geometries = [];
|
|
158
|
+
let geometry;
|
|
159
|
+
if (!$(/^(GEOMETRYCOLLECTION)/i, state)) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
white(state);
|
|
163
|
+
if (!$(/^(\()/, state)) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
while ((geometry = parseGeometry(state))) {
|
|
167
|
+
geometries.push(geometry);
|
|
168
|
+
white(state);
|
|
169
|
+
$(/^(,)/, state);
|
|
170
|
+
white(state);
|
|
171
|
+
}
|
|
172
|
+
if (!$(/^(\))/, state)) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
type: 'GeometryCollection',
|
|
177
|
+
geometries: geometries
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
// COORDINATES
|
|
181
|
+
function multicoords(state) {
|
|
182
|
+
white(state);
|
|
183
|
+
let depth = 0;
|
|
184
|
+
const rings = [];
|
|
185
|
+
const stack = [rings];
|
|
186
|
+
let pointer = rings;
|
|
187
|
+
let elem;
|
|
188
|
+
while ((elem = $(/^(\()/, state) || $(/^(\))/, state) || $(/^(,)/, state) || $(tuples, state))) {
|
|
189
|
+
if (elem === '(') {
|
|
190
|
+
stack.push(pointer);
|
|
191
|
+
pointer = [];
|
|
192
|
+
stack[stack.length - 1].push(pointer);
|
|
193
|
+
depth++;
|
|
194
|
+
}
|
|
195
|
+
else if (elem === ')') {
|
|
196
|
+
// For the case: Polygon(), ...
|
|
197
|
+
if (pointer.length === 0)
|
|
198
|
+
return null;
|
|
199
|
+
// @ts-ignore
|
|
200
|
+
pointer = stack.pop();
|
|
201
|
+
// the stack was empty, input was malformed
|
|
202
|
+
if (!pointer)
|
|
203
|
+
return null;
|
|
204
|
+
depth--;
|
|
205
|
+
if (depth === 0)
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
else if (elem === ',') {
|
|
209
|
+
pointer = [];
|
|
210
|
+
stack[stack.length - 1].push(pointer);
|
|
211
|
+
}
|
|
212
|
+
else if (!elem.split(/\s/g).some(isNaN)) {
|
|
213
|
+
Array.prototype.push.apply(pointer, elem.split(/\s/g).map(parseFloat));
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
white(state);
|
|
219
|
+
}
|
|
220
|
+
if (depth !== 0)
|
|
221
|
+
return null;
|
|
222
|
+
return rings;
|
|
223
|
+
}
|
|
224
|
+
function coords(state) {
|
|
225
|
+
const list = [];
|
|
226
|
+
let item;
|
|
227
|
+
let pt;
|
|
228
|
+
while ((pt = $(tuples, state) || $(/^(,)/, state))) {
|
|
229
|
+
if (pt === ',') {
|
|
230
|
+
list.push(item);
|
|
231
|
+
item = [];
|
|
232
|
+
}
|
|
233
|
+
else if (!pt.split(/\s/g).some(isNaN)) {
|
|
234
|
+
if (!item)
|
|
235
|
+
item = [];
|
|
236
|
+
Array.prototype.push.apply(item, pt.split(/\s/g).map(parseFloat));
|
|
237
|
+
}
|
|
238
|
+
white(state);
|
|
239
|
+
}
|
|
240
|
+
if (item)
|
|
241
|
+
list.push(item);
|
|
242
|
+
else
|
|
243
|
+
return null;
|
|
244
|
+
return list.length ? list : null;
|
|
245
|
+
}
|
|
246
|
+
// HELPERS
|
|
247
|
+
function $(regexp, state) {
|
|
248
|
+
const match = state._?.substring(state.i).match(regexp);
|
|
249
|
+
if (!match)
|
|
250
|
+
return null;
|
|
251
|
+
else {
|
|
252
|
+
state.i += match[0].length;
|
|
253
|
+
return match[0];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function white(state) {
|
|
257
|
+
$(/^\s*/, state);
|
|
258
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { WKBGeometryType, WKBHeader } from "./wkb-types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Check if a string is WKT.
|
|
4
|
+
* @param input A potential WKT geometry string
|
|
5
|
+
* @return `true` if input appears to be a WKT geometry string, `false` otherwise
|
|
6
|
+
|
|
7
|
+
* @note We only support the "geojson" subset of the OGC simple features standard
|
|
8
|
+
* @todo Does not handle leading spaces which appear to be permitted per the spec:
|
|
9
|
+
* "A WKT string contains no white space outside of double quotes.
|
|
10
|
+
* However padding with white space to improve human readability is permitted;
|
|
11
|
+
* the examples of WKT that are included in this document have
|
|
12
|
+
* spaces and line feeds inserted to improve clarity. Any padding is stripped out or ignored by parsers."
|
|
13
|
+
*/
|
|
14
|
+
export declare function isWKT(input: string | ArrayBuffer): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Get the geometry type of a WKT string.
|
|
17
|
+
* @param input A potential WKT geometry string
|
|
18
|
+
* @return `true` if input appears to be a WKT geometry string, `false` otherwise
|
|
19
|
+
|
|
20
|
+
* @note We only support the "geojson" subset of the OGC simple features standard
|
|
21
|
+
* @todo Does not handle leading spaces which appear to be permitted per the spec:
|
|
22
|
+
* "A WKT string contains no white space outside of double quotes.
|
|
23
|
+
* However padding with white space to improve human readability is permitted;
|
|
24
|
+
* the examples of WKT that are included in this document have
|
|
25
|
+
* spaces and line feeds inserted to improve clarity. Any padding is stripped out or ignored by parsers."
|
|
26
|
+
*/
|
|
27
|
+
export declare function getWKTGeometryType(input: string | ArrayBuffer): WKBGeometryType | null;
|
|
28
|
+
/**
|
|
29
|
+
* Check if an array buffer might be a TWKB array buffer
|
|
30
|
+
* @param arrayBuffer The array buffer to check
|
|
31
|
+
* @returns false if this is definitely not a TWKB array buffer, true if it might be a TWKB array buffer
|
|
32
|
+
*/
|
|
33
|
+
export declare function isTWKB(arrayBuffer: ArrayBuffer): boolean;
|
|
34
|
+
/** Sanity checks that first to 5-9 bytes could represent a supported WKB dialect header */
|
|
35
|
+
export declare function isWKB(arrayBuffer: ArrayBuffer): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Parses header and provides a byteOffset to start of geometry data
|
|
38
|
+
* @param dataView
|
|
39
|
+
* @param target optionally supply a WKBHeader object to avoid creating a new object for every call
|
|
40
|
+
* @returns a header object describing the WKB data
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseWKBHeader(dataView: DataView, target?: WKBHeader): WKBHeader;
|
|
43
|
+
//# sourceMappingURL=parse-wkb-header.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-wkb-header.d.ts","sourceRoot":"","sources":["../../../../../src/lib/geometry-converters/wkb/helpers/parse-wkb-header.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,eAAe,EAAE,SAAS,EAAC,uBAAoB;AAU5D;;;;;;;;;;;GAWG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,GAAG,OAAO,CAE1D;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,GAAG,eAAe,GAAG,IAAI,CAUtF;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAaxD;AAED,2FAA2F;AAC3F,wBAAgB,KAAK,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAkDvD;AAED;;;;;GAKG;AAEH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,SAAS,GAAG,SAAS,CA8EhF"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
import { EWKB_FLAG_Z, EWKB_FLAG_M, EWKB_FLAG_SRID, MAX_SRID, WKT_MAGIC_STRINGS, WKT_MAGIC_BYTES } from "./wkb-types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Check if a string is WKT.
|
|
7
|
+
* @param input A potential WKT geometry string
|
|
8
|
+
* @return `true` if input appears to be a WKT geometry string, `false` otherwise
|
|
9
|
+
|
|
10
|
+
* @note We only support the "geojson" subset of the OGC simple features standard
|
|
11
|
+
* @todo Does not handle leading spaces which appear to be permitted per the spec:
|
|
12
|
+
* "A WKT string contains no white space outside of double quotes.
|
|
13
|
+
* However padding with white space to improve human readability is permitted;
|
|
14
|
+
* the examples of WKT that are included in this document have
|
|
15
|
+
* spaces and line feeds inserted to improve clarity. Any padding is stripped out or ignored by parsers."
|
|
16
|
+
*/
|
|
17
|
+
export function isWKT(input) {
|
|
18
|
+
return getWKTGeometryType(input) !== null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Get the geometry type of a WKT string.
|
|
22
|
+
* @param input A potential WKT geometry string
|
|
23
|
+
* @return `true` if input appears to be a WKT geometry string, `false` otherwise
|
|
24
|
+
|
|
25
|
+
* @note We only support the "geojson" subset of the OGC simple features standard
|
|
26
|
+
* @todo Does not handle leading spaces which appear to be permitted per the spec:
|
|
27
|
+
* "A WKT string contains no white space outside of double quotes.
|
|
28
|
+
* However padding with white space to improve human readability is permitted;
|
|
29
|
+
* the examples of WKT that are included in this document have
|
|
30
|
+
* spaces and line feeds inserted to improve clarity. Any padding is stripped out or ignored by parsers."
|
|
31
|
+
*/
|
|
32
|
+
export function getWKTGeometryType(input) {
|
|
33
|
+
if (typeof input === 'string') {
|
|
34
|
+
const index = WKT_MAGIC_STRINGS.findIndex((magicString) => input.startsWith(magicString));
|
|
35
|
+
return index >= 0 ? (index + 1) : null;
|
|
36
|
+
}
|
|
37
|
+
const inputArray = new Uint8Array(input);
|
|
38
|
+
const index = WKT_MAGIC_BYTES.findIndex((magicBytes) => magicBytes.every((value, index) => value === inputArray[index]));
|
|
39
|
+
return index >= 0 ? (index + 1) : null;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check if an array buffer might be a TWKB array buffer
|
|
43
|
+
* @param arrayBuffer The array buffer to check
|
|
44
|
+
* @returns false if this is definitely not a TWKB array buffer, true if it might be a TWKB array buffer
|
|
45
|
+
*/
|
|
46
|
+
export function isTWKB(arrayBuffer) {
|
|
47
|
+
const dataView = new DataView(arrayBuffer);
|
|
48
|
+
const byteOffset = 0;
|
|
49
|
+
const type = dataView.getUint8(byteOffset);
|
|
50
|
+
const geometryType = type & 0x0f;
|
|
51
|
+
// Only geometry types 1 to 7 (point to geometry collection are currently defined)
|
|
52
|
+
if (geometryType < 1 || geometryType > 7) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
/** Sanity checks that first to 5-9 bytes could represent a supported WKB dialect header */
|
|
58
|
+
export function isWKB(arrayBuffer) {
|
|
59
|
+
const dataView = new DataView(arrayBuffer);
|
|
60
|
+
let byteOffset = 0;
|
|
61
|
+
const endianness = dataView.getUint8(byteOffset);
|
|
62
|
+
byteOffset += 1;
|
|
63
|
+
// Check valid endianness (only 0 or 1 are allowed)
|
|
64
|
+
if (endianness > 1) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const littleEndian = endianness === 1;
|
|
68
|
+
const geometry = dataView.getUint32(byteOffset, littleEndian);
|
|
69
|
+
byteOffset += 4;
|
|
70
|
+
// check valid geometry type (we don't support extension geometries)
|
|
71
|
+
const geometryType = geometry & 0x07;
|
|
72
|
+
if (geometryType === 0 || geometryType > 7) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const geometryFlags = geometry - geometryType;
|
|
76
|
+
// Accept iso-wkb flags
|
|
77
|
+
if (geometryFlags === 0 ||
|
|
78
|
+
geometryFlags === 1000 ||
|
|
79
|
+
geometryFlags === 2000 ||
|
|
80
|
+
geometryFlags === 3000) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
// Accept ewkb flags but reject otherwise
|
|
84
|
+
if ((geometryFlags & ~(EWKB_FLAG_Z | EWKB_FLAG_M | EWKB_FLAG_SRID)) !== 0) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
if (geometryFlags & EWKB_FLAG_SRID) {
|
|
88
|
+
const srid = dataView.getUint32(byteOffset, littleEndian);
|
|
89
|
+
byteOffset += 4;
|
|
90
|
+
if (srid > MAX_SRID) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Parses header and provides a byteOffset to start of geometry data
|
|
98
|
+
* @param dataView
|
|
99
|
+
* @param target optionally supply a WKBHeader object to avoid creating a new object for every call
|
|
100
|
+
* @returns a header object describing the WKB data
|
|
101
|
+
*/
|
|
102
|
+
// eslint-disable-next-line max-statements, complexity
|
|
103
|
+
export function parseWKBHeader(dataView, target) {
|
|
104
|
+
const wkbHeader = Object.assign(target || {}, {
|
|
105
|
+
type: 'wkb',
|
|
106
|
+
variant: 'wkb',
|
|
107
|
+
geometryType: 1,
|
|
108
|
+
dimensions: 2,
|
|
109
|
+
coordinates: 'xy',
|
|
110
|
+
littleEndian: true,
|
|
111
|
+
byteOffset: 0
|
|
112
|
+
});
|
|
113
|
+
if (isWKT(dataView.buffer)) {
|
|
114
|
+
// TODO - WKB header could include WKT type
|
|
115
|
+
throw new Error('WKB: Cannot parse WKT data');
|
|
116
|
+
}
|
|
117
|
+
// Check endianness of data
|
|
118
|
+
wkbHeader.littleEndian = dataView.getUint8(wkbHeader.byteOffset) === 1;
|
|
119
|
+
wkbHeader.byteOffset++;
|
|
120
|
+
// 4-digit code representing dimension and type of geometry
|
|
121
|
+
const geometryCode = dataView.getUint32(wkbHeader.byteOffset, wkbHeader.littleEndian);
|
|
122
|
+
wkbHeader.byteOffset += 4;
|
|
123
|
+
wkbHeader.geometryType = (geometryCode & 0x7);
|
|
124
|
+
// Check if iso-wkb variant: iso-wkb adds 1000, 2000 or 3000 to the geometry code
|
|
125
|
+
const isoType = (geometryCode - wkbHeader.geometryType) / 1000;
|
|
126
|
+
switch (isoType) {
|
|
127
|
+
case 0:
|
|
128
|
+
break;
|
|
129
|
+
case 1:
|
|
130
|
+
wkbHeader.variant = 'iso-wkb';
|
|
131
|
+
wkbHeader.dimensions = 3;
|
|
132
|
+
wkbHeader.coordinates = 'xyz';
|
|
133
|
+
break;
|
|
134
|
+
case 2:
|
|
135
|
+
wkbHeader.variant = 'iso-wkb';
|
|
136
|
+
wkbHeader.dimensions = 3;
|
|
137
|
+
wkbHeader.coordinates = 'xym';
|
|
138
|
+
break;
|
|
139
|
+
case 3:
|
|
140
|
+
wkbHeader.variant = 'iso-wkb';
|
|
141
|
+
wkbHeader.dimensions = 4;
|
|
142
|
+
wkbHeader.coordinates = 'xyzm';
|
|
143
|
+
break;
|
|
144
|
+
default:
|
|
145
|
+
throw new Error(`WKB: Unsupported iso-wkb type: ${isoType}`);
|
|
146
|
+
}
|
|
147
|
+
// Check if EWKB variant. Uses bitmasks for Z&M dimensions as well as optional SRID field
|
|
148
|
+
const ewkbZ = geometryCode & EWKB_FLAG_Z;
|
|
149
|
+
const ewkbM = geometryCode & EWKB_FLAG_M;
|
|
150
|
+
const ewkbSRID = geometryCode & EWKB_FLAG_SRID;
|
|
151
|
+
if (ewkbZ && ewkbM) {
|
|
152
|
+
wkbHeader.variant = 'ewkb';
|
|
153
|
+
wkbHeader.dimensions = 4;
|
|
154
|
+
wkbHeader.coordinates = 'xyzm';
|
|
155
|
+
}
|
|
156
|
+
else if (ewkbZ) {
|
|
157
|
+
wkbHeader.variant = 'ewkb';
|
|
158
|
+
wkbHeader.dimensions = 3;
|
|
159
|
+
wkbHeader.coordinates = 'xyz';
|
|
160
|
+
}
|
|
161
|
+
else if (ewkbM) {
|
|
162
|
+
wkbHeader.variant = 'ewkb';
|
|
163
|
+
wkbHeader.dimensions = 3;
|
|
164
|
+
wkbHeader.coordinates = 'xym';
|
|
165
|
+
}
|
|
166
|
+
// If SRID present read four more bytes
|
|
167
|
+
if (ewkbSRID) {
|
|
168
|
+
wkbHeader.variant = 'ewkb';
|
|
169
|
+
// 4-digit code representing dimension and type of geometry
|
|
170
|
+
wkbHeader.srid = dataView.getUint32(wkbHeader.byteOffset, wkbHeader.littleEndian);
|
|
171
|
+
wkbHeader.byteOffset += 4;
|
|
172
|
+
}
|
|
173
|
+
return wkbHeader;
|
|
174
|
+
}
|