Skip to content

Commit eb6f6cc

Browse files
authored
compute table offsets (#91)
* compute table offsets * flesh out table offsets
1 parent f1c31ef commit eb6f6cc

File tree

10 files changed

+133
-26
lines changed

10 files changed

+133
-26
lines changed

src/arc-layer.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import * as arrow from "apache-arrow";
1313
import * as ga from "@geoarrow/geoarrow-js";
1414
import { assignAccessor, extractAccessorsFromProps } from "./utils.js";
1515
import { child } from "@geoarrow/geoarrow-js";
16-
import { getPickingInfo } from "./picking.js";
16+
import {
17+
GeoArrowExtraPickingProps,
18+
computeChunkOffsets,
19+
getPickingInfo,
20+
} from "./picking.js";
1721
import { ColorAccessor, FloatAccessor, GeoArrowPickingInfo } from "./types.js";
1822
import { validateAccessors } from "./validate.js";
1923

@@ -108,7 +112,11 @@ export class GeoArrowArcLayer<
108112
static defaultProps = defaultProps;
109113
static layerName = "GeoArrowArcLayer";
110114

111-
getPickingInfo(params: GetPickingInfoParams): GeoArrowPickingInfo {
115+
getPickingInfo(
116+
params: GetPickingInfoParams & {
117+
sourceLayer: { props: GeoArrowExtraPickingProps };
118+
},
119+
): GeoArrowPickingInfo {
112120
return getPickingInfo(params, this.props.data);
113121
}
114122

@@ -137,6 +145,7 @@ export class GeoArrowArcLayer<
137145
"getSourcePosition",
138146
"getTargetPosition",
139147
]);
148+
const tableOffsets = computeChunkOffsets(table.data);
140149

141150
const layers: ArcLayer[] = [];
142151
for (
@@ -157,6 +166,7 @@ export class GeoArrowArcLayer<
157166

158167
// @ts-expect-error used for picking purposes
159168
recordBatchIdx,
169+
tableOffsets,
160170

161171
id: `${this.props.id}-geoarrow-arc-${recordBatchIdx}`,
162172
data: {

src/column-layer.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
import * as ga from "@geoarrow/geoarrow-js";
1919
import { ColorAccessor, FloatAccessor, GeoArrowPickingInfo } from "./types.js";
2020
import { EXTENSION_NAME } from "./constants.js";
21-
import { getPickingInfo } from "./picking.js";
21+
import {
22+
GeoArrowExtraPickingProps,
23+
computeChunkOffsets,
24+
getPickingInfo,
25+
} from "./picking.js";
2226
import { validateAccessors } from "./validate.js";
2327

2428
/** All properties supported by GeoArrowColumnLayer */
@@ -105,7 +109,11 @@ export class GeoArrowColumnLayer<
105109
static defaultProps = defaultProps;
106110
static layerName = "GeoArrowColumnLayer";
107111

108-
getPickingInfo(params: GetPickingInfoParams): GeoArrowPickingInfo {
112+
getPickingInfo(
113+
params: GetPickingInfoParams & {
114+
sourceLayer: { props: GeoArrowExtraPickingProps };
115+
},
116+
): GeoArrowPickingInfo {
109117
return getPickingInfo(params, this.props.data);
110118
}
111119

@@ -139,6 +147,7 @@ export class GeoArrowColumnLayer<
139147
const [accessors, otherProps] = extractAccessorsFromProps(this.props, [
140148
"getPosition",
141149
]);
150+
const tableOffsets = computeChunkOffsets(table.data);
142151

143152
const layers: ColumnLayer[] = [];
144153
for (
@@ -158,6 +167,7 @@ export class GeoArrowColumnLayer<
158167

159168
// @ts-expect-error used for picking purposes
160169
recordBatchIdx,
170+
tableOffsets,
161171

162172
id: `${this.props.id}-geoarrow-column-${recordBatchIdx}`,
163173
data: {

src/h3-hexagon-layer.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import type { H3HexagonLayerProps } from "@deck.gl/geo-layers/typed";
1111
import * as arrow from "apache-arrow";
1212
import { assignAccessor, extractAccessorsFromProps } from "./utils.js";
1313
import { GeoArrowPickingInfo } from "./types.js";
14-
import { getPickingInfo } from "./picking.js";
14+
import {
15+
GeoArrowExtraPickingProps,
16+
computeChunkOffsets,
17+
getPickingInfo,
18+
} from "./picking.js";
1519
import { validateAccessors } from "./validate.js";
1620

1721
/** All properties supported by GeoArrowH3HexagonLayer */
@@ -61,7 +65,11 @@ export class GeoArrowH3HexagonLayer<
6165
static defaultProps = defaultProps;
6266
static layerName = "GeoArrowH3HexagonLayer";
6367

64-
getPickingInfo(params: GetPickingInfoParams): GeoArrowPickingInfo {
68+
getPickingInfo(
69+
params: GetPickingInfoParams & {
70+
sourceLayer: { props: GeoArrowExtraPickingProps };
71+
},
72+
): GeoArrowPickingInfo {
6573
return getPickingInfo(params, this.props.data);
6674
}
6775

@@ -80,6 +88,7 @@ export class GeoArrowH3HexagonLayer<
8088
const [accessors, otherProps] = extractAccessorsFromProps(this.props, [
8189
"getHexagon",
8290
]);
91+
const tableOffsets = computeChunkOffsets(table.data);
8392

8493
const layers: H3HexagonLayer[] = [];
8594
for (
@@ -98,6 +107,7 @@ export class GeoArrowH3HexagonLayer<
98107

99108
// @ts-expect-error used for picking purposes
100109
recordBatchIdx,
110+
tableOffsets,
101111

102112
id: `${this.props.id}-geoarrow-arc-${recordBatchIdx}`,
103113

src/heatmap-layer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { FloatAccessor } from "./types.js";
1919
import { EXTENSION_NAME } from "./constants.js";
2020
import { validateAccessors } from "./validate.js";
21+
import { computeChunkOffsets } from "./picking.js";
2122

2223
/** All properties supported by GeoArrowHeatmapLayer */
2324
export type GeoArrowHeatmapLayerProps = Omit<
@@ -106,6 +107,7 @@ export class GeoArrowHeatmapLayer<
106107
const [accessors, otherProps] = extractAccessorsFromProps(this.props, [
107108
"getPosition",
108109
]);
110+
const tableOffsets = computeChunkOffsets(table.data);
109111

110112
const layers: HeatmapLayer[] = [];
111113
for (
@@ -125,6 +127,7 @@ export class GeoArrowHeatmapLayer<
125127

126128
// @ts-expect-error used for picking purposes
127129
recordBatchIdx,
130+
tableOffsets,
128131

129132
id: `${this.props.id}-geoarrow-heatmap-${recordBatchIdx}`,
130133
data: {

src/path-layer.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import {
1919
getMultiLineStringResolvedOffsets,
2020
invertOffsets,
2121
} from "./utils.js";
22-
import { getPickingInfo } from "./picking.js";
22+
import {
23+
GeoArrowExtraPickingProps,
24+
computeChunkOffsets,
25+
getPickingInfo,
26+
} from "./picking.js";
2327
import { ColorAccessor, FloatAccessor, GeoArrowPickingInfo } from "./types.js";
2428
import { EXTENSION_NAME } from "./constants.js";
2529
import { validateAccessors } from "./validate.js";
@@ -90,7 +94,11 @@ export class GeoArrowPathLayer<
9094
static defaultProps = defaultProps;
9195
static layerName = "GeoArrowPathLayer";
9296

93-
getPickingInfo(params: GetPickingInfoParams): GeoArrowPickingInfo {
97+
getPickingInfo(
98+
params: GetPickingInfoParams & {
99+
sourceLayer: { props: GeoArrowExtraPickingProps };
100+
},
101+
): GeoArrowPickingInfo {
94102
return getPickingInfo(params, this.props.data);
95103
}
96104

@@ -141,6 +149,7 @@ export class GeoArrowPathLayer<
141149
const [accessors, otherProps] = extractAccessorsFromProps(this.props, [
142150
"getPath",
143151
]);
152+
const tableOffsets = computeChunkOffsets(table.data);
144153

145154
const layers: PathLayer[] = [];
146155
for (
@@ -163,6 +172,7 @@ export class GeoArrowPathLayer<
163172

164173
// used for picking purposes
165174
recordBatchIdx,
175+
tableOffsets,
166176

167177
id: `${this.props.id}-geoarrow-path-${recordBatchIdx}`,
168178
data: {
@@ -208,6 +218,7 @@ export class GeoArrowPathLayer<
208218
const [accessors, otherProps] = extractAccessorsFromProps(this.props, [
209219
"getPath",
210220
]);
221+
const tableOffsets = computeChunkOffsets(table.data);
211222

212223
const layers: PathLayer[] = [];
213224
for (
@@ -237,6 +248,7 @@ export class GeoArrowPathLayer<
237248

238249
// used for picking purposes
239250
recordBatchIdx,
251+
tableOffsets,
240252
invertedGeomOffsets: invertOffsets(geomOffsets),
241253

242254
id: `${this.props.id}-geoarrow-path-${recordBatchIdx}`,

src/picking.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,40 @@ import * as arrow from "apache-arrow";
22
import { GetPickingInfoParams } from "@deck.gl/core/typed";
33
import { GeoArrowPickingInfo } from "./types";
44

5+
export interface GeoArrowExtraPickingProps {
6+
recordBatchIdx: number;
7+
tableOffsets: Uint32Array;
8+
invertedGeomOffsets?: Uint8Array | Uint16Array | Uint32Array;
9+
}
10+
511
export function getPickingInfo(
6-
{ info, sourceLayer }: GetPickingInfoParams,
12+
{
13+
info,
14+
sourceLayer,
15+
}: GetPickingInfoParams & {
16+
sourceLayer: { props: GeoArrowExtraPickingProps };
17+
},
718
table: arrow.Table,
819
): GeoArrowPickingInfo {
920
// Geometry index as rendered
1021
let index = info.index;
1122

12-
// if a MultiPoint dataset, map from the rendered index back to the feature
13-
// index
14-
// @ts-expect-error `invertedGeomOffsets` is manually set on layer props
23+
// if a Multi- geometry dataset, map from the rendered index back to the
24+
// feature index
1525
if (sourceLayer.props.invertedGeomOffsets) {
16-
// @ts-expect-error `invertedGeomOffsets` is manually set on layer props
1726
index = sourceLayer.props.invertedGeomOffsets[index];
1827
}
1928

20-
// @ts-expect-error `recordBatchIdx` is manually set on layer props
21-
const recordBatchIdx: number = sourceLayer.props.recordBatchIdx;
29+
const recordBatchIdx = sourceLayer.props.recordBatchIdx;
30+
const tableOffsets = sourceLayer.props.tableOffsets;
31+
2232
const batch = table.batches[recordBatchIdx];
2333
const row = batch.get(index);
2434
if (row === null) {
2535
return info;
2636
}
2737

28-
// @ts-expect-error hack: using private method to avoid recomputing via
29-
// batch lengths on each iteration
30-
const offsets: number[] = table._offsets;
31-
const currentBatchOffset = offsets[recordBatchIdx];
38+
const currentBatchOffset = tableOffsets[recordBatchIdx];
3239

3340
// Update index to be _global_ index, not within the specific record batch
3441
index += currentBatchOffset;
@@ -38,3 +45,16 @@ export function getPickingInfo(
3845
object: row,
3946
};
4047
}
48+
49+
// This is vendored from Arrow JS because it's a private API
50+
export function computeChunkOffsets<T extends arrow.DataType>(
51+
chunks: ReadonlyArray<arrow.Data<T>>,
52+
) {
53+
return chunks.reduce(
54+
(offsets, chunk, index) => {
55+
offsets[index + 1] = offsets[index] + chunk.length;
56+
return offsets;
57+
},
58+
new Uint32Array(chunks.length + 1),
59+
);
60+
}

src/scatterplot-layer.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {
1717
getGeometryVector,
1818
invertOffsets,
1919
} from "./utils.js";
20-
import { getPickingInfo } from "./picking.js";
20+
import {
21+
GeoArrowExtraPickingProps,
22+
computeChunkOffsets,
23+
getPickingInfo,
24+
} from "./picking.js";
2125
import { ColorAccessor, FloatAccessor, GeoArrowPickingInfo } from "./types.js";
2226
import { EXTENSION_NAME } from "./constants.js";
2327
import { validateAccessors } from "./validate.js";
@@ -91,7 +95,11 @@ export class GeoArrowScatterplotLayer<
9195
static defaultProps = defaultProps;
9296
static layerName = "GeoArrowScatterplotLayer";
9397

94-
getPickingInfo(params: GetPickingInfoParams): GeoArrowPickingInfo {
98+
getPickingInfo(
99+
params: GetPickingInfoParams & {
100+
sourceLayer: { props: GeoArrowExtraPickingProps };
101+
},
102+
): GeoArrowPickingInfo {
95103
return getPickingInfo(params, this.props.data);
96104
}
97105

@@ -137,6 +145,7 @@ export class GeoArrowScatterplotLayer<
137145
const [accessors, otherProps] = extractAccessorsFromProps(this.props, [
138146
"getPosition",
139147
]);
148+
const tableOffsets = computeChunkOffsets(table.data);
140149

141150
const layers: ScatterplotLayer[] = [];
142151
for (
@@ -156,6 +165,7 @@ export class GeoArrowScatterplotLayer<
156165

157166
// @ts-expect-error used for picking purposes
158167
recordBatchIdx,
168+
tableOffsets,
159169

160170
id: `${this.props.id}-geoarrow-scatterplot-${recordBatchIdx}`,
161171
data: {
@@ -201,6 +211,7 @@ export class GeoArrowScatterplotLayer<
201211
const [accessors, otherProps] = extractAccessorsFromProps(this.props, [
202212
"getPosition",
203213
]);
214+
const tableOffsets = computeChunkOffsets(table.data);
204215

205216
const layers: ScatterplotLayer[] = [];
206217
for (
@@ -222,6 +233,7 @@ export class GeoArrowScatterplotLayer<
222233

223234
// @ts-expect-error used for picking purposes
224235
recordBatchIdx,
236+
tableOffsets,
225237
invertedGeomOffsets: invertOffsets(geomOffsets),
226238

227239
id: `${this.props.id}-geoarrow-scatterplot-${recordBatchIdx}`,

0 commit comments

Comments
 (0)