Skip to content

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

feat: add circle draw tool and live length/radius labels (#3661)

Igor Dykhta854099ff9268
diff --git a/docs/api-reference/actions/actions.md b/docs/api-reference/actions/actions.md
index dee18c593..629ac9170 100644
--- a/docs/api-reference/actions/actions.md
+++ b/docs/api-reference/actions/actions.md
@@ -779,7 +779,7 @@ Set the Draw on Map editor mode
**Parameters**
-- `mode` **[string][162]** one of `EDITOR_MODES`: `EDIT`, `DRAW_POINT`, `DRAW_LINESTRING`, `DRAW_POLYGON`, `DRAW_RECTANGLE`
+- `mode` **[string][162]** one of `EDITOR_MODES`: `EDIT`, `DRAW_POINT`, `DRAW_LINESTRING`, `DRAW_POLYGON`, `DRAW_RECTANGLE`, `DRAW_CIRCLE`
**Examples**
diff --git a/docs/user-guides/draw-on-map.md b/docs/user-guides/draw-on-map.md
index a4b554de3..c633f92b5 100644
--- a/docs/user-guides/draw-on-map.md
+++ b/docs/user-guides/draw-on-map.md
@@ -10,7 +10,7 @@
- [Disable sketch tools](#disable-sketch-tools)
<!-- /TOC -->
-Draw on Map lets you sketch points, lines, polygons, and rectangles on the map. Sketches stay on the map until you delete them, convert them to a layer, or use a polygon or rectangle as a spatial filter.
+Draw on Map lets you sketch points, lines, polygons, rectangles, and circles on the map. Sketches stay on the map until you delete them, convert them to a layer, or use a polygon, rectangle, or circle as a spatial filter.
## Open the drawing tools
@@ -24,18 +24,19 @@ Press **Escape** while a draw tool is active to return to **Select**.
| --- | --- | --- |
| **Select** | Click a sketch to select it | Move and edit existing sketches |
| **Point** | Click the map | Stays in point mode so you can add several points |
-| **Line** | Click to add vertices, double-click or press **Enter** to finish | Stays in line mode so you can draw several lines |
+| **Line** | Click to add vertices, double-click or press **Enter** to finish | Stays in line mode so you can draw several lines. A length label appears at the cursor while you draw |
| **Polygon** | Click to add vertices, click the first vertex (or double-click) to close | Sketch only until you apply it as a filter |
| **Rectangle** | Click and drag, or click two opposite corners | Applied as a polygon filter when you finish drawing |
+| **Circle** | Click the center, then click or drag to set the radius | Tessellated polygon. A radius label appears at the rim while you draw. Sketch only until you apply it as a filter or convert it to a layer |
## Select, move, and edit
1. Choose **Select**, then click a sketch.
2. Drag a **point** to move it.
3. Drag the **body of a line** to move the whole line. Drag a vertex to reshape it. Click the line to insert a vertex.
-4. Drag the **interior of a polygon or rectangle** to move it. Drag a vertex to reshape it.
+4. Drag the **interior of a polygon, rectangle, or circle** to move it. Drag a vertex to reshape it.
-Right-click a selected sketch for **Edit Properties**, **Copy Geometry**, and **Delete**. **Filter Layers** appears only for polygons and rectangles.
+Right-click a selected sketch for **Edit Properties**, **Copy Geometry**, and **Delete**. **Filter Layers** appears only for polygons, rectangles, and circles.
## Edit properties
@@ -43,10 +44,10 @@ Right-click a sketch and choose **Edit Properties** to add, change, or remove na
## Filter layers
-Polygons and rectangles can filter other layers so that only features inside the shape remain visible.
+Polygons, rectangles, and circles can filter other layers so that only features inside the shape remain visible.
- **Rectangle** becomes a filter as soon as you finish drawing it.
-- **Polygon** stays a sketch until you right-click it and choose **Filter Layers**, then pick the layers to filter.
+- **Polygon** and **Circle** stay a sketch until you right-click and choose **Filter Layers**, then pick the layers to filter.
Filtered shapes use a dashed outline. A filter icon appears on the shape; click it to select the filter.
@@ -63,7 +64,7 @@ The drawing menu also includes:
## Disable sketch tools
-Point, Line, Copy all, Convert to Layer, and Edit Properties are enabled by default. Polygon and rectangle drawing stay available if you turn the extra tools off:
+Point, Line, Copy all, Convert to Layer, and Edit Properties are enabled by default. Polygon, rectangle, and circle drawing stay available if you turn the extra tools off:
```js
import {initApplicationConfig} from '@kepler.gl/utils';
diff --git a/src/actions/src/vis-state-actions.ts b/src/actions/src/vis-state-actions.ts
index 3dd07777b..0e94004f4 100644
--- a/src/actions/src/vis-state-actions.ts
+++ b/src/actions/src/vis-state-actions.ts
@@ -1780,7 +1780,7 @@ export type SetEditorModeUpdaterAction = {
};
/** Set the Draw on Map editor mode.
* @memberof visStateActions
- * @param mode one of `EDITOR_MODES`: `EDIT`, `DRAW_POINT`, `DRAW_LINESTRING`, `DRAW_POLYGON`, `DRAW_RECTANGLE`
+ * @param mode one of `EDITOR_MODES`: `EDIT`, `DRAW_POINT`, `DRAW_LINESTRING`, `DRAW_POLYGON`, `DRAW_RECTANGLE`, `DRAW_CIRCLE`
* @returns action
* @public
* @example
diff --git a/src/components/src/common/icons/circle.tsx b/src/components/src/common/icons/circle.tsx
new file mode 100644
index 000000000..bff36b80b
--- /dev/null
+++ b/src/components/src/common/icons/circle.tsx
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import React, {Component} from 'react';
+import Base, {BaseProps} from './base';
+
+export default class Circle extends Component<Partial<BaseProps>> {
+ static defaultProps = {
+ height: '16px',
+ predefinedClassName: 'data-ex-icons-circle',
+ viewBox: '0 0 22 16'
+ };
+
+ render() {
+ return (
+ <Base {...this.props}>
+ <circle cx="11" cy="8" r="6" stroke="currentColor" fill="transparent" strokeWidth="1.5" />
+ <circle cx="11" cy="8" r="1.5" />
+ <path
+ fillRule="evenodd"
+ clipRule="evenodd"
+ d="M17 10C18.1046 10 19 9.10457 19 8C19 6.89543 18.1046 6 17 6C15.8954 6 15 6.89543 15 8C15 9.10457 15.8954 10 17 10Z"
+ />
+ </Base>
+ );
+ }
+}
diff --git a/src/components/src/common/icons/index.tsx b/src/components/src/common/icons/index.tsx
index 6070912a6..b5c36d6f4 100644
--- a/src/components/src/common/icons/index.tsx
+++ b/src/components/src/common/icons/index.tsx
@@ -87,6 +87,7 @@ export {default as DrawPoint} from './draw-point';
export {default as LineString} from './line-string';
export {default as Polygon} from './polygon';
export {default as Rectangle} from './rectangle';
+export {default as Circle} from './circle';
export {default as TimelineMarker} from './timeline-marker';
export {default as OrderByList} from './order-by-list';
export {default as OrderByDataset} from './order-by-dataset';
diff --git a/src/components/src/map/map-draw-panel.tsx b/src/components/src/map/map-draw-panel.tsx
index 1edd73c2d..6dd09590f 100644
--- a/src/components/src/map/map-draw-panel.tsx
+++ b/src/components/src/map/map-draw-panel.tsx
@@ -18,7 +18,8 @@ import {
Layers,
LineString,
Polygon,
- Rectangle
+ Rectangle,
+ Circle
} from '../common/icons';
import {MapControlButton} from '../common/styled-components';
import ToolbarItem from '../common/toolbar-item';
@@ -71,6 +72,7 @@ function MapDrawPanelFactory(
cursor: CursorClick,
innerPolygon: Polygon,
rectangle: Rectangle,
+ circle: Circle,
point: DrawPoint,
line: LineString,
copy: Copy,
@@ -147,6 +149,13 @@ function MapDrawPanelFactory(
icon={actionIcons.rectangle}
active={editor.mode === EDITOR_MODES.DRAW_RECTANGLE}
/>
+ <ToolbarItem
+ className="draw-circle"
+ onClick={() => onSetEditorMode(EDITOR_MODES.DRAW_CIRCLE)}
+ label="toolbar.circle"
+ icon={actionIcons.circle}
+ active={editor.mode === EDITOR_MODES.DRAW_CIRCLE}
+ />
{enableSketches ? (
<>
<ToolbarSeparator />
diff --git a/src/constants/src/default-settings.ts b/src/constants/src/default-settings.ts
index 7726b1b1d..dcb90b6a6 100644
--- a/src/constants/src/default-settings.ts
+++ b/src/constants/src/default-settings.ts
@@ -1288,6 +1288,7 @@ export const EDITOR_LAYER_PICKING_RADIUS = 6;
export const EDITOR_MODES = {
DRAW_POLYGON: 'DRAW_POLYGON',
DRAW_RECTANGLE: 'DRAW_RECTANGLE',
+ DRAW_CIRCLE: 'DRAW_CIRCLE',
DRAW_LINESTRING: 'DRAW_LINESTRING',
DRAW_POINT: 'DRAW_POINT',
EDIT: 'EDIT_VERTEX'
@@ -1296,6 +1297,7 @@ export const EDITOR_MODES = {
export const EDITOR_DRAW_MODES: string[] = [
EDITOR_MODES.DRAW_POLYGON,
EDITOR_MODES.DRAW_RECTANGLE,
+ EDITOR_MODES.DRAW_CIRCLE,
EDITOR_MODES.DRAW_LINESTRING,
EDITOR_MODES.DRAW_POINT
];
diff --git a/src/layers/package.json b/src/layers/package.json
index 6d5f82d57..18d1d97fc 100644
--- a/src/layers/package.json
+++ b/src/layers/package.json
@@ -65,6 +65,7 @@
"@turf/bbox": "^7.2.0",
"@turf/boolean-within": "^7.2.0",
"@turf/center": "^7.2.0",
+ "@turf/distance": "^7.2.0",
"@turf/helpers": "^7.2.0",
"@types/geojson": "^7946.0.8",
"@types/keymirror": "^0.1.1",
diff --git a/src/layers/src/editor-layer/draw-circle-mode-extended.ts b/src/layers/src/editor-layer/draw-circle-mode-extended.ts
new file mode 100644
index 000000000..54e7b1b0a
--- /dev/null
+++ b/src/layers/src/editor-layer/draw-circle-mode-extended.ts
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import {DrawCircleFromCenterMode} from '@deck.gl-community/editable-layers';
+
+import {formatCircleRadiusLabel} from './editor-measure-labels';
+
+/**
+ * Extends DrawCircleFromCenterMode so both click-move-click and
+ * click-drag-release work, matching DrawRectangleModeExtended.
+ *
+ * The base TwoClickPolygonMode treats these as mutually exclusive via
+ * the `dragToDraw` modeConfig flag. This subclass removes those guards:
+ * - Click → move → click (two-click)
+ * - Mousedown → drag → mouseup (drag-to-draw)
+ *
+ * While the circle is being sized, a radius label is shown at the rim.
+ */
+export class DrawCircleModeExtended extends DrawCircleFromCenterMode {
+ handleClick(event, props) {
+ this.addClickSequence(event);
+ this.checkAndFinishPolygon(props);
+ }
+
+ handleStartDragging(event, _props) {
+ this.addClickSequence(event);
+ event.cancelPan();
+ }
+
+ handleStopDragging(event, props) {
+ this.addClickSequence(event);
+ this.checkAndFinishPolygon(props);
+ }
+
+ getTooltips() {
+ if (!this.position || !Number.isFinite(this.radius) || (this.radius ?? 0) <= 0) {
+ return [];
+ }
+
+ const text = formatCircleRadiusLabel(this.radius as number);
+ if (!text) {
+ return [];
+ }
+
+ return [
+ {
+ position: this.position,
+ text
+ }
+ ];
+ }
+}
diff --git a/src/layers/src/editor-layer/draw-line-string-mode-extended.ts b/src/layers/src/editor-layer/draw-line-string-mode-extended.ts
new file mode 100644
index 000000000..e884bbcd1
--- /dev/null
+++ b/src/layers/src/editor-layer/draw-line-string-mode-extended.ts
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import {DrawLineStringMode} from '@deck.gl-community/editable-layers';
+
+import {formatLineLengthLabel, getLineLengthKm} from './editor-measure-labels';
+
+/**
+ * Line sketching for Draw on Map. While vertices are being added, a length
+ * label is shown at the cursor (including the tentative segment).
+ */
+export class DrawLineStringModeExtended extends DrawLineStringMode {
+ getTooltips(props) {
+ const clickSequence = this.getClickSequence() || [];
+ if (!clickSequence.length) {
+ return [];
+ }
+
+ const lastPointer = props?.lastPointerMoveEvent?.mapCoords;
+ const coordinates = lastPointer ? [...clickSequence, lastPointer] : clickSequence;
+ if (coordinates.length < 2) {
+ return [];
+ }
+
+ const text = formatLineLengthLabel(getLineLengthKm(coordinates));
+ if (!text) {
+ return [];
+ }
+
+ return [
+ {
+ position: coordinates[coordinates.length - 1],
+ text
+ }
+ ];
+ }
+}
diff --git a/src/layers/src/editor-layer/editor-layer-utils.ts b/src/layers/src/editor-layer/editor-layer-utils.ts
index f11e88477..babf2a99d 100644
--- a/src/layers/src/editor-layer/editor-layer-utils.ts
+++ b/src/layers/src/editor-layer/editor-layer-utils.ts
@@ -166,6 +166,8 @@ export function getTooltip(
const tooltipText =
editor.mode === EDITOR_MODES.DRAW_RECTANGLE
? 'Click or drag to draw rectangle'
+ : editor.mode === EDITOR_MODES.DRAW_CIRCLE
+ ? 'Click or drag to draw circle'
: editor.mode === EDITOR_MODES.DRAW_POINT
? 'Click to add a point'
: editor.mode === EDITOR_MODES.DRAW_LINESTRING
diff --git a/src/layers/src/editor-layer/editor-layer.ts b/src/layers/src/editor-layer/editor-layer.ts
index 5a478a3ab..0e8a045d7 100644
--- a/src/layers/src/editor-layer/editor-layer.ts
+++ b/src/layers/src/editor-layer/editor-layer.ts
@@ -5,7 +5,6 @@ import {EditableGeoJsonLayer} from '@deck.gl-community/editable-layers';
import {Layer as DeckLayer, LayerProps as DeckLayerProps} from '@deck.gl/core';
import {
DrawPolygonMode,
- DrawLineStringMode,
DrawPointMode,
TranslateMode,
CompositeMode,
@@ -22,6 +21,8 @@ import {EDIT_TYPES} from './constants';
import {LINE_STYLE, FEATURE_STYLE, EDIT_HANDLE_STYLE, getFeatureDashArray} from './feature-styles';
import {ModifyModeExtended} from './modify-mode-extended';
import {DrawRectangleModeExtended} from './draw-rectangle-mode-extended';
+import {DrawCircleModeExtended} from './draw-circle-mode-extended';
+import {DrawLineStringModeExtended} from './draw-line-string-mode-extended';
import {isDrawingActive} from './editor-layer-utils';
const DEFAULT_COMPOSITE_MODE = new CompositeMode([
@@ -29,12 +30,6 @@ const DEFAULT_COMPOSITE_MODE = new CompositeMode([
new ModifyModeExtended() as unknown as GeoJsonEditMode
]);
-class DrawLineStringSketchMode extends DrawLineStringMode {
- getTooltips() {
- return [];
- }
-}
-
export type GetEditorLayerProps = {
editorMenuActive: boolean;
editor: Editor;
@@ -82,9 +77,11 @@ export function getEditorLayer({
// @ts-ignore
else if (editorMode === EDITOR_MODES.DRAW_RECTANGLE) mode = DrawRectangleModeExtended;
// @ts-ignore
+ else if (editorMode === EDITOR_MODES.DRAW_CIRCLE) mode = DrawCircleModeExtended;
+ // @ts-ignore
else if (sketchesEnabled && editorMode === EDITOR_MODES.DRAW_LINESTRING)
// @ts-ignore
- mode = DrawLineStringSketchMode;
+ mode = DrawLineStringModeExtended;
// @ts-ignore
else if (sketchesEnabled && editorMode === EDITOR_MODES.DRAW_POINT) mode = DrawPointMode;
}
@@ -111,7 +108,9 @@ export function getEditorLayer({
modeConfig: {
viewport,
screenSpace: true,
- lockRectangles: true
+ lockRectangles: true,
+ // Turf circle() default; 64 vertices is a smooth tessellated polygon.
+ steps: 64
},
pickingLineWidthExtraPixels: 5,
@@ -220,8 +219,20 @@ export function getEditorLayer({
guides: {shadowEnabled: false},
tooltips: {
shadowEnabled: false,
+ getSize: 14,
+ getColor: [255, 255, 255, 255],
+ background: true,
+ getBackgroundColor: [0, 0, 0, 200],
+ backgroundPadding: [6, 4],
+ getPixelOffset: [12, 0],
+ getTextAnchor: 'start',
+ getAlignmentBaseline: 'center',
+ fontFamily: 'sans-serif',
+ fontWeight: '600',
+ pickable: false,
_subLayerProps: {
- characters: {shadowEnabled: false}
+ characters: {shadowEnabled: false},
+ background: {shadowEnabled: false}
}
}
}
diff --git a/src/layers/src/editor-layer/editor-measure-labels.ts b/src/layers/src/editor-layer/editor-measure-labels.ts
new file mode 100644
index 000000000..f9d64fa8a
--- /dev/null
+++ b/src/layers/src/editor-layer/editor-measure-labels.ts
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import {point} from '@turf/helpers';
+import {distance as turfDistance} from '@turf/distance';
+
+function formatMeasureKmLabel(kind: 'Radius' | 'Length', km: number): string {
+ if (!Number.isFinite(km) || km <= 0) {
+ return '';
+ }
+ return `${kind}: ${km.toFixed(2)} km`;
+}
+
+/** Format a Turf radius (kilometers) as a map label. */
+export function formatCircleRadiusLabel(radiusKm: number): string {
+ return formatMeasureKmLabel('Radius', radiusKm);
+}
+
+/** Format a geodesic line length (kilometers) as a map label. */
+export function formatLineLengthLabel(lengthKm: number): string {
+ return formatMeasureKmLabel('Length', lengthKm);
+}
+
+/** Sum geodesic distances between consecutive coordinates, in kilometers. */
+export function getLineLengthKm(coordinates: number[][] = []): number {
+ let total = 0;
+ for (let i = 1; i < coordinates.length; i++) {
+ const from = coordinates[i - 1];
+ const to = coordinates[i];
+ if (!from || !to || from.length < 2 || to.length < 2) {
+ continue;
+ }
+ total += turfDistance(point(from), point(to));
+ }
+ return total;
+}
diff --git a/src/layers/src/index.ts b/src/layers/src/index.ts
index b0aec639f..ce87ce48a 100644
--- a/src/layers/src/index.ts
+++ b/src/layers/src/index.ts
@@ -145,6 +145,10 @@ export * from './h3-hexagon-layer';
export {default as Table} from './table';
export {getEditorLayer} from './editor-layer/editor-layer';
+export {
+ formatCircleRadiusLabel,
+ formatLineLengthLabel
+} from './editor-layer/editor-measure-labels';
export {
default as ScenegraphLayer,
diff --git a/src/localization/src/translations/ca.ts b/src/localization/src/translations/ca.ts
index a9622125e..5f4cb473e 100644
--- a/src/localization/src/translations/ca.ts
+++ b/src/localization/src/translations/ca.ts
@@ -374,6 +374,7 @@ export default {
line: 'línia',
polygon: 'polígon',
rectangle: 'rectangle',
+ circle: 'cercle',
copyAll: 'Copia-ho tot',
convertToLayer: 'Converteix\na capa',
hide: 'amaga',
diff --git a/src/localization/src/translations/cn.ts b/src/localization/src/translations/cn.ts
index a15097005..1a0485ab3 100644
--- a/src/localization/src/translations/cn.ts
+++ b/src/localization/src/translations/cn.ts
@@ -361,6 +361,7 @@ export default {
line: '线',
polygon: 'polygon',
rectangle: 'rectangle',
+ circle: '圆形',
copyAll: '复制全部',
convertToLayer: '转换为\n图层',
hide: '隐藏',
diff --git a/src/localization/src/translations/en.ts b/src/localization/src/translations/en.ts
index 762c48a92..1e898e2e4 100644
--- a/src/localization/src/translations/en.ts
+++ b/src/localization/src/translations/en.ts
@@ -423,6 +423,7 @@ export default {
line: 'Line',
polygon: 'Polygon',
rectangle: 'Rectangle',
+ circle: 'Circle',
copyAll: 'Copy all',
convertToLayer: 'Convert to\nLayer',
hide: 'Hide',
diff --git a/src/localization/src/translations/es.ts b/src/localization/src/translations/es.ts
index d5462ef18..a84efd296 100644
--- a/src/localization/src/translations/es.ts
+++ b/src/localization/src/translations/es.ts
@@ -375,6 +375,7 @@ export default {
line: 'línea',
polygon: 'polígono',
rectangle: 'rectángulo',
+ circle: 'círculo',
copyAll: 'Copiar todo',
convertToLayer: 'Convertir\na capa',
hide: 'esconder',
diff --git a/src/localization/src/translations/fi.ts b/src/localization/src/translations/fi.ts
index 0b7547075..b45be9765 100644
--- a/src/localization/src/translations/fi.ts
+++ b/src/localization/src/translations/fi.ts
@@ -373,6 +373,7 @@ export default {
line: 'viiva',
polygon: 'polygoni',
rectangle: 'nelikulmio',
+ circle: 'ympyrä',
copyAll: 'Kopioi kaikki',
convertToLayer: 'Muunna\ntasoksi',
hide: 'piilota',
diff --git a/src/localization/src/translations/ja.ts b/src/localization/src/translations/ja.ts
index 696eb6a8e..91903b183 100644
--- a/src/localization/src/translations/ja.ts
+++ b/src/localization/src/translations/ja.ts
@@ -371,6 +371,7 @@ export default {
line: 'ライン',
polygon: 'ポリゴン',
rectangle: '長方形',
+ circle: '円',
copyAll: 'すべてコピー',
convertToLayer: 'レイヤに\n変換',
hide: '非表示',
diff --git a/src/localization/src/translations/pt.ts b/src/localization/src/translations/pt.ts
index f950cf1d9..2992db6b3 100644
--- a/src/localization/src/translations/pt.ts
+++ b/src/localization/src/translations/pt.ts
@@ -375,6 +375,7 @@ export default {
line: 'linha',
polygon: 'polígono',
rectangle: 'retângulo',
+ circle: 'círculo',
copyAll: 'Copiar tudo',
convertToLayer: 'Converter\nem camada',
hide: 'esconder',
diff --git a/src/localization/src/translations/ru.ts b/src/localization/src/translations/ru.ts
index 91fee7bde..953193b63 100644
--- a/src/localization/src/translations/ru.ts
+++ b/src/localization/src/translations/ru.ts
@@ -374,6 +374,7 @@ export default {
line: 'Линия',
polygon: 'Многоугольник',
rectangle: 'Квадрат',
+ circle: 'Круг',
copyAll: 'Копировать всё',
convertToLayer: 'Преобразовать\nв слой',
hide: 'Скрыть',
diff --git a/src/utils/src/editor-feature-utils.ts b/src/utils/src/editor-feature-utils.ts
index 5b5723aab..39cb973af 100644
--- a/src/utils/src/editor-feature-utils.ts
+++ b/src/utils/src/editor-feature-utils.ts
@@ -11,7 +11,8 @@ const EDITOR_INTERNAL_PROPERTIES = new Set([
'guideType',
'editHandleType',
'shape',
- 'renderType'
+ 'renderType',
+ 'editProperties'
]);
export function isReservedEditorProperty(key: string): boolean {
diff --git a/test/browser/components/map/map-control-test.js b/test/browser/components/map/map-control-test.js
index 8d1b40512..a8739164d 100644
--- a/test/browser/components/map/map-control-test.js
+++ b/test/browser/components/map/map-control-test.js
@@ -297,8 +297,8 @@ test('MapControlFactory - show panels', t => {
t.equal(wrapper.find(MapControlToolbar).length, 1, 'should render 1 MapControlToolbar');
t.equal(
wrapper.find(MapControlToolbar).at(0).find(ToolbarItem).length,
- 7,
- 'should render 7 ToolbarItem'
+ 8,
+ 'should render 8 ToolbarItem'
);
// show locale
diff --git a/test/node/reducers/vis-state-test.js b/test/node/reducers/vis-state-test.js
index 27f1a3aa8..9569156ae 100644
--- a/test/node/reducers/vis-state-test.js
+++ b/test/node/reducers/vis-state-test.js
@@ -6038,6 +6038,39 @@ test('#visStateReducer -> SET_FEATURES line keeps draw mode', t => {
t.end();
});
+test('#visStateReducer -> SET_FEATURES closed circle switches to edit', t => {
+ let state = reducer(INITIAL_VIS_STATE, VisStateActions.setEditorMode(EDITOR_MODES.DRAW_CIRCLE));
+ state = reducer(
+ state,
+ VisStateActions.setFeatures([
+ {
+ type: 'Feature',
+ id: 'circle-1',
+ properties: {
+ isClosed: true,
+ shape: 'Circle'
+ },
+ geometry: {
+ type: 'Polygon',
+ coordinates: [
+ [
+ [0, 1],
+ [1, 0],
+ [0, -1],
+ [-1, 0],
+ [0, 1]
+ ]
+ ]
+ }
+ }
+ ])
+ );
+
+ t.equal(state.editor.mode, EDITOR_MODES.EDIT, 'Closed circle sketches should switch to select');
+ t.equal(state.editor.features.length, 1, 'Should store the circle sketch');
+ t.end();
+});
+
test('#visStateReducer -> CONVERT_EDITOR_FEATURES_TO_LAYER', t => {
const emptyState = reducer(INITIAL_VIS_STATE, VisStateActions.convertEditorFeaturesToLayer());
t.equal(emptyState, INITIAL_VIS_STATE, 'Should no-op when there are no sketch features');
@@ -6113,6 +6146,13 @@ test('#visStateReducer -> CONVERT_EDITOR_FEATURES_TO_LAYER disabled by config',
'Should ignore point draw mode when sketches are disabled'
);
+ const circleMode = reducer(startState, VisStateActions.setEditorMode(EDITOR_MODES.DRAW_CIRCLE));
+ t.equal(
+ circleMode.editor.mode,
+ EDITOR_MODES.DRAW_CIRCLE,
+ 'Should allow circle draw mode when sketches are disabled'
+ );
+
const ignoredProperties = reducer(
startState,
VisStateActions.setEditorFeatureProperties(lineFeature, {name: 'route'})
diff --git a/test/node/utils/editor-feature-utils-test.js b/test/node/utils/editor-feature-utils-test.js
index 1d3cd56dc..82344f6a3 100644
--- a/test/node/utils/editor-feature-utils-test.js
+++ b/test/node/utils/editor-feature-utils-test.js
@@ -43,6 +43,45 @@ test('editor-feature-utils -> sanitizeEditorFeature', t => {
t.end();
});
+test('editor-feature-utils -> sanitizeEditorFeature circle is a regular polygon', t => {
+ const ring = [
+ [0, 1],
+ [1, 0],
+ [0, -1],
+ [-1, 0],
+ [0, 1]
+ ];
+ const feature = {
+ type: 'Feature',
+ id: 'circle-1',
+ properties: {
+ isClosed: true,
+ shape: 'Circle',
+ editProperties: {
+ shape: 'Circle',
+ center: [0, 0],
+ radius: {value: 1, unit: 'kilometers'}
+ }
+ },
+ geometry: {
+ type: 'Polygon',
+ coordinates: [ring]
+ }
+ };
+
+ t.deepEqual(
+ sanitizeEditorFeature(feature),
+ {
+ type: 'Feature',
+ id: 'circle-1',
+ properties: {},
+ geometry: feature.geometry
+ },
+ 'Should keep tessellated Polygon geometry and drop circle editor metadata'
+ );
+ t.end();
+});
+
test('editor-feature-utils -> editorFeaturesToFeatureCollection', t => {
const point = {
id: 'point-1',
diff --git a/test/node/utils/editor-utils-test.js b/test/node/utils/editor-utils-test.js
index f67921669..80284be84 100644
--- a/test/node/utils/editor-utils-test.js
+++ b/test/node/utils/editor-utils-test.js
@@ -7,7 +7,23 @@ import {EditableGeoJsonLayer} from '@deck.gl-community/editable-layers';
import {INITIAL_VIS_STATE} from '@kepler.gl/reducers';
import {VisStateActions} from '@kepler.gl/actions';
import {EDITOR_LAYER_ID, EDITOR_MODES} from '@kepler.gl/constants';
-import {EditorLayerUtils, getEditorLayer} from '@kepler.gl/layers';
+import {EditorLayerUtils, getEditorLayer, formatCircleRadiusLabel, formatLineLengthLabel} from '@kepler.gl/layers';
+
+test('editorLayerUtils -> formatCircleRadiusLabel', t => {
+ t.equal(formatCircleRadiusLabel(0), '', 'Should hide invalid radii');
+ t.equal(formatCircleRadiusLabel(0.25), 'Radius: 0.25 km', 'Should show radius in kilometers');
+ t.equal(formatCircleRadiusLabel(1.5), 'Radius: 1.50 km', 'Should keep two decimal places');
+ t.equal(formatCircleRadiusLabel(12.345), 'Radius: 12.35 km', 'Should round to two decimal places');
+ t.end();
+});
+
+test('editorLayerUtils -> formatLineLengthLabel', t => {
+ t.equal(formatLineLengthLabel(0), '', 'Should hide invalid lengths');
+ t.equal(formatLineLengthLabel(0.25), 'Length: 0.25 km', 'Should show length in kilometers');
+ t.equal(formatLineLengthLabel(1.5), 'Length: 1.50 km', 'Should keep two decimal places');
+ t.equal(formatLineLengthLabel(12.345), 'Length: 12.35 km', 'Should round to two decimal places');
+ t.end();
+});
test('editorLayerUtils -> isDrawingActive', t => {
t.equal(
@@ -35,6 +51,11 @@ test('editorLayerUtils -> isDrawingActive', t => {
true,
'Should return true for point drawing mode'
);
+ t.equal(
+ EditorLayerUtils.isDrawingActive(true, EDITOR_MODES.DRAW_CIRCLE),
+ true,
+ 'Should return true for circle drawing mode'
+ );
t.end();
});
@@ -119,6 +140,16 @@ test('editorLayerUtils -> getTooltip', t => {
'Should return a tooltip for line drawing'
);
+ t.equal(
+ EditorLayerUtils.getTooltip(info, {
+ editor: {...editor, mode: EDITOR_MODES.DRAW_CIRCLE},
+ theme: {},
+ editorMenuActive: true
+ })?.text,
+ 'Click or drag to draw circle',
+ 'Should return a tooltip for circle drawing'
+ );
+
info.layer.id = EDITOR_LAYER_ID;
t.deepEqual(
@@ -366,6 +397,75 @@ test('editorLayerUtils -> getEditorLayer', t => {
});
t.ok(editorLayer instanceof EditableGeoJsonLayer, 'Should return an editable layer');
+ const circleLayer = getEditorLayer({
+ editorMenuActive: true,
+ editor: {...editor, mode: EDITOR_MODES.DRAW_CIRCLE},
+ onSetFeatures: VisStateActions.setFeatures,
+ setSelectedFeature: VisStateActions.setSelectedFeature,
+ featureCollection: {
+ features: [],
+ type: 'FeatureCollection'
+ },
+ selectedFeatureIndexes: [],
+ viewport: null
+ });
+ t.equal(
+ circleLayer.props.mode.name,
+ 'DrawCircleModeExtended',
+ 'Should use the circle draw mode when DRAW_CIRCLE is active'
+ );
+ t.equal(circleLayer.props.modeConfig.steps, 64, 'Should tessellate circles with 64 vertices');
+ t.equal(circleLayer.props._subLayerProps.tooltips.background, true, 'Radius label should use a background');
+
+ const CircleMode = circleLayer.props.mode;
+ const circleMode = new CircleMode();
+ t.deepEqual(circleMode.getTooltips(), [], 'Should hide the radius label before the circle has a radius');
+
+ circleMode.radius = 0.25;
+ circleMode.position = [10, 20];
+ t.deepEqual(
+ circleMode.getTooltips(),
+ [{position: [10, 20], text: 'Radius: 0.25 km'}],
+ 'Should show a radius label at the circle rim while drawing'
+ );
+
+ const lineLayer = getEditorLayer({
+ editorMenuActive: true,
+ editor: {...editor, mode: EDITOR_MODES.DRAW_LINESTRING},
+ onSetFeatures: VisStateActions.setFeatures,
+ setSelectedFeature: VisStateActions.setSelectedFeature,
+ featureCollection: {
+ features: [],
+ type: 'FeatureCollection'
+ },
+ selectedFeatureIndexes: [],
+ viewport: null
+ });
+ t.equal(
+ lineLayer.props.mode.name,
+ 'DrawLineStringModeExtended',
+ 'Should use the line draw mode when DRAW_LINESTRING is active'
+ );
+
+ const LineMode = lineLayer.props.mode;
+ const lineMode = new LineMode();
+ t.deepEqual(lineMode.getTooltips({}), [], 'Should hide the length label before the line has a vertex');
+
+ lineMode.addClickSequence({mapCoords: [0, 0]});
+ t.deepEqual(
+ lineMode.getTooltips({lastPointerMoveEvent: {mapCoords: [0, 0]}}),
+ [],
+ 'Should hide the length label when the tentative segment has no length'
+ );
+
+ const lengthTooltips = lineMode.getTooltips({lastPointerMoveEvent: {mapCoords: [1, 0]}});
+ t.equal(lengthTooltips.length, 1, 'Should show a length label while drawing a line');
+ t.deepEqual(lengthTooltips[0].position, [1, 0], 'Should place the length label at the cursor');
+ t.ok(
+ /^Length: \d+\.\d{2} km$/.test(lengthTooltips[0].text),
+ 'Should format the length as Length: xx km'
+ );
+
t.end();
});
diff --git a/yarn.lock b/yarn.lock
index b2566624b..8b9c2dc8e 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5950,6 +5950,7 @@ __metadata:
"@turf/bbox": "npm:^7.2.0"
"@turf/boolean-within": "npm:^7.2.0"
"@turf/center": "npm:^7.2.0"
+ "@turf/distance": "npm:^7.2.0"
"@turf/helpers": "npm:^7.2.0"
"@types/geojson": "npm:^7946.0.8"
"@types/keymirror": "npm:^0.1.1"