Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

feat(layers): fit map when the layer type dropdown sets the first type (#3795)

Igor Dykhta2b031d56de97
diff --git a/src/actions/src/vis-state-actions.ts b/src/actions/src/vis-state-actions.ts
index 317d39b40..946a07465 100644
--- a/src/actions/src/vis-state-actions.ts
+++ b/src/actions/src/vis-state-actions.ts
@@ -173,22 +173,30 @@ export function layerSetIsValid(
export type LayerTypeChangeUpdaterAction = {
oldLayer: Layer;
newType: string;
+ /**
+ * Fit the map when this is the layer type dropdown assigning a type to an
+ * empty layer. Omitted for programmatic calls and saved-config applies.
+ */
+ fitBounds?: boolean;
};
/**
* Update layer type. Previews layer config will be copied if applicable.
* @param oldLayer - layer to be updated
* @param newType - new type
+ * @param options.fitBounds fit the map when the layer type dropdown gives an empty layer its first type
* @returns action
* @public
*/
export function layerTypeChange(
oldLayer: Layer,
- newType: string
+ newType: string,
+ options?: {fitBounds?: boolean}
): Merge<LayerTypeChangeUpdaterAction, {type: typeof ActionTypes.LAYER_TYPE_CHANGE}> {
return {
type: ActionTypes.LAYER_TYPE_CHANGE,
oldLayer,
- newType
+ newType,
+ ...(options?.fitBounds ? {fitBounds: true} : {})
};
}
export type LayerVisualChannelConfigChangeUpdaterAction = {
diff --git a/src/components/src/side-panel/layer-panel/layer-panel.tsx b/src/components/src/side-panel/layer-panel/layer-panel.tsx
index ac2857bd2..76fbf48c7 100644
--- a/src/components/src/side-panel/layer-panel/layer-panel.tsx
+++ b/src/components/src/side-panel/layer-panel/layer-panel.tsx
@@ -83,7 +83,9 @@ function LayerPanelFactory(
};
updateLayerType = (newType: string) => {
- this.props.layerTypeChange(this.props.layer, newType);
+ // Only the layer type dropdown sets fitBounds. Programmatic
+ // layerTypeChange and applyLayerConfig leave the camera alone.
+ this.props.layerTypeChange(this.props.layer, newType, {fitBounds: true});
};
updateLayerVisConfig = (newVisConfig: Partial<LayerVisConfig>) => {
diff --git a/src/reducers/src/vis-state-updaters.ts b/src/reducers/src/vis-state-updaters.ts
index db4d526fe..663752f78 100644
--- a/src/reducers/src/vis-state-updaters.ts
+++ b/src/reducers/src/vis-state-updaters.ts
@@ -1047,6 +1047,36 @@ export function setInitialLayerConfig(layer, datasets, layerClasses): Layer {
? newLayer.setInitialLayerConfig(dataset)
: newLayer;
}
+/**
+ * Fit the map when the layer type dropdown gives an empty layer its first
+ * type and that layer already points at a dataset. Later type changes,
+ * programmatic `layerTypeChange`, and saved-config applies leave the camera
+ * alone. Gated by `enableFitBoundsOnManualLayer`.
+ */
+function maybeFitManualLayerBounds(
+ state: VisState,
+ oldLayer: Layer,
+ layer: Layer,
+ fitBounds?: boolean
+): VisState {
+ if (
+ !fitBounds ||
+ oldLayer.type ||
+ !layer.config.dataId ||
+ !getApplicationConfig().enableFitBoundsOnManualLayer
+ ) {
+ return state;
+ }
+ const bounds = findMapBounds([layer]);
+ if (!bounds) {
+ return state;
+ }
+ return withTask(
+ state,
+ ACTION_TASK_FIT_BOUNDS().map(() => fitMapBounds(bounds))
+ );
+}
+
/**
* Update layer type. Previews layer config will be copied if applicable.
* @memberof visStateUpdaters
@@ -1136,7 +1166,7 @@ export function layerTypeChangeUpdater(
layerOrder: replaceLayerEntryInLayerOrder(newState.layerOrder, oldLayer.id, newLayer.id)
};
- return newState;
+ return maybeFitManualLayerBounds(newState, oldLayer, layer, action.fitBounds);
}
/**
diff --git a/src/utils/src/application-config.ts b/src/utils/src/application-config.ts
index a251a14ee..3520e1062 100644
--- a/src/utils/src/application-config.ts
+++ b/src/utils/src/application-config.ts
@@ -206,6 +206,20 @@ export type KeplerApplicationConfig = {
/** Whether to show the map scale bar at the bottom-left of the map. Enabled by default. */
enableMapScale?: boolean;
+ /**
+ * Fit the map to a dataset when the layer type dropdown gives a manually
+ * added layer its first type. The layer already has a dataset (chosen from
+ * Add Layer). Programmatic `layerTypeChange`, saved configs, and later type
+ * changes do not move the map.
+ * Enabled by default. Disable with {@link initApplicationConfig}.
+ *
+ * @example
+ * ```
+ * initApplicationConfig({enableFitBoundsOnManualLayer: false});
+ * ```
+ */
+ enableFitBoundsOnManualLayer?: boolean;
+
/** Whether to enable the swipe compare mode in split map view. Enabled by default. */
enableSwipeMode?: boolean;
@@ -453,6 +467,8 @@ const DEFAULT_APPLICATION_CONFIG: Required<KeplerApplicationConfig> = {
enableMapScale: true,
+ enableFitBoundsOnManualLayer: true,
+
enableSwipeMode: true,
enableGlobeView: true,
diff --git a/test/node/reducers/vis-state-test.js b/test/node/reducers/vis-state-test.js
index ea581ac42..a4946c927 100644
--- a/test/node/reducers/vis-state-test.js
+++ b/test/node/reducers/vis-state-test.js
@@ -37,6 +37,7 @@ import {
getDefaultFilter,
histogramFromDomain,
LayerTimeInterval,
+ getApplicationConfig,
initApplicationConfig
} from '@kepler.gl/utils';
import {
@@ -450,6 +451,87 @@ test('#visStateReducer -> LAYER_TYPE_CHANGE.1', async t => {
t.end();
});
+test('#visStateReducer -> LAYER_TYPE_CHANGE fit bounds on manual layer', async t => {
+ const datasets = await createNewDataEntryMock({
+ info: {id: 'puppy', label: 'puppy'},
+ data: {
+ rows: mockData.data,
+ fields: mockData.fields
+ }
+ });
+
+ const makeEmptyLayerState = () => {
+ const layer = new Layer({id: 'manual-layer', dataId: 'puppy', isVisible: true});
+ return {
+ state: {
+ ...INITIAL_VIS_STATE,
+ datasets,
+ layers: [layer],
+ layerData: [{}],
+ layerOrder: [layer.id]
+ },
+ layer
+ };
+ };
+
+ drainTasksForTesting();
+ const previousFitBounds = getApplicationConfig().enableFitBoundsOnManualLayer;
+
+ try {
+ initApplicationConfig({enableFitBoundsOnManualLayer: false});
+ const disabled = makeEmptyLayerState();
+ reducer(
+ disabled.state,
+ VisStateActions.layerTypeChange(disabled.layer, 'point', {fitBounds: true})
+ );
+ t.equal(
+ drainTasksForTesting().length,
+ 0,
+ 'should not fit bounds when enableFitBoundsOnManualLayer is off'
+ );
+
+ initApplicationConfig({enableFitBoundsOnManualLayer: true});
+ const programmatic = makeEmptyLayerState();
+ reducer(programmatic.state, VisStateActions.layerTypeChange(programmatic.layer, 'point'));
+ t.equal(
+ drainTasksForTesting().length,
+ 0,
+ 'should not fit bounds for a programmatic layerTypeChange'
+ );
+
+ const enabled = makeEmptyLayerState();
+ const nextState = reducer(
+ enabled.state,
+ VisStateActions.layerTypeChange(enabled.layer, 'point', {fitBounds: true})
+ );
+ const [fitTask, ...extra] = drainTasksForTesting();
+ t.equal(extra.length, 0, 'should schedule one fit-bounds task');
+ t.equal(fitTask.label, 'ACTION_TASK_FIT_BOUNDS', 'should schedule ACTION_TASK_FIT_BOUNDS');
+
+ const fitAction = succeedTaskInTest(fitTask, null);
+ t.equal(fitAction.type, '@@kepler.gl/FIT_BOUNDS', 'task should dispatch FIT_BOUNDS');
+ t.deepEqual(
+ fitAction.payload,
+ nextState.layers[0].meta.bounds,
+ 'should fit to the new layer bounds'
+ );
+ t.ok(nextState.layers[0].meta.bounds, 'new layer should have bounds');
+
+ const typedLayer = nextState.layers[0];
+ reducer(nextState, VisStateActions.layerTypeChange(typedLayer, 'hexagon', {fitBounds: true}));
+ t.equal(
+ drainTasksForTesting().length,
+ 0,
+ 'should not fit bounds when changing the type of an existing layer'
+ );
+ } finally {
+ initApplicationConfig({enableFitBoundsOnManualLayer: previousFitBounds});
+ drainTasksForTesting();
+ }
+
+ t.end();
+});
+
test('#visStateReducer -> LAYER_TYPE_CHANGE.2', async t => {
const pointLayer = new PointLayer({id: 'a', dataId: 'smoothie'});
const mockColorRange = {