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

[Chore]: Technical: Translate heatmap-layer #1773

Merged
merged 4 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/layers/base-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export type VisualChannel = {
// TODO: define fixed
fixed?: any;

supportedFieldTypes?: boolean;
supportedFieldTypes?: Array<keyof typeof ALL_FIELD_TYPES>;

aggregation?: 'colorAggregation' | 'sizeAggregation';
};
Expand Down Expand Up @@ -426,7 +426,7 @@ class Layer {

getDefaultLayerConfig(
props: Partial<LayerBaseConfig> = {}
): LayerBaseConfig & LayerColorConfig & LayerSizeConfig {
): LayerBaseConfig & Partial<LayerColorConfig & LayerSizeConfig> {
return {
dataId: props.dataId || null,
label: props.label || DEFAULT_LAYER_LABEL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,43 @@ import {createSelector} from 'reselect';
import memoize from 'lodash.memoize';
import {CHANNEL_SCALES, SCALE_FUNC, ALL_FIELD_TYPES} from 'constants/default-settings';
import {hexToRgb} from 'utils/color-utils';
import MapboxGLLayer from '../mapboxgl-layer';
import MapboxGLLayer, {MapboxLayerGLConfig} from '../mapboxgl-layer';
import HeatmapLayerIcon from './heatmap-layer-icon';
import {LayerColumn, LayerWeightConfig, VisualChannels} from '../base-layer';
import {DataContainerInterface} from 'utils/table-utils/data-container-interface';
import {VisConfigColorRange, VisConfigNumber} from '../layer-factory';
import {ColorRange} from 'constants/color-ranges';
import {HexColor, Merge} from 'reducers';

export type HeatmapLayerVisConfigSettings = {
opacity: VisConfigNumber;
colorRange: VisConfigColorRange;
radius: VisConfigNumber;
};

export type HeatmapLayerColumnsConfig = {lat: LayerColumn; lng: LayerColumn};

export type HeatmapLayerVisConfig = {
opacity: number;
colorRange: ColorRange;
radius: number;
};

export type HeatmapLayerVisualChannelConfig = LayerWeightConfig;
export type HeatmapLayerConfig = Merge<
MapboxLayerGLConfig,
{columns: HeatmapLayerColumnsConfig; visConfig: HeatmapLayerVisConfig}
> &
HeatmapLayerVisualChannelConfig;

export const MAX_ZOOM_LEVEL = 18;

export const pointPosAccessor = ({lat, lng}) => dc => d => [
dc.valueAt(d.index, lng.fieldIdx),
dc.valueAt(d.index, lat.fieldIdx)
];
export const pointPosAccessor = ({lat, lng}: HeatmapLayerColumnsConfig) => (
dc: DataContainerInterface
) => d => [dc.valueAt(d.index, lng.fieldIdx), dc.valueAt(d.index, lat.fieldIdx)];

export const pointColResolver = ({lat, lng}) => `${lat.fieldIdx}-${lng.fieldIdx}`;
export const pointColResolver = ({lat, lng}: HeatmapLayerColumnsConfig) =>
`${lat.fieldIdx}-${lng.fieldIdx}`;

export const heatmapVisConfigs = {
opacity: 'opacity',
Expand All @@ -42,8 +68,8 @@ export const heatmapVisConfigs = {

/**
*
* @param {Object} colorRange
* @return {Array} [
* @param colorRange
* @return [
* 0, "rgba(33,102,172,0)",
* 0.2, "rgb(103,169,207)",
* 0.4, "rgb(209,229,240)",
Expand All @@ -52,12 +78,12 @@ export const heatmapVisConfigs = {
* 1, "rgb(178,24,43)"
* ]
*/
const heatmapDensity = colorRange => {
const heatmapDensity = (colorRange: ColorRange): string[] => {
const scaleFunction = SCALE_FUNC.quantize;

const colors = ['#000000', ...colorRange.colors];
const colors: HexColor[] = ['#000000', ...colorRange.colors];

const scale = scaleFunction()
const scale = scaleFunction<HexColor>()
.domain([0, 1])
.range(colors);

Expand All @@ -74,18 +100,24 @@ const heatmapDensity = colorRange => {
};

class HeatmapLayer extends MapboxGLLayer {
declare visConfigSettings: HeatmapLayerVisConfigSettings;
declare config: HeatmapLayerConfig;

getPosition: (config: HeatmapLayerColumnsConfig) => any;

constructor(props) {
super(props);
this.registerVisConfig(heatmapVisConfigs);
this.getPosition = memoize(pointPosAccessor, pointColResolver);
}

get type() {
get type(): 'heatmap' {
return 'heatmap';
}

get visualChannels() {
get visualChannels(): VisualChannels {
return {
// @ts-expect-error
weight: {
property: 'weight',
field: 'weightField',
Expand Down Expand Up @@ -117,18 +149,16 @@ class HeatmapLayer extends MapboxGLLayer {
};
}

getDefaultLayerConfig(props = {}) {
getDefaultLayerConfig(props = {}): HeatmapLayerConfig {
// mapbox heatmap layer color is always based on density
// no need to set colorField, colorDomain and colorScale
/* eslint-disable no-unused-vars */
const {colorField, colorDomain, colorScale, ...layerConfig} = {
...super.getDefaultLayerConfig(props),

weightField: null,
weightDomain: [0, 1],
weightScale: 'linear'
};
/* eslint-enable no-unused-vars */

return layerConfig;
}
Expand Down