Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#123): added-iconlayer-support #129

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

209 changes: 209 additions & 0 deletions src/icon-layer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import {
CompositeLayer,
CompositeLayerProps,
DefaultProps,
GetPickingInfoParams,
Layer,
LayersList,
assert,
} from "@deck.gl/core";
import { IconLayer } from "@deck.gl/layers";
import type { IconLayerProps } from "@deck.gl/layers";
import * as arrow from "apache-arrow";
import * as ga from "@geoarrow/geoarrow-js";
import {
assignAccessor,
extractAccessorsFromProps,
getGeometryVector,
invertOffsets,
} from "./utils.js";
import {
GeoArrowExtraPickingProps,
computeChunkOffsets,
getPickingInfo,
} from "./picking.js";
import { ColorAccessor, FloatAccessor, GeoArrowPickingInfo } from "./types.js";
import { EXTENSION_NAME } from "./constants.js";
import { validateAccessors } from "./validate.js";

/** All properties supported by GeoArrowScatterplotLayer */
export type GeoArrowIconLayerProps = Omit<
IconLayerProps<arrow.Table>,
| "data"
| "getPosition"
| "getRadius"
| "getFillColor"
| "getLineColor"
Comment on lines +34 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't exist on IconLayerProps

| "iconAtlas"
| "iconMapping"
Comment on lines +37 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want to omit the properties that we redefine. It doesn't look like we redefine these, so you don't want to remove these from the type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said we do want to redefine any accessors. So we would usually want to add getIcon, getSize, getColor, getAngle, getPixelOffset. We need to check that deck works with a vectorized getIcon function though. No other layers have a string return type from a get* accessor

> &
_GeoArrowIconLayerPropsProps &
CompositeLayerProps;

/** Properties added by GeoArrowScatterplotLayer */
type _GeoArrowIconLayerPropsProps = {
data: arrow.Table;

/**
* If `true`, validate the arrays provided (e.g. chunk lengths)
* @default true
*/
_validate?: boolean;
/**
* Center position accessor.
* If not provided, will be inferred by finding a column with extension type
* `"geoarrow.point"` or `"geoarrow.multipoint"`.
*/
getPosition?: ga.vector.PointVector | ga.vector.MultiPointVector;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, you should remove | ga.vector.MultiPointVector since this layer as written only supports points

/**
* Radius accessor.
* @default 1
*/
getRadius?: FloatAccessor;
/**
* Fill color accessor.
* @default [0, 0, 0, 255]
*/
getFillColor?: ColorAccessor;
/**
* Stroke color accessor.
* @default [0, 0, 0, 255]
*/
getLineColor?: ColorAccessor;
/**
* Stroke width accessor.
* @default 1
*/
getLineWidth?: FloatAccessor;
Comment on lines +58 to +77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't make sense for IconLayer

};

// Remove data and getPosition from the upstream default props
const {
data: _data,
getPosition: _getPosition,
..._upstreamDefaultProps
} = IconLayer.defaultProps;

// Default props added by us
const ourDefaultProps = {
_validate: true,
};

// @ts-expect-error Type error in merging default props with ours
const defaultProps: DefaultProps<GeoArrowIconLayerProps> = {
..._upstreamDefaultProps,
...ourDefaultProps,
};

export class GeoArrowIconLayer<
ExtraProps extends {} = {},
> extends CompositeLayer<GeoArrowIconLayerProps & ExtraProps> {
static defaultProps = defaultProps;
static layerName = "GeoArrowIconLayerProps";

getPickingInfo(
params: GetPickingInfoParams & {
sourceLayer: { props: GeoArrowExtraPickingProps };
},
): GeoArrowPickingInfo {
return getPickingInfo(params, this.props.data);
}

renderLayers(): Layer<{}> | LayersList | null {
const { data: table } = this.props;

if (this.props.getPosition !== undefined) {
const geometryColumn = this.props.getPosition;
if (
geometryColumn !== undefined &&
ga.vector.isPointVector(geometryColumn)
) {
return this._renderLayersPoint(geometryColumn);
}

throw new Error("getPosition should pass in an arrow Vector of Point");
} else {
const pointVector = getGeometryVector(table, EXTENSION_NAME.POINT);
if (pointVector !== null) {
return this._renderLayersPoint(pointVector);
}
}

throw new Error("getPosition not GeoArrow point");
}

_renderLayersPoint(
geometryColumn: ga.vector.PointVector,
): Layer<{}> | LayersList | null {
const { data: table } = this.props;

if (this.props._validate) {
assert(ga.vector.isPointVector(geometryColumn));
validateAccessors(this.props, table);
}

// Exclude manually-set accessors
const [accessors, otherProps] = extractAccessorsFromProps(this.props, [
"getPosition",
]);
const tableOffsets = computeChunkOffsets(table.data);

const layers: IconLayer[] = [];
for (
let recordBatchIdx = 0;
recordBatchIdx < table.batches.length;
recordBatchIdx++
) {
const geometryData = geometryColumn.data[recordBatchIdx];
const flatCoordsData = ga.child.getPointChild(geometryData);
const flatCoordinateArray = flatCoordsData.values;

const props: IconLayerProps = {
// Note: because this is a composite layer and not doing the rendering
// itself, we still have to pass in our defaultProps
...ourDefaultProps,
...otherProps,

// used for picking purposes
recordBatchIdx,
tableOffsets,

id: `${this.props.id}-geoarrow-iconlayer-${recordBatchIdx}`,
data: {
// @ts-expect-error passed through to enable use by function accessors
data: table.batches[recordBatchIdx],
length: geometryData.length,
attributes: {
getPosition: {
value: flatCoordinateArray,
size: geometryData.type.listSize,
},
},
},
};

for (const [propName, propInput] of Object.entries(accessors)) {
assignAccessor({
props,
propName,
propInput,
chunkIdx: recordBatchIdx,
});
}

// @ts-expect-error iconAtlas is an async prop
const iconAtlas = this.props.iconAtlasConfig;
// @ts-expect-error iconMapping is an async prop
const iconMapping = this.props.iconMapping;
const props_ = {
...this.getSubLayerProps(props),
iconAtlas,
iconMapping,
};
const layer = new IconLayer(props_);
layers.push(layer);
}

return layers;
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { GeoArrowScatterplotLayer } from "./scatterplot-layer.js";
export { GeoArrowSolidPolygonLayer } from "./solid-polygon-layer.js";
export { GeoArrowTextLayer as _GeoArrowTextLayer } from "./text-layer.js";
export { GeoArrowTripsLayer } from "./trips-layer.js";
export { GeoArrowIconLayer } from "./icon-layer.js";

export type { GeoArrowArcLayerProps } from "./arc-layer.js";
export type { GeoArrowColumnLayerProps } from "./column-layer.js";
Expand All @@ -21,5 +22,6 @@ export type { GeoArrowScatterplotLayerProps } from "./scatterplot-layer.js";
export type { GeoArrowSolidPolygonLayerProps } from "./solid-polygon-layer.js";
export type { GeoArrowTextLayerProps as _GeoArrowTextLayerProps } from "./text-layer.js";
export type { GeoArrowTripsLayerProps } from "./trips-layer.js";
export type { GeoArrowIconLayerProps } from "./icon-layer.js";

export type { GeoArrowPickingInfo } from "./types.js";
Loading