Skip to content

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

feat: add a center handle to move bitmap layer bounds (#3756)

Igor Dykhta019a8b34c962
diff --git a/src/layers/src/bitmap-layer/bitmap-bounds-edit-mode.ts b/src/layers/src/bitmap-layer/bitmap-bounds-edit-mode.ts
new file mode 100644
index 000000000..48c704109
--- /dev/null
+++ b/src/layers/src/bitmap-layer/bitmap-bounds-edit-mode.ts
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import {
+ CompositeMode,
+ GeoJsonEditMode,
+ ModifyMode,
+ TranslateMode
+} from '@deck.gl-community/editable-layers';
+
+// Use the library's non-vertex handle type so ModifyMode does not treat this as a corner.
+// ('move' is not in EditHandleType: existing | intermediate | snap-* | scale | rotate)
+export const BITMAP_MOVE_HANDLE_TYPE = 'scale';
+
+type PickLike = {isGuide?: boolean; object?: any};
+
+function isMoveHandlePick(picks?: PickLike[]): boolean {
+ return Boolean(
+ picks?.some(
+ pick =>
+ pick?.isGuide &&
+ pick?.object?.properties?.guideType === 'editHandle' &&
+ pick?.object?.properties?.editHandleType === BITMAP_MOVE_HANDLE_TYPE
+ )
+ );
+}
+
+/**
+ * Only the center handle may translate the rectangle. Dragging the bitmap
+ * itself (or a corner) must not move it, so the map can still pan.
+ */
+class BitmapTranslateMode extends TranslateMode {
+ handlePointerMove(event, props) {
+ this._isTranslatable = isMoveHandlePick(event?.pointerDownPicks || event?.picks);
+ this.updateCursor(props);
+ }
+
+ handleStartDragging(event, props) {
+ this._isTranslatable = isMoveHandlePick(event?.picks);
+ super.handleStartDragging(event, props);
+ }
+
+ handleDragging(event, props) {
+ this._isTranslatable = isMoveHandlePick(event?.pointerDownPicks);
+ super.handleDragging(event, props);
+ }
+}
+
+/**
+ * Corner resize must not treat the center move handle as a vertex.
+ * Skip modify when that handle is the drag target so TranslateMode can
+ * move the whole rectangle.
+ */
+class BitmapModifyMode extends ModifyMode {
+ handlePointerMove(event, props) {
+ if (isMoveHandlePick(event?.picks)) {
+ props.onUpdateCursor('move');
+ return;
+ }
+ super.handlePointerMove(event, props);
+ }
+
+ handleStartDragging(event, props) {
+ if (isMoveHandlePick(event?.picks)) {
+ return;
+ }
+ super.handleStartDragging(event, props);
+ }
+
+ handleDragging(event, props) {
+ if (isMoveHandlePick(event?.pointerDownPicks)) {
+ return;
+ }
+ super.handleDragging(event, props);
+ }
+
+ handleStopDragging(event, props) {
+ if (isMoveHandlePick(event?.pointerDownPicks)) {
+ return;
+ }
+ super.handleStopDragging(event, props);
+ }
+}
+
+/**
+ * Rectangle edit mode: yellow corners resize, center handle moves the bitmap.
+ */
+export class BitmapBoundsEditMode extends CompositeMode {
+ constructor() {
+ super([
+ new BitmapTranslateMode() as unknown as GeoJsonEditMode,
+ new BitmapModifyMode() as unknown as GeoJsonEditMode
+ ]);
+ }
+
+ getGuides(props) {
+ const guides = super.getGuides(props);
+ const selectedIndex = props.selectedIndexes?.[0];
+ const feature =
+ typeof selectedIndex === 'number' ? props.data?.features?.[selectedIndex] : null;
+ const coords = feature?.geometry?.coordinates?.[0];
+ if (!coords || coords.length < 4) {
+ return guides;
+ }
+
+ // Closing ring repeats the first vertex; exclude it from the centroid.
+ const first = coords[0];
+ const last = coords[coords.length - 1];
+ const pts = first[0] === last[0] && first[1] === last[1] ? coords.slice(0, -1) : coords;
+ const lng = pts.reduce((sum, point) => sum + point[0], 0) / pts.length;
+ const lat = pts.reduce((sum, point) => sum + point[1], 0) / pts.length;
+
+ guides.features.push({
+ type: 'Feature',
+ properties: {
+ guideType: 'editHandle',
+ editHandleType: BITMAP_MOVE_HANDLE_TYPE,
+ featureIndex: selectedIndex
+ },
+ geometry: {
+ type: 'Point',
+ coordinates: [lng, lat]
+ }
+ });
+
+ return guides;
+ }
+}
+
+export const BITMAP_BOUNDS_EDIT_MODE = new BitmapBoundsEditMode();
diff --git a/src/layers/src/bitmap-layer/bitmap-layer.ts b/src/layers/src/bitmap-layer/bitmap-layer.ts
index dc3e5f8c5..bb63a706a 100644
--- a/src/layers/src/bitmap-layer/bitmap-layer.ts
+++ b/src/layers/src/bitmap-layer/bitmap-layer.ts
@@ -1,17 +1,17 @@
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
-import {BitmapLayer as DeckBitmapLayer, PathLayer, ScatterplotLayer} from '@deck.gl/layers';
import {
- EditableGeoJsonLayer,
- ModifyMode,
- TranslateMode,
- CompositeMode,
- GeoJsonEditMode
-} from '@deck.gl-community/editable-layers';
+ BitmapLayer as DeckBitmapLayer,
+ IconLayer,
+ PathLayer,
+ ScatterplotLayer
+} from '@deck.gl/layers';
+import {EditableGeoJsonLayer} from '@deck.gl-community/editable-layers';
import Layer from '../base-layer';
import BitmapLayerIcon from './bitmap-layer-icon';
+import {BITMAP_BOUNDS_EDIT_MODE, BITMAP_MOVE_HANDLE_TYPE} from './bitmap-bounds-edit-mode';
import {FindDefaultLayerPropsReturnValue} from '../layer-utils';
import {
LAYER_VIS_CONFIGS,
@@ -23,10 +23,21 @@ import {
import {KeplerTable as KeplerDataset, Datasets as KeplerDatasets} from '@kepler.gl/table';
import {VisConfigNumber, VisConfigBoolean} from '@kepler.gl/types';
-const EDIT_MODE = new CompositeMode([
- new TranslateMode() as unknown as GeoJsonEditMode,
- new ModifyMode() as unknown as GeoJsonEditMode
-]);
+const CORNER_HANDLE_COLOR: [number, number, number, number] = [255, 200, 0, 255];
+const MOVE_HANDLE_COLOR: [number, number, number, number] = [38, 181, 242, 255];
+const MOVE_HANDLE_RADIUS = 10;
+const CORNER_HANDLE_RADIUS = 6;
+
+const MOVE_HANDLE_ICON_URL = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(
+ `<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">
+ <g fill="#fff" stroke="rgba(0,0,0,0.25)" stroke-width="1.5" stroke-linejoin="round">
+ <path d="M32 6l8 12h-5v10h-6V18h-5z"/>
+ <path d="M58 32l-12 8v-5H36v-6h10v-5z"/>
+ <path d="M32 58l-8-12h5V36h6v10h5z"/>
+ <path d="M6 32l12-8v5h10v6H18v5z"/>
+ </g>
+ </svg>`
+)}`;
export type BitmapLayerVisConfigSettings = {
opacity: VisConfigNumber;
@@ -503,7 +514,7 @@ export default class BitmapOverlayLayer extends Layer {
id: `${this.id}-edit`,
// @ts-ignore
data: this._editFeatureCollection,
- mode: EDIT_MODE,
+ mode: BITMAP_BOUNDS_EDIT_MODE,
selectedFeatureIndexes: [0],
pickable: true,
pickingRadius: 12,
@@ -516,9 +527,16 @@ export default class BitmapOverlayLayer extends Layer {
getLineColor: [255, 255, 255, 200],
getLineWidth: 2,
lineWidthUnits: 'pixels',
- getEditHandlePointColor: [255, 200, 0, 255],
- getEditHandlePointRadius: 6,
+ getEditHandlePointColor: (handle: {properties?: {editHandleType?: string}}) =>
+ handle?.properties?.editHandleType === BITMAP_MOVE_HANDLE_TYPE
+ ? MOVE_HANDLE_COLOR
+ : CORNER_HANDLE_COLOR,
+ getEditHandlePointRadius: (handle: {properties?: {editHandleType?: string}}) =>
+ handle?.properties?.editHandleType === BITMAP_MOVE_HANDLE_TYPE
+ ? MOVE_HANDLE_RADIUS
+ : CORNER_HANDLE_RADIUS,
editHandlePointRadiusUnits: 'pixels',
+ editHandlePointRadiusMaxPixels: 12,
onEdit: ({updatedData, editType}) => {
this._editFeatureCollection = updatedData;
@@ -557,6 +575,26 @@ export default class BitmapOverlayLayer extends Layer {
}
})
);
+
+ const [aW, aS, aE, aN] = activeBounds;
+ layers.push(
+ new IconLayer({
+ id: `${this.id}-move-handle`,
+ data: [{position: [(aW + aE) / 2, (aS + aN) / 2]}],
+ getPosition: (d: {position: [number, number]}) => d.position,
+ getIcon: () => ({
+ url: MOVE_HANDLE_ICON_URL,
+ width: 64,
+ height: 64,
+ anchorX: 32,
+ anchorY: 32
+ }),
+ getSize: 16,
+ sizeUnits: 'pixels',
+ pickable: false,
+ visible
+ })
+ );
}
return layers;
diff --git a/src/layers/src/bitmap-layer/index.ts b/src/layers/src/bitmap-layer/index.ts
index 40e2c5f1c..6f3ff1f58 100644
--- a/src/layers/src/bitmap-layer/index.ts
+++ b/src/layers/src/bitmap-layer/index.ts
@@ -3,3 +3,4 @@
export {default} from './bitmap-layer';
export {default as BitmapLayerIcon} from './bitmap-layer-icon';
+export {BitmapBoundsEditMode, BITMAP_MOVE_HANDLE_TYPE} from './bitmap-bounds-edit-mode';
diff --git a/src/layers/src/index.ts b/src/layers/src/index.ts
index 5092d7155..8426b3a82 100644
--- a/src/layers/src/index.ts
+++ b/src/layers/src/index.ts
@@ -73,6 +73,10 @@ export {default as Tile3DLayerIcon} from './tile3d-layer/tile3d-layer-icon';
import {default as BitmapOverlayLayer} from './bitmap-layer/bitmap-layer';
export {default as BitmapOverlayLayer} from './bitmap-layer/bitmap-layer';
export {default as BitmapLayerIcon} from './bitmap-layer/bitmap-layer-icon';
+export {
+ BitmapBoundsEditMode,
+ BITMAP_MOVE_HANDLE_TYPE
+} from './bitmap-layer/bitmap-bounds-edit-mode';
export {
CATEGORICAL_COLORMAP_ID,
DATA_SOURCE_COLOR_DEFAULTS,
diff --git a/src/localization/src/translations/en.ts b/src/localization/src/translations/en.ts
index d4005b09f..7b503726f 100644
--- a/src/localization/src/translations/en.ts
+++ b/src/localization/src/translations/en.ts
@@ -172,7 +172,7 @@ export default {
opacity: 'Opacity',
imageUrl: 'Image URL',
showBounds: 'Show Bounds',
- editBounds: 'Drag corners to resize',
+ editBounds: 'Drag corners to resize, center to move',
alignMode: 'Align to map',
boundsWest: 'West',
boundsSouth: 'South',
diff --git a/test/browser/layer-tests/bitmap-layer-specs.js b/test/browser/layer-tests/bitmap-layer-specs.js
index 93253bd52..445e9f2ca 100644
--- a/test/browser/layer-tests/bitmap-layer-specs.js
+++ b/test/browser/layer-tests/bitmap-layer-specs.js
@@ -5,6 +5,10 @@ import test from 'tape-catch';
import {KeplerGlLayers} from '@kepler.gl/layers';
import {DatasetType} from '@kepler.gl/constants';
import {testCreateCases} from 'test/helpers/layer-utils';
+import {
+ BitmapBoundsEditMode,
+ BITMAP_MOVE_HANDLE_TYPE
+} from '../../../src/layers/src/bitmap-layer/bitmap-bounds-edit-mode';
const {BitmapOverlayLayer} = KeplerGlLayers;
@@ -287,9 +291,225 @@ test('#BitmapOverlayLayer -> renderLayer with editBounds', t => {
const deckLayers = layer.renderLayer(opts);
- t.equal(deckLayers.length, 2, 'should render bitmap + editable layer (no static bounds)');
+ t.ok(deckLayers.length >= 2, 'should render bitmap + editable layer (no static bounds)');
t.equal(deckLayers[0].id, layer.id, 'first should be bitmap');
t.equal(deckLayers[1].id, `${layer.id}-edit`, 'second should be editable layer');
+ const moveHandleLayer = deckLayers.find(l => l.id === `${layer.id}-move-handle`);
+ t.ok(moveHandleLayer, 'should render a center move-handle icon');
+
+ t.end();
+});
+
+test('#BitmapOverlayLayer -> edit mode includes center move handle', t => {
+ const mode = new BitmapBoundsEditMode();
+ const bounds = {
+ type: 'FeatureCollection',
+ features: [
+ {
+ type: 'Feature',
+ geometry: {
+ type: 'Polygon',
+ coordinates: [
+ [
+ [-122.52, 37.82],
+ [-122.35, 37.82],
+ [-122.35, 37.7],
+ [-122.52, 37.7],
+ [-122.52, 37.82]
+ ]
+ ]
+ },
+ properties: {shape: 'Rectangle'}
+ }
+ ]
+ };
+
+ const guides = mode.getGuides({
+ data: bounds,
+ selectedIndexes: [0],
+ modeConfig: {lockRectangles: true},
+ lastPointerMoveEvent: null,
+ onEdit: () => {},
+ onUpdateCursor: () => {}
+ });
+
+ const moveHandle = guides.features.find(
+ feature => feature.properties?.editHandleType === BITMAP_MOVE_HANDLE_TYPE
+ );
+ t.ok(moveHandle, 'should add a center move handle');
+ t.equal(moveHandle.geometry.coordinates[0], -122.435, 'move handle lng should be the centroid');
+ t.ok(
+ Math.abs(moveHandle.geometry.coordinates[1] - 37.76) < 1e-10,
+ 'move handle lat should be the centroid'
+ );
+
+ t.end();
+});
+
+test('#BitmapOverlayLayer -> dragging the center handle translates the rectangle', t => {
+ const mode = new BitmapBoundsEditMode();
+ const data = {
+ type: 'FeatureCollection',
+ features: [
+ {
+ type: 'Feature',
+ geometry: {
+ type: 'Polygon',
+ coordinates: [
+ [
+ [-122.52, 37.82],
+ [-122.35, 37.82],
+ [-122.35, 37.7],
+ [-122.52, 37.7],
+ [-122.52, 37.82]
+ ]
+ ]
+ },
+ properties: {shape: 'Rectangle'}
+ }
+ ]
+ };
+
+ let lastEdit = null;
+ const props = {
+ data,
+ selectedIndexes: [0],
+ modeConfig: {lockRectangles: true},
+ lastPointerMoveEvent: null,
+ onEdit: action => {
+ lastEdit = action;
+ },
+ onUpdateCursor: () => {}
+ };
+
+ const movePick = {
+ isGuide: true,
+ index: 0,
+ object: {
+ type: 'Feature',
+ properties: {
+ guideType: 'editHandle',
+ editHandleType: BITMAP_MOVE_HANDLE_TYPE,
+ featureIndex: 0
+ },
+ geometry: {type: 'Point', coordinates: [-122.435, 37.76]}
+ }
+ };
+
+ const start = [-122.435, 37.76];
+ const end = [-122.335, 37.86];
+ const cancelPan = () => {};
+
+ mode.handlePointerMove({picks: [movePick], mapCoords: start}, props);
+ mode.handleStartDragging(
+ {
+ picks: [movePick],
+ pointerDownPicks: [movePick],
+ mapCoords: start,
+ pointerDownMapCoords: start,
+ cancelPan
+ },
+ props
+ );
+ mode.handleDragging(
+ {
+ picks: [movePick],
+ pointerDownPicks: [movePick],
+ mapCoords: end,
+ pointerDownMapCoords: start,
+ cancelPan
+ },
+ props
+ );
+
+ t.ok(lastEdit, 'should emit an edit action');
+ t.equal(lastEdit.editType, 'translating', 'should translate rather than resize');
+ const coords = lastEdit.updatedData.features[0].geometry.coordinates[0];
+ const lngs = coords.slice(0, -1).map(c => c[0]);
+ const lats = coords.slice(0, -1).map(c => c[1]);
+ const width = Math.max(...lngs) - Math.min(...lngs);
+ const height = Math.max(...lats) - Math.min(...lats);
+ t.ok(Math.abs(width - 0.17) < 0.02, 'translated rectangle should keep its width');
+ t.ok(Math.abs(height - 0.12) < 0.02, 'translated rectangle should keep its height');
+ t.ok(Math.min(...lngs) > -122.52, 'rectangle should move east');
+ t.ok(Math.min(...lats) > 37.7, 'rectangle should move north');
+
+ t.end();
+});
+
+test('#BitmapOverlayLayer -> dragging the bitmap body does not translate', t => {
+ const mode = new BitmapBoundsEditMode();
+ const data = {
+ type: 'FeatureCollection',
+ features: [
+ {
+ type: 'Feature',
+ geometry: {
+ type: 'Polygon',
+ coordinates: [
+ [
+ [-122.52, 37.82],
+ [-122.35, 37.82],
+ [-122.35, 37.7],
+ [-122.52, 37.7],
+ [-122.52, 37.82]
+ ]
+ ]
+ },
+ properties: {shape: 'Rectangle'}
+ }
+ ]
+ };
+
+ let lastEdit = null;
+ let cancelPanCalled = false;
+ const props = {
+ data,
+ selectedIndexes: [0],
+ modeConfig: {lockRectangles: true},
+ lastPointerMoveEvent: null,
+ onEdit: action => {
+ lastEdit = action;
+ },
+ onUpdateCursor: () => {}
+ };
+
+ const bodyPick = {
+ isGuide: false,
+ index: 0,
+ object: data.features[0]
+ };
+
+ const start = [-122.435, 37.76];
+ const end = [-122.335, 37.86];
+ const cancelPan = () => {
+ cancelPanCalled = true;
+ };
+
+ mode.handlePointerMove({picks: [bodyPick], mapCoords: start}, props);
+ mode.handleStartDragging(
+ {
+ picks: [bodyPick],
+ pointerDownPicks: [bodyPick],
+ mapCoords: start,
+ pointerDownMapCoords: start,
+ cancelPan
+ },
+ props
+ );
+ mode.handleDragging(
+ {
+ picks: [bodyPick],
+ pointerDownPicks: [bodyPick],
+ mapCoords: end,
+ pointerDownMapCoords: start,
+ cancelPan
+ },
+ props
+ );
+
+ t.equal(lastEdit, null, 'should not translate when dragging the image body');
+ t.equal(cancelPanCalled, false, 'should not block map pan');
t.end();
});