@loaders.gl/arrow 4.2.0-alpha.4 → 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.
- package/dist/arrow-loader.d.ts +1 -1
- package/dist/arrow-loader.d.ts.map +1 -1
- package/dist/arrow-loader.js +29 -18
- package/dist/arrow-writer.d.ts +1 -1
- package/dist/arrow-writer.d.ts.map +1 -1
- package/dist/arrow-writer.js +22 -15
- package/dist/dist.dev.js +370 -375
- package/dist/dist.min.js +23 -0
- package/dist/geoarrow/convert-geoarrow-to-binary-geometry.js +345 -288
- package/dist/geoarrow/convert-geoarrow-to-geojson-geometry.js +151 -124
- package/dist/geoarrow/get-arrow-bounds.js +32 -22
- package/dist/geoarrow-loader.d.ts +1 -1
- package/dist/geoarrow-loader.d.ts.map +1 -1
- package/dist/geoarrow-loader.js +22 -15
- package/dist/geoarrow-writer.js +24 -15
- package/dist/index.cjs +54 -71
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +16 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/lib/arrow-table-batch.d.ts +1 -1
- package/dist/lib/arrow-table-batch.d.ts.map +1 -1
- package/dist/lib/arrow-table-batch.js +54 -51
- package/dist/lib/arrow-table.js +3 -1
- package/dist/lib/encode-arrow.d.ts +1 -1
- package/dist/lib/encode-arrow.d.ts.map +1 -1
- package/dist/lib/encode-arrow.js +31 -16
- package/dist/lib/encode-geoarrow.d.ts +1 -1
- package/dist/lib/encode-geoarrow.d.ts.map +1 -1
- package/dist/lib/encode-geoarrow.js +31 -16
- package/dist/parsers/parse-arrow-in-batches.d.ts +2 -2
- package/dist/parsers/parse-arrow-in-batches.d.ts.map +1 -1
- package/dist/parsers/parse-arrow-in-batches.js +49 -21
- package/dist/parsers/parse-arrow-sync.d.ts +1 -1
- package/dist/parsers/parse-arrow-sync.d.ts.map +1 -1
- package/dist/parsers/parse-arrow-sync.js +26 -22
- package/dist/parsers/parse-geoarrow-in-batches.d.ts +1 -1
- package/dist/parsers/parse-geoarrow-in-batches.d.ts.map +1 -1
- package/dist/parsers/parse-geoarrow-in-batches.js +7 -2
- package/dist/parsers/parse-geoarrow-sync.d.ts +1 -1
- package/dist/parsers/parse-geoarrow-sync.d.ts.map +1 -1
- package/dist/parsers/parse-geoarrow-sync.js +12 -10
- package/dist/schema/arrow-type-utils.js +36 -9
- package/dist/schema/convert-arrow-schema.js +253 -240
- package/dist/tables/convert-arrow-to-columnar-table.d.ts +1 -1
- package/dist/tables/convert-arrow-to-columnar-table.d.ts.map +1 -1
- package/dist/tables/convert-arrow-to-columnar-table.js +22 -13
- package/dist/tables/convert-arrow-to-geojson-table.d.ts +1 -1
- package/dist/tables/convert-arrow-to-geojson-table.d.ts.map +1 -1
- package/dist/tables/convert-arrow-to-geojson-table.js +47 -32
- package/dist/tables/convert-columnar-to-row-table.js +22 -16
- package/dist/tables/convert-table-to-arrow.js +58 -1
- package/dist/triangulate-on-worker.js +23 -18
- package/dist/types.js +8 -6
- package/dist/workers/arrow-worker.js +3 -1
- package/dist/workers/hard-clone.js +93 -49
- package/dist/workers/triangulation-worker-node.d.ts +1 -1
- package/dist/workers/triangulation-worker-node.d.ts.map +1 -1
- package/dist/workers/triangulation-worker-node.js +3 -1
- package/dist/workers/triangulation-worker.js +48 -48
- package/package.json +12 -8
- package/dist/arrow-loader.js.map +0 -1
- package/dist/arrow-writer.js.map +0 -1
- package/dist/geoarrow/convert-geoarrow-to-binary-geometry.js.map +0 -1
- package/dist/geoarrow/convert-geoarrow-to-geojson-geometry.js.map +0 -1
- package/dist/geoarrow/get-arrow-bounds.js.map +0 -1
- package/dist/geoarrow-loader.js.map +0 -1
- package/dist/geoarrow-writer.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/arrow-table-batch.js.map +0 -1
- package/dist/lib/arrow-table.js.map +0 -1
- package/dist/lib/encode-arrow.js.map +0 -1
- package/dist/lib/encode-geoarrow.js.map +0 -1
- package/dist/parsers/parse-arrow-in-batches.js.map +0 -1
- package/dist/parsers/parse-arrow-sync.js.map +0 -1
- package/dist/parsers/parse-geoarrow-in-batches.js.map +0 -1
- package/dist/parsers/parse-geoarrow-sync.js.map +0 -1
- package/dist/schema/arrow-type-utils.js.map +0 -1
- package/dist/schema/convert-arrow-schema.js.map +0 -1
- package/dist/tables/convert-arrow-to-columnar-table.js.map +0 -1
- package/dist/tables/convert-arrow-to-geojson-table.js.map +0 -1
- package/dist/tables/convert-columnar-to-row-table.js.map +0 -1
- package/dist/tables/convert-table-to-arrow.js.map +0 -1
- package/dist/triangulate-on-worker.js.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/workers/arrow-worker.js.map +0 -1
- package/dist/workers/hard-clone.js.map +0 -1
- package/dist/workers/triangulation-worker-node.js.map +0 -1
- 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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
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
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
geometryIndicies
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
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
|
-
|
|
308
|
-
|
|
309
|
-
|
|
365
|
+
else {
|
|
366
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
367
|
+
featureIds[i] = i;
|
|
368
|
+
}
|
|
310
369
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
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
|