@margoengine/loaders 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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1198 -3
- package/dist/3dm.d.ts +161 -0
- package/dist/3dm.js +289 -0
- package/dist/3ds.d.ts +124 -0
- package/dist/3ds.js +392 -0
- package/dist/acclaim.d.ts +90 -0
- package/dist/acclaim.js +312 -0
- package/dist/amf.d.ts +45 -0
- package/dist/amf.js +256 -0
- package/dist/babylon.d.ts +197 -0
- package/dist/babylon.js +701 -0
- package/dist/common.d.ts +7 -0
- package/dist/common.js +6 -0
- package/dist/dae-animation.d.ts +66 -0
- package/dist/dae-animation.js +266 -0
- package/dist/dae-common.d.ts +95 -0
- package/dist/dae-common.js +226 -0
- package/dist/dae-controllers.d.ts +57 -0
- package/dist/dae-controllers.js +164 -0
- package/dist/dae-pose.d.ts +131 -0
- package/dist/dae-pose.js +381 -0
- package/dist/dae-scene.d.ts +203 -0
- package/dist/dae-scene.js +279 -0
- package/dist/dae.d.ts +161 -0
- package/dist/dae.js +633 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf.d.ts +81 -0
- package/dist/dxf.js +774 -0
- package/dist/engine.d.ts +36 -0
- package/dist/engine.js +117 -0
- package/dist/font-opentype.d.ts +14 -0
- package/dist/font-opentype.js +33 -0
- package/dist/font-woff.d.ts +23 -0
- package/dist/font-woff.js +293 -0
- package/dist/font.d.ts +41 -0
- package/dist/font.js +26 -0
- package/dist/gcode.d.ts +55 -0
- package/dist/gcode.js +278 -0
- package/dist/geo-model.d.ts +84 -0
- package/dist/geo-model.js +220 -0
- package/dist/geo-mvt.d.ts +15 -0
- package/dist/geo-mvt.js +302 -0
- package/dist/geo-shp.d.ts +15 -0
- package/dist/geo-shp.js +234 -0
- package/dist/geo-xml.d.ts +6 -0
- package/dist/geo-xml.js +165 -0
- package/dist/geo.d.ts +8 -0
- package/dist/geo.js +59 -0
- package/dist/geometry.d.ts +60 -0
- package/dist/geometry.js +273 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +28 -0
- package/dist/kmz.d.ts +78 -0
- package/dist/kmz.js +173 -0
- package/dist/las.d.ts +92 -0
- package/dist/las.js +110 -0
- package/dist/ldraw.d.ts +118 -0
- package/dist/ldraw.js +474 -0
- package/dist/load.d.ts +72 -0
- package/dist/load.js +205 -0
- package/dist/lottie.d.ts +154 -0
- package/dist/lottie.js +893 -0
- package/dist/lwo.d.ts +81 -0
- package/dist/lwo.js +454 -0
- package/dist/lzf.d.ts +13 -0
- package/dist/lzf.js +56 -0
- package/dist/materialx.d.ts +120 -0
- package/dist/materialx.js +563 -0
- package/dist/md2.d.ts +52 -0
- package/dist/md2.js +112 -0
- package/dist/mmcif.d.ts +14 -0
- package/dist/mmcif.js +246 -0
- package/dist/mmd.d.ts +244 -0
- package/dist/mmd.js +958 -0
- package/dist/mocap.d.ts +71 -0
- package/dist/mocap.js +178 -0
- package/dist/molecule-common.d.ts +4 -0
- package/dist/molecule-common.js +54 -0
- package/dist/mtl.d.ts +120 -0
- package/dist/mtl.js +469 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +63 -0
- package/dist/nrrd.js +743 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +143 -0
- package/dist/obj.js +456 -0
- package/dist/off.d.ts +10 -0
- package/dist/off.js +155 -0
- package/dist/outline-font.d.ts +87 -0
- package/dist/outline-font.js +110 -0
- package/dist/outline.d.ts +20 -0
- package/dist/outline.js +215 -0
- package/dist/pcd.d.ts +34 -0
- package/dist/pcd.js +258 -0
- package/dist/pdb.d.ts +67 -0
- package/dist/pdb.js +126 -0
- package/dist/ply.d.ts +35 -0
- package/dist/ply.js +392 -0
- package/dist/polygon.d.ts +9 -0
- package/dist/polygon.js +49 -0
- package/dist/prwm.d.ts +24 -0
- package/dist/prwm.js +114 -0
- package/dist/render.d.ts +27 -0
- package/dist/render.js +153 -0
- package/dist/sfnt-cff.d.ts +37 -0
- package/dist/sfnt-cff.js +749 -0
- package/dist/sfnt-layout.d.ts +24 -0
- package/dist/sfnt-layout.js +266 -0
- package/dist/sfnt-reader.d.ts +28 -0
- package/dist/sfnt-reader.js +87 -0
- package/dist/sfnt-variation.d.ts +40 -0
- package/dist/sfnt-variation.js +318 -0
- package/dist/shared/asset-url.js +82 -0
- package/dist/shared/crc32.d.ts +2 -0
- package/dist/shared/crc32.js +47 -0
- package/dist/shared/errors.js +36 -0
- package/dist/shared/inflate.js +284 -0
- package/dist/shared/normals.js +26 -0
- package/dist/shared/scene-scope.js +56 -0
- package/dist/shared/trailing-data.js +42 -0
- package/dist/stl.d.ts +64 -0
- package/dist/stl.js +309 -0
- package/dist/svg.d.ts +90 -0
- package/dist/svg.js +1007 -0
- package/dist/tds.d.ts +2 -0
- package/dist/tds.js +2 -0
- package/dist/textures.d.ts +42 -0
- package/dist/textures.js +53 -0
- package/dist/threemf-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +514 -0
- package/dist/threemf-materials.d.ts +75 -0
- package/dist/threemf-materials.js +314 -0
- package/dist/threemf-mesh.d.ts +60 -0
- package/dist/threemf-mesh.js +201 -0
- package/dist/threemf.d.ts +101 -0
- package/dist/threemf.js +415 -0
- package/dist/tilt.d.ts +62 -0
- package/dist/tilt.js +186 -0
- package/dist/tmx.d.ts +151 -0
- package/dist/tmx.js +394 -0
- package/dist/ttf.d.ts +78 -0
- package/dist/ttf.js +568 -0
- package/dist/types.d.ts +90 -0
- package/dist/types.js +35 -0
- package/dist/usd.d.ts +2 -0
- package/dist/usd.js +2 -0
- package/dist/usd.worker.d.ts +1 -0
- package/dist/usdz.d.ts +50 -0
- package/dist/usdz.js +646 -0
- package/dist/util.d.ts +75 -0
- package/dist/util.js +241 -0
- package/dist/vdb.d.ts +111 -0
- package/dist/vdb.js +663 -0
- package/dist/vrml.d.ts +53 -0
- package/dist/vrml.js +532 -0
- package/dist/vtk-cells.d.ts +29 -0
- package/dist/vtk-cells.js +211 -0
- package/dist/vtk-legacy.d.ts +57 -0
- package/dist/vtk-legacy.js +439 -0
- package/dist/vtk-reader.d.ts +19 -0
- package/dist/vtk-reader.js +134 -0
- package/dist/vtk-xml.d.ts +96 -0
- package/dist/vtk-xml.js +578 -0
- package/dist/vtk.d.ts +13 -0
- package/dist/vtk.js +234 -0
- package/dist/woff-glyf.d.ts +17 -0
- package/dist/woff-glyf.js +215 -0
- package/dist/worker.d.ts +10 -0
- package/dist/worker.js +93 -0
- package/dist/workers/usd.js +3 -0
- package/dist/xfile.d.ts +99 -0
- package/dist/xfile.js +706 -0
- package/dist/xml.d.ts +107 -0
- package/dist/xml.js +843 -0
- package/dist/xyz.d.ts +16 -0
- package/dist/xyz.js +110 -0
- package/dist/zip.d.ts +38 -0
- package/dist/zip.js +150 -0
- package/package.json +246 -6
package/dist/geo-mvt.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
import { toBytes, throwIfAborted } from './util.js';
|
|
3
|
+
import { buildGeo, geoLimit, GEO, area, ringPolygons, } from './geo-model.js';
|
|
4
|
+
class PBF {
|
|
5
|
+
constructor(bytes) {
|
|
6
|
+
this.bytes = bytes;
|
|
7
|
+
this.at = 0;
|
|
8
|
+
}
|
|
9
|
+
integer() {
|
|
10
|
+
let n = 0n;
|
|
11
|
+
for (let i = 0; i < 10; i++) {
|
|
12
|
+
if (this.at >= this.bytes.length)
|
|
13
|
+
throw new LoaderError(GEO, 'truncated', 'protobuf varint ends early');
|
|
14
|
+
const byte = this.bytes[this.at++];
|
|
15
|
+
if (i === 9 && byte > 1)
|
|
16
|
+
throw new LoaderError(GEO, 'range', 'protobuf varint exceeds uint64');
|
|
17
|
+
n |= BigInt(byte & 127) << BigInt(i * 7);
|
|
18
|
+
if (!(byte & 128))
|
|
19
|
+
return n;
|
|
20
|
+
}
|
|
21
|
+
throw new LoaderError(GEO, 'syntax', 'unterminated protobuf varint');
|
|
22
|
+
}
|
|
23
|
+
number() {
|
|
24
|
+
const n = this.integer();
|
|
25
|
+
if (n > BigInt(Number.MAX_SAFE_INTEGER))
|
|
26
|
+
throw new LoaderError(GEO, 'range', 'protobuf number exceeds exact precision');
|
|
27
|
+
return Number(n);
|
|
28
|
+
}
|
|
29
|
+
data() {
|
|
30
|
+
const n = this.number();
|
|
31
|
+
if (n > this.bytes.length - this.at)
|
|
32
|
+
throw new LoaderError(GEO, 'truncated', 'protobuf payload ends early');
|
|
33
|
+
const bytes = this.bytes.subarray(this.at, this.at + n);
|
|
34
|
+
this.at += n;
|
|
35
|
+
return bytes;
|
|
36
|
+
}
|
|
37
|
+
string() {
|
|
38
|
+
try {
|
|
39
|
+
return new TextDecoder('utf-8', { fatal: true }).decode(this.data());
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
if (e instanceof LoaderError)
|
|
43
|
+
throw e;
|
|
44
|
+
throw new LoaderError(GEO, 'syntax', 'protobuf string is not UTF-8');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
fixed(n) {
|
|
48
|
+
if (this.at + n > this.bytes.length)
|
|
49
|
+
throw new LoaderError(GEO, 'truncated', 'protobuf fixed value ends early');
|
|
50
|
+
const view = new DataView(this.bytes.buffer, this.bytes.byteOffset + this.at, n);
|
|
51
|
+
this.at += n;
|
|
52
|
+
return view;
|
|
53
|
+
}
|
|
54
|
+
skip(wire) {
|
|
55
|
+
if (wire === 0)
|
|
56
|
+
this.integer();
|
|
57
|
+
else if (wire === 1)
|
|
58
|
+
this.fixed(8);
|
|
59
|
+
else if (wire === 2)
|
|
60
|
+
this.data();
|
|
61
|
+
else if (wire === 5)
|
|
62
|
+
this.fixed(4);
|
|
63
|
+
else
|
|
64
|
+
throw new LoaderError(GEO, 'unsupported', `protobuf wire type ${wire} is unsupported`);
|
|
65
|
+
}
|
|
66
|
+
fields(each) {
|
|
67
|
+
while (this.at < this.bytes.length) {
|
|
68
|
+
const tag = this.number(), field = Math.floor(tag / 8), wire = tag % 8;
|
|
69
|
+
if (!field)
|
|
70
|
+
throw new LoaderError(GEO, 'syntax', 'protobuf field number is zero');
|
|
71
|
+
if (!each(field, wire))
|
|
72
|
+
this.skip(wire);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const exact = (n) => n >= BigInt(Number.MIN_SAFE_INTEGER) && n <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(n) : String(n);
|
|
77
|
+
function value(bytes) {
|
|
78
|
+
const r = new PBF(bytes);
|
|
79
|
+
let result = null, seen = false;
|
|
80
|
+
r.fields((f, w) => {
|
|
81
|
+
if (f < 1 || f > 7)
|
|
82
|
+
return false;
|
|
83
|
+
const wire = f === 1 ? 2 : f === 2 ? 5 : f === 3 ? 1 : 0;
|
|
84
|
+
if (w !== wire)
|
|
85
|
+
throw new LoaderError(GEO, 'syntax', 'MVT value has the wrong wire type');
|
|
86
|
+
if (seen)
|
|
87
|
+
throw new LoaderError(GEO, 'syntax', 'MVT value must have exactly one value');
|
|
88
|
+
seen = true;
|
|
89
|
+
if (f === 1)
|
|
90
|
+
result = r.string();
|
|
91
|
+
else if (f === 2)
|
|
92
|
+
result = r.fixed(4).getFloat32(0, true);
|
|
93
|
+
else if (f === 3)
|
|
94
|
+
result = r.fixed(8).getFloat64(0, true);
|
|
95
|
+
else {
|
|
96
|
+
const n = r.integer();
|
|
97
|
+
result =
|
|
98
|
+
f === 7
|
|
99
|
+
? n !== 0n
|
|
100
|
+
: exact(f === 4 ? BigInt.asIntN(64, n) : f === 6 ? (n & 1n ? -((n + 1n) >> 1n) : n >> 1n) : n);
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
});
|
|
104
|
+
if (!seen || (typeof result === 'number' && !Number.isFinite(result)))
|
|
105
|
+
throw new LoaderError(GEO, 'syntax', 'invalid MVT value');
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
function packed(r, wire, target) {
|
|
109
|
+
if (wire === 0)
|
|
110
|
+
target.push(r.number());
|
|
111
|
+
else if (wire === 2) {
|
|
112
|
+
const inner = new PBF(r.data());
|
|
113
|
+
while (inner.at < inner.bytes.length)
|
|
114
|
+
target.push(inner.number());
|
|
115
|
+
}
|
|
116
|
+
else
|
|
117
|
+
throw new LoaderError(GEO, 'syntax', 'packed protobuf values need varints');
|
|
118
|
+
}
|
|
119
|
+
function feature(bytes, keys, values, layer, extent, options, version) {
|
|
120
|
+
const r = new PBF(bytes), tags = [], commands = [];
|
|
121
|
+
let id, type = 0;
|
|
122
|
+
r.fields((f, w) => {
|
|
123
|
+
if (f === 1) {
|
|
124
|
+
if (w !== 0)
|
|
125
|
+
throw new LoaderError(GEO, 'syntax', 'MVT id needs a varint');
|
|
126
|
+
id = exact(r.integer());
|
|
127
|
+
}
|
|
128
|
+
else if (f === 2)
|
|
129
|
+
packed(r, w, tags);
|
|
130
|
+
else if (f === 3) {
|
|
131
|
+
if (w !== 0)
|
|
132
|
+
throw new LoaderError(GEO, 'syntax', 'MVT type needs a varint');
|
|
133
|
+
type = r.number();
|
|
134
|
+
}
|
|
135
|
+
else if (f === 4)
|
|
136
|
+
packed(r, w, commands);
|
|
137
|
+
else
|
|
138
|
+
return false;
|
|
139
|
+
return true;
|
|
140
|
+
});
|
|
141
|
+
if (tags.length % 2)
|
|
142
|
+
throw new LoaderError(GEO, 'syntax', 'MVT tags must be key/value pairs');
|
|
143
|
+
const properties = Object.create(null);
|
|
144
|
+
for (let t = 0; t < tags.length; t += 2) {
|
|
145
|
+
const k = tags[t], v = tags[t + 1];
|
|
146
|
+
if (k >= keys.length || v >= values.length)
|
|
147
|
+
throw new LoaderError(GEO, 'range', 'MVT tag index is out of range');
|
|
148
|
+
properties[keys[k]] = values[v];
|
|
149
|
+
}
|
|
150
|
+
if (type < 0 || type > 3)
|
|
151
|
+
throw new LoaderError(GEO, 'unsupported', `unknown MVT geometry ${type}`);
|
|
152
|
+
let x = 0, y = 0, at = 0, total = 0, closed = false;
|
|
153
|
+
const paths = [];
|
|
154
|
+
let path;
|
|
155
|
+
const position = () => {
|
|
156
|
+
if (at + 2 > commands.length)
|
|
157
|
+
throw new LoaderError(GEO, 'truncated', 'MVT coordinate delta ends early');
|
|
158
|
+
const dx = commands[at++], dy = commands[at++];
|
|
159
|
+
if (dx > 0xffffffff || dy > 0xffffffff)
|
|
160
|
+
throw new LoaderError(GEO, 'range', 'MVT deltas must be uint32');
|
|
161
|
+
x += dx % 2 ? -(dx + 1) / 2 : dx / 2;
|
|
162
|
+
y += dy % 2 ? -(dy + 1) / 2 : dy / 2;
|
|
163
|
+
if (!Number.isSafeInteger(x) || !Number.isSafeInteger(y))
|
|
164
|
+
throw new LoaderError(GEO, 'range', 'MVT coordinate exceeds exact precision');
|
|
165
|
+
if (++total > (options.maxVertices ?? 8000000))
|
|
166
|
+
throw new LoaderError(GEO, 'limit', 'MVT geometry exceeds the vertex limit');
|
|
167
|
+
if (!options.tile)
|
|
168
|
+
return [x, y];
|
|
169
|
+
const tile = options.tile, n = 2 ** tile.z, longitude = ((tile.x + x / extent) / n) * 360 - 180;
|
|
170
|
+
const latitude = (Math.atan(Math.sinh(Math.PI * (1 - (2 * (tile.y + y / extent)) / n))) * 180) / Math.PI;
|
|
171
|
+
return [longitude, latitude];
|
|
172
|
+
};
|
|
173
|
+
while (at < commands.length) {
|
|
174
|
+
const command = commands[at++], op = command % 8, count = Math.floor(command / 8);
|
|
175
|
+
if (command > 0xffffffff || !count)
|
|
176
|
+
throw new LoaderError(GEO, 'syntax', 'invalid MVT geometry command');
|
|
177
|
+
if (op === 1 || op === 2) {
|
|
178
|
+
if (count > (commands.length - at) / 2)
|
|
179
|
+
throw new LoaderError(GEO, 'truncated', 'MVT command count exceeds its coordinates');
|
|
180
|
+
if (op === 2 && (!path || closed || type === 1))
|
|
181
|
+
throw new LoaderError(GEO, 'syntax', 'MVT LineTo needs an open line or polygon');
|
|
182
|
+
if (op === 1 && type !== 1 && count !== 1)
|
|
183
|
+
throw new LoaderError(GEO, 'syntax', 'line/polygon MoveTo count must be one');
|
|
184
|
+
if (op === 1 && type === 3 && path && !closed)
|
|
185
|
+
throw new LoaderError(GEO, 'syntax', 'MVT polygon ring was not closed');
|
|
186
|
+
for (let i = 0; i < count; i++) {
|
|
187
|
+
const p = position();
|
|
188
|
+
if (op === 1) {
|
|
189
|
+
path = [p];
|
|
190
|
+
paths.push(path);
|
|
191
|
+
closed = false;
|
|
192
|
+
}
|
|
193
|
+
else
|
|
194
|
+
path.push(p);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else if (op === 7) {
|
|
198
|
+
if (type !== 3 || count !== 1 || !path || closed || path.length < 3)
|
|
199
|
+
throw new LoaderError(GEO, 'syntax', 'invalid MVT ClosePath');
|
|
200
|
+
path.push([...path[0]]);
|
|
201
|
+
closed = true;
|
|
202
|
+
}
|
|
203
|
+
else
|
|
204
|
+
throw new LoaderError(GEO, 'unsupported', `unknown MVT geometry command ${op}`);
|
|
205
|
+
}
|
|
206
|
+
let geometry = null;
|
|
207
|
+
if (type === 1)
|
|
208
|
+
geometry = { type: 'MultiPoint', coordinates: paths.flat() };
|
|
209
|
+
else if (type === 2)
|
|
210
|
+
geometry = { type: 'MultiLineString', coordinates: paths };
|
|
211
|
+
else if (type === 3) {
|
|
212
|
+
if (path && !closed)
|
|
213
|
+
throw new LoaderError(GEO, 'syntax', 'MVT polygon was not closed');
|
|
214
|
+
const polygons = [];
|
|
215
|
+
// Projection reverses screen-y winding, so classify in the corresponding coordinate convention.
|
|
216
|
+
for (const ring of version === 1 ? [] : paths) {
|
|
217
|
+
const signed = area(ring) * (options.tile ? -1 : 1);
|
|
218
|
+
if (signed > 0)
|
|
219
|
+
polygons.push([ring]);
|
|
220
|
+
else if (signed < 0 && polygons.length)
|
|
221
|
+
polygons.at(-1).push(ring);
|
|
222
|
+
else
|
|
223
|
+
throw new LoaderError(GEO, 'syntax', 'MVT polygon needs a nondegenerate exterior before holes');
|
|
224
|
+
}
|
|
225
|
+
if (version === 1)
|
|
226
|
+
polygons.push(...ringPolygons(paths));
|
|
227
|
+
geometry = { type: 'MultiPolygon', coordinates: polygons };
|
|
228
|
+
}
|
|
229
|
+
const result = { type: 'Feature', properties, geometry, layer, extent };
|
|
230
|
+
if (id !== undefined)
|
|
231
|
+
result.id = id;
|
|
232
|
+
return result;
|
|
233
|
+
}
|
|
234
|
+
/** Reads MVT 1/2 protobuf vector tiles, preserving layers, tags, IDs, multipart lines and polygon holes. */
|
|
235
|
+
export function parseMVT(input, options = {}) {
|
|
236
|
+
throwIfAborted(options.signal, GEO);
|
|
237
|
+
const bytes = toBytes(input, GEO), max = geoLimit(options, 'maxBytes', 256 << 20), maxFeatures = geoLimit(options, 'maxFeatures', 100000);
|
|
238
|
+
if (bytes.length > max)
|
|
239
|
+
throw new LoaderError(GEO, 'limit', `MVT exceeds ${max} bytes`);
|
|
240
|
+
if (options.tile) {
|
|
241
|
+
const { x, y, z } = options.tile;
|
|
242
|
+
if (!Number.isSafeInteger(z) ||
|
|
243
|
+
z < 0 ||
|
|
244
|
+
z > 30 ||
|
|
245
|
+
!Number.isSafeInteger(x) ||
|
|
246
|
+
!Number.isSafeInteger(y) ||
|
|
247
|
+
x < 0 ||
|
|
248
|
+
y < 0 ||
|
|
249
|
+
x >= 2 ** z ||
|
|
250
|
+
y >= 2 ** z)
|
|
251
|
+
throw new LoaderError(GEO, 'range', 'MVT tile address must fit zoom 0..30');
|
|
252
|
+
}
|
|
253
|
+
const result = [], r = new PBF(bytes);
|
|
254
|
+
const layerNames = new Set();
|
|
255
|
+
r.fields((f, w) => {
|
|
256
|
+
if (f !== 3)
|
|
257
|
+
return false;
|
|
258
|
+
if (w !== 2)
|
|
259
|
+
throw new LoaderError(GEO, 'syntax', 'MVT layer must be length-delimited');
|
|
260
|
+
const layer = new PBF(r.data()), keys = [], values = [], features = [];
|
|
261
|
+
let name = '', extent = 4096, version = 1;
|
|
262
|
+
layer.fields((field, wire) => {
|
|
263
|
+
if (([1, 2, 3, 4].includes(field) && wire !== 2) || ([5, 15].includes(field) && wire !== 0))
|
|
264
|
+
throw new LoaderError(GEO, 'syntax', 'MVT layer field has the wrong wire type');
|
|
265
|
+
if (field === 1)
|
|
266
|
+
name = layer.string();
|
|
267
|
+
else if (field === 2) {
|
|
268
|
+
if (features.length >= maxFeatures)
|
|
269
|
+
throw new LoaderError(GEO, 'limit', 'MVT layer has too many features');
|
|
270
|
+
features.push(layer.data());
|
|
271
|
+
}
|
|
272
|
+
else if (field === 3)
|
|
273
|
+
keys.push(layer.string());
|
|
274
|
+
else if (field === 4)
|
|
275
|
+
values.push(value(layer.data()));
|
|
276
|
+
else if (field === 5)
|
|
277
|
+
extent = layer.number();
|
|
278
|
+
else if (field === 15)
|
|
279
|
+
version = layer.number();
|
|
280
|
+
else
|
|
281
|
+
return false;
|
|
282
|
+
return true;
|
|
283
|
+
});
|
|
284
|
+
if (!name ||
|
|
285
|
+
!Number.isSafeInteger(extent) ||
|
|
286
|
+
extent < 1 ||
|
|
287
|
+
extent > 0xffffffff ||
|
|
288
|
+
version < 1 ||
|
|
289
|
+
version > 2 ||
|
|
290
|
+
layerNames.has(name))
|
|
291
|
+
throw new LoaderError(GEO, 'header', 'invalid MVT layer name, extent or version');
|
|
292
|
+
layerNames.add(name);
|
|
293
|
+
if (!options.layers || options.layers.includes(name))
|
|
294
|
+
for (const bytes of features) {
|
|
295
|
+
if (result.length >= maxFeatures)
|
|
296
|
+
throw new LoaderError(GEO, 'limit', 'MVT has too many features');
|
|
297
|
+
result.push(feature(bytes, keys, values, name, extent, options, version));
|
|
298
|
+
}
|
|
299
|
+
return true;
|
|
300
|
+
});
|
|
301
|
+
return buildGeo(result, options, options.tile ? 'OGC:CRS84' : 'tile-local');
|
|
302
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type BinaryInput } from './util.js';
|
|
2
|
+
import { type GeoOptions, type GeoResult } from './geo-model.js';
|
|
3
|
+
/** Shapefile sidecars and vector parse bounds. */
|
|
4
|
+
export interface ShapefileOptions extends GeoOptions {
|
|
5
|
+
/** Optional matching dBASE attribute table, in original record order. */
|
|
6
|
+
dbf?: BinaryInput;
|
|
7
|
+
/** Source .prj text. Preserved as the result's CRS; coordinates are never silently reprojected. */
|
|
8
|
+
prj?: string;
|
|
9
|
+
/** dBASE string encoding. Default windows-1252. Pass the .cpg label when available. */
|
|
10
|
+
encoding?: string;
|
|
11
|
+
/** Keep records marked deleted by dBASE. Default false. */
|
|
12
|
+
includeDeleted?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** Reads Shapefile points, multipoints, polylines, polygons and multipatches, including Z/M and optional dBASE. */
|
|
15
|
+
export declare function parseShapefile(input: BinaryInput, options?: ShapefileOptions): GeoResult;
|
package/dist/geo-shp.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
import { toBytes, throwIfAborted } from './util.js';
|
|
3
|
+
import { buildGeo, geoLimit, GEO, ringPolygons, } from './geo-model.js';
|
|
4
|
+
function dbf(input, options) {
|
|
5
|
+
const bytes = toBytes(input, GEO), view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
|
|
6
|
+
if (bytes.length < 33)
|
|
7
|
+
throw new LoaderError(GEO, 'truncated', 'dBASE header is incomplete');
|
|
8
|
+
const records = view.getUint32(4, true), header = view.getUint16(8, true), stride = view.getUint16(10, true);
|
|
9
|
+
if (header < 33 ||
|
|
10
|
+
(header - 33) % 32 ||
|
|
11
|
+
header > bytes.length ||
|
|
12
|
+
bytes[header - 1] !== 13 ||
|
|
13
|
+
stride < 1 ||
|
|
14
|
+
header + records * stride > bytes.length)
|
|
15
|
+
throw new LoaderError(GEO, 'truncated', 'invalid dBASE header or record lengths');
|
|
16
|
+
if (records > geoLimit(options, 'maxFeatures', 100000))
|
|
17
|
+
throw new LoaderError(GEO, 'limit', 'too many dBASE records');
|
|
18
|
+
let decoder;
|
|
19
|
+
try {
|
|
20
|
+
decoder = new TextDecoder(options.encoding ?? 'windows-1252');
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
throw new LoaderError(GEO, 'unsupported', 'unsupported dBASE encoding; pass a TextDecoder encoding label');
|
|
24
|
+
}
|
|
25
|
+
const fields = [];
|
|
26
|
+
let offset = 1;
|
|
27
|
+
for (let at = 32; at < header - 1; at += 32) {
|
|
28
|
+
const name = decoder.decode(bytes.subarray(at, at + 11)).replace(/\0.*$/, '');
|
|
29
|
+
const type = String.fromCharCode(bytes[at + 11]), length = bytes[at + 16];
|
|
30
|
+
if (!name || !length || fields.some((f) => f.name === name))
|
|
31
|
+
throw new LoaderError(GEO, 'syntax', 'invalid or duplicate dBASE field');
|
|
32
|
+
fields.push({ name, type, length, offset });
|
|
33
|
+
offset += length;
|
|
34
|
+
}
|
|
35
|
+
if (offset !== stride)
|
|
36
|
+
throw new LoaderError(GEO, 'range', 'dBASE field widths do not match record width');
|
|
37
|
+
return Array.from({ length: records }, (_, r) => {
|
|
38
|
+
const start = header + r * stride, properties = Object.create(null);
|
|
39
|
+
if (bytes[start] !== 32 && bytes[start] !== 42)
|
|
40
|
+
throw new LoaderError(GEO, 'syntax', 'invalid dBASE deletion marker');
|
|
41
|
+
for (const field of fields) {
|
|
42
|
+
const at = start + field.offset, raw = decoder
|
|
43
|
+
.decode(bytes.subarray(at, at + field.length))
|
|
44
|
+
.replace(/\0+$/, '')
|
|
45
|
+
.trim();
|
|
46
|
+
let value = raw;
|
|
47
|
+
if (field.type === 'N' || field.type === 'F') {
|
|
48
|
+
if (!raw || /^\*+$/.test(raw))
|
|
49
|
+
value = null;
|
|
50
|
+
else {
|
|
51
|
+
const n = Number(raw);
|
|
52
|
+
if (!Number.isFinite(n))
|
|
53
|
+
throw new LoaderError(GEO, 'syntax', `invalid dBASE number in ${field.name}`);
|
|
54
|
+
value = Number.isInteger(n) && !Number.isSafeInteger(n) ? raw : n;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else if (field.type === 'L')
|
|
58
|
+
value = /^[YyTt]$/.test(raw) ? true : /^[NnFf]$/.test(raw) ? false : null;
|
|
59
|
+
else if (field.type === 'D')
|
|
60
|
+
value = /^\d{8}$/.test(raw) ? `${raw.slice(0, 4)}-${raw.slice(4, 6)}-${raw.slice(6)}` : null;
|
|
61
|
+
else if (field.type === 'I' && field.length === 4)
|
|
62
|
+
value = view.getInt32(at, true);
|
|
63
|
+
else if ((field.type === 'B' || field.type === 'O') && field.length === 8)
|
|
64
|
+
value = view.getFloat64(at, true);
|
|
65
|
+
properties[field.name] = value;
|
|
66
|
+
}
|
|
67
|
+
return { properties, deleted: bytes[start] === 42 };
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/** Reads Shapefile points, multipoints, polylines, polygons and multipatches, including Z/M and optional dBASE. */
|
|
71
|
+
export function parseShapefile(input, options = {}) {
|
|
72
|
+
throwIfAborted(options.signal, GEO);
|
|
73
|
+
const bytes = toBytes(input, GEO), maxBytes = geoLimit(options, 'maxBytes', 256 << 20), maxFeatures = geoLimit(options, 'maxFeatures', 100000);
|
|
74
|
+
if (bytes.length > maxBytes)
|
|
75
|
+
throw new LoaderError(GEO, 'limit', `Shapefile exceeds ${maxBytes} bytes`);
|
|
76
|
+
if (bytes.length < 100)
|
|
77
|
+
throw new LoaderError(GEO, 'truncated', 'Shapefile header is incomplete');
|
|
78
|
+
const file = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
|
|
79
|
+
if (file.getInt32(0, false) !== 9994 || file.getInt32(28, true) !== 1000)
|
|
80
|
+
throw new LoaderError(GEO, 'header', 'invalid Shapefile identifier or version');
|
|
81
|
+
if (file.getInt32(24, false) * 2 !== bytes.length)
|
|
82
|
+
throw new LoaderError(GEO, 'truncated', 'Shapefile length does not match its header');
|
|
83
|
+
const declared = file.getInt32(32, true), rows = options.dbf ? dbf(options.dbf, options) : undefined, features = [];
|
|
84
|
+
if (![0, 1, 3, 5, 8, 11, 13, 15, 18, 21, 23, 25, 28, 31].includes(declared))
|
|
85
|
+
throw new LoaderError(GEO, 'unsupported', `unsupported Shapefile shape ${declared}`);
|
|
86
|
+
let at = 100, record = 0;
|
|
87
|
+
while (at < bytes.length) {
|
|
88
|
+
if (record >= maxFeatures)
|
|
89
|
+
throw new LoaderError(GEO, 'limit', `Shapefile records exceed ${maxFeatures}`);
|
|
90
|
+
if (at + 8 > bytes.length)
|
|
91
|
+
throw new LoaderError(GEO, 'truncated', 'Shapefile record header is incomplete');
|
|
92
|
+
const id = file.getInt32(at, false), length = file.getInt32(at + 4, false) * 2;
|
|
93
|
+
at += 8;
|
|
94
|
+
if (id < 1 || length < 4 || at + length > bytes.length)
|
|
95
|
+
throw new LoaderError(GEO, 'truncated', 'invalid Shapefile record length or number');
|
|
96
|
+
const v = new DataView(bytes.buffer, bytes.byteOffset + at, length), shape = v.getInt32(0, true);
|
|
97
|
+
if (shape !== 0 && shape !== declared)
|
|
98
|
+
throw new LoaderError(GEO, 'range', 'record shape type differs from file header');
|
|
99
|
+
const need = (end) => {
|
|
100
|
+
if (end > length)
|
|
101
|
+
throw new LoaderError(GEO, 'truncated', 'Shapefile record ends inside geometry');
|
|
102
|
+
};
|
|
103
|
+
const u32 = (offset) => {
|
|
104
|
+
need(offset + 4);
|
|
105
|
+
return v.getUint32(offset, true);
|
|
106
|
+
};
|
|
107
|
+
const double = (offset) => {
|
|
108
|
+
need(offset + 8);
|
|
109
|
+
const n = v.getFloat64(offset, true);
|
|
110
|
+
if (!Number.isFinite(n))
|
|
111
|
+
throw new LoaderError(GEO, 'range', 'Shapefile coordinates must be finite');
|
|
112
|
+
return n;
|
|
113
|
+
};
|
|
114
|
+
let geometry = null;
|
|
115
|
+
if ([1, 11, 21].includes(shape)) {
|
|
116
|
+
const p = [double(4), double(12)];
|
|
117
|
+
let end = 20;
|
|
118
|
+
if (shape === 11) {
|
|
119
|
+
p.push(double(end));
|
|
120
|
+
end += 8;
|
|
121
|
+
}
|
|
122
|
+
if (shape === 21 || (shape === 11 && length >= end + 8)) {
|
|
123
|
+
if (p.length === 2)
|
|
124
|
+
p.push(0);
|
|
125
|
+
p.push(double(end));
|
|
126
|
+
end += 8;
|
|
127
|
+
}
|
|
128
|
+
if (end !== length)
|
|
129
|
+
throw new LoaderError(GEO, 'syntax', 'Shapefile point has trailing data');
|
|
130
|
+
geometry = { type: 'Point', coordinates: p };
|
|
131
|
+
}
|
|
132
|
+
else if ([3, 5, 8, 13, 15, 18, 23, 25, 28, 31].includes(shape)) {
|
|
133
|
+
need(40);
|
|
134
|
+
const multi = [8, 18, 28].includes(shape), parts = multi ? 0 : u32(36), n = u32(multi ? 36 : 40);
|
|
135
|
+
if (n > (options.maxVertices ?? 8000000) || parts > n)
|
|
136
|
+
throw new LoaderError(GEO, 'limit', 'Shapefile point/part count exceeds bounds');
|
|
137
|
+
let cursor = multi ? 40 : 44;
|
|
138
|
+
const starts = [];
|
|
139
|
+
for (let p = 0; p < parts; p++) {
|
|
140
|
+
const index = u32(cursor);
|
|
141
|
+
cursor += 4;
|
|
142
|
+
if (index >= n || (p === 0 && index !== 0) || (p > 0 && index <= starts[p - 1]))
|
|
143
|
+
throw new LoaderError(GEO, 'range', 'invalid Shapefile part offsets');
|
|
144
|
+
starts.push(index);
|
|
145
|
+
}
|
|
146
|
+
if (!multi && !parts && n)
|
|
147
|
+
throw new LoaderError(GEO, 'range', 'nonempty Shapefile needs parts');
|
|
148
|
+
const patchTypes = [];
|
|
149
|
+
if (shape === 31)
|
|
150
|
+
for (let p = 0; p < parts; p++) {
|
|
151
|
+
patchTypes.push(u32(cursor));
|
|
152
|
+
cursor += 4;
|
|
153
|
+
}
|
|
154
|
+
need(cursor + n * 16);
|
|
155
|
+
const coords = Array.from({ length: n }, (_, i) => [
|
|
156
|
+
double(cursor + i * 16),
|
|
157
|
+
double(cursor + i * 16 + 8),
|
|
158
|
+
]);
|
|
159
|
+
cursor += n * 16;
|
|
160
|
+
if ([13, 15, 18, 31].includes(shape)) {
|
|
161
|
+
need(cursor + 16 + n * 8);
|
|
162
|
+
cursor += 16;
|
|
163
|
+
for (let i = 0; i < n; i++)
|
|
164
|
+
coords[i].push(double(cursor + i * 8));
|
|
165
|
+
cursor += n * 8;
|
|
166
|
+
}
|
|
167
|
+
if ([13, 15, 18, 23, 25, 28, 31].includes(shape) && cursor < length) {
|
|
168
|
+
need(cursor + 16 + n * 8);
|
|
169
|
+
cursor += 16;
|
|
170
|
+
for (let i = 0; i < n; i++) {
|
|
171
|
+
if (coords[i].length === 2)
|
|
172
|
+
coords[i].push(0);
|
|
173
|
+
coords[i].push(double(cursor + i * 8));
|
|
174
|
+
}
|
|
175
|
+
cursor += n * 8;
|
|
176
|
+
}
|
|
177
|
+
if (cursor !== length)
|
|
178
|
+
throw new LoaderError(GEO, 'syntax', 'Shapefile record has trailing geometry data');
|
|
179
|
+
if (multi)
|
|
180
|
+
geometry = { type: 'MultiPoint', coordinates: coords };
|
|
181
|
+
else {
|
|
182
|
+
const pieces = starts.map((start, p) => coords.slice(start, starts[p + 1] ?? n));
|
|
183
|
+
if ([3, 13, 23].includes(shape))
|
|
184
|
+
geometry = { type: 'MultiLineString', coordinates: pieces };
|
|
185
|
+
else if (shape === 31) {
|
|
186
|
+
const polygons = [];
|
|
187
|
+
// Ring sequences (ESRI Shapefile Technical Description, MultiPatch): an Outer Ring and the Inner Rings
|
|
188
|
+
// after it, or a First Ring and the Rings after it, make one polygon; a lone Ring is its own.
|
|
189
|
+
let open, opener = -1;
|
|
190
|
+
for (let p = 0; p < pieces.length; p++) {
|
|
191
|
+
const patch = pieces[p], kind = patchTypes[p];
|
|
192
|
+
if (kind === 0 || kind === 1) {
|
|
193
|
+
if (patch.length < 3)
|
|
194
|
+
throw new LoaderError(GEO, 'range', 'multipatch strips/fans need at least three points');
|
|
195
|
+
for (let i = 2; i < patch.length; i++) {
|
|
196
|
+
const tri = kind === 1
|
|
197
|
+
? [patch[0], patch[i - 1], patch[i]]
|
|
198
|
+
: i % 2
|
|
199
|
+
? [patch[i - 1], patch[i - 2], patch[i]]
|
|
200
|
+
: [patch[i - 2], patch[i - 1], patch[i]];
|
|
201
|
+
polygons.push([[...tri, tri[0]]]);
|
|
202
|
+
}
|
|
203
|
+
opener = -1;
|
|
204
|
+
}
|
|
205
|
+
else if (open && ((kind === 3 && opener === 2) || (kind === 5 && opener === 4)))
|
|
206
|
+
open.push(patch);
|
|
207
|
+
else if (kind >= 2 && kind <= 5) {
|
|
208
|
+
polygons.push((open = [patch]));
|
|
209
|
+
opener = kind === 2 || kind === 4 ? kind : -1;
|
|
210
|
+
}
|
|
211
|
+
else
|
|
212
|
+
throw new LoaderError(GEO, 'unsupported', `unknown multipatch part type ${kind}`);
|
|
213
|
+
}
|
|
214
|
+
geometry = { type: 'MultiPolygon', coordinates: polygons };
|
|
215
|
+
}
|
|
216
|
+
else
|
|
217
|
+
geometry = { type: 'MultiPolygon', coordinates: ringPolygons(pieces) };
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
else if (shape !== 0)
|
|
221
|
+
throw new LoaderError(GEO, 'unsupported', `unsupported Shapefile shape ${shape}`);
|
|
222
|
+
if (!rows?.[record]?.deleted || options.includeDeleted) {
|
|
223
|
+
const properties = rows?.[record]?.properties ?? Object.create(null);
|
|
224
|
+
if (rows?.[record]?.deleted)
|
|
225
|
+
properties.deleted = true;
|
|
226
|
+
features.push({ type: 'Feature', id, properties, geometry });
|
|
227
|
+
}
|
|
228
|
+
record++;
|
|
229
|
+
at += length;
|
|
230
|
+
}
|
|
231
|
+
if (rows && rows.length !== record)
|
|
232
|
+
throw new LoaderError(GEO, 'range', 'dBASE record count differs from Shapefile');
|
|
233
|
+
return buildGeo(features, options, options.prj ?? 'unspecified');
|
|
234
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type BinaryInput } from './util.js';
|
|
2
|
+
import { type GeoOptions, type GeoResult } from './geo-model.js';
|
|
3
|
+
/** Reads GPX 1.0/1.1 waypoints, routes and separate track segments, keeping elevations and timestamps. */
|
|
4
|
+
export declare function parseGPX(input: BinaryInput | string, options?: GeoOptions): GeoResult;
|
|
5
|
+
/** Reads KML placemarks, holes and MultiGeometry/gx tracks without fetching external links or models. */
|
|
6
|
+
export declare function parseKML(input: BinaryInput | string, options?: GeoOptions): GeoResult;
|