@loaders.gl/arrow 4.2.0-alpha.3 → 4.2.0-alpha.5

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 (90) hide show
  1. package/dist/arrow-loader.d.ts +1 -1
  2. package/dist/arrow-loader.d.ts.map +1 -1
  3. package/dist/arrow-loader.js +30 -19
  4. package/dist/arrow-worker.js +1 -1
  5. package/dist/arrow-writer.d.ts +1 -1
  6. package/dist/arrow-writer.d.ts.map +1 -1
  7. package/dist/arrow-writer.js +23 -16
  8. package/dist/dist.dev.js +370 -375
  9. package/dist/dist.min.js +23 -0
  10. package/dist/geoarrow/convert-geoarrow-to-binary-geometry.js +345 -288
  11. package/dist/geoarrow/convert-geoarrow-to-geojson-geometry.js +151 -124
  12. package/dist/geoarrow/get-arrow-bounds.js +32 -22
  13. package/dist/geoarrow-loader.d.ts +1 -1
  14. package/dist/geoarrow-loader.d.ts.map +1 -1
  15. package/dist/geoarrow-loader.js +22 -15
  16. package/dist/geoarrow-writer.js +25 -16
  17. package/dist/index.cjs +54 -71
  18. package/dist/index.cjs.map +7 -0
  19. package/dist/index.d.ts +16 -16
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +8 -1
  22. package/dist/lib/arrow-table-batch.d.ts +1 -1
  23. package/dist/lib/arrow-table-batch.d.ts.map +1 -1
  24. package/dist/lib/arrow-table-batch.js +54 -51
  25. package/dist/lib/arrow-table.js +3 -1
  26. package/dist/lib/encode-arrow.d.ts +1 -1
  27. package/dist/lib/encode-arrow.d.ts.map +1 -1
  28. package/dist/lib/encode-arrow.js +31 -16
  29. package/dist/lib/encode-geoarrow.d.ts +1 -1
  30. package/dist/lib/encode-geoarrow.d.ts.map +1 -1
  31. package/dist/lib/encode-geoarrow.js +31 -16
  32. package/dist/parsers/parse-arrow-in-batches.d.ts +2 -2
  33. package/dist/parsers/parse-arrow-in-batches.d.ts.map +1 -1
  34. package/dist/parsers/parse-arrow-in-batches.js +49 -21
  35. package/dist/parsers/parse-arrow-sync.d.ts +1 -1
  36. package/dist/parsers/parse-arrow-sync.d.ts.map +1 -1
  37. package/dist/parsers/parse-arrow-sync.js +26 -22
  38. package/dist/parsers/parse-geoarrow-in-batches.d.ts +1 -1
  39. package/dist/parsers/parse-geoarrow-in-batches.d.ts.map +1 -1
  40. package/dist/parsers/parse-geoarrow-in-batches.js +7 -2
  41. package/dist/parsers/parse-geoarrow-sync.d.ts +1 -1
  42. package/dist/parsers/parse-geoarrow-sync.d.ts.map +1 -1
  43. package/dist/parsers/parse-geoarrow-sync.js +12 -10
  44. package/dist/schema/arrow-type-utils.js +36 -9
  45. package/dist/schema/convert-arrow-schema.js +253 -240
  46. package/dist/tables/convert-arrow-to-columnar-table.d.ts +1 -1
  47. package/dist/tables/convert-arrow-to-columnar-table.d.ts.map +1 -1
  48. package/dist/tables/convert-arrow-to-columnar-table.js +22 -13
  49. package/dist/tables/convert-arrow-to-geojson-table.d.ts +1 -1
  50. package/dist/tables/convert-arrow-to-geojson-table.d.ts.map +1 -1
  51. package/dist/tables/convert-arrow-to-geojson-table.js +47 -32
  52. package/dist/tables/convert-columnar-to-row-table.js +22 -16
  53. package/dist/tables/convert-table-to-arrow.js +58 -1
  54. package/dist/triangulate-on-worker.js +24 -19
  55. package/dist/types.js +8 -6
  56. package/dist/workers/arrow-worker.js +3 -1
  57. package/dist/workers/hard-clone.js +93 -49
  58. package/dist/workers/triangulation-worker-node.d.ts +1 -1
  59. package/dist/workers/triangulation-worker-node.d.ts.map +1 -1
  60. package/dist/workers/triangulation-worker-node.js +3 -1
  61. package/dist/workers/triangulation-worker.js +48 -48
  62. package/package.json +12 -8
  63. package/dist/arrow-loader.js.map +0 -1
  64. package/dist/arrow-writer.js.map +0 -1
  65. package/dist/geoarrow/convert-geoarrow-to-binary-geometry.js.map +0 -1
  66. package/dist/geoarrow/convert-geoarrow-to-geojson-geometry.js.map +0 -1
  67. package/dist/geoarrow/get-arrow-bounds.js.map +0 -1
  68. package/dist/geoarrow-loader.js.map +0 -1
  69. package/dist/geoarrow-writer.js.map +0 -1
  70. package/dist/index.js.map +0 -1
  71. package/dist/lib/arrow-table-batch.js.map +0 -1
  72. package/dist/lib/arrow-table.js.map +0 -1
  73. package/dist/lib/encode-arrow.js.map +0 -1
  74. package/dist/lib/encode-geoarrow.js.map +0 -1
  75. package/dist/parsers/parse-arrow-in-batches.js.map +0 -1
  76. package/dist/parsers/parse-arrow-sync.js.map +0 -1
  77. package/dist/parsers/parse-geoarrow-in-batches.js.map +0 -1
  78. package/dist/parsers/parse-geoarrow-sync.js.map +0 -1
  79. package/dist/schema/arrow-type-utils.js.map +0 -1
  80. package/dist/schema/convert-arrow-schema.js.map +0 -1
  81. package/dist/tables/convert-arrow-to-columnar-table.js.map +0 -1
  82. package/dist/tables/convert-arrow-to-geojson-table.js.map +0 -1
  83. package/dist/tables/convert-columnar-to-row-table.js.map +0 -1
  84. package/dist/tables/convert-table-to-arrow.js.map +0 -1
  85. package/dist/triangulate-on-worker.js.map +0 -1
  86. package/dist/types.js.map +0 -1
  87. package/dist/workers/arrow-worker.js.map +0 -1
  88. package/dist/workers/hard-clone.js.map +0 -1
  89. package/dist/workers/triangulation-worker-node.js.map +0 -1
  90. package/dist/workers/triangulation-worker.js.map +0 -1
@@ -1,320 +1,377 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { earcut } from '@math.gl/polygon';
2
5
  import { updateBoundsFromGeoArrowSamples } from "./get-arrow-bounds.js";
3
- var BinaryGeometryType = function (BinaryGeometryType) {
4
- BinaryGeometryType["points"] = "points";
5
- BinaryGeometryType["lines"] = "lines";
6
- BinaryGeometryType["polygons"] = "polygons";
7
- return BinaryGeometryType;
8
- }(BinaryGeometryType || {});
6
+ /**
7
+ * Binary geometry type
8
+ */
9
+ var BinaryGeometryType;
10
+ (function (BinaryGeometryType) {
11
+ BinaryGeometryType["points"] = "points";
12
+ BinaryGeometryType["lines"] = "lines";
13
+ BinaryGeometryType["polygons"] = "polygons";
14
+ })(BinaryGeometryType || (BinaryGeometryType = {}));
15
+ /**
16
+ * binary geometry template, see deck.gl BinaryGeometry
17
+ */
9
18
  export function getBinaryGeometryTemplate() {
10
- return {
11
- globalFeatureIds: {
12
- value: new Uint32Array(0),
13
- size: 1
14
- },
15
- positions: {
16
- value: new Float32Array(0),
17
- size: 2
18
- },
19
- properties: [],
20
- numericProps: {},
21
- featureIds: {
22
- value: new Uint32Array(0),
23
- size: 1
24
- }
25
- };
19
+ return {
20
+ globalFeatureIds: { value: new Uint32Array(0), size: 1 },
21
+ positions: { value: new Float32Array(0), size: 2 },
22
+ properties: [],
23
+ numericProps: {},
24
+ featureIds: { value: new Uint32Array(0), size: 1 }
25
+ };
26
26
  }
27
+ /**
28
+ * get binary geometries from geoarrow column
29
+ *
30
+ * @param geoColumn the geoarrow column, e.g. arrowTable.getChildAt(geoColumnIndex)
31
+ * @param geoEncoding the geo encoding of the geoarrow column, e.g. getGeoArrowEncoding(arrowTable.schema, geoColumnName)
32
+ * @param options options for getting binary geometries {meanCenter: boolean}
33
+ * @returns BinaryDataFromGeoArrow
34
+ */
27
35
  export function getBinaryGeometriesFromArrow(geoColumn, geoEncoding, options) {
28
- const featureTypes = {
29
- polygon: geoEncoding === 'geoarrow.multipolygon' || geoEncoding === 'geoarrow.polygon',
30
- point: geoEncoding === 'geoarrow.multipoint' || geoEncoding === 'geoarrow.point',
31
- line: geoEncoding === 'geoarrow.multilinestring' || geoEncoding === 'geoarrow.linestring'
32
- };
33
- const chunks = (options === null || options === void 0 ? void 0 : options.chunkIndex) !== undefined && (options === null || options === void 0 ? void 0 : options.chunkIndex) >= 0 ? [geoColumn.data[options === null || options === void 0 ? void 0 : options.chunkIndex]] : geoColumn.data;
34
- let bounds = [Infinity, Infinity, -Infinity, -Infinity];
35
- let globalFeatureIdOffset = (options === null || options === void 0 ? void 0 : options.chunkOffset) || 0;
36
- const binaryGeometries = [];
37
- chunks.forEach(chunk => {
38
- const {
39
- featureIds,
40
- flatCoordinateArray,
41
- nDim,
42
- geomOffset,
43
- triangles
44
- } = getBinaryGeometriesFromChunk(chunk, geoEncoding, options);
45
- const globalFeatureIds = new Uint32Array(featureIds.length);
46
- for (let i = 0; i < featureIds.length; i++) {
47
- globalFeatureIds[i] = featureIds[i] + globalFeatureIdOffset;
48
- }
49
- const binaryContent = {
50
- globalFeatureIds: {
51
- value: globalFeatureIds,
52
- size: 1
53
- },
54
- positions: {
55
- value: flatCoordinateArray,
56
- size: nDim
57
- },
58
- featureIds: {
59
- value: featureIds,
60
- size: 1
61
- },
62
- properties: [...Array(chunk.length).keys()].map(i => ({
63
- index: i + globalFeatureIdOffset
64
- }))
36
+ const featureTypes = {
37
+ polygon: geoEncoding === 'geoarrow.multipolygon' || geoEncoding === 'geoarrow.polygon',
38
+ point: geoEncoding === 'geoarrow.multipoint' || geoEncoding === 'geoarrow.point',
39
+ line: geoEncoding === 'geoarrow.multilinestring' || geoEncoding === 'geoarrow.linestring'
65
40
  };
66
- globalFeatureIdOffset += chunk.length;
67
- binaryGeometries.push({
68
- shape: 'binary-feature-collection',
69
- points: {
70
- type: 'Point',
71
- ...getBinaryGeometryTemplate(),
72
- ...(featureTypes.point ? binaryContent : {})
73
- },
74
- lines: {
75
- type: 'LineString',
76
- ...getBinaryGeometryTemplate(),
77
- ...(featureTypes.line ? binaryContent : {}),
78
- pathIndices: {
79
- value: featureTypes.line ? geomOffset : new Uint16Array(0),
80
- size: 1
41
+ const chunks = options?.chunkIndex !== undefined && options?.chunkIndex >= 0
42
+ ? [geoColumn.data[options?.chunkIndex]]
43
+ : geoColumn.data;
44
+ let bounds = [Infinity, Infinity, -Infinity, -Infinity];
45
+ let globalFeatureIdOffset = options?.chunkOffset || 0;
46
+ const binaryGeometries = [];
47
+ chunks.forEach((chunk) => {
48
+ const { featureIds, flatCoordinateArray, nDim, geomOffset, triangles } = getBinaryGeometriesFromChunk(chunk, geoEncoding, options);
49
+ const globalFeatureIds = new Uint32Array(featureIds.length);
50
+ for (let i = 0; i < featureIds.length; i++) {
51
+ globalFeatureIds[i] = featureIds[i] + globalFeatureIdOffset;
81
52
  }
82
- },
83
- polygons: {
84
- type: 'Polygon',
85
- ...getBinaryGeometryTemplate(),
86
- ...(featureTypes.polygon ? binaryContent : {}),
87
- polygonIndices: {
88
- value: featureTypes.polygon ? geomOffset : new Uint16Array(0),
89
- size: 1
90
- },
91
- primitivePolygonIndices: {
92
- value: featureTypes.polygon ? geomOffset : new Uint16Array(0),
93
- size: 1
94
- },
95
- ...(triangles ? {
96
- triangles: {
97
- value: triangles,
98
- size: 1
99
- }
100
- } : {})
101
- }
53
+ const binaryContent = {
54
+ globalFeatureIds: { value: globalFeatureIds, size: 1 },
55
+ positions: {
56
+ value: flatCoordinateArray,
57
+ size: nDim
58
+ },
59
+ featureIds: { value: featureIds, size: 1 },
60
+ // eslint-disable-next-line no-loop-func
61
+ properties: [...Array(chunk.length).keys()].map((i) => ({
62
+ index: i + globalFeatureIdOffset
63
+ }))
64
+ };
65
+ // TODO: check if chunks are sequentially accessed
66
+ globalFeatureIdOffset += chunk.length;
67
+ // NOTE: deck.gl defines the BinaryFeatures structure must have points, lines, polygons even if they are empty
68
+ binaryGeometries.push({
69
+ shape: 'binary-feature-collection',
70
+ points: {
71
+ type: 'Point',
72
+ ...getBinaryGeometryTemplate(),
73
+ ...(featureTypes.point ? binaryContent : {})
74
+ },
75
+ lines: {
76
+ type: 'LineString',
77
+ ...getBinaryGeometryTemplate(),
78
+ ...(featureTypes.line ? binaryContent : {}),
79
+ pathIndices: { value: featureTypes.line ? geomOffset : new Uint16Array(0), size: 1 }
80
+ },
81
+ polygons: {
82
+ type: 'Polygon',
83
+ ...getBinaryGeometryTemplate(),
84
+ ...(featureTypes.polygon ? binaryContent : {}),
85
+ polygonIndices: {
86
+ // use geomOffset as polygonIndices same as primitivePolygonIndices since we are using earcut to get triangule indices
87
+ value: featureTypes.polygon ? geomOffset : new Uint16Array(0),
88
+ size: 1
89
+ },
90
+ primitivePolygonIndices: {
91
+ value: featureTypes.polygon ? geomOffset : new Uint16Array(0),
92
+ size: 1
93
+ },
94
+ ...(triangles ? { triangles: { value: triangles, size: 1 } } : {})
95
+ }
96
+ });
97
+ bounds = updateBoundsFromGeoArrowSamples(flatCoordinateArray, nDim, bounds);
102
98
  });
103
- bounds = updateBoundsFromGeoArrowSamples(flatCoordinateArray, nDim, bounds);
104
- });
105
- return {
106
- binaryGeometries,
107
- bounds,
108
- featureTypes,
109
- ...(options !== null && options !== void 0 && options.calculateMeanCenters ? {
110
- meanCenters: getMeanCentersFromBinaryGeometries(binaryGeometries)
111
- } : {})
112
- };
99
+ return {
100
+ binaryGeometries,
101
+ bounds,
102
+ featureTypes,
103
+ ...(options?.calculateMeanCenters
104
+ ? { meanCenters: getMeanCentersFromBinaryGeometries(binaryGeometries) }
105
+ : {})
106
+ };
113
107
  }
108
+ /**
109
+ * Get mean centers from binary geometries
110
+ * @param binaryGeometries binary geometries from geoarrow column, an array of BinaryFeatureCollection
111
+ * @returns mean centers of the binary geometries
112
+ */
114
113
  export function getMeanCentersFromBinaryGeometries(binaryGeometries) {
115
- const globalMeanCenters = [];
116
- binaryGeometries.forEach(binaryGeometry => {
117
- let binaryGeometryType = null;
118
- if (binaryGeometry.points && binaryGeometry.points.positions.value.length > 0) {
119
- binaryGeometryType = BinaryGeometryType.points;
120
- } else if (binaryGeometry.lines && binaryGeometry.lines.positions.value.length > 0) {
121
- binaryGeometryType = BinaryGeometryType.lines;
122
- } else if (binaryGeometry.polygons && binaryGeometry.polygons.positions.value.length > 0) {
123
- binaryGeometryType = BinaryGeometryType.polygons;
124
- }
125
- const binaryContent = binaryGeometryType ? binaryGeometry[binaryGeometryType] : null;
126
- if (binaryContent && binaryGeometryType !== null) {
127
- var _binaryContent$primit;
128
- const featureIds = binaryContent.featureIds.value;
129
- const flatCoordinateArray = binaryContent.positions.value;
130
- const nDim = binaryContent.positions.size;
131
- const primitivePolygonIndices = binaryContent.type === 'Polygon' ? (_binaryContent$primit = binaryContent.primitivePolygonIndices) === null || _binaryContent$primit === void 0 ? void 0 : _binaryContent$primit.value : undefined;
132
- const meanCenters = getMeanCentersFromGeometry(featureIds, flatCoordinateArray, nDim, binaryGeometryType, primitivePolygonIndices);
133
- meanCenters.forEach(center => {
134
- globalMeanCenters.push(center);
135
- });
136
- }
137
- });
138
- return globalMeanCenters;
114
+ const globalMeanCenters = [];
115
+ binaryGeometries.forEach((binaryGeometry) => {
116
+ let binaryGeometryType = null;
117
+ if (binaryGeometry.points && binaryGeometry.points.positions.value.length > 0) {
118
+ binaryGeometryType = BinaryGeometryType.points;
119
+ }
120
+ else if (binaryGeometry.lines && binaryGeometry.lines.positions.value.length > 0) {
121
+ binaryGeometryType = BinaryGeometryType.lines;
122
+ }
123
+ else if (binaryGeometry.polygons && binaryGeometry.polygons.positions.value.length > 0) {
124
+ binaryGeometryType = BinaryGeometryType.polygons;
125
+ }
126
+ const binaryContent = binaryGeometryType ? binaryGeometry[binaryGeometryType] : null;
127
+ if (binaryContent && binaryGeometryType !== null) {
128
+ const featureIds = binaryContent.featureIds.value;
129
+ const flatCoordinateArray = binaryContent.positions.value;
130
+ const nDim = binaryContent.positions.size;
131
+ const primitivePolygonIndices = binaryContent.type === 'Polygon' ? binaryContent.primitivePolygonIndices?.value : undefined;
132
+ const meanCenters = getMeanCentersFromGeometry(featureIds, flatCoordinateArray, nDim, binaryGeometryType, primitivePolygonIndices);
133
+ meanCenters.forEach((center) => {
134
+ globalMeanCenters.push(center);
135
+ });
136
+ }
137
+ });
138
+ return globalMeanCenters;
139
139
  }
140
+ /**
141
+ * Get mean centers from raw coordinates and feature ids
142
+ * @param featureIds Array of feature ids indexes by vertex
143
+ * @param flatCoordinateArray Array of vertex, e.g. x, y or x, y, z, positions
144
+ * @param nDim number of dimensions per position
145
+ * @returns - mean centers of each polygon
146
+ */
140
147
  function getMeanCentersFromGeometry(featureIds, flatCoordinateArray, nDim, geometryType, primitivePolygonIndices) {
141
- const meanCenters = [];
142
- const vertexCount = flatCoordinateArray.length;
143
- let vertexIndex = 0;
144
- let coordIdx = 0;
145
- let primitiveIdx = 0;
146
- while (vertexIndex < vertexCount) {
147
- const featureId = featureIds[vertexIndex / nDim];
148
- const center = [0, 0];
149
- let vertexCountInFeature = 0;
150
- while (vertexIndex < vertexCount && featureIds[coordIdx] === featureId) {
151
- if (geometryType === BinaryGeometryType.polygons && (primitivePolygonIndices === null || primitivePolygonIndices === void 0 ? void 0 : primitivePolygonIndices[primitiveIdx]) === coordIdx) {
152
- vertexIndex += nDim;
153
- primitiveIdx++;
154
- } else {
155
- center[0] += flatCoordinateArray[vertexIndex];
156
- center[1] += flatCoordinateArray[vertexIndex + 1];
157
- vertexIndex += nDim;
158
- vertexCountInFeature++;
159
- }
160
- coordIdx += 1;
148
+ const meanCenters = [];
149
+ const vertexCount = flatCoordinateArray.length;
150
+ let vertexIndex = 0;
151
+ let coordIdx = 0;
152
+ let primitiveIdx = 0;
153
+ while (vertexIndex < vertexCount) {
154
+ const featureId = featureIds[vertexIndex / nDim];
155
+ const center = [0, 0];
156
+ let vertexCountInFeature = 0;
157
+ while (vertexIndex < vertexCount && featureIds[coordIdx] === featureId) {
158
+ if (geometryType === BinaryGeometryType.polygons &&
159
+ primitivePolygonIndices?.[primitiveIdx] === coordIdx) {
160
+ // skip the first point since it is the same as the last point in each ring for polygons
161
+ vertexIndex += nDim;
162
+ primitiveIdx++;
163
+ }
164
+ else {
165
+ center[0] += flatCoordinateArray[vertexIndex];
166
+ center[1] += flatCoordinateArray[vertexIndex + 1];
167
+ vertexIndex += nDim;
168
+ vertexCountInFeature++;
169
+ }
170
+ coordIdx += 1;
171
+ }
172
+ center[0] /= vertexCountInFeature;
173
+ center[1] /= vertexCountInFeature;
174
+ meanCenters.push(center);
161
175
  }
162
- center[0] /= vertexCountInFeature;
163
- center[1] /= vertexCountInFeature;
164
- meanCenters.push(center);
165
- }
166
- return meanCenters;
176
+ return meanCenters;
167
177
  }
178
+ /**
179
+ * get binary geometries from geoarrow column
180
+ * @param chunk one chunk/batch of geoarrow column
181
+ * @param geoEncoding geo encoding of the geoarrow column
182
+ * @param options options for getting binary geometries
183
+ * @returns BinaryGeometryContent
184
+ */
168
185
  function getBinaryGeometriesFromChunk(chunk, geoEncoding, options) {
169
- switch (geoEncoding) {
170
- case 'geoarrow.point':
171
- case 'geoarrow.multipoint':
172
- return getBinaryPointsFromChunk(chunk, geoEncoding);
173
- case 'geoarrow.linestring':
174
- case 'geoarrow.multilinestring':
175
- return getBinaryLinesFromChunk(chunk, geoEncoding);
176
- case 'geoarrow.polygon':
177
- case 'geoarrow.multipolygon':
178
- return getBinaryPolygonsFromChunk(chunk, geoEncoding, options);
179
- default:
180
- throw Error('invalid geoarrow encoding');
181
- }
186
+ switch (geoEncoding) {
187
+ case 'geoarrow.point':
188
+ case 'geoarrow.multipoint':
189
+ return getBinaryPointsFromChunk(chunk, geoEncoding);
190
+ case 'geoarrow.linestring':
191
+ case 'geoarrow.multilinestring':
192
+ return getBinaryLinesFromChunk(chunk, geoEncoding);
193
+ case 'geoarrow.polygon':
194
+ case 'geoarrow.multipolygon':
195
+ return getBinaryPolygonsFromChunk(chunk, geoEncoding, options);
196
+ default:
197
+ throw Error('invalid geoarrow encoding');
198
+ }
182
199
  }
200
+ /**
201
+ * get triangle indices. Allows deck.gl to skip performing costly triangulation on main thread.
202
+ * @param polygonIndices Indices within positions of the start of each simple Polygon
203
+ * @param primitivePolygonIndices Indices within positions of the start of each primitive Polygon/ring
204
+ * @param flatCoordinateArray Array of x, y or x, y, z positions
205
+ * @param nDim - number of dimensions per position
206
+ * @returns triangle indices or null if invalid polygon and earcut fails
207
+ */
183
208
  export function getTriangleIndices(polygonIndices, primitivePolygonIndices, flatCoordinateArray, nDim) {
184
- try {
185
- let primitiveIndex = 0;
186
- const triangles = [];
187
- for (let i = 0; i < polygonIndices.length - 1; i++) {
188
- const startIdx = polygonIndices[i];
189
- const endIdx = polygonIndices[i + 1];
190
- const slicedFlatCoords = flatCoordinateArray.subarray(startIdx * nDim, endIdx * nDim);
191
- const holeIndices = [];
192
- while (primitivePolygonIndices[primitiveIndex] < endIdx) {
193
- if (primitivePolygonIndices[primitiveIndex] > startIdx) {
194
- holeIndices.push(primitivePolygonIndices[primitiveIndex] - startIdx);
209
+ try {
210
+ let primitiveIndex = 0;
211
+ const triangles = [];
212
+ // loop polygonIndices to get triangles
213
+ for (let i = 0; i < polygonIndices.length - 1; i++) {
214
+ const startIdx = polygonIndices[i];
215
+ const endIdx = polygonIndices[i + 1];
216
+ // get subarray of flatCoordinateArray
217
+ const slicedFlatCoords = flatCoordinateArray.subarray(startIdx * nDim, endIdx * nDim);
218
+ // get holeIndices for earcut
219
+ const holeIndices = [];
220
+ while (primitivePolygonIndices[primitiveIndex] < endIdx) {
221
+ if (primitivePolygonIndices[primitiveIndex] > startIdx) {
222
+ holeIndices.push(primitivePolygonIndices[primitiveIndex] - startIdx);
223
+ }
224
+ primitiveIndex++;
225
+ }
226
+ // TODO check if each ring is closed
227
+ const triangleIndices = earcut(slicedFlatCoords, holeIndices.length > 0 ? holeIndices : undefined, nDim);
228
+ if (triangleIndices.length === 0) {
229
+ throw Error('earcut failed e.g. invalid polygon');
230
+ }
231
+ for (let j = 0; j < triangleIndices.length; j++) {
232
+ triangles.push(triangleIndices[j] + startIdx);
233
+ }
195
234
  }
196
- primitiveIndex++;
197
- }
198
- const triangleIndices = earcut(slicedFlatCoords, holeIndices.length > 0 ? holeIndices : undefined, nDim);
199
- if (triangleIndices.length === 0) {
200
- throw Error('earcut failed e.g. invalid polygon');
201
- }
202
- for (let j = 0; j < triangleIndices.length; j++) {
203
- triangles.push(triangleIndices[j] + startIdx);
204
- }
235
+ // convert traingles to Uint32Array
236
+ const trianglesUint32 = new Uint32Array(triangles.length);
237
+ for (let i = 0; i < triangles.length; i++) {
238
+ trianglesUint32[i] = triangles[i];
239
+ }
240
+ return trianglesUint32;
205
241
  }
206
- const trianglesUint32 = new Uint32Array(triangles.length);
207
- for (let i = 0; i < triangles.length; i++) {
208
- trianglesUint32[i] = triangles[i];
242
+ catch (error) {
243
+ // if earcut fails, return null
244
+ return null;
209
245
  }
210
- return trianglesUint32;
211
- } catch (error) {
212
- return null;
213
- }
214
246
  }
247
+ /**
248
+ * get binary polygons from geoarrow polygon column
249
+ * @param chunk one chunk of geoarrow polygon column
250
+ * @param geoEncoding the geo encoding of the geoarrow polygon column
251
+ * @param options options for getting binary geometries
252
+ * @returns BinaryGeometryContent
253
+ */
215
254
  function getBinaryPolygonsFromChunk(chunk, geoEncoding, options) {
216
- const isMultiPolygon = geoEncoding === 'geoarrow.multipolygon';
217
- const polygonData = isMultiPolygon ? chunk.children[0] : chunk;
218
- const polygonOffset = polygonData.valueOffsets;
219
- const partData = isMultiPolygon ? chunk.valueOffsets.map(i => polygonOffset.at(i) || i) : chunk.valueOffsets;
220
- const ringData = polygonData.children[0];
221
- const pointData = ringData.children[0];
222
- const coordData = pointData.children[0];
223
- const nDim = pointData.stride;
224
- const geomOffset = ringData.valueOffsets;
225
- const flatCoordinateArray = coordData.values;
226
- const geometryIndicies = new Uint16Array(polygonOffset.length);
227
- for (let i = 0; i < polygonOffset.length; i++) {
228
- geometryIndicies[i] = geomOffset[polygonOffset[i]];
229
- }
230
- const numOfVertices = flatCoordinateArray.length / nDim;
231
- const featureIds = new Uint32Array(numOfVertices);
232
- for (let i = 0; i < partData.length - 1; i++) {
233
- const startIdx = geomOffset[partData[i]];
234
- const endIdx = geomOffset[partData[i + 1]];
235
- for (let j = startIdx; j < endIdx; j++) {
236
- featureIds[j] = i;
255
+ const isMultiPolygon = geoEncoding === 'geoarrow.multipolygon';
256
+ const polygonData = isMultiPolygon ? chunk.children[0] : chunk;
257
+ const polygonOffset = polygonData.valueOffsets;
258
+ const partData = isMultiPolygon
259
+ ? chunk.valueOffsets.map((i) => polygonOffset.at(i) || i)
260
+ : chunk.valueOffsets;
261
+ const ringData = polygonData.children[0];
262
+ const pointData = ringData.children[0];
263
+ const coordData = pointData.children[0];
264
+ const nDim = pointData.stride;
265
+ const geomOffset = ringData.valueOffsets;
266
+ const flatCoordinateArray = coordData.values;
267
+ const geometryIndicies = new Uint16Array(polygonOffset.length);
268
+ for (let i = 0; i < polygonOffset.length; i++) {
269
+ geometryIndicies[i] = geomOffset[polygonOffset[i]];
270
+ }
271
+ const numOfVertices = flatCoordinateArray.length / nDim;
272
+ const featureIds = new Uint32Array(numOfVertices);
273
+ for (let i = 0; i < partData.length - 1; i++) {
274
+ const startIdx = geomOffset[partData[i]];
275
+ const endIdx = geomOffset[partData[i + 1]];
276
+ for (let j = startIdx; j < endIdx; j++) {
277
+ featureIds[j] = i;
278
+ }
237
279
  }
238
- }
239
- const triangles = options !== null && options !== void 0 && options.triangulate ? getTriangleIndices(geometryIndicies, geomOffset, flatCoordinateArray, nDim) : null;
240
- return {
241
- featureIds,
242
- nDim,
243
- flatCoordinateArray,
244
- geomOffset,
245
- geometryIndicies,
246
- ...(options !== null && options !== void 0 && options.triangulate && triangles ? {
247
- triangles
248
- } : {})
249
- };
280
+ const triangles = options?.triangulate
281
+ ? getTriangleIndices(geometryIndicies, geomOffset, flatCoordinateArray, nDim)
282
+ : null;
283
+ return {
284
+ featureIds,
285
+ nDim,
286
+ flatCoordinateArray,
287
+ geomOffset,
288
+ geometryIndicies,
289
+ ...(options?.triangulate && triangles ? { triangles } : {})
290
+ };
250
291
  }
292
+ /**
293
+ * get binary lines from geoarrow line column
294
+ * @param chunk one chunk/batch of geoarrow column
295
+ * @param geoEncoding the geo encoding of the geoarrow column
296
+ * @returns BinaryGeometryContent
297
+ */
251
298
  function getBinaryLinesFromChunk(chunk, geoEncoding) {
252
- const isMultiLineString = geoEncoding === 'geoarrow.multilinestring';
253
- const lineData = isMultiLineString ? chunk.children[0] : chunk;
254
- const pointData = lineData.children[0];
255
- const coordData = pointData.children[0];
256
- const nDim = pointData.stride;
257
- const geomOffset = lineData.valueOffsets;
258
- const flatCoordinateArray = coordData.values;
259
- const geometryIndicies = new Uint16Array(0);
260
- const numOfVertices = flatCoordinateArray.length / nDim;
261
- const featureIds = new Uint32Array(numOfVertices);
262
- if (isMultiLineString) {
263
- const partData = chunk.valueOffsets;
264
- for (let i = 0; i < partData.length - 1; i++) {
265
- const startIdx = geomOffset[partData[i]];
266
- const endIdx = geomOffset[partData[i + 1]];
267
- for (let j = startIdx; j < endIdx; j++) {
268
- featureIds[j] = i;
269
- }
299
+ const isMultiLineString = geoEncoding === 'geoarrow.multilinestring';
300
+ const lineData = isMultiLineString ? chunk.children[0] : chunk;
301
+ const pointData = lineData.children[0];
302
+ const coordData = pointData.children[0];
303
+ const nDim = pointData.stride;
304
+ const geomOffset = lineData.valueOffsets;
305
+ const flatCoordinateArray = coordData.values;
306
+ // geometryIndicies is not needed for line string
307
+ const geometryIndicies = new Uint16Array(0);
308
+ const numOfVertices = flatCoordinateArray.length / nDim;
309
+ const featureIds = new Uint32Array(numOfVertices);
310
+ if (isMultiLineString) {
311
+ const partData = chunk.valueOffsets;
312
+ for (let i = 0; i < partData.length - 1; i++) {
313
+ const startIdx = geomOffset[partData[i]];
314
+ const endIdx = geomOffset[partData[i + 1]];
315
+ for (let j = startIdx; j < endIdx; j++) {
316
+ featureIds[j] = i;
317
+ }
318
+ }
270
319
  }
271
- } else {
272
- for (let i = 0; i < chunk.length; i++) {
273
- const startIdx = geomOffset[i];
274
- const endIdx = geomOffset[i + 1];
275
- for (let j = startIdx; j < endIdx; j++) {
276
- featureIds[j] = i;
277
- }
320
+ else {
321
+ for (let i = 0; i < chunk.length; i++) {
322
+ const startIdx = geomOffset[i];
323
+ const endIdx = geomOffset[i + 1];
324
+ for (let j = startIdx; j < endIdx; j++) {
325
+ featureIds[j] = i;
326
+ }
327
+ }
278
328
  }
279
- }
280
- return {
281
- featureIds,
282
- flatCoordinateArray,
283
- nDim,
284
- geomOffset,
285
- geometryIndicies
286
- };
329
+ return {
330
+ featureIds,
331
+ flatCoordinateArray,
332
+ nDim,
333
+ geomOffset,
334
+ geometryIndicies
335
+ };
287
336
  }
337
+ /**
338
+ * get binary points from geoarrow point column
339
+ * @param chunk one chunk/batch of geoarrow column
340
+ * @param geoEncoding geo encoding of the geoarrow column
341
+ * @returns BinaryGeometryContent
342
+ */
288
343
  function getBinaryPointsFromChunk(chunk, geoEncoding) {
289
- const isMultiPoint = geoEncoding === 'geoarrow.multipoint';
290
- const pointData = isMultiPoint ? chunk.children[0] : chunk;
291
- const coordData = pointData.children[0];
292
- const nDim = pointData.stride;
293
- const flatCoordinateArray = coordData.values;
294
- const geometryIndicies = new Uint16Array(0);
295
- const geomOffset = new Int32Array(0);
296
- const numOfVertices = flatCoordinateArray.length / nDim;
297
- const featureIds = new Uint32Array(numOfVertices);
298
- if (isMultiPoint) {
299
- const partData = chunk.valueOffsets;
300
- for (let i = 0; i < partData.length - 1; i++) {
301
- const startIdx = partData[i];
302
- const endIdx = partData[i + 1];
303
- for (let j = startIdx; j < endIdx; j++) {
304
- featureIds[j] = i;
305
- }
344
+ const isMultiPoint = geoEncoding === 'geoarrow.multipoint';
345
+ const pointData = isMultiPoint ? chunk.children[0] : chunk;
346
+ const coordData = pointData.children[0];
347
+ const nDim = pointData.stride;
348
+ const flatCoordinateArray = coordData.values;
349
+ // geometryIndices is not needed for point
350
+ const geometryIndicies = new Uint16Array(0);
351
+ // geomOffset is not needed for point
352
+ const geomOffset = new Int32Array(0);
353
+ const numOfVertices = flatCoordinateArray.length / nDim;
354
+ const featureIds = new Uint32Array(numOfVertices);
355
+ if (isMultiPoint) {
356
+ const partData = chunk.valueOffsets;
357
+ for (let i = 0; i < partData.length - 1; i++) {
358
+ const startIdx = partData[i];
359
+ const endIdx = partData[i + 1];
360
+ for (let j = startIdx; j < endIdx; j++) {
361
+ featureIds[j] = i;
362
+ }
363
+ }
306
364
  }
307
- } else {
308
- for (let i = 0; i < chunk.length; i++) {
309
- featureIds[i] = i;
365
+ else {
366
+ for (let i = 0; i < chunk.length; i++) {
367
+ featureIds[i] = i;
368
+ }
310
369
  }
311
- }
312
- return {
313
- featureIds,
314
- flatCoordinateArray,
315
- nDim,
316
- geomOffset,
317
- geometryIndicies
318
- };
370
+ return {
371
+ featureIds,
372
+ flatCoordinateArray,
373
+ nDim,
374
+ geomOffset,
375
+ geometryIndicies
376
+ };
319
377
  }
320
- //# sourceMappingURL=convert-geoarrow-to-binary-geometry.js.map