-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparse-test-cases.ts
135 lines (103 loc) · 3.62 KB
/
parse-test-cases.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import type {Geometry, BinaryGeometry} from '@loaders.gl/schema';
export interface TestCase {
/** Geometry in WKT */
wkt: string;
/** Geometry in WKB, stored in base64 */
wkb: string;
/** Geometry in EWKB, stored in base64 */
ewkb: string;
/** Geometry in WKB XDR (big endian), stored in base64 */
wkbXdr: string;
/** Geometry in EWKB XDR (big endian), stored in base64 */
ewkbXdr: string;
/** Geometry in Tiny WKB, stored in base64 */
twkb: string;
/** GeoJSON-formatted geometry */
geoJSON: Geometry;
/** Geometry in EWKB, stored in base64, without coordinate system identifier */
ewkbNoSrid: string;
/** Geometry in EWKB (big endian), stored in base64, without coordinate system identifier */
ewkbXdrNoSrid: string;
/** Equivalent data in loaders.gl binary format (but stored as "normal" JS arrays) */
binary: BinaryGeometry;
}
interface ParsedTestCase {
/** Geometry in WKT */
wkt: string;
/** Geometry in WKB, stored in hex */
wkbHex: string;
/** Geometry in WKB */
wkb: ArrayBuffer;
/** Geometry in EWKB */
ewkb: ArrayBuffer;
/** Geometry in WKB XDR (big endian) */
wkbXdr: ArrayBuffer;
/** Geometry in WKB, stored in hex */
wkbHexXdr: string;
/** Geometry in EWKB XDR (big endian) */
ewkbXdr: ArrayBuffer;
/** Geometry in Tiny WKB */
twkb: ArrayBuffer;
/** GeoJSON-formatted geometry */
geoJSON: Geometry;
/** Geometry in EWKB, without coordinate system identifier */
ewkbNoSrid: ArrayBuffer;
/** Geometry in EWKB (big endian), without coordinate system identifier */
ewkbXdrNoSrid: ArrayBuffer;
/** Equivalent data in loaders.gl binary format*/
binary: BinaryGeometry;
}
/**
* Convert a hex string to an ArrayBuffer.
*
* @param hexString - hex representation of bytes
* @return Parsed bytes
*/
export default function hexStringToArrayBuffer(hexString: string): ArrayBuffer {
// remove the leading 0x
hexString = hexString.replace(/^0x/, '');
// split the string into pairs of octets
const pairs = hexString.match(/[\dA-F]{2}/gi);
// convert the octets to integers
const integers = pairs ? pairs.map((s) => parseInt(s, 16)) : [];
return new Uint8Array(integers).buffer;
}
export function parseTestCases(
testCases: Record<string, TestCase>
): Record<string, ParsedTestCase> {
const parsedTestCases: Record<string, ParsedTestCase> = {};
for (const [key, value] of Object.entries(testCases)) {
const {wkt, wkb, ewkb, wkbXdr, ewkbXdr, twkb, geoJSON, ewkbNoSrid, ewkbXdrNoSrid, binary} =
value;
// Convert binary arrays into typedArray
if (binary && binary.positions) {
binary.positions.value = new Float64Array(binary.positions.value);
}
if (binary && binary.type === 'LineString') {
binary.pathIndices.value = new Uint32Array(binary.pathIndices.value);
}
if (binary && binary.type === 'Polygon') {
binary.polygonIndices.value = new Uint32Array(binary.polygonIndices.value);
binary.primitivePolygonIndices.value = new Uint32Array(binary.primitivePolygonIndices.value);
}
const parsedTestCase: ParsedTestCase = {
wkt,
geoJSON,
wkbHex: wkb,
wkbHexXdr: wkbXdr,
wkb: hexStringToArrayBuffer(wkb),
ewkb: hexStringToArrayBuffer(ewkb),
twkb: hexStringToArrayBuffer(twkb),
wkbXdr: hexStringToArrayBuffer(wkbXdr),
ewkbXdr: hexStringToArrayBuffer(ewkbXdr),
ewkbNoSrid: hexStringToArrayBuffer(ewkbNoSrid),
ewkbXdrNoSrid: hexStringToArrayBuffer(ewkbXdrNoSrid),
binary
};
parsedTestCases[key] = parsedTestCase;
}
return parsedTestCases;
}