Skip to content

Commit 95d2850

Browse files
authored
feat: Add simple wrappings of geohash and s2 layers (#168)
* feat: Add geohash and s2 layers * bump version
1 parent dcbbd85 commit 95d2850

File tree

5 files changed

+322
-2
lines changed

5 files changed

+322
-2
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/deck.gl-layers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@geoarrow/deck.gl-layers",
3-
"version": "0.4.0-beta.5",
3+
"version": "0.4.0-beta.6",
44
"type": "module",
55
"description": "",
66
"source": "src/index.ts",

packages/deck.gl-layers/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export { GeoArrowArcLayer } from "./layers/arc-layer.js";
1111
export type { GeoArrowColumnLayerProps } from "./layers/column-layer.js";
1212
export { GeoArrowColumnLayer } from "./layers/column-layer.js";
1313

14+
export type { GeoArrowGeohashLayerProps } from "./layers/geohash-layer.js";
15+
export { GeoArrowGeohashLayer } from "./layers/geohash-layer.js";
16+
1417
export type { GeoArrowH3HexagonLayerProps } from "./layers/h3-hexagon-layer.js";
1518
export { GeoArrowH3HexagonLayer } from "./layers/h3-hexagon-layer.js";
1619

@@ -26,6 +29,9 @@ export { GeoArrowPointCloudLayer } from "./layers/point-cloud-layer.js";
2629
export type { GeoArrowPolygonLayerProps } from "./layers/polygon-layer.js";
2730
export { GeoArrowPolygonLayer } from "./layers/polygon-layer.js";
2831

32+
export type { GeoArrowS2LayerProps } from "./layers/s2-layer.js";
33+
export { GeoArrowS2Layer } from "./layers/s2-layer.js";
34+
2935
export type { GeoArrowScatterplotLayerProps } from "./layers/scatterplot-layer.js";
3036
export { GeoArrowScatterplotLayer } from "./layers/scatterplot-layer.js";
3137

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import {
2+
CompositeLayer,
3+
CompositeLayerProps,
4+
DefaultProps,
5+
GetPickingInfoParams,
6+
Layer,
7+
LayersList,
8+
} from "@deck.gl/core";
9+
import type { GeohashLayerProps } from "@deck.gl/geo-layers";
10+
import { GeohashLayer } from "@deck.gl/geo-layers";
11+
import * as arrow from "apache-arrow";
12+
13+
import { ColorAccessor, FloatAccessor, GeoArrowPickingInfo } from "../types";
14+
import { assignAccessor, extractAccessorsFromProps } from "../utils/utils";
15+
import { GeoArrowExtraPickingProps, getPickingInfo } from "../utils/picking";
16+
import { validateAccessors } from "../utils/validate";
17+
18+
/** All properties supported by GeoArrowGeohashLayer */
19+
export type GeoArrowGeohashLayerProps = Omit<
20+
GeohashLayerProps,
21+
| "data"
22+
| "getGeohash"
23+
| "getFillColor"
24+
| "getLineColor"
25+
| "getLineWidth"
26+
| "getElevation"
27+
> &
28+
_GeoArrowGeohashLayerProps &
29+
// Omit<GeoArrowPolygonLayerProps, "getPolygon"> &
30+
CompositeLayerProps;
31+
32+
/** Props added by the GeoArrowGeohashLayer */
33+
type _GeoArrowGeohashLayerProps = {
34+
data: arrow.RecordBatch;
35+
36+
/**
37+
* Called for each data object to retrieve the quadkey string identifier.
38+
*/
39+
getGeohash: arrow.Data<arrow.Utf8>;
40+
41+
/** Fill color accessor.
42+
* @default [0, 0, 0, 255]
43+
*/
44+
getFillColor?: ColorAccessor;
45+
46+
/** Stroke color accessor.
47+
* @default [0, 0, 0, 255]
48+
*/
49+
getLineColor?: ColorAccessor;
50+
51+
/**
52+
* Line width value of accessor.
53+
* @default 1
54+
*/
55+
getLineWidth?: FloatAccessor;
56+
57+
/**
58+
* Elevation value of accessor.
59+
*
60+
* Only used if `extruded: true`.
61+
*
62+
* @default 1000
63+
*/
64+
getElevation?: FloatAccessor;
65+
66+
/**
67+
* If `true`, validate the arrays provided (e.g. chunk lengths)
68+
* @default true
69+
*/
70+
_validate?: boolean;
71+
};
72+
73+
// Remove data from the upstream default props
74+
const {
75+
// @ts-expect-error https://github.com/visgl/deck.gl/issues/9854
76+
data: _data,
77+
getGeohash: _getGeohash,
78+
..._defaultProps
79+
} = GeohashLayer.defaultProps;
80+
81+
// Default props added by us
82+
const ourDefaultProps = {
83+
_validate: true,
84+
};
85+
86+
// Type errors from https://github.com/visgl/deck.gl/issues/9854
87+
// // @ts-expect-error getFillColor
88+
const defaultProps: DefaultProps<GeoArrowGeohashLayerProps> = {
89+
// ..._polygonDefaultProps,
90+
..._defaultProps,
91+
...ourDefaultProps,
92+
};
93+
94+
export class GeoArrowGeohashLayer<
95+
ExtraProps extends {} = {},
96+
> extends CompositeLayer<GeoArrowGeohashLayerProps & ExtraProps> {
97+
static defaultProps = defaultProps;
98+
static layerName = "GeoArrowGeohashLayer";
99+
100+
getPickingInfo(
101+
params: GetPickingInfoParams & {
102+
sourceLayer: { props: GeoArrowExtraPickingProps };
103+
},
104+
): GeoArrowPickingInfo {
105+
return getPickingInfo(params, this.props.data);
106+
}
107+
108+
renderLayers(): Layer<{}> | LayersList | null {
109+
return this._renderLayer();
110+
}
111+
112+
_renderLayer(): Layer<{}> | LayersList | null {
113+
const { data: batch, getGeohash } = this.props;
114+
115+
if (this.props._validate) {
116+
validateAccessors(this.props, batch);
117+
}
118+
119+
// Exclude manually-set accessors
120+
const [accessors, otherProps] = extractAccessorsFromProps(this.props, [
121+
"getGeohash",
122+
]);
123+
const GeohashVector = new arrow.Vector([getGeohash]);
124+
125+
const props: GeohashLayerProps = {
126+
// Note: because this is a composite layer and not doing the rendering
127+
// itself, we still have to pass in our defaultProps
128+
...ourDefaultProps,
129+
...otherProps,
130+
131+
// type errors from https://github.com/visgl/deck.gl/issues/9854
132+
// @ts-expect-error until above issue fixed.
133+
id: `${this.props.id}-geoarrow-geohash`,
134+
135+
data: {
136+
// type errors from https://github.com/visgl/deck.gl/issues/9854
137+
// // @ts-expect-error passed through to enable use by function accessors
138+
data: batch,
139+
length: batch.numRows,
140+
},
141+
// We must load back to pure JS strings / bigint
142+
getGeohash: (_, objectInfo) => {
143+
return GeohashVector.get(objectInfo.index)!;
144+
},
145+
};
146+
147+
for (const [propName, propInput] of Object.entries(accessors)) {
148+
assignAccessor({
149+
props,
150+
propName,
151+
propInput,
152+
});
153+
}
154+
155+
return new GeohashLayer(this.getSubLayerProps(props));
156+
}
157+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import {
2+
CompositeLayer,
3+
CompositeLayerProps,
4+
DefaultProps,
5+
GetPickingInfoParams,
6+
Layer,
7+
LayersList,
8+
} from "@deck.gl/core";
9+
import type { S2LayerProps } from "@deck.gl/geo-layers";
10+
import { S2Layer } from "@deck.gl/geo-layers";
11+
import * as arrow from "apache-arrow";
12+
13+
import { ColorAccessor, FloatAccessor, GeoArrowPickingInfo } from "../types";
14+
import { assignAccessor, extractAccessorsFromProps } from "../utils/utils";
15+
import { GeoArrowExtraPickingProps, getPickingInfo } from "../utils/picking";
16+
import { validateAccessors } from "../utils/validate";
17+
18+
/** All properties supported by GeoArrowS2Layer */
19+
export type GeoArrowS2LayerProps = Omit<
20+
S2LayerProps,
21+
| "data"
22+
| "getS2Token"
23+
| "getFillColor"
24+
| "getLineColor"
25+
| "getLineWidth"
26+
| "getElevation"
27+
> &
28+
_GeoArrowS2LayerProps &
29+
// Omit<GeoArrowPolygonLayerProps, "getPolygon"> &
30+
CompositeLayerProps;
31+
32+
/** Props added by the GeoArrowS2Layer */
33+
type _GeoArrowS2LayerProps = {
34+
data: arrow.RecordBatch;
35+
36+
/**
37+
* Called for each data object to retrieve the quadkey string identifier.
38+
*/
39+
getS2Token: arrow.Data<arrow.Utf8 | arrow.Uint64>;
40+
41+
/** Fill color accessor.
42+
* @default [0, 0, 0, 255]
43+
*/
44+
getFillColor?: ColorAccessor;
45+
46+
/** Stroke color accessor.
47+
* @default [0, 0, 0, 255]
48+
*/
49+
getLineColor?: ColorAccessor;
50+
51+
/**
52+
* Line width value of accessor.
53+
* @default 1
54+
*/
55+
getLineWidth?: FloatAccessor;
56+
57+
/**
58+
* Elevation value of accessor.
59+
*
60+
* Only used if `extruded: true`.
61+
*
62+
* @default 1000
63+
*/
64+
getElevation?: FloatAccessor;
65+
66+
/**
67+
* If `true`, validate the arrays provided (e.g. chunk lengths)
68+
* @default true
69+
*/
70+
_validate?: boolean;
71+
};
72+
73+
// Remove data from the upstream default props
74+
const {
75+
data: _data,
76+
getS2Token: _getPentagon,
77+
..._defaultProps
78+
} = S2Layer.defaultProps;
79+
80+
// Default props added by us
81+
const ourDefaultProps = {
82+
_validate: true,
83+
};
84+
85+
// @ts-expect-error getFillColor
86+
const defaultProps: DefaultProps<GeoArrowS2LayerProps> = {
87+
// ..._polygonDefaultProps,
88+
..._defaultProps,
89+
...ourDefaultProps,
90+
};
91+
92+
export class GeoArrowS2Layer<ExtraProps extends {} = {}> extends CompositeLayer<
93+
GeoArrowS2LayerProps & ExtraProps
94+
> {
95+
static defaultProps = defaultProps;
96+
static layerName = "GeoArrowS2Layer";
97+
98+
getPickingInfo(
99+
params: GetPickingInfoParams & {
100+
sourceLayer: { props: GeoArrowExtraPickingProps };
101+
},
102+
): GeoArrowPickingInfo {
103+
return getPickingInfo(params, this.props.data);
104+
}
105+
106+
renderLayers(): Layer<{}> | LayersList | null {
107+
return this._renderLayer();
108+
}
109+
110+
_renderLayer(): Layer<{}> | LayersList | null {
111+
const { data: batch, getS2Token } = this.props;
112+
113+
if (this.props._validate) {
114+
validateAccessors(this.props, batch);
115+
}
116+
117+
// Exclude manually-set accessors
118+
const [accessors, otherProps] = extractAccessorsFromProps(this.props, [
119+
"getS2Token",
120+
]);
121+
const s2Vector = new arrow.Vector([getS2Token]);
122+
123+
const props: S2LayerProps = {
124+
// Note: because this is a composite layer and not doing the rendering
125+
// itself, we still have to pass in our defaultProps
126+
...ourDefaultProps,
127+
...otherProps,
128+
129+
id: `${this.props.id}-geoarrow-S2`,
130+
131+
data: {
132+
// @ts-expect-error passed through to enable use by function accessors
133+
data: batch,
134+
length: batch.numRows,
135+
},
136+
// We must load back to pure JS strings / bigint
137+
getS2Token: (_, objectInfo) => {
138+
const value = s2Vector.get(objectInfo.index)!;
139+
if (typeof value === "string") {
140+
return value;
141+
} else {
142+
return value.toString(16);
143+
}
144+
},
145+
};
146+
147+
for (const [propName, propInput] of Object.entries(accessors)) {
148+
assignAccessor({
149+
props,
150+
propName,
151+
propInput,
152+
});
153+
}
154+
155+
return new S2Layer(this.getSubLayerProps(props));
156+
}
157+
}

0 commit comments

Comments
 (0)