@margoengine/loaders 0.0.0-stage → 1.0.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.
Files changed (226) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1665 -2
  4. package/dist/3dm.d.ts +161 -0
  5. package/dist/3dm.js +289 -0
  6. package/dist/3ds.d.ts +124 -0
  7. package/dist/3ds.js +392 -0
  8. package/dist/acclaim.d.ts +90 -0
  9. package/dist/acclaim.js +312 -0
  10. package/dist/amf.d.ts +45 -0
  11. package/dist/amf.js +256 -0
  12. package/dist/babylon.d.ts +197 -0
  13. package/dist/babylon.js +701 -0
  14. package/dist/common.d.ts +7 -0
  15. package/dist/common.js +6 -0
  16. package/dist/crease.d.ts +7 -0
  17. package/dist/crease.js +67 -0
  18. package/dist/dae-animation.d.ts +66 -0
  19. package/dist/dae-animation.js +266 -0
  20. package/dist/dae-common.d.ts +95 -0
  21. package/dist/dae-common.js +226 -0
  22. package/dist/dae-controllers.d.ts +57 -0
  23. package/dist/dae-controllers.js +164 -0
  24. package/dist/dae-kinematics.d.ts +65 -0
  25. package/dist/dae-kinematics.js +461 -0
  26. package/dist/dae-pose.d.ts +131 -0
  27. package/dist/dae-pose.js +381 -0
  28. package/dist/dae-scene.d.ts +203 -0
  29. package/dist/dae-scene.js +279 -0
  30. package/dist/dae.d.ts +166 -0
  31. package/dist/dae.js +644 -0
  32. package/dist/detect.d.ts +20 -0
  33. package/dist/detect.js +72 -0
  34. package/dist/dxf-hatch.d.ts +8 -0
  35. package/dist/dxf-hatch.js +346 -0
  36. package/dist/dxf-leaders.d.ts +59 -0
  37. package/dist/dxf-leaders.js +258 -0
  38. package/dist/dxf.d.ts +122 -0
  39. package/dist/dxf.js +985 -0
  40. package/dist/engine.d.ts +41 -0
  41. package/dist/engine.js +120 -0
  42. package/dist/font-opentype.d.ts +14 -0
  43. package/dist/font-opentype.js +33 -0
  44. package/dist/font-woff.d.ts +23 -0
  45. package/dist/font-woff.js +293 -0
  46. package/dist/font.d.ts +41 -0
  47. package/dist/font.js +26 -0
  48. package/dist/gcode.d.ts +92 -0
  49. package/dist/gcode.js +369 -0
  50. package/dist/geo-model.d.ts +84 -0
  51. package/dist/geo-model.js +220 -0
  52. package/dist/geo-mvt.d.ts +15 -0
  53. package/dist/geo-mvt.js +302 -0
  54. package/dist/geo-shp.d.ts +15 -0
  55. package/dist/geo-shp.js +234 -0
  56. package/dist/geo-xml.d.ts +6 -0
  57. package/dist/geo-xml.js +165 -0
  58. package/dist/geo.d.ts +8 -0
  59. package/dist/geo.js +59 -0
  60. package/dist/geometry.d.ts +60 -0
  61. package/dist/geometry.js +273 -0
  62. package/dist/index.d.ts +54 -0
  63. package/dist/index.js +28 -0
  64. package/dist/kmz.d.ts +78 -0
  65. package/dist/kmz.js +173 -0
  66. package/dist/las.d.ts +92 -0
  67. package/dist/las.js +110 -0
  68. package/dist/ldraw.d.ts +118 -0
  69. package/dist/ldraw.js +474 -0
  70. package/dist/load.d.ts +72 -0
  71. package/dist/load.js +205 -0
  72. package/dist/lottie.d.ts +154 -0
  73. package/dist/lottie.js +893 -0
  74. package/dist/lwo.d.ts +81 -0
  75. package/dist/lwo.js +454 -0
  76. package/dist/lzf.d.ts +13 -0
  77. package/dist/lzf.js +56 -0
  78. package/dist/materialx.d.ts +120 -0
  79. package/dist/materialx.js +563 -0
  80. package/dist/md2.d.ts +52 -0
  81. package/dist/md2.js +112 -0
  82. package/dist/mmcif.d.ts +14 -0
  83. package/dist/mmcif.js +246 -0
  84. package/dist/mmd.d.ts +244 -0
  85. package/dist/mmd.js +958 -0
  86. package/dist/mocap.d.ts +71 -0
  87. package/dist/mocap.js +178 -0
  88. package/dist/molecule-common.d.ts +4 -0
  89. package/dist/molecule-common.js +54 -0
  90. package/dist/mtl.d.ts +125 -0
  91. package/dist/mtl.js +550 -0
  92. package/dist/normals.d.ts +7 -0
  93. package/dist/normals.js +31 -0
  94. package/dist/nrrd.d.ts +70 -0
  95. package/dist/nrrd.js +785 -0
  96. package/dist/nurbs.d.ts +118 -0
  97. package/dist/nurbs.js +265 -0
  98. package/dist/obj.d.ts +145 -0
  99. package/dist/obj.js +462 -0
  100. package/dist/off.d.ts +10 -0
  101. package/dist/off.js +155 -0
  102. package/dist/outline-font.d.ts +87 -0
  103. package/dist/outline-font.js +110 -0
  104. package/dist/outline.d.ts +20 -0
  105. package/dist/outline.js +215 -0
  106. package/dist/pcd.d.ts +34 -0
  107. package/dist/pcd.js +258 -0
  108. package/dist/pdb.d.ts +67 -0
  109. package/dist/pdb.js +126 -0
  110. package/dist/ply.d.ts +35 -0
  111. package/dist/ply.js +392 -0
  112. package/dist/polygon.d.ts +9 -0
  113. package/dist/polygon.js +49 -0
  114. package/dist/prwm.d.ts +24 -0
  115. package/dist/prwm.js +114 -0
  116. package/dist/render.d.ts +32 -0
  117. package/dist/render.js +181 -0
  118. package/dist/sfnt-cff.d.ts +37 -0
  119. package/dist/sfnt-cff.js +749 -0
  120. package/dist/sfnt-layout.d.ts +24 -0
  121. package/dist/sfnt-layout.js +266 -0
  122. package/dist/sfnt-reader.d.ts +28 -0
  123. package/dist/sfnt-reader.js +87 -0
  124. package/dist/sfnt-variation.d.ts +40 -0
  125. package/dist/sfnt-variation.js +318 -0
  126. package/dist/shared/asset-url.js +82 -0
  127. package/dist/shared/crc32.d.ts +2 -0
  128. package/dist/shared/crc32.js +47 -0
  129. package/dist/shared/errors.js +36 -0
  130. package/dist/shared/inflate.js +284 -0
  131. package/dist/shared/normals.js +26 -0
  132. package/dist/shared/scene-scope.js +56 -0
  133. package/dist/shared/trailing-data.js +45 -0
  134. package/dist/stl.d.ts +64 -0
  135. package/dist/stl.js +309 -0
  136. package/dist/svg.d.ts +90 -0
  137. package/dist/svg.js +1007 -0
  138. package/dist/tds.d.ts +2 -0
  139. package/dist/tds.js +2 -0
  140. package/dist/textures.d.ts +42 -0
  141. package/dist/textures.js +53 -0
  142. package/dist/threemf-beam-reader.d.ts +19 -0
  143. package/dist/threemf-beam-reader.js +165 -0
  144. package/dist/threemf-beams.d.ts +52 -0
  145. package/dist/threemf-beams.js +268 -0
  146. package/dist/threemf-boolean-read.d.ts +10 -0
  147. package/dist/threemf-boolean-read.js +113 -0
  148. package/dist/threemf-boolean.d.ts +59 -0
  149. package/dist/threemf-boolean.js +20 -0
  150. package/dist/threemf-csg.d.ts +14 -0
  151. package/dist/threemf-csg.js +193 -0
  152. package/dist/threemf-displacement-read.d.ts +8 -0
  153. package/dist/threemf-displacement-read.js +213 -0
  154. package/dist/threemf-displacement.d.ts +71 -0
  155. package/dist/threemf-displacement.js +18 -0
  156. package/dist/threemf-evaluate.d.ts +36 -0
  157. package/dist/threemf-evaluate.js +86 -0
  158. package/dist/threemf-functions.d.ts +33 -0
  159. package/dist/threemf-functions.js +359 -0
  160. package/dist/threemf-lattice-mesh.d.ts +9 -0
  161. package/dist/threemf-lattice-mesh.js +115 -0
  162. package/dist/threemf-lattice.d.ts +139 -0
  163. package/dist/threemf-lattice.js +379 -0
  164. package/dist/threemf-levelset.d.ts +23 -0
  165. package/dist/threemf-levelset.js +105 -0
  166. package/dist/threemf-materials.d.ts +162 -0
  167. package/dist/threemf-materials.js +497 -0
  168. package/dist/threemf-mesh.d.ts +75 -0
  169. package/dist/threemf-mesh.js +422 -0
  170. package/dist/threemf-secure.d.ts +50 -0
  171. package/dist/threemf-secure.js +153 -0
  172. package/dist/threemf-tessellate.d.ts +37 -0
  173. package/dist/threemf-tessellate.js +277 -0
  174. package/dist/threemf-volume-colour.d.ts +25 -0
  175. package/dist/threemf-volume-colour.js +233 -0
  176. package/dist/threemf-volume-sample.d.ts +8 -0
  177. package/dist/threemf-volume-sample.js +65 -0
  178. package/dist/threemf-volumetric-read.d.ts +15 -0
  179. package/dist/threemf-volumetric-read.js +539 -0
  180. package/dist/threemf-volumetric.d.ts +177 -0
  181. package/dist/threemf-volumetric.js +28 -0
  182. package/dist/threemf.d.ts +170 -0
  183. package/dist/threemf.js +609 -0
  184. package/dist/tilt.d.ts +62 -0
  185. package/dist/tilt.js +186 -0
  186. package/dist/tmx.d.ts +151 -0
  187. package/dist/tmx.js +394 -0
  188. package/dist/ttf.d.ts +78 -0
  189. package/dist/ttf.js +568 -0
  190. package/dist/types.d.ts +108 -0
  191. package/dist/types.js +35 -0
  192. package/dist/usd.d.ts +2 -0
  193. package/dist/usd.js +2 -0
  194. package/dist/usd.worker.d.ts +1 -0
  195. package/dist/usdz.d.ts +50 -0
  196. package/dist/usdz.js +646 -0
  197. package/dist/util.d.ts +75 -0
  198. package/dist/util.js +241 -0
  199. package/dist/vdb.d.ts +111 -0
  200. package/dist/vdb.js +663 -0
  201. package/dist/vrml.d.ts +53 -0
  202. package/dist/vrml.js +532 -0
  203. package/dist/vtk-cells.d.ts +29 -0
  204. package/dist/vtk-cells.js +211 -0
  205. package/dist/vtk-legacy.d.ts +57 -0
  206. package/dist/vtk-legacy.js +439 -0
  207. package/dist/vtk-reader.d.ts +19 -0
  208. package/dist/vtk-reader.js +134 -0
  209. package/dist/vtk-xml.d.ts +96 -0
  210. package/dist/vtk-xml.js +578 -0
  211. package/dist/vtk.d.ts +13 -0
  212. package/dist/vtk.js +234 -0
  213. package/dist/woff-glyf.d.ts +17 -0
  214. package/dist/woff-glyf.js +215 -0
  215. package/dist/worker.d.ts +10 -0
  216. package/dist/worker.js +93 -0
  217. package/dist/workers/usd.js +3 -0
  218. package/dist/xfile.d.ts +99 -0
  219. package/dist/xfile.js +706 -0
  220. package/dist/xml.d.ts +107 -0
  221. package/dist/xml.js +843 -0
  222. package/dist/xyz.d.ts +16 -0
  223. package/dist/xyz.js +110 -0
  224. package/dist/zip.d.ts +38 -0
  225. package/dist/zip.js +150 -0
  226. package/package.json +289 -4
@@ -0,0 +1,220 @@
1
+ import { triangulate } from '@margoengine/geometry/triangulate';
2
+ import { LoaderError } from './types.js';
3
+ import { limits, throwIfAborted } from './util.js';
4
+ export const GEO = 'Geo';
5
+ export function geoLimit(options, key, fallback) {
6
+ const value = options[key] ?? fallback;
7
+ if (!Number.isSafeInteger(value) || value < 1)
8
+ throw new LoaderError(GEO, 'limit', `${key} must be a positive safe integer`);
9
+ return value;
10
+ }
11
+ export function geoText(input, options) {
12
+ throwIfAborted(options.signal, GEO);
13
+ const max = geoLimit(options, 'maxBytes', 256 << 20);
14
+ if ((typeof input === 'string' ? new TextEncoder().encode(input).length : input.length) > max)
15
+ throw new LoaderError(GEO, 'limit', `input exceeds ${max} bytes`);
16
+ return typeof input === 'string' ? input : new TextDecoder().decode(input);
17
+ }
18
+ export function area(ring) {
19
+ let sum = 0;
20
+ for (let i = 0, j = ring.length - 1; i < ring.length; j = i++)
21
+ sum += ring[j][0] * ring[i][1] - ring[i][0] * ring[j][1];
22
+ return sum / 2;
23
+ }
24
+ export function inside(p, ring) {
25
+ let result = false;
26
+ for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
27
+ const a = ring[i], b = ring[j];
28
+ if (a[1] > p[1] !== b[1] > p[1] && p[0] < ((b[0] - a[0]) * (p[1] - a[1])) / (b[1] - a[1]) + a[0])
29
+ result = !result;
30
+ }
31
+ return result;
32
+ }
33
+ /**
34
+ * The two ordinates a ring is triangulated in: x and y, the plane of every map polygon, unless the ring stands
35
+ * vertical (a MultiPatch wall), where its x/y outline has no area; then the plane its Newell normal faces.
36
+ */
37
+ export function planeAxes(ring) {
38
+ let nx = 0, ny = 0, nz = 0;
39
+ for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
40
+ const a = ring[j], b = ring[i], az = a[2] ?? 0, bz = b[2] ?? 0;
41
+ nx += (a[1] - b[1]) * (az + bz);
42
+ ny += (az - bz) * (a[0] + b[0]);
43
+ nz += (a[0] - b[0]) * (a[1] + b[1]);
44
+ }
45
+ const x = Math.abs(nx), y = Math.abs(ny), z = Math.abs(nz);
46
+ // Kept in x/y unless that projection is degenerate, so lon/lat polygons with metre heights never change plane.
47
+ if (z > Math.hypot(x, y, z) * 1e-9 || (x === 0 && y === 0))
48
+ return [0, 1];
49
+ return x >= y ? [1, 2] : [2, 0];
50
+ }
51
+ /** Internal containment grouping, independent of source ring winding or part ordering. */
52
+ export function ringPolygons(rings) {
53
+ const ordered = rings
54
+ .map((ring, i) => ({ ring, i, area: Math.abs(area(ring)), parent: -1, depth: 0 }))
55
+ .sort((a, b) => b.area - a.area);
56
+ const polygons = [], owner = new Map();
57
+ for (let i = 0; i < ordered.length; i++) {
58
+ const entry = ordered[i];
59
+ for (let j = i - 1; j >= 0; j--)
60
+ if (inside(entry.ring[0], ordered[j].ring)) {
61
+ entry.parent = j;
62
+ entry.depth = ordered[j].depth + 1;
63
+ break;
64
+ }
65
+ if (!(entry.depth % 2)) {
66
+ const polygon = [entry.ring];
67
+ polygons.push(polygon);
68
+ owner.set(i, polygon);
69
+ }
70
+ else {
71
+ let parent = entry.parent;
72
+ while (parent >= 0 && !owner.has(parent))
73
+ parent = ordered[parent].parent;
74
+ owner.get(parent).push(entry.ring);
75
+ }
76
+ }
77
+ return polygons;
78
+ }
79
+ /** Internal normalization and tessellation shared by all vector formats. */
80
+ export function buildGeo(features, options = {}, crs = 'OGC:CRS84') {
81
+ throwIfAborted(options.signal, GEO);
82
+ const limit = limits(options, GEO), maxFeatures = geoLimit(options, 'maxFeatures', 100000), maxDepth = geoLimit(options, 'maxDepth', 64);
83
+ if (features.length > maxFeatures)
84
+ throw new LoaderError(GEO, 'limit', `features exceed ${maxFeatures}`);
85
+ const positions = [], points = [], pointFeatures = [], lines = [], lineFeatures = [], indices = [], triangleFeatures = [], polygons = [];
86
+ const groups = [];
87
+ const vertex = (p) => {
88
+ if (!Array.isArray(p) || p.length < 2 || p.some((v) => typeof v !== 'number' || !Number.isFinite(v)))
89
+ throw new LoaderError(GEO, 'syntax', 'positions need finite x/y and optional z');
90
+ if (positions.length / 3 >= limit.maxVertices)
91
+ throw new LoaderError(GEO, 'limit', `vertices exceed ${limit.maxVertices}`);
92
+ const index = positions.length / 3;
93
+ positions.push(p[0], p[1], p[2] ?? 0);
94
+ return index;
95
+ };
96
+ const path = (coords, feature, closed = false) => {
97
+ throwIfAborted(options.signal, GEO);
98
+ if (Array.isArray(coords) && coords.length === 0 && !closed)
99
+ return [];
100
+ if (!Array.isArray(coords) || coords.length < (closed ? 4 : 2))
101
+ throw new LoaderError(GEO, 'syntax', closed ? 'polygon rings need four closed coordinates' : 'lines need two positions');
102
+ let source = coords;
103
+ if (closed) {
104
+ const a = coords[0], b = coords.at(-1);
105
+ if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length || a.some((v, i) => v !== b[i]))
106
+ throw new LoaderError(GEO, 'syntax', 'polygon rings must be closed');
107
+ source = coords.slice(0, -1);
108
+ }
109
+ const result = source.map(vertex);
110
+ for (let i = 0; i + 1 < result.length; i++) {
111
+ lines.push(result[i], result[i + 1]);
112
+ lineFeatures.push(feature);
113
+ }
114
+ if (closed) {
115
+ lines.push(result.at(-1), result[0]);
116
+ lineFeatures.push(feature);
117
+ }
118
+ return result;
119
+ };
120
+ const geometry = (g, feature, depth = 0) => {
121
+ if (depth > maxDepth)
122
+ throw new LoaderError(GEO, 'limit', `geometry collections exceed ${maxDepth}`);
123
+ if (!g || typeof g !== 'object')
124
+ throw new LoaderError(GEO, 'syntax', 'expected a geometry object');
125
+ switch (g.type) {
126
+ case 'Point':
127
+ points.push(vertex(g.coordinates));
128
+ pointFeatures.push(feature);
129
+ break;
130
+ case 'MultiPoint':
131
+ if (!Array.isArray(g.coordinates))
132
+ throw new LoaderError(GEO, 'syntax', 'MultiPoint needs coordinates');
133
+ for (const p of g.coordinates) {
134
+ points.push(vertex(p));
135
+ pointFeatures.push(feature);
136
+ }
137
+ break;
138
+ case 'LineString':
139
+ path(g.coordinates, feature);
140
+ break;
141
+ case 'MultiLineString':
142
+ if (!Array.isArray(g.coordinates))
143
+ throw new LoaderError(GEO, 'syntax', 'MultiLineString needs coordinates');
144
+ for (const line of g.coordinates)
145
+ path(line, feature);
146
+ break;
147
+ case 'Polygon': {
148
+ if (!Array.isArray(g.coordinates))
149
+ throw new LoaderError(GEO, 'syntax', 'Polygon needs coordinate rings');
150
+ if (!g.coordinates.length)
151
+ break;
152
+ const rings = g.coordinates.map((r) => path(r, feature, true)), [u, w] = planeAxes(g.coordinates[0]), flat = [], mapping = [], holes = [];
153
+ for (let r = 0; r < rings.length; r++) {
154
+ if (r)
155
+ holes.push(mapping.length);
156
+ for (const v of rings[r]) {
157
+ mapping.push(v);
158
+ flat.push(positions[v * 3 + u], positions[v * 3 + w]);
159
+ }
160
+ }
161
+ const triangles = triangulate(flat, holes, 2);
162
+ if (indices.length / 3 + triangles.length / 3 > limit.maxTriangles)
163
+ throw new LoaderError(GEO, 'limit', `triangles exceed ${limit.maxTriangles}`);
164
+ for (const index of triangles)
165
+ indices.push(mapping[index]);
166
+ for (let t = 0; t < triangles.length; t += 3)
167
+ triangleFeatures.push(feature);
168
+ polygons.push({ feature, rings: rings.map((r) => Uint32Array.from(r)) });
169
+ break;
170
+ }
171
+ case 'MultiPolygon':
172
+ if (!Array.isArray(g.coordinates))
173
+ throw new LoaderError(GEO, 'syntax', 'MultiPolygon needs coordinates');
174
+ for (const polygon of g.coordinates)
175
+ geometry({ type: 'Polygon', coordinates: polygon }, feature, depth);
176
+ break;
177
+ case 'GeometryCollection':
178
+ if (!Array.isArray(g.geometries))
179
+ throw new LoaderError(GEO, 'syntax', 'GeometryCollection needs geometries');
180
+ for (const child of g.geometries)
181
+ geometry(child, feature, depth + 1);
182
+ break;
183
+ default:
184
+ throw new LoaderError(GEO, 'unsupported', `unknown geometry type ${g.type}`);
185
+ }
186
+ };
187
+ for (let f = 0; f < features.length; f++) {
188
+ throwIfAborted(options.signal, GEO);
189
+ const feature = features[f];
190
+ const start = indices.length;
191
+ if (feature.geometry)
192
+ geometry(feature.geometry, f);
193
+ if (indices.length > start)
194
+ groups.push({
195
+ start,
196
+ count: indices.length - start,
197
+ objectId: String(feature.id ?? f),
198
+ name: typeof feature.properties?.name === 'string' ? feature.properties.name : undefined,
199
+ });
200
+ }
201
+ const mesh = { kind: indices.length ? 'mesh' : 'points', positions: Float32Array.from(positions) };
202
+ if (mesh.positions.some((v) => !Number.isFinite(v)))
203
+ throw new LoaderError(GEO, 'range', 'render coordinates exceed float32 range');
204
+ if (indices.length) {
205
+ mesh.indices = Uint32Array.from(indices);
206
+ mesh.groups = groups;
207
+ }
208
+ return {
209
+ features,
210
+ positions: Float64Array.from(positions),
211
+ points: Uint32Array.from(points),
212
+ pointFeatures: Uint32Array.from(pointFeatures),
213
+ lines: Uint32Array.from(lines),
214
+ lineFeatures: Uint32Array.from(lineFeatures),
215
+ triangleFeatures: Uint32Array.from(triangleFeatures),
216
+ polygons,
217
+ mesh,
218
+ crs,
219
+ };
220
+ }
@@ -0,0 +1,15 @@
1
+ import { type BinaryInput } from './util.js';
2
+ import { type GeoOptions, type GeoResult } from './geo-model.js';
3
+ /** Vector tile coordinates and optional layer selection. */
4
+ export interface MVTOptions extends GeoOptions {
5
+ /** With a tile address, return WGS84 lon/lat; otherwise retain local tile coordinates (y down). */
6
+ tile?: {
7
+ x: number;
8
+ y: number;
9
+ z: number;
10
+ };
11
+ /** Read only these named layers. Default all. */
12
+ layers?: readonly string[];
13
+ }
14
+ /** Reads MVT 1/2 protobuf vector tiles, preserving layers, tags, IDs, multipart lines and polygon holes. */
15
+ export declare function parseMVT(input: BinaryInput, options?: MVTOptions): GeoResult;
@@ -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;