@loaders.gl/geospatial 5.0.0-beta.10 → 5.0.0-beta.11

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 (132) hide show
  1. package/dist/dbf-loader-types.cjs +1 -1
  2. package/dist/dbf-loader.cjs +2 -4
  3. package/dist/dbf-loader.cjs.map +2 -2
  4. package/dist/dbf-worker.js +22 -79
  5. package/dist/flatgeobuf/flatgeobuf-loader-types.cjs +1 -1
  6. package/dist/flatgeobuf/flatgeobuf-loader-types.js +1 -1
  7. package/dist/flatgeobuf/lib/parse-flatgeobuf.cjs +6 -20
  8. package/dist/flatgeobuf/lib/parse-flatgeobuf.cjs.map +2 -2
  9. package/dist/flatgeobuf/lib/parse-flatgeobuf.d.ts +4 -4
  10. package/dist/flatgeobuf/lib/parse-flatgeobuf.d.ts.map +1 -1
  11. package/dist/flatgeobuf/lib/parse-flatgeobuf.js +9 -22
  12. package/dist/flatgeobuf/lib/parse-flatgeobuf.js.map +1 -1
  13. package/dist/flatgeobuf-loader-types.cjs +1 -1
  14. package/dist/flatgeobuf-worker.js +655 -77
  15. package/dist/geopackage/geopackage-source-loader.cjs +1 -1
  16. package/dist/geopackage/geopackage-source-loader.cjs.map +2 -2
  17. package/dist/geopackage/geopackage-source-loader.js +1 -1
  18. package/dist/geopackage/geopackage-source-loader.js.map +1 -1
  19. package/dist/geopackage/lib/parse-geopackage.cjs +2 -6
  20. package/dist/geopackage/lib/parse-geopackage.cjs.map +2 -2
  21. package/dist/geopackage/lib/parse-geopackage.d.ts +3 -3
  22. package/dist/geopackage/lib/parse-geopackage.d.ts.map +1 -1
  23. package/dist/geopackage/lib/parse-geopackage.js +2 -6
  24. package/dist/geopackage/lib/parse-geopackage.js.map +1 -1
  25. package/dist/geopackage-source-loader.cjs +1 -1
  26. package/dist/geopackage-source-loader.cjs.map +2 -2
  27. package/dist/gpx-loader-types.cjs +1 -1
  28. package/dist/kml/gpx-loader-types.cjs +1 -1
  29. package/dist/kml/gpx-loader-types.js +1 -1
  30. package/dist/kml/kml-loader-types.cjs +1 -1
  31. package/dist/kml/kml-loader-types.js +1 -1
  32. package/dist/kml/kml-writer.cjs +1 -1
  33. package/dist/kml/kml-writer.js +1 -1
  34. package/dist/kml/kmz-loader-types.cjs +1 -1
  35. package/dist/kml/kmz-loader-types.js +1 -1
  36. package/dist/kml/lib/feature-collection-to-arrow.cjs +1 -1
  37. package/dist/kml/lib/feature-collection-to-arrow.cjs.map +2 -2
  38. package/dist/kml/lib/feature-collection-to-arrow.js +1 -1
  39. package/dist/kml/lib/feature-collection-to-arrow.js.map +1 -1
  40. package/dist/kml/tcx-loader-types.cjs +1 -1
  41. package/dist/kml/tcx-loader-types.js +1 -1
  42. package/dist/kml-loader-types.cjs +1 -1
  43. package/dist/kml-writer.cjs +1 -1
  44. package/dist/kmz-loader-types.cjs +1 -1
  45. package/dist/shapefile/dbf-loader-types.cjs +1 -1
  46. package/dist/shapefile/dbf-loader-types.js +1 -1
  47. package/dist/shapefile/dbf-loader.cjs +2 -4
  48. package/dist/shapefile/dbf-loader.cjs.map +2 -2
  49. package/dist/shapefile/dbf-loader.d.ts.map +1 -1
  50. package/dist/shapefile/dbf-loader.js +3 -5
  51. package/dist/shapefile/dbf-loader.js.map +1 -1
  52. package/dist/shapefile/lib/parsers/build-geoarrow-geometry-arrow.cjs +1 -5
  53. package/dist/shapefile/lib/parsers/build-geoarrow-geometry-arrow.cjs.map +2 -2
  54. package/dist/shapefile/lib/parsers/build-geoarrow-geometry-arrow.d.ts +5 -4
  55. package/dist/shapefile/lib/parsers/build-geoarrow-geometry-arrow.d.ts.map +1 -1
  56. package/dist/shapefile/lib/parsers/build-geoarrow-geometry-arrow.js +2 -6
  57. package/dist/shapefile/lib/parsers/build-geoarrow-geometry-arrow.js.map +1 -1
  58. package/dist/shapefile/lib/parsers/build-wkb-geometry-arrow.cjs.map +2 -2
  59. package/dist/shapefile/lib/parsers/build-wkb-geometry-arrow.d.ts +4 -3
  60. package/dist/shapefile/lib/parsers/build-wkb-geometry-arrow.d.ts.map +1 -1
  61. package/dist/shapefile/lib/parsers/build-wkb-geometry-arrow.js.map +1 -1
  62. package/dist/shapefile/lib/parsers/parse-shp-to-arrow.cjs +7 -10
  63. package/dist/shapefile/lib/parsers/parse-shp-to-arrow.cjs.map +2 -2
  64. package/dist/shapefile/lib/parsers/parse-shp-to-arrow.d.ts +3 -3
  65. package/dist/shapefile/lib/parsers/parse-shp-to-arrow.d.ts.map +1 -1
  66. package/dist/shapefile/lib/parsers/parse-shp-to-arrow.js +11 -9
  67. package/dist/shapefile/lib/parsers/parse-shp-to-arrow.js.map +1 -1
  68. package/dist/shapefile/lib/shapefile-worker-transport.cjs +0 -3
  69. package/dist/shapefile/lib/shapefile-worker-transport.cjs.map +2 -2
  70. package/dist/shapefile/lib/shapefile-worker-transport.d.ts.map +1 -1
  71. package/dist/shapefile/lib/shapefile-worker-transport.js +1 -5
  72. package/dist/shapefile/lib/shapefile-worker-transport.js.map +1 -1
  73. package/dist/shapefile/shapefile-arrow-loader.cjs +53 -48
  74. package/dist/shapefile/shapefile-arrow-loader.cjs.map +2 -2
  75. package/dist/shapefile/shapefile-arrow-loader.d.ts +5 -3
  76. package/dist/shapefile/shapefile-arrow-loader.d.ts.map +1 -1
  77. package/dist/shapefile/shapefile-arrow-loader.js +54 -52
  78. package/dist/shapefile/shapefile-arrow-loader.js.map +1 -1
  79. package/dist/shapefile/shapefile-loader-types.cjs +1 -1
  80. package/dist/shapefile/shapefile-loader-types.d.ts +4 -4
  81. package/dist/shapefile/shapefile-loader-types.js +1 -1
  82. package/dist/shapefile/shapefile-loader.cjs +19 -55
  83. package/dist/shapefile/shapefile-loader.cjs.map +2 -2
  84. package/dist/shapefile/shapefile-loader.d.ts +5 -42
  85. package/dist/shapefile/shapefile-loader.d.ts.map +1 -1
  86. package/dist/shapefile/shapefile-loader.js +22 -64
  87. package/dist/shapefile/shapefile-loader.js.map +1 -1
  88. package/dist/shapefile/shp-loader-types.cjs +1 -1
  89. package/dist/shapefile/shp-loader-types.js +1 -1
  90. package/dist/shapefile/shp-loader.cjs +1 -44
  91. package/dist/shapefile/shp-loader.cjs.map +2 -2
  92. package/dist/shapefile/shp-loader.d.ts +1 -115
  93. package/dist/shapefile/shp-loader.d.ts.map +1 -1
  94. package/dist/shapefile/shp-loader.js +1 -48
  95. package/dist/shapefile/shp-loader.js.map +1 -1
  96. package/dist/shapefile-arrow-loader.cjs +53 -48
  97. package/dist/shapefile-arrow-loader.cjs.map +2 -2
  98. package/dist/shapefile-loader-types.cjs +1 -1
  99. package/dist/shapefile-loader.cjs +19 -55
  100. package/dist/shapefile-loader.cjs.map +2 -2
  101. package/dist/shapefile-worker.js +117 -272
  102. package/dist/shp-loader-types.cjs +1 -1
  103. package/dist/shp-loader.cjs +1 -44
  104. package/dist/shp-loader.cjs.map +2 -2
  105. package/dist/shp-worker.js +16964 -17085
  106. package/dist/tcx-loader-types.cjs +1 -1
  107. package/dist/wkt/lib/version.cjs +1 -1
  108. package/dist/wkt/lib/version.js +1 -1
  109. package/dist/wkt-worker.js +1 -1
  110. package/dist/workers/shapefile-worker.cjs +3 -3
  111. package/dist/workers/shapefile-worker.cjs.map +2 -2
  112. package/dist/workers/shapefile-worker.js +4 -4
  113. package/dist/workers/shapefile-worker.js.map +1 -1
  114. package/dist/workers/shp-worker.cjs +1 -1
  115. package/dist/workers/shp-worker.cjs.map +2 -2
  116. package/dist/workers/shp-worker.js +2 -2
  117. package/dist/workers/shp-worker.js.map +1 -1
  118. package/package.json +6 -6
  119. package/src/flatgeobuf/lib/parse-flatgeobuf.ts +15 -33
  120. package/src/geopackage/geopackage-source-loader.ts +1 -1
  121. package/src/geopackage/lib/parse-geopackage.ts +4 -9
  122. package/src/kml/lib/feature-collection-to-arrow.ts +1 -1
  123. package/src/shapefile/dbf-loader.ts +4 -7
  124. package/src/shapefile/lib/parsers/build-geoarrow-geometry-arrow.ts +5 -9
  125. package/src/shapefile/lib/parsers/build-wkb-geometry-arrow.ts +4 -3
  126. package/src/shapefile/lib/parsers/parse-shp-to-arrow.ts +14 -20
  127. package/src/shapefile/lib/shapefile-worker-transport.ts +1 -6
  128. package/src/shapefile/shapefile-arrow-loader.ts +76 -70
  129. package/src/shapefile/shapefile-loader.ts +40 -90
  130. package/src/shapefile/shp-loader.ts +7 -61
  131. package/src/workers/shapefile-worker.ts +4 -4
  132. package/src/workers/shp-worker.ts +2 -2
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/shapefile/shapefile-arrow-loader.ts
31
31
  var shapefile_arrow_loader_exports = {};
32
32
  __export(shapefile_arrow_loader_exports, {
33
+ SHAPEFILE_HEADER_KEY: () => SHAPEFILE_HEADER_KEY,
33
34
  parseShapefileToArrow: () => parseShapefileToArrow,
34
35
  parseShapefileToArrowInBatches: () => parseShapefileToArrowInBatches
35
36
  });
@@ -77,7 +78,7 @@ async function parseShapefileToArrow(arrayBuffer, options, context) {
77
78
  transform
78
79
  );
79
80
  if (options?.geoarrow?.encodingPreference === "geoarrow.geometry" || options?.shapefile?.geoarrow?.encodingPreference === "geoarrow.geometry") {
80
- const geometryColumn = geometryTable.data.getChild(GEOMETRY_COLUMN_NAME);
81
+ const geometryColumn = geometryTable.getChild(GEOMETRY_COLUMN_NAME);
81
82
  const geometries = geometryColumn?.toArray() || [];
82
83
  geometryTable = (0, import_feature_table.convertFeaturesToGeoArrowTable)(
83
84
  geometries.map((geometry) => ({
@@ -107,11 +108,18 @@ async function parseShapefileToArrow(arrayBuffer, options, context) {
107
108
  context
108
109
  );
109
110
  propertySchema = (0, import_schema_utils.convertArrowToSchema)(propertyData.schema);
110
- propertyTable = { shape: "arrow-table", data: propertyData, schema: propertySchema };
111
+ propertyTable = propertyData;
111
112
  }
112
- const schema = buildOutputSchema(propertySchema, header, geometryTable.schema);
113
+ const schema = buildOutputSchema(
114
+ propertySchema,
115
+ header,
116
+ (0, import_schema_utils.convertArrowToSchema)(geometryTable.schema)
117
+ );
113
118
  const table = propertyTable ? appendGeometryColumnToArrowTable(propertyTable, geometryTable, schema) : geometryTable;
114
- return Object.assign(table, { loaderData: { header, encoding: cpg, prj, shx } });
119
+ const encoded = encodeShapefileHeader({ header, encoding: cpg, prj, shx });
120
+ table.schema.metadata.set(SHAPEFILE_HEADER_KEY, encoded);
121
+ for (const batch of table.batches) batch.schema.metadata.set(SHAPEFILE_HEADER_KEY, encoded);
122
+ return table;
115
123
  }
116
124
  async function* parseShapefileToArrowInBatches(asyncIterator, options, context) {
117
125
  if (getTypedGeoArrowEncoding(options)) {
@@ -139,6 +147,7 @@ async function* parseShapefileToArrowInBatches(asyncIterator, options, context)
139
147
  const shapeIterator = getAsyncIterator(shapeIterable);
140
148
  const shapeHeader = await getNextNonMetadataValue(shapeIterator);
141
149
  const header = shapeHeader;
150
+ const encodedHeader = encodeShapefileHeader({ header, encoding: cpg, prj, shx });
142
151
  let propertyIterator = null;
143
152
  let propertySchema = null;
144
153
  const dbfResponse = context?.url ? await context.fetch((0, import_parse_shapefile.replaceExtension)(context.url, "dbf")).catch(() => null) : null;
@@ -160,13 +169,13 @@ async function* parseShapefileToArrowInBatches(asyncIterator, options, context)
160
169
  );
161
170
  propertyIterator = getAsyncIterator(propertyIterable);
162
171
  const firstPropertyBatch = await getNextArrowBatch(propertyIterator);
163
- propertySchema = firstPropertyBatch?.schema || null;
172
+ propertySchema = firstPropertyBatch ? (0, import_schema_utils.convertArrowToSchema)(firstPropertyBatch.schema) : null;
164
173
  const outputSchema2 = buildOutputSchema(propertySchema, header);
165
174
  const propertyQueue = [];
166
175
  const geometryQueue = [];
167
176
  let yieldedDataBatch2 = false;
168
- if (firstPropertyBatch && firstPropertyBatch.length > 0) {
169
- propertyQueue.push(firstPropertyBatch.data);
177
+ if (firstPropertyBatch && firstPropertyBatch.numRows > 0) {
178
+ propertyQueue.push(new arrow.Table(firstPropertyBatch.schema, [firstPropertyBatch]));
170
179
  }
171
180
  let shapeDone = false;
172
181
  let propertyDone = false;
@@ -178,7 +187,7 @@ async function* parseShapefileToArrowInBatches(asyncIterator, options, context)
178
187
  } else if (shapeBatch.value?.batchType !== "metadata") {
179
188
  const transform = getReprojectionTransform(prj, options);
180
189
  geometryQueue.push(
181
- makeGeometryArrowTable(shapeBatch.value, header, transform).data
190
+ makeGeometryArrowTable(shapeBatch.value, header, transform)
182
191
  );
183
192
  }
184
193
  }
@@ -186,8 +195,8 @@ async function* parseShapefileToArrowInBatches(asyncIterator, options, context)
186
195
  const propertyBatch = await getNextArrowBatch(propertyIterator);
187
196
  if (!propertyBatch) {
188
197
  propertyDone = true;
189
- } else if (propertyBatch.length > 0) {
190
- propertyQueue.push(propertyBatch.data);
198
+ } else if (propertyBatch.numRows > 0) {
199
+ propertyQueue.push(new arrow.Table(propertyBatch.schema, [propertyBatch]));
191
200
  }
192
201
  }
193
202
  const rowCount = Math.min(getQueuedRowCount(geometryQueue), getQueuedRowCount(propertyQueue));
@@ -199,16 +208,14 @@ async function* parseShapefileToArrowInBatches(asyncIterator, options, context)
199
208
  }
200
209
  const propertyTable = takeRowsFromQueue(propertyQueue, rowCount);
201
210
  const geometryTable = takeRowsFromQueue(geometryQueue, rowCount);
202
- const batch = appendGeometryColumnToArrowTable(
203
- { shape: "arrow-table", schema: propertySchema || void 0, data: propertyTable },
204
- { shape: "arrow-table", data: geometryTable },
205
- outputSchema2
206
- );
211
+ const batch = appendGeometryColumnToArrowTable(propertyTable, geometryTable, outputSchema2);
207
212
  yieldedDataBatch2 = true;
208
- yield { ...(0, import_loader_utils2.makeTableScanBatch)(batch), loaderData: { header, encoding: cpg, prj, shx } };
213
+ const recordBatch = batch.batches[0];
214
+ recordBatch.schema.metadata.set(SHAPEFILE_HEADER_KEY, encodedHeader);
215
+ yield recordBatch;
209
216
  }
210
217
  if (!yieldedDataBatch2) {
211
- yield { ...makeEmptyArrowBatch(outputSchema2), loaderData: { header, encoding: cpg, prj, shx } };
218
+ yield makeEmptyArrowBatch(outputSchema2, encodedHeader);
212
219
  }
213
220
  return;
214
221
  }
@@ -229,13 +236,13 @@ async function* parseShapefileToArrowInBatches(asyncIterator, options, context)
229
236
  transform
230
237
  );
231
238
  yieldedDataBatch = true;
232
- yield {
233
- ...(0, import_loader_utils2.makeTableScanBatch)({ ...arrowTable, schema: outputSchema }),
234
- loaderData: { header, encoding: cpg, prj, shx }
235
- };
239
+ for (const batch of arrowTable.batches) {
240
+ batch.schema.metadata.set(SHAPEFILE_HEADER_KEY, encodedHeader);
241
+ yield batch;
242
+ }
236
243
  }
237
244
  if (!yieldedDataBatch) {
238
- yield { ...makeEmptyArrowBatch(outputSchema), loaderData: { header, encoding: cpg, prj, shx } };
245
+ yield makeEmptyArrowBatch(outputSchema, encodedHeader);
239
246
  }
240
247
  }
241
248
  function buildOutputSchema(propertySchema, header, geometrySchema) {
@@ -260,19 +267,15 @@ function makeGeometryArrowTable(geometries, header, transform) {
260
267
  return (0, import_build_wkb_geometry_arrow.makeWKBGeometryArrowTable)(geometries, geometrySchema, transform);
261
268
  }
262
269
  function appendGeometryColumnToArrowTable(propertyTable, geometryTable, schema) {
263
- const propertyBatch = propertyTable.data.batches[0];
264
- const geometryBatch = geometryTable.data.batches[0];
270
+ const propertyBatch = propertyTable.batches[0];
271
+ const geometryBatch = geometryTable.batches[0];
265
272
  const arrowSchema = (0, import_schema_utils2.convertSchemaToArrow)(schema);
266
273
  const structField = new arrow.Struct(arrowSchema.fields);
267
274
  const children = [...propertyBatch.data.children, geometryBatch.data.children[0]];
268
- const rowCount = Math.max(propertyTable.data.numRows, geometryTable.data.numRows);
275
+ const rowCount = Math.max(propertyTable.numRows, geometryTable.numRows);
269
276
  const structData = new arrow.Data(structField, 0, rowCount, 0, void 0, children);
270
277
  const recordBatch = new arrow.RecordBatch(arrowSchema, structData);
271
- return {
272
- shape: "arrow-table",
273
- schema,
274
- data: new arrow.Table(arrowSchema, [recordBatch])
275
- };
278
+ return new arrow.Table(arrowSchema, [recordBatch]);
276
279
  }
277
280
  function getReprojectionTransform(sourceCrs, options) {
278
281
  const { reproject = false, targetCrs = "WGS84" } = options?.gis || {};
@@ -350,21 +353,11 @@ async function getNextNonMetadataValue(iterator) {
350
353
  async function getNextArrowBatch(iterator) {
351
354
  while (true) {
352
355
  const result = await iterator.next();
353
- if (result.done) {
354
- return null;
355
- }
356
- if ((0, import_schema_utils.isNativeArrowRecordBatch)(result.value)) {
357
- const recordBatch = result.value;
358
- const schema = (0, import_schema_utils.convertArrowToSchema)(recordBatch.schema);
359
- const batch = Object.getPrototypeOf(recordBatch) === arrow.RecordBatch.prototype ? recordBatch : importDBFRecordBatch(recordBatch, schema);
360
- const data = new arrow.Table(batch.schema, [batch]);
361
- return (0, import_loader_utils2.makeTableScanBatch)({
362
- shape: "arrow-table",
363
- schema,
364
- data
365
- });
356
+ if (result.done) return null;
357
+ if ((0, import_schema_utils.isArrowRecordBatch)(result.value)) {
358
+ const batch = result.value;
359
+ return Object.getPrototypeOf(batch) === arrow.RecordBatch.prototype ? batch : importDBFRecordBatch(batch, (0, import_schema_utils.convertArrowToSchema)(batch.schema));
366
360
  }
367
- if (result.value?.shape === "arrow-table") return result.value;
368
361
  }
369
362
  }
370
363
  function importDBFRecordBatch(batch, tableSchema) {
@@ -398,15 +391,27 @@ function takeRowsFromQueue(queue, rowCount) {
398
391
  return table;
399
392
  }
400
393
  const result = table.slice(0, rowCount);
401
- queue[0] = table.slice(rowCount, table.numRows - rowCount);
394
+ queue[0] = table.slice(rowCount);
402
395
  return result;
403
396
  }
404
- function makeEmptyArrowBatch(schema) {
405
- const table = new import_schema_utils2.ArrowTableBuilder(schema).finishTable();
406
- return (0, import_loader_utils2.makeTableScanBatch)({ shape: "arrow-table", data: table, schema });
397
+ function makeEmptyArrowBatch(schema, header) {
398
+ const nativeSchema = (0, import_schema_utils2.convertSchemaToArrow)(schema);
399
+ nativeSchema.metadata.set(SHAPEFILE_HEADER_KEY, header);
400
+ return (0, import_schema_utils2.makeArrowMetadataBatch)(nativeSchema);
401
+ }
402
+ var SHAPEFILE_HEADER_KEY = "loaders.gl.shapefile.header";
403
+ function encodeShapefileHeader({
404
+ shx,
405
+ ...header
406
+ }) {
407
+ return JSON.stringify({
408
+ ...header,
409
+ ...shx ? { shx: { offsets: Array.from(shx.offsets), lengths: Array.from(shx.lengths) } } : {}
410
+ });
407
411
  }
408
412
  // Annotate the CommonJS export names for ESM import in node:
409
413
  0 && (module.exports = {
414
+ SHAPEFILE_HEADER_KEY,
410
415
  parseShapefileToArrow,
411
416
  parseShapefileToArrowInBatches
412
417
  });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/shapefile/shapefile-arrow-loader.ts"],
4
- "sourcesContent": ["// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport {CRSReprojectionError, type LoaderContext} from '@loaders.gl/core/loader-utils';\nimport {\n makeTableScanBatch,\n parseFromContext,\n parseInBatchesFromContext,\n toArrayBufferIterator\n} from '@loaders.gl/core/loader-utils';\nimport * as arrow from 'apache-arrow';\nimport {convertArrowToSchema, isNativeArrowRecordBatch} from '@loaders.gl/core/schema-utils';\nimport type {\n ArrowTable,\n ArrowTableBatch,\n Field,\n Schema as TableSchema\n} from '@loaders.gl/core/schema';\nimport {ArrowTableBuilder, convertSchemaToArrow} from '@loaders.gl/core/schema-utils';\nimport {convertFeaturesToGeoArrowTable} from '@loaders.gl/core/arrow-geometry/feature-table';\nimport {convertWKBToGeometry} from '@loaders.gl/core/arrow-geometry/geometry-codecs';\nimport {type GeoParquetGeometryType} from '@loaders.gl/core/schema';\nimport {\n makeWKBGeometryField,\n setWKBGeometryColumnMetadata\n} from '@loaders.gl/core/arrow-geometry/geometry-field';\nimport {projectionEngine} from '@math.gl/geospatial/projection';\nimport type {ReadonlyCRSDefinition, WKTCRSDefinition} from '@math.gl/geospatial/crs';\nimport {SHPLoaderWithParser} from './shp-loader';\nimport {DBFLoaderWithParser} from './dbf-loader';\nimport type {ShapefileLoaderOptions} from './shapefile-loader-types';\nimport type {SHPResult} from './lib/parsers/parse-shp';\nimport type {SHPHeader} from './lib/parsers/parse-shp-header';\nimport {loadShapefileSidecarFiles, replaceExtension} from './lib/parsers/parse-shapefile';\nimport {\n type SHPWKBGeometry,\n makeWKBGeometryArrowTable\n} from './lib/parsers/build-wkb-geometry-arrow';\nimport {makeSHPGeoArrowGeometryTable} from './lib/parsers/build-geoarrow-geometry-arrow';\nconst GEOMETRY_COLUMN_NAME = 'geometry';\n\n/** Parses a shapefile and returns an Arrow table with a WKB geometry column. */\nexport async function parseShapefileToArrow(\n arrayBuffer: ArrayBuffer,\n options?: ShapefileLoaderOptions,\n context?: LoaderContext\n): Promise<ArrowTable> {\n const {cpg, prj, shx} = await loadShapefileSidecarFiles(options, context);\n const transform = getReprojectionTransform(prj, options);\n const geoArrowEncoding = getTypedGeoArrowEncoding(options);\n let header: SHPHeader | undefined;\n let geometryTable: ArrowTable;\n\n if (geoArrowEncoding) {\n geometryTable = makeSHPGeoArrowGeometryTable(arrayBuffer, options, {transform});\n } else {\n const shpResult = (await parseFromContext(\n arrayBuffer,\n SHPLoaderWithParser,\n {\n ...options,\n shp: {\n ...options?.shp,\n shape: 'wkb'\n }\n },\n context!\n )) as SHPResult;\n header = shpResult.header;\n geometryTable = makeGeometryArrowTable(\n shpResult.geometries as (SHPWKBGeometry | null)[],\n header,\n transform\n );\n if (\n options?.geoarrow?.encodingPreference === 'geoarrow.geometry' ||\n options?.shapefile?.geoarrow?.encodingPreference === 'geoarrow.geometry'\n ) {\n const geometryColumn = geometryTable.data.getChild(GEOMETRY_COLUMN_NAME);\n const geometries = geometryColumn?.toArray() || [];\n geometryTable = convertFeaturesToGeoArrowTable(\n geometries.map(geometry => ({\n type: 'Feature' as const,\n geometry: geometry ? convertWKBToGeometry(geometry.slice().buffer) : null,\n properties: {}\n })),\n {encodingPreference: 'geoarrow.geometry'}\n );\n }\n }\n\n let propertySchema: TableSchema | null = null;\n let propertyTable: ArrowTable | null = null;\n\n const dbfResponse = context?.url\n ? await context.fetch(replaceExtension(context.url, 'dbf')).catch(() => null)\n : null;\n if (dbfResponse?.ok) {\n const propertyData = (await parseFromContext(\n dbfResponse as any,\n DBFLoaderWithParser,\n {\n ...options,\n dbf: {\n ...options?.dbf,\n shape: 'arrow' as const,\n encoding: cpg || 'latin1'\n }\n },\n context!\n )) as arrow.Table;\n propertySchema = convertArrowToSchema(propertyData.schema);\n propertyTable = {shape: 'arrow-table', data: propertyData, schema: propertySchema};\n }\n\n const schema = buildOutputSchema(propertySchema, header, geometryTable.schema);\n const table = propertyTable\n ? appendGeometryColumnToArrowTable(propertyTable, geometryTable, schema)\n : geometryTable;\n return Object.assign(table, {loaderData: {header, encoding: cpg, prj, shx}});\n}\n\n/** Parses a shapefile into Arrow batches while keeping DBF-derived schema stable. */\nexport async function* parseShapefileToArrowInBatches(\n asyncIterator:\n | AsyncIterable<ArrayBufferLike | ArrayBufferView>\n | Iterable<ArrayBufferLike | ArrayBufferView>,\n options?: ShapefileLoaderOptions,\n context?: LoaderContext\n): AsyncIterable<ArrowTableBatch> {\n if (getTypedGeoArrowEncoding(options)) {\n throw new Error('Typed GeoArrow shapefile output is only supported for non-streaming parse.');\n }\n\n const {cpg, prj, shx} = await loadShapefileSidecarFiles(options, context);\n const batchSize =\n typeof options?.core?.batchSize === 'number'\n ? options.core.batchSize\n : (options?.shapefile?.batchSize ??\n options?.shp?.batchSize ??\n options?.dbf?.batchSize ??\n 10000);\n const childOptions = {\n ...options,\n core: {...options?.core, batchMaxRowsPerSecond: 0}\n };\n\n const shapeIterable = await parseInBatchesFromContext(\n toArrayBufferIterator(asyncIterator),\n SHPLoaderWithParser,\n {\n ...childOptions,\n shp: {\n ...options?.shp,\n shape: 'wkb',\n batchSize\n }\n },\n context!\n );\n const shapeIterator = getAsyncIterator(shapeIterable);\n\n const shapeHeader = await getNextNonMetadataValue(shapeIterator);\n const header = shapeHeader as SHPHeader;\n\n let propertyIterator: AsyncIterator<any> | null = null;\n let propertySchema: TableSchema | null = null;\n\n const dbfResponse = context?.url\n ? await context.fetch(replaceExtension(context.url, 'dbf')).catch(() => null)\n : null;\n if (dbfResponse?.ok) {\n const dbfOptions = {\n ...childOptions,\n dbf: {\n ...options?.dbf,\n shape: 'arrow' as const,\n batchSize,\n encoding: cpg || 'latin1'\n }\n };\n const propertyIterable = await parseInBatchesFromContext(\n dbfResponse,\n DBFLoaderWithParser,\n dbfOptions,\n context!\n );\n propertyIterator = getAsyncIterator(propertyIterable);\n\n const firstPropertyBatch = await getNextArrowBatch(propertyIterator);\n propertySchema = firstPropertyBatch?.schema || null;\n const outputSchema = buildOutputSchema(propertySchema, header);\n const propertyQueue: arrow.Table[] = [];\n const geometryQueue: arrow.Table[] = [];\n let yieldedDataBatch = false;\n\n if (firstPropertyBatch && firstPropertyBatch.length > 0) {\n propertyQueue.push(firstPropertyBatch.data);\n }\n\n let shapeDone = false;\n let propertyDone = false;\n while (\n !shapeDone ||\n !propertyDone ||\n getQueuedRowCount(geometryQueue) > 0 ||\n getQueuedRowCount(propertyQueue) > 0\n ) {\n if (!shapeDone && getQueuedRowCount(geometryQueue) === 0) {\n const shapeBatch = await shapeIterator.next();\n if (shapeBatch.done) {\n shapeDone = true;\n } else if (shapeBatch.value?.batchType !== 'metadata') {\n const transform = getReprojectionTransform(prj, options);\n geometryQueue.push(\n makeGeometryArrowTable(shapeBatch.value as (SHPWKBGeometry | null)[], header, transform)\n .data\n );\n }\n }\n\n if (!propertyDone && getQueuedRowCount(propertyQueue) < getQueuedRowCount(geometryQueue)) {\n const propertyBatch = await getNextArrowBatch(propertyIterator);\n if (!propertyBatch) {\n propertyDone = true;\n } else if (propertyBatch.length > 0) {\n propertyQueue.push(propertyBatch.data);\n }\n }\n\n const rowCount = Math.min(getQueuedRowCount(geometryQueue), getQueuedRowCount(propertyQueue));\n if (rowCount === 0) {\n if (\n (shapeDone && getQueuedRowCount(geometryQueue) === 0) ||\n (propertyDone && getQueuedRowCount(propertyQueue) === 0)\n ) {\n break;\n }\n continue;\n }\n\n const propertyTable = takeRowsFromQueue(propertyQueue, rowCount);\n const geometryTable = takeRowsFromQueue(geometryQueue, rowCount);\n const batch = appendGeometryColumnToArrowTable(\n {shape: 'arrow-table', schema: propertySchema || undefined, data: propertyTable},\n {shape: 'arrow-table', data: geometryTable},\n outputSchema\n );\n yieldedDataBatch = true;\n yield {...makeTableScanBatch(batch), loaderData: {header, encoding: cpg, prj, shx}};\n }\n if (!yieldedDataBatch) {\n yield {...makeEmptyArrowBatch(outputSchema), loaderData: {header, encoding: cpg, prj, shx}};\n }\n return;\n }\n\n const outputSchema = buildOutputSchema(null, header);\n let yieldedDataBatch = false;\n\n while (true) {\n const shapeBatch = await shapeIterator.next();\n if (shapeBatch.done) {\n break;\n }\n if (shapeBatch.value?.batchType === 'metadata') {\n continue;\n }\n const transform = getReprojectionTransform(prj, options);\n const arrowTable = makeGeometryArrowTable(\n shapeBatch.value as (SHPWKBGeometry | null)[],\n header,\n transform\n );\n yieldedDataBatch = true;\n yield {\n ...makeTableScanBatch({...arrowTable, schema: outputSchema}),\n loaderData: {header, encoding: cpg, prj, shx}\n };\n }\n if (!yieldedDataBatch) {\n yield {...makeEmptyArrowBatch(outputSchema), loaderData: {header, encoding: cpg, prj, shx}};\n }\n}\n\n/** Creates the output Arrow schema by appending the WKB geometry column to DBF fields. */\nfunction buildOutputSchema(\n propertySchema: TableSchema | null,\n header?: SHPHeader,\n geometrySchema?: TableSchema\n): TableSchema {\n const geometryField: Field =\n geometrySchema?.fields[0] || makeWKBGeometryField(GEOMETRY_COLUMN_NAME);\n const schema: TableSchema = {\n fields: [...(propertySchema?.fields || []), geometryField],\n metadata: {\n ...(propertySchema?.metadata || {}),\n ...(geometrySchema?.metadata || {})\n }\n };\n\n if (!geometrySchema) {\n setWKBGeometryColumnMetadata(schema.metadata!, {\n geometryColumnName: GEOMETRY_COLUMN_NAME,\n geometryTypes: inferGeometryTypes(header)\n });\n }\n\n return schema;\n}\n\nfunction makeGeometryArrowTable(\n geometries: (SHPWKBGeometry | null | undefined)[],\n header?: SHPHeader,\n transform?: (coordinate: number[]) => number[]\n) {\n const geometrySchema = buildOutputSchema(null, header);\n return makeWKBGeometryArrowTable(geometries, geometrySchema, transform);\n}\n\nfunction appendGeometryColumnToArrowTable(\n propertyTable: ArrowTable,\n geometryTable: ArrowTable,\n schema: TableSchema\n): ArrowTable {\n const propertyBatch = propertyTable.data.batches[0];\n const geometryBatch = geometryTable.data.batches[0];\n const arrowSchema = convertSchemaToArrow(schema);\n const structField = new arrow.Struct(arrowSchema.fields);\n const children = [...propertyBatch.data.children, geometryBatch.data.children[0]];\n const rowCount = Math.max(propertyTable.data.numRows, geometryTable.data.numRows);\n const structData = new arrow.Data(structField, 0, rowCount, 0, undefined, children);\n const recordBatch = new arrow.RecordBatch(arrowSchema, structData);\n\n return {\n shape: 'arrow-table',\n schema,\n data: new arrow.Table(arrowSchema, [recordBatch])\n };\n}\n\nfunction getReprojectionTransform(\n sourceCrs: WKTCRSDefinition | undefined,\n options?: ShapefileLoaderOptions\n): ((coordinate: number[]) => number[]) | undefined {\n const {reproject = false, targetCrs = 'WGS84'} = options?.gis || {};\n if (!reproject) {\n return undefined;\n }\n if (!sourceCrs) {\n throw new CRSReprojectionError(\n 'missing-source-crs',\n 'Shapefile reprojection requires a source CRS from the .prj sidecar file',\n {targetCrs}\n );\n }\n try {\n const projection = projectionEngine.createProjection({\n from: sourceCrs,\n to: (targetCrs || 'WGS84') as ReadonlyCRSDefinition\n });\n return coordinate => projection.projectSync(coordinate);\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n throw new CRSReprojectionError(\n 'transformation-failed',\n `Shapefile reprojection failed: ${message}`,\n {sourceCrs, targetCrs: targetCrs || 'WGS84', cause: error}\n );\n }\n}\n\nfunction getTypedGeoArrowEncoding(options?: ShapefileLoaderOptions): boolean {\n const encoding = options?.shapefile?.geoarrowEncoding || options?.shp?.geoarrowEncoding;\n const preference =\n options?.geoarrow?.encodingPreference ||\n options?.shapefile?.geoarrow?.encodingPreference ||\n options?.shp?.geoarrow?.encodingPreference;\n return encoding === 'geoarrow' || preference === 'optimized';\n}\n\n/** Infers GeoParquet geometry type metadata from parsed geometries or the SHP header. */\nfunction inferGeometryTypes(header?: SHPHeader): GeoParquetGeometryType[] {\n const fallbackType = getGeometryTypeFromHeader(header?.type);\n return fallbackType ? [fallbackType] : [];\n}\n\n/** Maps SHP header geometry type codes to GeoParquet geometry type strings. */\nfunction getGeometryTypeFromHeader(type?: number): GeoParquetGeometryType | null {\n switch (type) {\n case 1:\n case 11:\n case 21:\n return type === 11 ? 'Point Z' : 'Point';\n case 3:\n case 13:\n case 23:\n return type === 13 ? 'LineString Z' : 'LineString';\n case 5:\n case 15:\n case 25:\n return type === 15 ? 'Polygon Z' : 'Polygon';\n case 8:\n case 18:\n case 28:\n return type === 18 ? 'MultiPoint Z' : 'MultiPoint';\n default:\n return null;\n }\n}\n\n/** Normalizes sync or async iterables to a single async iterator interface. */\nfunction getAsyncIterator(iterable: AsyncIterable<any> | Iterable<any>): AsyncIterator<any> {\n const iterator = iterable[Symbol.asyncIterator]?.() || iterable[Symbol.iterator]?.();\n return iterator as AsyncIterator<any>;\n}\n\n/** Reads the next non-metadata value from a parser iterator. */\nasync function getNextNonMetadataValue(iterator: AsyncIterator<any>): Promise<any> {\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return null;\n }\n if (result.value?.batchType !== 'metadata') {\n return result.value;\n }\n }\n}\n\n/** Reads both native and legacy DBF column batches. */\nasync function getNextArrowBatch(iterator: AsyncIterator<any>): Promise<ArrowTableBatch | null> {\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return null;\n }\n if (isNativeArrowRecordBatch(result.value)) {\n const recordBatch = result.value;\n const schema = convertArrowToSchema(recordBatch.schema);\n const batch =\n Object.getPrototypeOf(recordBatch) === arrow.RecordBatch.prototype\n ? recordBatch\n : importDBFRecordBatch(recordBatch, schema);\n const data = new arrow.Table(batch.schema, [batch]);\n return makeTableScanBatch({\n shape: 'arrow-table',\n schema,\n data\n });\n }\n if (result.value?.shape === 'arrow-table') return result.value;\n }\n}\n\n/** Rebuilds primitive DBF columns with local Arrow classes while reusing their buffers. */\nfunction importDBFRecordBatch(\n batch: arrow.RecordBatch,\n tableSchema: TableSchema\n): arrow.RecordBatch {\n const schema = convertSchemaToArrow(tableSchema);\n const children = batch.data.children.map(\n (column, index) =>\n new arrow.Data(\n schema.fields[index].type,\n column.offset,\n column.length,\n column.nullCount,\n column.buffers,\n undefined,\n undefined,\n column.variadicBuffers\n )\n );\n const data = arrow.makeData({\n type: new arrow.Struct(schema.fields),\n length: batch.numRows,\n children\n });\n return new arrow.RecordBatch(schema, data, new Map(batch.metadata));\n}\n\nfunction getQueuedRowCount(queue: arrow.Table[]): number {\n return queue.reduce((rowCount, table) => rowCount + table.numRows, 0);\n}\n\nfunction takeRowsFromQueue(queue: arrow.Table[], rowCount: number): arrow.Table {\n const table = queue[0];\n if (rowCount === table.numRows) {\n queue.shift();\n return table;\n }\n const result = table.slice(0, rowCount);\n queue[0] = table.slice(rowCount, table.numRows - rowCount);\n return result;\n}\n\n/** Creates an explicit empty Arrow batch so zero-row shapefiles still expose schema in batch mode. */\nfunction makeEmptyArrowBatch(schema: TableSchema): ArrowTableBatch {\n const table = new ArrowTableBuilder(schema).finishTable();\n return makeTableScanBatch({shape: 'arrow-table', data: table, schema});\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,0BAAuD;AACvD,IAAAA,uBAKO;AACP,YAAuB;AACvB,0BAA6D;AAO7D,IAAAC,uBAAsD;AACtD,2BAA6C;AAC7C,6BAAmC;AAEnC,4BAGO;AACP,wBAA+B;AAE/B,wBAAkC;AAClC,wBAAkC;AAIlC,6BAA0D;AAC1D,sCAGO;AACP,2CAA2C;AAC3C,IAAM,uBAAuB;AAG7B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AAAA,MACP,OAAO;AAAA,MACP;AAAA,MACA;AAAA,MACA,OAAO;AAAA,IACT;AAAA,EACJ;AACA,QAAM,OAAa,eAAS;AAAA,IAC1B,MAAM,IAAU,aAAO,OAAO,MAAM;AAAA,IACpC,QAAQ,MAAM;AAAA,IACd;AAAA,EACF,CAAC;AACD,SAAO,IAAU,kBAAY,QAAQ,MAAM,IAAI,IAAI,MAAM,QAAQ,CAAC;AACpE;AAEA,SAAS,kBAAkB,OAA8B;AACvD,SAAO,MAAM,OAAO,CAAC,UAAU,UAAU,WAAW,MAAM,SAAS,CAAC;AACtE;AAEA,SAAS,kBAAkB,OAAsB,UAA+B;AAC9E,QAAM,QAAQ,MAAM,CAAC;AACrB,MAAI,aAAa,MAAM,SAAS;AAC9B,UAAM,MAAM;AACZ,WAAO;AAAA,EACT;AACA,QAAM,SAAS,MAAM,MAAM,GAAG,QAAQ;AACtC,QAAM,CAAC,IAAI,MAAM,MAAM,UAAU,MAAM,UAAU,QAAQ;AACzD,SAAO;AACT;AAGA,SAAS,oBAAoB,QAAsC;AACjE,QAAM,QAAQ,IAAI,uCAAkB,MAAM,EAAE,YAAY;AACxD,aAAO,yCAAmB,EAAC,OAAO,eAAe,MAAM,OAAO,OAAM,CAAC;AACvE;",
4
+ "sourcesContent": ["// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport {CRSReprojectionError, type LoaderContext} from '@loaders.gl/core/loader-utils';\nimport {\n parseFromContext,\n parseInBatchesFromContext,\n toArrayBufferIterator\n} from '@loaders.gl/core/loader-utils';\nimport * as arrow from 'apache-arrow';\nimport {convertArrowToSchema, isArrowRecordBatch} from '@loaders.gl/core/schema-utils';\nimport type {Field, Schema as TableSchema} from '@loaders.gl/core/schema';\nimport {makeArrowMetadataBatch, convertSchemaToArrow} from '@loaders.gl/core/schema-utils';\nimport {convertFeaturesToGeoArrowTable} from '@loaders.gl/core/arrow-geometry/feature-table';\nimport {convertWKBToGeometry} from '@loaders.gl/core/arrow-geometry/geometry-codecs';\nimport {type GeoParquetGeometryType} from '@loaders.gl/core/schema';\nimport {\n makeWKBGeometryField,\n setWKBGeometryColumnMetadata\n} from '@loaders.gl/core/arrow-geometry/geometry-field';\nimport {projectionEngine} from '@math.gl/geospatial/projection';\nimport type {ReadonlyCRSDefinition, WKTCRSDefinition} from '@math.gl/geospatial/crs';\nimport {SHPLoaderWithParser} from './shp-loader';\nimport {DBFLoaderWithParser} from './dbf-loader';\nimport type {ShapefileLoaderOptions} from './shapefile-loader-types';\nimport type {SHPResult} from './lib/parsers/parse-shp';\nimport type {SHPHeader} from './lib/parsers/parse-shp-header';\nimport {loadShapefileSidecarFiles, replaceExtension} from './lib/parsers/parse-shapefile';\nimport {\n type SHPWKBGeometry,\n makeWKBGeometryArrowTable\n} from './lib/parsers/build-wkb-geometry-arrow';\nimport {makeSHPGeoArrowGeometryTable} from './lib/parsers/build-geoarrow-geometry-arrow';\nconst GEOMETRY_COLUMN_NAME = 'geometry';\n\n/** Parses a shapefile and returns an Arrow table with a WKB geometry column. */\nexport async function parseShapefileToArrow(\n arrayBuffer: ArrayBuffer,\n options?: ShapefileLoaderOptions,\n context?: LoaderContext\n): Promise<arrow.Table> {\n const {cpg, prj, shx} = await loadShapefileSidecarFiles(options, context);\n const transform = getReprojectionTransform(prj, options);\n const geoArrowEncoding = getTypedGeoArrowEncoding(options);\n let header: SHPHeader | undefined;\n let geometryTable: arrow.Table;\n\n if (geoArrowEncoding) {\n geometryTable = makeSHPGeoArrowGeometryTable(arrayBuffer, options, {transform});\n } else {\n const shpResult = (await parseFromContext(\n arrayBuffer,\n SHPLoaderWithParser,\n {\n ...options,\n shp: {\n ...options?.shp,\n shape: 'wkb'\n }\n },\n context!\n )) as SHPResult;\n header = shpResult.header;\n geometryTable = makeGeometryArrowTable(\n shpResult.geometries as (SHPWKBGeometry | null)[],\n header,\n transform\n );\n if (\n options?.geoarrow?.encodingPreference === 'geoarrow.geometry' ||\n options?.shapefile?.geoarrow?.encodingPreference === 'geoarrow.geometry'\n ) {\n const geometryColumn = geometryTable.getChild(GEOMETRY_COLUMN_NAME);\n const geometries = geometryColumn?.toArray() || [];\n geometryTable = convertFeaturesToGeoArrowTable(\n geometries.map(geometry => ({\n type: 'Feature' as const,\n geometry: geometry ? convertWKBToGeometry(geometry.slice().buffer) : null,\n properties: {}\n })),\n {encodingPreference: 'geoarrow.geometry'}\n );\n }\n }\n\n let propertySchema: TableSchema | null = null;\n let propertyTable: arrow.Table | null = null;\n\n const dbfResponse = context?.url\n ? await context.fetch(replaceExtension(context.url, 'dbf')).catch(() => null)\n : null;\n if (dbfResponse?.ok) {\n const propertyData = (await parseFromContext(\n dbfResponse as any,\n DBFLoaderWithParser,\n {\n ...options,\n dbf: {\n ...options?.dbf,\n shape: 'arrow' as const,\n encoding: cpg || 'latin1'\n }\n },\n context!\n )) as arrow.Table;\n propertySchema = convertArrowToSchema(propertyData.schema);\n propertyTable = propertyData;\n }\n\n const schema = buildOutputSchema(\n propertySchema,\n header,\n convertArrowToSchema(geometryTable.schema)\n );\n const table = propertyTable\n ? appendGeometryColumnToArrowTable(propertyTable, geometryTable, schema)\n : geometryTable;\n const encoded = encodeShapefileHeader({header, encoding: cpg, prj, shx});\n table.schema.metadata.set(SHAPEFILE_HEADER_KEY, encoded);\n for (const batch of table.batches) batch.schema.metadata.set(SHAPEFILE_HEADER_KEY, encoded);\n return table;\n}\n\n/** Parses a shapefile into Arrow batches while keeping DBF-derived schema stable. */\nexport async function* parseShapefileToArrowInBatches(\n asyncIterator:\n | AsyncIterable<ArrayBufferLike | ArrayBufferView>\n | Iterable<ArrayBufferLike | ArrayBufferView>,\n options?: ShapefileLoaderOptions,\n context?: LoaderContext\n): AsyncIterable<arrow.RecordBatch> {\n if (getTypedGeoArrowEncoding(options)) {\n throw new Error('Typed GeoArrow shapefile output is only supported for non-streaming parse.');\n }\n\n const {cpg, prj, shx} = await loadShapefileSidecarFiles(options, context);\n const batchSize =\n typeof options?.core?.batchSize === 'number'\n ? options.core.batchSize\n : (options?.shapefile?.batchSize ??\n options?.shp?.batchSize ??\n options?.dbf?.batchSize ??\n 10000);\n const childOptions = {\n ...options,\n core: {...options?.core, batchMaxRowsPerSecond: 0}\n };\n\n const shapeIterable = await parseInBatchesFromContext(\n toArrayBufferIterator(asyncIterator),\n SHPLoaderWithParser,\n {\n ...childOptions,\n shp: {\n ...options?.shp,\n shape: 'wkb',\n batchSize\n }\n },\n context!\n );\n const shapeIterator = getAsyncIterator(shapeIterable);\n\n const shapeHeader = await getNextNonMetadataValue(shapeIterator);\n const header = shapeHeader as SHPHeader;\n const encodedHeader = encodeShapefileHeader({header, encoding: cpg, prj, shx});\n\n let propertyIterator: AsyncIterator<any> | null = null;\n let propertySchema: TableSchema | null = null;\n\n const dbfResponse = context?.url\n ? await context.fetch(replaceExtension(context.url, 'dbf')).catch(() => null)\n : null;\n if (dbfResponse?.ok) {\n const dbfOptions = {\n ...childOptions,\n dbf: {\n ...options?.dbf,\n shape: 'arrow' as const,\n batchSize,\n encoding: cpg || 'latin1'\n }\n };\n const propertyIterable = await parseInBatchesFromContext(\n dbfResponse,\n DBFLoaderWithParser,\n dbfOptions,\n context!\n );\n propertyIterator = getAsyncIterator(propertyIterable);\n\n const firstPropertyBatch = await getNextArrowBatch(propertyIterator);\n propertySchema = firstPropertyBatch ? convertArrowToSchema(firstPropertyBatch.schema) : null;\n const outputSchema = buildOutputSchema(propertySchema, header);\n const propertyQueue: arrow.Table[] = [];\n const geometryQueue: arrow.Table[] = [];\n let yieldedDataBatch = false;\n\n if (firstPropertyBatch && firstPropertyBatch.numRows > 0) {\n propertyQueue.push(new arrow.Table(firstPropertyBatch.schema, [firstPropertyBatch]));\n }\n\n let shapeDone = false;\n let propertyDone = false;\n while (\n !shapeDone ||\n !propertyDone ||\n getQueuedRowCount(geometryQueue) > 0 ||\n getQueuedRowCount(propertyQueue) > 0\n ) {\n if (!shapeDone && getQueuedRowCount(geometryQueue) === 0) {\n const shapeBatch = await shapeIterator.next();\n if (shapeBatch.done) {\n shapeDone = true;\n } else if (shapeBatch.value?.batchType !== 'metadata') {\n const transform = getReprojectionTransform(prj, options);\n geometryQueue.push(\n makeGeometryArrowTable(shapeBatch.value as (SHPWKBGeometry | null)[], header, transform)\n );\n }\n }\n\n if (!propertyDone && getQueuedRowCount(propertyQueue) < getQueuedRowCount(geometryQueue)) {\n const propertyBatch = await getNextArrowBatch(propertyIterator);\n if (!propertyBatch) {\n propertyDone = true;\n } else if (propertyBatch.numRows > 0) {\n propertyQueue.push(new arrow.Table(propertyBatch.schema, [propertyBatch]));\n }\n }\n\n const rowCount = Math.min(getQueuedRowCount(geometryQueue), getQueuedRowCount(propertyQueue));\n if (rowCount === 0) {\n if (\n (shapeDone && getQueuedRowCount(geometryQueue) === 0) ||\n (propertyDone && getQueuedRowCount(propertyQueue) === 0)\n ) {\n break;\n }\n continue;\n }\n\n const propertyTable = takeRowsFromQueue(propertyQueue, rowCount);\n const geometryTable = takeRowsFromQueue(geometryQueue, rowCount);\n const batch = appendGeometryColumnToArrowTable(propertyTable, geometryTable, outputSchema);\n yieldedDataBatch = true;\n const recordBatch = batch.batches[0];\n recordBatch.schema.metadata.set(SHAPEFILE_HEADER_KEY, encodedHeader);\n yield recordBatch;\n }\n if (!yieldedDataBatch) {\n yield makeEmptyArrowBatch(outputSchema, encodedHeader);\n }\n return;\n }\n\n const outputSchema = buildOutputSchema(null, header);\n let yieldedDataBatch = false;\n\n while (true) {\n const shapeBatch = await shapeIterator.next();\n if (shapeBatch.done) {\n break;\n }\n if (shapeBatch.value?.batchType === 'metadata') {\n continue;\n }\n const transform = getReprojectionTransform(prj, options);\n const arrowTable = makeGeometryArrowTable(\n shapeBatch.value as (SHPWKBGeometry | null)[],\n header,\n transform\n );\n yieldedDataBatch = true;\n for (const batch of arrowTable.batches) {\n batch.schema.metadata.set(SHAPEFILE_HEADER_KEY, encodedHeader);\n yield batch;\n }\n }\n if (!yieldedDataBatch) {\n yield makeEmptyArrowBatch(outputSchema, encodedHeader);\n }\n}\n\n/** Creates the output Arrow schema by appending the WKB geometry column to DBF fields. */\nfunction buildOutputSchema(\n propertySchema: TableSchema | null,\n header?: SHPHeader,\n geometrySchema?: TableSchema\n): TableSchema {\n const geometryField: Field =\n geometrySchema?.fields[0] || makeWKBGeometryField(GEOMETRY_COLUMN_NAME);\n const schema: TableSchema = {\n fields: [...(propertySchema?.fields || []), geometryField],\n metadata: {\n ...(propertySchema?.metadata || {}),\n ...(geometrySchema?.metadata || {})\n }\n };\n\n if (!geometrySchema) {\n setWKBGeometryColumnMetadata(schema.metadata!, {\n geometryColumnName: GEOMETRY_COLUMN_NAME,\n geometryTypes: inferGeometryTypes(header)\n });\n }\n\n return schema;\n}\n\nfunction makeGeometryArrowTable(\n geometries: (SHPWKBGeometry | null | undefined)[],\n header?: SHPHeader,\n transform?: (coordinate: number[]) => number[]\n) {\n const geometrySchema = buildOutputSchema(null, header);\n return makeWKBGeometryArrowTable(geometries, geometrySchema, transform);\n}\n\nfunction appendGeometryColumnToArrowTable(\n propertyTable: arrow.Table,\n geometryTable: arrow.Table,\n schema: TableSchema\n): arrow.Table {\n const propertyBatch = propertyTable.batches[0];\n const geometryBatch = geometryTable.batches[0];\n const arrowSchema = convertSchemaToArrow(schema);\n const structField = new arrow.Struct(arrowSchema.fields);\n const children = [...propertyBatch.data.children, geometryBatch.data.children[0]];\n const rowCount = Math.max(propertyTable.numRows, geometryTable.numRows);\n const structData = new arrow.Data(structField, 0, rowCount, 0, undefined, children);\n const recordBatch = new arrow.RecordBatch(arrowSchema, structData);\n\n return new arrow.Table(arrowSchema, [recordBatch]);\n}\n\nfunction getReprojectionTransform(\n sourceCrs: WKTCRSDefinition | undefined,\n options?: ShapefileLoaderOptions\n): ((coordinate: number[]) => number[]) | undefined {\n const {reproject = false, targetCrs = 'WGS84'} = options?.gis || {};\n if (!reproject) {\n return undefined;\n }\n if (!sourceCrs) {\n throw new CRSReprojectionError(\n 'missing-source-crs',\n 'Shapefile reprojection requires a source CRS from the .prj sidecar file',\n {targetCrs}\n );\n }\n try {\n const projection = projectionEngine.createProjection({\n from: sourceCrs,\n to: (targetCrs || 'WGS84') as ReadonlyCRSDefinition\n });\n return coordinate => projection.projectSync(coordinate);\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n throw new CRSReprojectionError(\n 'transformation-failed',\n `Shapefile reprojection failed: ${message}`,\n {sourceCrs, targetCrs: targetCrs || 'WGS84', cause: error}\n );\n }\n}\n\nfunction getTypedGeoArrowEncoding(options?: ShapefileLoaderOptions): boolean {\n const encoding = options?.shapefile?.geoarrowEncoding || options?.shp?.geoarrowEncoding;\n const preference =\n options?.geoarrow?.encodingPreference ||\n options?.shapefile?.geoarrow?.encodingPreference ||\n options?.shp?.geoarrow?.encodingPreference;\n return encoding === 'geoarrow' || preference === 'optimized';\n}\n\n/** Infers GeoParquet geometry type metadata from parsed geometries or the SHP header. */\nfunction inferGeometryTypes(header?: SHPHeader): GeoParquetGeometryType[] {\n const fallbackType = getGeometryTypeFromHeader(header?.type);\n return fallbackType ? [fallbackType] : [];\n}\n\n/** Maps SHP header geometry type codes to GeoParquet geometry type strings. */\nfunction getGeometryTypeFromHeader(type?: number): GeoParquetGeometryType | null {\n switch (type) {\n case 1:\n case 11:\n case 21:\n return type === 11 ? 'Point Z' : 'Point';\n case 3:\n case 13:\n case 23:\n return type === 13 ? 'LineString Z' : 'LineString';\n case 5:\n case 15:\n case 25:\n return type === 15 ? 'Polygon Z' : 'Polygon';\n case 8:\n case 18:\n case 28:\n return type === 18 ? 'MultiPoint Z' : 'MultiPoint';\n default:\n return null;\n }\n}\n\n/** Normalizes sync or async iterables to a single async iterator interface. */\nfunction getAsyncIterator(iterable: AsyncIterable<any> | Iterable<any>): AsyncIterator<any> {\n const iterator = iterable[Symbol.asyncIterator]?.() || iterable[Symbol.iterator]?.();\n return iterator as AsyncIterator<any>;\n}\n\n/** Reads the next non-metadata value from a parser iterator. */\nasync function getNextNonMetadataValue(iterator: AsyncIterator<any>): Promise<any> {\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return null;\n }\n if (result.value?.batchType !== 'metadata') {\n return result.value;\n }\n }\n}\n\n/** Reads native DBF batches and imports columns originating from another Arrow build. */\nasync function getNextArrowBatch(\n iterator: AsyncIterator<unknown>\n): Promise<arrow.RecordBatch | null> {\n while (true) {\n const result = await iterator.next();\n if (result.done) return null;\n if (isArrowRecordBatch(result.value)) {\n const batch = result.value;\n return Object.getPrototypeOf(batch) === arrow.RecordBatch.prototype\n ? batch\n : importDBFRecordBatch(batch, convertArrowToSchema(batch.schema));\n }\n }\n}\n\n/** Rebuilds primitive DBF columns with local Arrow classes while reusing their buffers. */\nfunction importDBFRecordBatch(\n batch: arrow.RecordBatch,\n tableSchema: TableSchema\n): arrow.RecordBatch {\n const schema = convertSchemaToArrow(tableSchema);\n const children = batch.data.children.map(\n (column, index) =>\n new arrow.Data(\n schema.fields[index].type,\n column.offset,\n column.length,\n column.nullCount,\n column.buffers,\n undefined,\n undefined,\n column.variadicBuffers\n )\n );\n const data = arrow.makeData({\n type: new arrow.Struct(schema.fields),\n length: batch.numRows,\n children\n });\n return new arrow.RecordBatch(schema, data, new Map(batch.metadata));\n}\n\nfunction getQueuedRowCount(queue: arrow.Table[]): number {\n return queue.reduce((rowCount, table) => rowCount + table.numRows, 0);\n}\n\nfunction takeRowsFromQueue(queue: arrow.Table[], rowCount: number): arrow.Table {\n const table = queue[0];\n if (rowCount === table.numRows) {\n queue.shift();\n return table;\n }\n const result = table.slice(0, rowCount);\n queue[0] = table.slice(rowCount);\n return result;\n}\n\n/** Creates an explicit empty Arrow batch so zero-row shapefiles still expose their schema. */\nfunction makeEmptyArrowBatch(schema: TableSchema, header: string): arrow.RecordBatch {\n const nativeSchema = convertSchemaToArrow(schema);\n nativeSchema.metadata.set(SHAPEFILE_HEADER_KEY, header);\n return makeArrowMetadataBatch(nativeSchema);\n}\n\n/** Schema metadata key for the Shapefile header and sidecar data. */\nexport const SHAPEFILE_HEADER_KEY = 'loaders.gl.shapefile.header';\n\n/** Encodes Shapefile sidecars for schema and worker transport. */\nfunction encodeShapefileHeader({\n shx,\n ...header\n}: {\n header?: SHPHeader;\n encoding?: string;\n prj?: WKTCRSDefinition;\n shx?: {offsets: Int32Array; lengths: Int32Array};\n}): string {\n return JSON.stringify({\n ...header,\n ...(shx ? {shx: {offsets: Array.from(shx.offsets), lengths: Array.from(shx.lengths)}} : {})\n });\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,0BAAuD;AACvD,IAAAA,uBAIO;AACP,YAAuB;AACvB,0BAAuD;AAEvD,IAAAC,uBAA2D;AAC3D,2BAA6C;AAC7C,6BAAmC;AAEnC,4BAGO;AACP,wBAA+B;AAE/B,wBAAkC;AAClC,wBAAkC;AAIlC,6BAA0D;AAC1D,sCAGO;AACP,2CAA2C;AAC3C,IAAM,uBAAuB;AAG7B,eAAsB,sBACpB,aACA,SACA,SACsB;AACtB,QAAM,EAAC,KAAK,KAAK,IAAG,IAAI,UAAM,kDAA0B,SAAS,OAAO;AACxE,QAAM,YAAY,yBAAyB,KAAK,OAAO;AACvD,QAAM,mBAAmB,yBAAyB,OAAO;AACzD,MAAI;AACJ,MAAI;AAEJ,MAAI,kBAAkB;AACpB,wBAAgB,mEAA6B,aAAa,SAAS,EAAC,UAAS,CAAC;AAAA,EAChF,OAAO;AACL,UAAM,YAAa,UAAM;AAAA,MACvB;AAAA,MACA;AAAA,MACA;AAAA,QACE,GAAG;AAAA,QACH,KAAK;AAAA,UACH,GAAG,SAAS;AAAA,UACZ,OAAO;AAAA,QACT;AAAA,MACF;AAAA,MACA;AAAA,IACF;AACA,aAAS,UAAU;AACnB,oBAAgB;AAAA,MACd,UAAU;AAAA,MACV;AAAA,MACA;AAAA,IACF;AACA,QACE,SAAS,UAAU,uBAAuB,uBAC1C,SAAS,WAAW,UAAU,uBAAuB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GAAG,SAAS,MAAM,KAAK,IAAI,OAAO,EAAC,EAAC,IAAI,CAAC;AAAA,EAC3F,CAAC;AACH;",
6
6
  "names": ["import_loader_utils", "import_schema_utils", "outputSchema", "yieldedDataBatch"]
7
7
  }
@@ -1,8 +1,10 @@
1
1
  import { type LoaderContext } from '@loaders.gl/core/loader-utils';
2
- import type { ArrowTable, ArrowTableBatch } from '@loaders.gl/core/schema';
2
+ import * as arrow from 'apache-arrow';
3
3
  import type { ShapefileLoaderOptions } from "./shapefile-loader-types.js";
4
4
  /** Parses a shapefile and returns an Arrow table with a WKB geometry column. */
5
- export declare function parseShapefileToArrow(arrayBuffer: ArrayBuffer, options?: ShapefileLoaderOptions, context?: LoaderContext): Promise<ArrowTable>;
5
+ export declare function parseShapefileToArrow(arrayBuffer: ArrayBuffer, options?: ShapefileLoaderOptions, context?: LoaderContext): Promise<arrow.Table>;
6
6
  /** Parses a shapefile into Arrow batches while keeping DBF-derived schema stable. */
7
- export declare function parseShapefileToArrowInBatches(asyncIterator: AsyncIterable<ArrayBufferLike | ArrayBufferView> | Iterable<ArrayBufferLike | ArrayBufferView>, options?: ShapefileLoaderOptions, context?: LoaderContext): AsyncIterable<ArrowTableBatch>;
7
+ export declare function parseShapefileToArrowInBatches(asyncIterator: AsyncIterable<ArrayBufferLike | ArrayBufferView> | Iterable<ArrayBufferLike | ArrayBufferView>, options?: ShapefileLoaderOptions, context?: LoaderContext): AsyncIterable<arrow.RecordBatch>;
8
+ /** Schema metadata key for the Shapefile header and sidecar data. */
9
+ export declare const SHAPEFILE_HEADER_KEY = "loaders.gl.shapefile.header";
8
10
  //# sourceMappingURL=shapefile-arrow-loader.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"shapefile-arrow-loader.d.ts","sourceRoot":"","sources":["../../src/shapefile/shapefile-arrow-loader.ts"],"names":[],"mappings":"AAIA,OAAO,EAAuB,KAAK,aAAa,EAAC,MAAM,+BAA+B,CAAC;AASvF,OAAO,KAAK,EACV,UAAU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
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
  import { CRSReprojectionError } from '@loaders.gl/core/loader-utils';
5
- import { makeTableScanBatch, parseFromContext, parseInBatchesFromContext, toArrayBufferIterator } from '@loaders.gl/core/loader-utils';
5
+ import { parseFromContext, parseInBatchesFromContext, toArrayBufferIterator } from '@loaders.gl/core/loader-utils';
6
6
  import * as arrow from 'apache-arrow';
7
- import { convertArrowToSchema, isNativeArrowRecordBatch } from '@loaders.gl/core/schema-utils';
8
- import { ArrowTableBuilder, convertSchemaToArrow } from '@loaders.gl/core/schema-utils';
7
+ import { convertArrowToSchema, isArrowRecordBatch } from '@loaders.gl/core/schema-utils';
8
+ import { makeArrowMetadataBatch, convertSchemaToArrow } from '@loaders.gl/core/schema-utils';
9
9
  import { convertFeaturesToGeoArrowTable } from '@loaders.gl/core/arrow-geometry/feature-table';
10
10
  import { convertWKBToGeometry } from '@loaders.gl/core/arrow-geometry/geometry-codecs';
11
11
  import { makeWKBGeometryField, setWKBGeometryColumnMetadata } from '@loaders.gl/core/arrow-geometry/geometry-field';
@@ -38,7 +38,7 @@ export async function parseShapefileToArrow(arrayBuffer, options, context) {
38
38
  geometryTable = makeGeometryArrowTable(shpResult.geometries, header, transform);
39
39
  if (options?.geoarrow?.encodingPreference === 'geoarrow.geometry' ||
40
40
  options?.shapefile?.geoarrow?.encodingPreference === 'geoarrow.geometry') {
41
- const geometryColumn = geometryTable.data.getChild(GEOMETRY_COLUMN_NAME);
41
+ const geometryColumn = geometryTable.getChild(GEOMETRY_COLUMN_NAME);
42
42
  const geometries = geometryColumn?.toArray() || [];
43
43
  geometryTable = convertFeaturesToGeoArrowTable(geometries.map(geometry => ({
44
44
  type: 'Feature',
@@ -62,13 +62,17 @@ export async function parseShapefileToArrow(arrayBuffer, options, context) {
62
62
  }
63
63
  }, context));
64
64
  propertySchema = convertArrowToSchema(propertyData.schema);
65
- propertyTable = { shape: 'arrow-table', data: propertyData, schema: propertySchema };
65
+ propertyTable = propertyData;
66
66
  }
67
- const schema = buildOutputSchema(propertySchema, header, geometryTable.schema);
67
+ const schema = buildOutputSchema(propertySchema, header, convertArrowToSchema(geometryTable.schema));
68
68
  const table = propertyTable
69
69
  ? appendGeometryColumnToArrowTable(propertyTable, geometryTable, schema)
70
70
  : geometryTable;
71
- return Object.assign(table, { loaderData: { header, encoding: cpg, prj, shx } });
71
+ const encoded = encodeShapefileHeader({ header, encoding: cpg, prj, shx });
72
+ table.schema.metadata.set(SHAPEFILE_HEADER_KEY, encoded);
73
+ for (const batch of table.batches)
74
+ batch.schema.metadata.set(SHAPEFILE_HEADER_KEY, encoded);
75
+ return table;
72
76
  }
73
77
  /** Parses a shapefile into Arrow batches while keeping DBF-derived schema stable. */
74
78
  export async function* parseShapefileToArrowInBatches(asyncIterator, options, context) {
@@ -97,6 +101,7 @@ export async function* parseShapefileToArrowInBatches(asyncIterator, options, co
97
101
  const shapeIterator = getAsyncIterator(shapeIterable);
98
102
  const shapeHeader = await getNextNonMetadataValue(shapeIterator);
99
103
  const header = shapeHeader;
104
+ const encodedHeader = encodeShapefileHeader({ header, encoding: cpg, prj, shx });
100
105
  let propertyIterator = null;
101
106
  let propertySchema = null;
102
107
  const dbfResponse = context?.url
@@ -115,13 +120,13 @@ export async function* parseShapefileToArrowInBatches(asyncIterator, options, co
115
120
  const propertyIterable = await parseInBatchesFromContext(dbfResponse, DBFLoaderWithParser, dbfOptions, context);
116
121
  propertyIterator = getAsyncIterator(propertyIterable);
117
122
  const firstPropertyBatch = await getNextArrowBatch(propertyIterator);
118
- propertySchema = firstPropertyBatch?.schema || null;
123
+ propertySchema = firstPropertyBatch ? convertArrowToSchema(firstPropertyBatch.schema) : null;
119
124
  const outputSchema = buildOutputSchema(propertySchema, header);
120
125
  const propertyQueue = [];
121
126
  const geometryQueue = [];
122
127
  let yieldedDataBatch = false;
123
- if (firstPropertyBatch && firstPropertyBatch.length > 0) {
124
- propertyQueue.push(firstPropertyBatch.data);
128
+ if (firstPropertyBatch && firstPropertyBatch.numRows > 0) {
129
+ propertyQueue.push(new arrow.Table(firstPropertyBatch.schema, [firstPropertyBatch]));
125
130
  }
126
131
  let shapeDone = false;
127
132
  let propertyDone = false;
@@ -136,8 +141,7 @@ export async function* parseShapefileToArrowInBatches(asyncIterator, options, co
136
141
  }
137
142
  else if (shapeBatch.value?.batchType !== 'metadata') {
138
143
  const transform = getReprojectionTransform(prj, options);
139
- geometryQueue.push(makeGeometryArrowTable(shapeBatch.value, header, transform)
140
- .data);
144
+ geometryQueue.push(makeGeometryArrowTable(shapeBatch.value, header, transform));
141
145
  }
142
146
  }
143
147
  if (!propertyDone && getQueuedRowCount(propertyQueue) < getQueuedRowCount(geometryQueue)) {
@@ -145,8 +149,8 @@ export async function* parseShapefileToArrowInBatches(asyncIterator, options, co
145
149
  if (!propertyBatch) {
146
150
  propertyDone = true;
147
151
  }
148
- else if (propertyBatch.length > 0) {
149
- propertyQueue.push(propertyBatch.data);
152
+ else if (propertyBatch.numRows > 0) {
153
+ propertyQueue.push(new arrow.Table(propertyBatch.schema, [propertyBatch]));
150
154
  }
151
155
  }
152
156
  const rowCount = Math.min(getQueuedRowCount(geometryQueue), getQueuedRowCount(propertyQueue));
@@ -159,12 +163,14 @@ export async function* parseShapefileToArrowInBatches(asyncIterator, options, co
159
163
  }
160
164
  const propertyTable = takeRowsFromQueue(propertyQueue, rowCount);
161
165
  const geometryTable = takeRowsFromQueue(geometryQueue, rowCount);
162
- const batch = appendGeometryColumnToArrowTable({ shape: 'arrow-table', schema: propertySchema || undefined, data: propertyTable }, { shape: 'arrow-table', data: geometryTable }, outputSchema);
166
+ const batch = appendGeometryColumnToArrowTable(propertyTable, geometryTable, outputSchema);
163
167
  yieldedDataBatch = true;
164
- yield { ...makeTableScanBatch(batch), loaderData: { header, encoding: cpg, prj, shx } };
168
+ const recordBatch = batch.batches[0];
169
+ recordBatch.schema.metadata.set(SHAPEFILE_HEADER_KEY, encodedHeader);
170
+ yield recordBatch;
165
171
  }
166
172
  if (!yieldedDataBatch) {
167
- yield { ...makeEmptyArrowBatch(outputSchema), loaderData: { header, encoding: cpg, prj, shx } };
173
+ yield makeEmptyArrowBatch(outputSchema, encodedHeader);
168
174
  }
169
175
  return;
170
176
  }
@@ -181,13 +187,13 @@ export async function* parseShapefileToArrowInBatches(asyncIterator, options, co
181
187
  const transform = getReprojectionTransform(prj, options);
182
188
  const arrowTable = makeGeometryArrowTable(shapeBatch.value, header, transform);
183
189
  yieldedDataBatch = true;
184
- yield {
185
- ...makeTableScanBatch({ ...arrowTable, schema: outputSchema }),
186
- loaderData: { header, encoding: cpg, prj, shx }
187
- };
190
+ for (const batch of arrowTable.batches) {
191
+ batch.schema.metadata.set(SHAPEFILE_HEADER_KEY, encodedHeader);
192
+ yield batch;
193
+ }
188
194
  }
189
195
  if (!yieldedDataBatch) {
190
- yield { ...makeEmptyArrowBatch(outputSchema), loaderData: { header, encoding: cpg, prj, shx } };
196
+ yield makeEmptyArrowBatch(outputSchema, encodedHeader);
191
197
  }
192
198
  }
193
199
  /** Creates the output Arrow schema by appending the WKB geometry column to DBF fields. */
@@ -213,19 +219,15 @@ function makeGeometryArrowTable(geometries, header, transform) {
213
219
  return makeWKBGeometryArrowTable(geometries, geometrySchema, transform);
214
220
  }
215
221
  function appendGeometryColumnToArrowTable(propertyTable, geometryTable, schema) {
216
- const propertyBatch = propertyTable.data.batches[0];
217
- const geometryBatch = geometryTable.data.batches[0];
222
+ const propertyBatch = propertyTable.batches[0];
223
+ const geometryBatch = geometryTable.batches[0];
218
224
  const arrowSchema = convertSchemaToArrow(schema);
219
225
  const structField = new arrow.Struct(arrowSchema.fields);
220
226
  const children = [...propertyBatch.data.children, geometryBatch.data.children[0]];
221
- const rowCount = Math.max(propertyTable.data.numRows, geometryTable.data.numRows);
227
+ const rowCount = Math.max(propertyTable.numRows, geometryTable.numRows);
222
228
  const structData = new arrow.Data(structField, 0, rowCount, 0, undefined, children);
223
229
  const recordBatch = new arrow.RecordBatch(arrowSchema, structData);
224
- return {
225
- shape: 'arrow-table',
226
- schema,
227
- data: new arrow.Table(arrowSchema, [recordBatch])
228
- };
230
+ return new arrow.Table(arrowSchema, [recordBatch]);
229
231
  }
230
232
  function getReprojectionTransform(sourceCrs, options) {
231
233
  const { reproject = false, targetCrs = 'WGS84' } = options?.gis || {};
@@ -299,28 +301,18 @@ async function getNextNonMetadataValue(iterator) {
299
301
  }
300
302
  }
301
303
  }
302
- /** Reads both native and legacy DBF column batches. */
304
+ /** Reads native DBF batches and imports columns originating from another Arrow build. */
303
305
  async function getNextArrowBatch(iterator) {
304
306
  while (true) {
305
307
  const result = await iterator.next();
306
- if (result.done) {
308
+ if (result.done)
307
309
  return null;
310
+ if (isArrowRecordBatch(result.value)) {
311
+ const batch = result.value;
312
+ return Object.getPrototypeOf(batch) === arrow.RecordBatch.prototype
313
+ ? batch
314
+ : importDBFRecordBatch(batch, convertArrowToSchema(batch.schema));
308
315
  }
309
- if (isNativeArrowRecordBatch(result.value)) {
310
- const recordBatch = result.value;
311
- const schema = convertArrowToSchema(recordBatch.schema);
312
- const batch = Object.getPrototypeOf(recordBatch) === arrow.RecordBatch.prototype
313
- ? recordBatch
314
- : importDBFRecordBatch(recordBatch, schema);
315
- const data = new arrow.Table(batch.schema, [batch]);
316
- return makeTableScanBatch({
317
- shape: 'arrow-table',
318
- schema,
319
- data
320
- });
321
- }
322
- if (result.value?.shape === 'arrow-table')
323
- return result.value;
324
316
  }
325
317
  }
326
318
  /** Rebuilds primitive DBF columns with local Arrow classes while reusing their buffers. */
@@ -344,12 +336,22 @@ function takeRowsFromQueue(queue, rowCount) {
344
336
  return table;
345
337
  }
346
338
  const result = table.slice(0, rowCount);
347
- queue[0] = table.slice(rowCount, table.numRows - rowCount);
339
+ queue[0] = table.slice(rowCount);
348
340
  return result;
349
341
  }
350
- /** Creates an explicit empty Arrow batch so zero-row shapefiles still expose schema in batch mode. */
351
- function makeEmptyArrowBatch(schema) {
352
- const table = new ArrowTableBuilder(schema).finishTable();
353
- return makeTableScanBatch({ shape: 'arrow-table', data: table, schema });
342
+ /** Creates an explicit empty Arrow batch so zero-row shapefiles still expose their schema. */
343
+ function makeEmptyArrowBatch(schema, header) {
344
+ const nativeSchema = convertSchemaToArrow(schema);
345
+ nativeSchema.metadata.set(SHAPEFILE_HEADER_KEY, header);
346
+ return makeArrowMetadataBatch(nativeSchema);
347
+ }
348
+ /** Schema metadata key for the Shapefile header and sidecar data. */
349
+ export const SHAPEFILE_HEADER_KEY = 'loaders.gl.shapefile.header';
350
+ /** Encodes Shapefile sidecars for schema and worker transport. */
351
+ function encodeShapefileHeader({ shx, ...header }) {
352
+ return JSON.stringify({
353
+ ...header,
354
+ ...(shx ? { shx: { offsets: Array.from(shx.offsets), lengths: Array.from(shx.lengths) } } : {})
355
+ });
354
356
  }
355
357
  //# sourceMappingURL=shapefile-arrow-loader.js.map