@deck.gl-community/editable-layers 9.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -0
- package/dist/constants.d.ts +14 -0
- package/dist/constants.js +14 -0
- package/dist/curve-utils.d.ts +2 -0
- package/dist/curve-utils.js +61 -0
- package/dist/edit-modes/composite-mode.d.ts +14 -0
- package/dist/edit-modes/composite-mode.js +47 -0
- package/dist/edit-modes/draw-90degree-polygon-mode.d.ts +11 -0
- package/dist/edit-modes/draw-90degree-polygon-mode.js +179 -0
- package/dist/edit-modes/draw-circle-by-diameter-mode.d.ts +24 -0
- package/dist/edit-modes/draw-circle-by-diameter-mode.js +78 -0
- package/dist/edit-modes/draw-circle-from-center-mode.d.ts +22 -0
- package/dist/edit-modes/draw-circle-from-center-mode.js +70 -0
- package/dist/edit-modes/draw-ellipse-by-bounding-box-mode.d.ts +5 -0
- package/dist/edit-modes/draw-ellipse-by-bounding-box-mode.js +20 -0
- package/dist/edit-modes/draw-ellipse-using-three-points-mode.d.ts +5 -0
- package/dist/edit-modes/draw-ellipse-using-three-points-mode.js +16 -0
- package/dist/edit-modes/draw-line-string-mode.d.ts +25 -0
- package/dist/edit-modes/draw-line-string-mode.js +170 -0
- package/dist/edit-modes/draw-point-mode.d.ts +8 -0
- package/dist/edit-modes/draw-point-mode.js +28 -0
- package/dist/edit-modes/draw-polygon-by-dragging-mode.d.ts +14 -0
- package/dist/edit-modes/draw-polygon-by-dragging-mode.js +87 -0
- package/dist/edit-modes/draw-polygon-mode.d.ts +10 -0
- package/dist/edit-modes/draw-polygon-mode.js +143 -0
- package/dist/edit-modes/draw-rectangle-from-center-mode.d.ts +5 -0
- package/dist/edit-modes/draw-rectangle-from-center-mode.js +17 -0
- package/dist/edit-modes/draw-rectangle-mode.d.ts +5 -0
- package/dist/edit-modes/draw-rectangle-mode.js +11 -0
- package/dist/edit-modes/draw-rectangle-using-three-points-mode.d.ts +5 -0
- package/dist/edit-modes/draw-rectangle-using-three-points-mode.js +28 -0
- package/dist/edit-modes/draw-square-from-center-mode.d.ts +5 -0
- package/dist/edit-modes/draw-square-from-center-mode.js +35 -0
- package/dist/edit-modes/draw-square-mode.d.ts +5 -0
- package/dist/edit-modes/draw-square-mode.js +28 -0
- package/dist/edit-modes/duplicate-mode.d.ts +7 -0
- package/dist/edit-modes/duplicate-mode.js +17 -0
- package/dist/edit-modes/edit-mode.d.ts +11 -0
- package/dist/edit-modes/edit-mode.js +2 -0
- package/dist/edit-modes/elevation-mode.d.ts +13 -0
- package/dist/edit-modes/elevation-mode.js +49 -0
- package/dist/edit-modes/extend-line-string-mode.d.ts +9 -0
- package/dist/edit-modes/extend-line-string-mode.js +72 -0
- package/dist/edit-modes/extrude-mode.d.ts +15 -0
- package/dist/edit-modes/extrude-mode.js +186 -0
- package/dist/edit-modes/geojson-edit-mode.d.ts +33 -0
- package/dist/edit-modes/geojson-edit-mode.js +208 -0
- package/dist/edit-modes/immutable-feature-collection.d.ts +43 -0
- package/dist/edit-modes/immutable-feature-collection.js +300 -0
- package/dist/edit-modes/measure-angle-mode.d.ts +11 -0
- package/dist/edit-modes/measure-angle-mode.js +99 -0
- package/dist/edit-modes/measure-area-mode.d.ts +8 -0
- package/dist/edit-modes/measure-area-mode.js +50 -0
- package/dist/edit-modes/measure-distance-mode.d.ts +19 -0
- package/dist/edit-modes/measure-distance-mode.js +171 -0
- package/dist/edit-modes/modify-mode.d.ts +15 -0
- package/dist/edit-modes/modify-mode.js +203 -0
- package/dist/edit-modes/resize-circle-mode.d.ts +16 -0
- package/dist/edit-modes/resize-circle-mode.js +142 -0
- package/dist/edit-modes/rotate-mode.d.ts +17 -0
- package/dist/edit-modes/rotate-mode.js +151 -0
- package/dist/edit-modes/scale-mode.d.ts +27 -0
- package/dist/edit-modes/scale-mode.js +173 -0
- package/dist/edit-modes/snappable-mode.d.ts +21 -0
- package/dist/edit-modes/snappable-mode.js +109 -0
- package/dist/edit-modes/split-polygon-mode.d.ts +10 -0
- package/dist/edit-modes/split-polygon-mode.js +164 -0
- package/dist/edit-modes/three-click-polygon-mode.d.ts +10 -0
- package/dist/edit-modes/three-click-polygon-mode.js +72 -0
- package/dist/edit-modes/transform-mode.d.ts +9 -0
- package/dist/edit-modes/transform-mode.js +63 -0
- package/dist/edit-modes/translate-mode.d.ts +13 -0
- package/dist/edit-modes/translate-mode.js +113 -0
- package/dist/edit-modes/two-click-polygon-mode.d.ts +13 -0
- package/dist/edit-modes/two-click-polygon-mode.js +93 -0
- package/dist/edit-modes/types.d.ts +86 -0
- package/dist/edit-modes/types.js +1 -0
- package/dist/edit-modes/utils.d.ts +36 -0
- package/dist/edit-modes/utils.js +381 -0
- package/dist/edit-modes/view-mode.d.ts +3 -0
- package/dist/edit-modes/view-mode.js +3 -0
- package/dist/editable-layers/editable-geojson-layer.d.ts +98 -0
- package/dist/editable-layers/editable-geojson-layer.js +450 -0
- package/dist/editable-layers/editable-h3-cluster-layer.d.ts +34 -0
- package/dist/editable-layers/editable-h3-cluster-layer.js +164 -0
- package/dist/editable-layers/editable-layer.d.ts +49 -0
- package/dist/editable-layers/editable-layer.js +195 -0
- package/dist/editable-layers/editable-path-layer.d.ts +9 -0
- package/dist/editable-layers/editable-path-layer.js +34 -0
- package/dist/editable-layers/elevated-edit-handle-layer.d.ts +7 -0
- package/dist/editable-layers/elevated-edit-handle-layer.js +24 -0
- package/dist/editable-layers/junction-scatterplot-layer.d.ts +14 -0
- package/dist/editable-layers/junction-scatterplot-layer.js +41 -0
- package/dist/editable-layers/path-marker-layer/arrow-2d-geometry.d.ts +4 -0
- package/dist/editable-layers/path-marker-layer/arrow-2d-geometry.js +55 -0
- package/dist/editable-layers/path-marker-layer/create-path-markers.d.ts +16 -0
- package/dist/editable-layers/path-marker-layer/create-path-markers.js +75 -0
- package/dist/editable-layers/path-marker-layer/path-marker-layer.d.ts +40 -0
- package/dist/editable-layers/path-marker-layer/path-marker-layer.js +121 -0
- package/dist/editable-layers/path-marker-layer/polyline.d.ts +18 -0
- package/dist/editable-layers/path-marker-layer/polyline.js +37 -0
- package/dist/editable-layers/path-outline-layer/path-outline-layer.d.ts +26 -0
- package/dist/editable-layers/path-outline-layer/path-outline-layer.js +106 -0
- package/dist/editable-layers/selection-layer.d.ts +26 -0
- package/dist/editable-layers/selection-layer.js +167 -0
- package/dist/geojson-types.d.ts +58 -0
- package/dist/geojson-types.js +2 -0
- package/dist/index.cjs +5825 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +62 -0
- package/dist/lib/constants.d.ts +6 -0
- package/dist/lib/constants.js +6 -0
- package/dist/lib/deck-renderer/deck-cache.d.ts +14 -0
- package/dist/lib/deck-renderer/deck-cache.js +51 -0
- package/dist/lib/deck-renderer/deck-drawer.d.ts +63 -0
- package/dist/lib/deck-renderer/deck-drawer.js +232 -0
- package/dist/lib/feature.d.ts +10 -0
- package/dist/lib/feature.js +16 -0
- package/dist/lib/layer-mouse-event.d.ts +11 -0
- package/dist/lib/layer-mouse-event.js +24 -0
- package/dist/lib/layers/junctions-layer.d.ts +8 -0
- package/dist/lib/layers/junctions-layer.js +33 -0
- package/dist/lib/layers/segments-layer.d.ts +18 -0
- package/dist/lib/layers/segments-layer.js +94 -0
- package/dist/lib/layers/texts-layer.d.ts +8 -0
- package/dist/lib/layers/texts-layer.js +32 -0
- package/dist/lib/math.d.ts +11 -0
- package/dist/lib/math.js +22 -0
- package/dist/lib/nebula-layer.d.ts +13 -0
- package/dist/lib/nebula-layer.js +26 -0
- package/dist/lib/nebula.d.ts +34 -0
- package/dist/lib/nebula.js +254 -0
- package/dist/lib/style.d.ts +19 -0
- package/dist/lib/style.js +20 -0
- package/dist/memoize.d.ts +6 -0
- package/dist/memoize.js +40 -0
- package/dist/mode-handlers/composite-mode-handler.d.ts +24 -0
- package/dist/mode-handlers/composite-mode-handler.js +55 -0
- package/dist/mode-handlers/draw-90degree-polygon-handler.d.ts +13 -0
- package/dist/mode-handlers/draw-90degree-polygon-handler.js +169 -0
- package/dist/mode-handlers/draw-circle-by-bounding-box-handler.d.ts +9 -0
- package/dist/mode-handlers/draw-circle-by-bounding-box-handler.js +29 -0
- package/dist/mode-handlers/draw-circle-from-center-handler.d.ts +9 -0
- package/dist/mode-handlers/draw-circle-from-center-handler.js +27 -0
- package/dist/mode-handlers/draw-ellipse-by-bounding-box-handler.d.ts +9 -0
- package/dist/mode-handlers/draw-ellipse-by-bounding-box-handler.js +30 -0
- package/dist/mode-handlers/draw-ellipse-using-three-points-handler.d.ts +9 -0
- package/dist/mode-handlers/draw-ellipse-using-three-points-handler.js +37 -0
- package/dist/mode-handlers/draw-line-string-handler.d.ts +9 -0
- package/dist/mode-handlers/draw-line-string-handler.js +83 -0
- package/dist/mode-handlers/draw-point-handler.d.ts +5 -0
- package/dist/mode-handlers/draw-point-handler.js +11 -0
- package/dist/mode-handlers/draw-polygon-handler.d.ts +11 -0
- package/dist/mode-handlers/draw-polygon-handler.js +92 -0
- package/dist/mode-handlers/draw-rectangle-handler.d.ts +9 -0
- package/dist/mode-handlers/draw-rectangle-handler.js +18 -0
- package/dist/mode-handlers/draw-rectangle-using-three-points-handler.d.ts +9 -0
- package/dist/mode-handlers/draw-rectangle-using-three-points-handler.js +49 -0
- package/dist/mode-handlers/duplicate-handler.d.ts +9 -0
- package/dist/mode-handlers/duplicate-handler.js +19 -0
- package/dist/mode-handlers/elevation-handler.d.ts +19 -0
- package/dist/mode-handlers/elevation-handler.js +48 -0
- package/dist/mode-handlers/extrude-handler.d.ts +18 -0
- package/dist/mode-handlers/extrude-handler.js +176 -0
- package/dist/mode-handlers/mode-handler.d.ts +61 -0
- package/dist/mode-handlers/mode-handler.js +286 -0
- package/dist/mode-handlers/modify-handler.d.ts +19 -0
- package/dist/mode-handlers/modify-handler.js +193 -0
- package/dist/mode-handlers/rotate-handler.d.ts +17 -0
- package/dist/mode-handlers/rotate-handler.js +74 -0
- package/dist/mode-handlers/scale-handler.d.ts +17 -0
- package/dist/mode-handlers/scale-handler.js +76 -0
- package/dist/mode-handlers/snappable-handler.d.ts +33 -0
- package/dist/mode-handlers/snappable-handler.js +133 -0
- package/dist/mode-handlers/split-polygon-handler.d.ts +11 -0
- package/dist/mode-handlers/split-polygon-handler.js +154 -0
- package/dist/mode-handlers/three-click-polygon-handler.d.ts +5 -0
- package/dist/mode-handlers/three-click-polygon-handler.js +18 -0
- package/dist/mode-handlers/translate-handler.d.ts +17 -0
- package/dist/mode-handlers/translate-handler.js +72 -0
- package/dist/mode-handlers/two-click-polygon-handler.d.ts +5 -0
- package/dist/mode-handlers/two-click-polygon-handler.js +18 -0
- package/dist/mode-handlers/view-handler.d.ts +8 -0
- package/dist/mode-handlers/view-handler.js +10 -0
- package/dist/shaderlib/color/color.d.ts +8 -0
- package/dist/shaderlib/color/color.js +51 -0
- package/dist/shaderlib/outline/outline.d.ts +8 -0
- package/dist/shaderlib/outline/outline.js +97 -0
- package/dist/shaderlib/utils/utils.d.ts +8 -0
- package/dist/shaderlib/utils/utils.js +28 -0
- package/dist/translateFromCenter.d.ts +4 -0
- package/dist/translateFromCenter.js +19 -0
- package/dist/types.d.ts +35 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +20 -0
- package/dist/utils.js +144 -0
- package/package.json +84 -0
- package/src/constants.ts +15 -0
- package/src/curve-utils.ts +77 -0
- package/src/edit-modes/composite-mode.ts +74 -0
- package/src/edit-modes/draw-90degree-polygon-mode.ts +220 -0
- package/src/edit-modes/draw-circle-by-diameter-mode.ts +88 -0
- package/src/edit-modes/draw-circle-from-center-mode.ts +79 -0
- package/src/edit-modes/draw-ellipse-by-bounding-box-mode.ts +25 -0
- package/src/edit-modes/draw-ellipse-using-three-points-mode.ts +23 -0
- package/src/edit-modes/draw-line-string-mode.ts +200 -0
- package/src/edit-modes/draw-point-mode.ts +35 -0
- package/src/edit-modes/draw-polygon-by-dragging-mode.ts +106 -0
- package/src/edit-modes/draw-polygon-mode.ts +171 -0
- package/src/edit-modes/draw-rectangle-from-center-mode.ts +23 -0
- package/src/edit-modes/draw-rectangle-mode.ts +14 -0
- package/src/edit-modes/draw-rectangle-using-three-points-mode.ts +36 -0
- package/src/edit-modes/draw-square-from-center-mode.ts +46 -0
- package/src/edit-modes/draw-square-mode.ts +36 -0
- package/src/edit-modes/duplicate-mode.ts +21 -0
- package/src/edit-modes/edit-mode.ts +30 -0
- package/src/edit-modes/elevation-mode.ts +86 -0
- package/src/edit-modes/extend-line-string-mode.ts +87 -0
- package/src/edit-modes/extrude-mode.ts +254 -0
- package/src/edit-modes/geojson-edit-mode.ts +283 -0
- package/src/edit-modes/immutable-feature-collection.ts +417 -0
- package/src/edit-modes/measure-angle-mode.ts +127 -0
- package/src/edit-modes/measure-area-mode.ts +62 -0
- package/src/edit-modes/measure-distance-mode.ts +215 -0
- package/src/edit-modes/modify-mode.ts +293 -0
- package/src/edit-modes/resize-circle-mode.ts +202 -0
- package/src/edit-modes/rotate-mode.ts +208 -0
- package/src/edit-modes/scale-mode.ts +231 -0
- package/src/edit-modes/snappable-mode.ts +174 -0
- package/src/edit-modes/split-polygon-mode.ts +201 -0
- package/src/edit-modes/three-click-polygon-mode.ts +111 -0
- package/src/edit-modes/transform-mode.ts +75 -0
- package/src/edit-modes/translate-mode.ts +161 -0
- package/src/edit-modes/two-click-polygon-mode.ts +132 -0
- package/src/edit-modes/types.ts +135 -0
- package/src/edit-modes/utils.ts +513 -0
- package/src/edit-modes/view-mode.ts +3 -0
- package/src/editable-layers/editable-geojson-layer.ts +603 -0
- package/src/editable-layers/editable-h3-cluster-layer.ts +226 -0
- package/src/editable-layers/editable-layer.ts +252 -0
- package/src/editable-layers/editable-path-layer.ts +51 -0
- package/src/editable-layers/elevated-edit-handle-layer.ts +33 -0
- package/src/editable-layers/junction-scatterplot-layer.ts +53 -0
- package/src/editable-layers/path-marker-layer/arrow-2d-geometry.ts +61 -0
- package/src/editable-layers/path-marker-layer/create-path-markers.ts +107 -0
- package/src/editable-layers/path-marker-layer/path-marker-layer.ts +179 -0
- package/src/editable-layers/path-marker-layer/polyline.ts +40 -0
- package/src/editable-layers/path-outline-layer/path-outline-layer.ts +147 -0
- package/src/editable-layers/selection-layer.ts +209 -0
- package/src/geojson-types.ts +89 -0
- package/src/index.ts +125 -0
- package/src/lib/constants.ts +6 -0
- package/src/lib/deck-renderer/deck-cache.ts +61 -0
- package/src/lib/deck-renderer/deck-drawer.ts +263 -0
- package/src/lib/feature.ts +27 -0
- package/src/lib/layer-mouse-event.ts +32 -0
- package/src/lib/layers/junctions-layer.ts +40 -0
- package/src/lib/layers/segments-layer.ts +108 -0
- package/src/lib/layers/texts-layer.ts +43 -0
- package/src/lib/math.ts +26 -0
- package/src/lib/nebula-layer.ts +33 -0
- package/src/lib/nebula.ts +323 -0
- package/src/lib/style.ts +22 -0
- package/src/memoize.ts +43 -0
- package/src/mode-handlers/composite-mode-handler.ts +89 -0
- package/src/mode-handlers/draw-90degree-polygon-handler.ts +201 -0
- package/src/mode-handlers/draw-circle-by-bounding-box-handler.ts +39 -0
- package/src/mode-handlers/draw-circle-from-center-handler.ts +38 -0
- package/src/mode-handlers/draw-ellipse-by-bounding-box-handler.ts +41 -0
- package/src/mode-handlers/draw-ellipse-using-three-points-handler.ts +46 -0
- package/src/mode-handlers/draw-line-string-handler.ts +108 -0
- package/src/mode-handlers/draw-point-handler.ts +15 -0
- package/src/mode-handlers/draw-polygon-handler.ts +121 -0
- package/src/mode-handlers/draw-rectangle-handler.ts +28 -0
- package/src/mode-handlers/draw-rectangle-using-three-points-handler.ts +60 -0
- package/src/mode-handlers/duplicate-handler.ts +25 -0
- package/src/mode-handlers/elevation-handler.ts +88 -0
- package/src/mode-handlers/extrude-handler.ts +245 -0
- package/src/mode-handlers/mode-handler.ts +394 -0
- package/src/mode-handlers/modify-handler.ts +263 -0
- package/src/mode-handlers/rotate-handler.ts +107 -0
- package/src/mode-handlers/scale-handler.ts +105 -0
- package/src/mode-handlers/snappable-handler.ts +184 -0
- package/src/mode-handlers/split-polygon-handler.ts +177 -0
- package/src/mode-handlers/three-click-polygon-handler.ts +25 -0
- package/src/mode-handlers/translate-handler.ts +110 -0
- package/src/mode-handlers/two-click-polygon-handler.ts +25 -0
- package/src/mode-handlers/view-handler.ts +13 -0
- package/src/shaderlib/color/color.ts +56 -0
- package/src/shaderlib/outline/outline.ts +101 -0
- package/src/shaderlib/utils/utils.ts +33 -0
- package/src/translateFromCenter.ts +48 -0
- package/src/types.ts +39 -0
- package/src/utils.ts +185 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import turfBearing from '@turf/bearing';
|
|
2
|
+
import turfDistance from '@turf/distance';
|
|
3
|
+
import turfTransformTranslate from '@turf/transform-translate';
|
|
4
|
+
import { point } from '@turf/helpers';
|
|
5
|
+
import { FeatureCollection, Position } from '../geojson-types';
|
|
6
|
+
import { PointerMoveEvent, StartDraggingEvent, StopDraggingEvent } from '../edit-modes/types';
|
|
7
|
+
import { EditAction, ModeHandler } from './mode-handler';
|
|
8
|
+
|
|
9
|
+
// TODO edit-modes: delete handlers once EditMode fully implemented
|
|
10
|
+
export class TranslateHandler extends ModeHandler {
|
|
11
|
+
_geometryBeforeTranslate: FeatureCollection | null | undefined;
|
|
12
|
+
_isTranslatable: boolean = undefined!;
|
|
13
|
+
|
|
14
|
+
handlePointerMove(event: PointerMoveEvent): {
|
|
15
|
+
editAction: EditAction | null | undefined;
|
|
16
|
+
cancelMapPan: boolean;
|
|
17
|
+
} {
|
|
18
|
+
let editAction: EditAction | null | undefined = null;
|
|
19
|
+
|
|
20
|
+
this._isTranslatable =
|
|
21
|
+
Boolean(this._geometryBeforeTranslate) || this.isSelectionPicked(event.picks);
|
|
22
|
+
|
|
23
|
+
if (!this._isTranslatable || !event.pointerDownMapCoords) {
|
|
24
|
+
// Nothing to do
|
|
25
|
+
return { editAction: null, cancelMapPan: false };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (event.isDragging && this._geometryBeforeTranslate) {
|
|
29
|
+
// Translate the geometry
|
|
30
|
+
editAction = this.getTranslateAction(
|
|
31
|
+
event.pointerDownMapCoords,
|
|
32
|
+
event.mapCoords,
|
|
33
|
+
'translating'
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return { editAction, cancelMapPan: true };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
handleStartDragging(event: StartDraggingEvent): EditAction | null | undefined {
|
|
41
|
+
if (!this._isTranslatable) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this._geometryBeforeTranslate = this.getSelectedFeaturesAsFeatureCollection();
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
handleStopDragging(event: StopDraggingEvent): EditAction | null | undefined {
|
|
50
|
+
let editAction: EditAction | null | undefined = null;
|
|
51
|
+
|
|
52
|
+
if (this._geometryBeforeTranslate) {
|
|
53
|
+
// Translate the geometry
|
|
54
|
+
editAction = this.getTranslateAction(
|
|
55
|
+
event.pointerDownMapCoords,
|
|
56
|
+
event.mapCoords,
|
|
57
|
+
'translated'
|
|
58
|
+
);
|
|
59
|
+
this._geometryBeforeTranslate = null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return editAction;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
getCursor({ isDragging }: { isDragging: boolean }): string {
|
|
66
|
+
if (this._isTranslatable) {
|
|
67
|
+
return 'move';
|
|
68
|
+
}
|
|
69
|
+
return isDragging ? 'grabbing' : 'grab';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getTranslateAction(
|
|
73
|
+
startDragPoint: Position,
|
|
74
|
+
currentPoint: Position,
|
|
75
|
+
editType: string
|
|
76
|
+
): EditAction | null | undefined {
|
|
77
|
+
if (!this._geometryBeforeTranslate) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const p1 = point(startDragPoint);
|
|
81
|
+
const p2 = point(currentPoint);
|
|
82
|
+
|
|
83
|
+
const distanceMoved = turfDistance(p1, p2);
|
|
84
|
+
const direction = turfBearing(p1, p2);
|
|
85
|
+
|
|
86
|
+
// @ts-expect-error turf type diff
|
|
87
|
+
const movedFeatures: FeatureCollection = turfTransformTranslate(
|
|
88
|
+
// @ts-expect-error turf type diff
|
|
89
|
+
this._geometryBeforeTranslate,
|
|
90
|
+
distanceMoved,
|
|
91
|
+
direction
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
let updatedData = this.getImmutableFeatureCollection();
|
|
95
|
+
|
|
96
|
+
const selectedIndexes = this.getSelectedFeatureIndexes();
|
|
97
|
+
for (let i = 0; i < selectedIndexes.length; i++) {
|
|
98
|
+
const selectedIndex = selectedIndexes[i];
|
|
99
|
+
const movedFeature = movedFeatures.features[i];
|
|
100
|
+
updatedData = updatedData.replaceGeometry(selectedIndex, movedFeature.geometry);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
updatedData: updatedData.getObject(),
|
|
105
|
+
editType,
|
|
106
|
+
featureIndexes: selectedIndexes,
|
|
107
|
+
editContext: null,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ClickEvent } from '../edit-modes/types';
|
|
2
|
+
import { ModeHandler, EditAction } from './mode-handler';
|
|
3
|
+
|
|
4
|
+
// TODO edit-modes: delete handlers once EditMode fully implemented
|
|
5
|
+
export class TwoClickPolygonHandler extends ModeHandler {
|
|
6
|
+
handleClick(event: ClickEvent): EditAction | null | undefined {
|
|
7
|
+
super.handleClick(event);
|
|
8
|
+
|
|
9
|
+
const tentativeFeature = this.getTentativeFeature();
|
|
10
|
+
const clickSequence = this.getClickSequence();
|
|
11
|
+
|
|
12
|
+
if (
|
|
13
|
+
clickSequence.length > 1 &&
|
|
14
|
+
tentativeFeature &&
|
|
15
|
+
tentativeFeature.geometry.type === 'Polygon'
|
|
16
|
+
) {
|
|
17
|
+
const editAction = this.getAddFeatureOrBooleanPolygonAction(tentativeFeature.geometry);
|
|
18
|
+
this.resetClickSequence();
|
|
19
|
+
this._setTentativeFeature(null);
|
|
20
|
+
return editAction;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Position } from '../geojson-types';
|
|
2
|
+
import { EditHandle, ModeHandler } from './mode-handler';
|
|
3
|
+
|
|
4
|
+
// TODO edit-modes: delete handlers once EditMode fully implemented
|
|
5
|
+
export class ViewHandler extends ModeHandler {
|
|
6
|
+
getCursor({ isDragging }: { isDragging: boolean }): string {
|
|
7
|
+
return isDragging ? 'grabbing' : 'grab';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
getEditHandles(picks?: Array<Record<string, any>>, mapCoords?: Position): EditHandle[] {
|
|
11
|
+
return [];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// TODO - this module is a WIP
|
|
2
|
+
|
|
3
|
+
/* eslint-disable camelcase */
|
|
4
|
+
const INITIAL_STATE: Record<string, number> = {
|
|
5
|
+
color_uOpacity: 1.0,
|
|
6
|
+
color_uDesaturate: 0.0,
|
|
7
|
+
color_uBrightness: 1.0,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
function getUniforms(opts = INITIAL_STATE) {
|
|
11
|
+
const uniforms: Record<string, number> = {};
|
|
12
|
+
if (opts.opacity) {
|
|
13
|
+
uniforms.color_uOpacity = opts.opacity;
|
|
14
|
+
}
|
|
15
|
+
return uniforms;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const vs = `\
|
|
19
|
+
in vec4 color_vColor;
|
|
20
|
+
|
|
21
|
+
color_setColor(vec4 color) {
|
|
22
|
+
color_vColor = color;
|
|
23
|
+
}
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const fs = `\
|
|
27
|
+
uniform float color_uOpacity;
|
|
28
|
+
uniform float color_uDesaturate;
|
|
29
|
+
uniform float color_uBrightness;
|
|
30
|
+
|
|
31
|
+
in vec4 color_vColor;
|
|
32
|
+
|
|
33
|
+
out vec4 fragColor;
|
|
34
|
+
|
|
35
|
+
vec4 color_getColor() {
|
|
36
|
+
return color_vColor;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
vec4 color_filterColor(vec4 color) {
|
|
40
|
+
// apply desaturation and brightness
|
|
41
|
+
if (color_uDesaturate > 0.01) {
|
|
42
|
+
float luminance = (color.r + color.g + color.b) * 0.333333333 + color_uBrightness;
|
|
43
|
+
color = vec4(mix(color.rgb, vec3(luminance), color_uDesaturate), color.a);
|
|
44
|
+
|
|
45
|
+
// Apply opacity
|
|
46
|
+
color = vec4(color.rgb, color.a * color_uOpacity);
|
|
47
|
+
return color;
|
|
48
|
+
}
|
|
49
|
+
`;
|
|
50
|
+
|
|
51
|
+
export default {
|
|
52
|
+
name: 'color',
|
|
53
|
+
vs,
|
|
54
|
+
fs,
|
|
55
|
+
getUniforms,
|
|
56
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
const INITIAL_STATE: Record<string, any> = {
|
|
3
|
+
outlineEnabled: false,
|
|
4
|
+
outlineRenderShadowmap: false,
|
|
5
|
+
outlineShadowmap: null,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
function getUniforms({ outlineEnabled, outlineRenderShadowmap, outlineShadowmap } = INITIAL_STATE) {
|
|
9
|
+
const uniforms: Record<string, any> = {};
|
|
10
|
+
if (outlineEnabled !== undefined) {
|
|
11
|
+
// ? 1.0 : 0.0;
|
|
12
|
+
uniforms.outline_uEnabled = outlineEnabled;
|
|
13
|
+
}
|
|
14
|
+
if (outlineRenderShadowmap !== undefined) {
|
|
15
|
+
// ? 1.0 : 0.0;
|
|
16
|
+
uniforms.outline_uRenderOutlines = outlineRenderShadowmap;
|
|
17
|
+
}
|
|
18
|
+
if (outlineShadowmap !== undefined) {
|
|
19
|
+
uniforms.outline_uShadowmap = outlineShadowmap;
|
|
20
|
+
}
|
|
21
|
+
return uniforms;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const vs = `\
|
|
25
|
+
#version 300 es
|
|
26
|
+
in float instanceZLevel;
|
|
27
|
+
out float outline_vzLevel;
|
|
28
|
+
out vec4 outline_vPosition;
|
|
29
|
+
|
|
30
|
+
// Set the z level for the outline shadowmap rendering
|
|
31
|
+
void outline_setZLevel(float zLevel) {
|
|
32
|
+
outline_vzLevel = zLevel;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Store an adjusted position for texture2DProj
|
|
36
|
+
void outline_setUV(vec4 position) {
|
|
37
|
+
// mat4(
|
|
38
|
+
// 0.5, 0.0, 0.0, 0.0,
|
|
39
|
+
// 0.0, 0.5, 0.0, 0.0,
|
|
40
|
+
// 0.0, 0.0, 0.5, 0.0,
|
|
41
|
+
// 0.5, 0.5, 0.5, 1.0
|
|
42
|
+
// ) * position;
|
|
43
|
+
outline_vPosition = vec4(position.xyz * 0.5 + position.w * 0.5, position.w);
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
const fs = `\
|
|
48
|
+
uniform bool outline_uEnabled;
|
|
49
|
+
uniform bool outline_uRenderOutlines;
|
|
50
|
+
uniform sampler2D outline_uShadowmap;
|
|
51
|
+
|
|
52
|
+
in float outline_vzLevel;
|
|
53
|
+
// in vec2 outline_vUV;
|
|
54
|
+
in vec4 outline_vPosition;
|
|
55
|
+
|
|
56
|
+
out vec4 fragColor;
|
|
57
|
+
|
|
58
|
+
const float OUTLINE_Z_LEVEL_ERROR = 0.01;
|
|
59
|
+
|
|
60
|
+
// Return a darker color in shadowmap
|
|
61
|
+
vec4 outline_filterShadowColor(vec4 color) {
|
|
62
|
+
return vec4(outline_vzLevel / 255., outline_vzLevel / 255., outline_vzLevel / 255., 1.);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Return a darker color if in shadowmap
|
|
66
|
+
vec4 outline_filterDarkenColor(vec4 color) {
|
|
67
|
+
if (outline_uEnabled) {
|
|
68
|
+
float maxZLevel;
|
|
69
|
+
if (outline_vPosition.q > 0.0) {
|
|
70
|
+
maxZLevel = texture2DProj(outline_uShadowmap, outline_vPosition).r * 255.;
|
|
71
|
+
} else {
|
|
72
|
+
discard;
|
|
73
|
+
}
|
|
74
|
+
if (maxZLevel < outline_vzLevel + OUTLINE_Z_LEVEL_ERROR) {
|
|
75
|
+
vec4(color.rgb * 0.5, color.a);
|
|
76
|
+
} else {
|
|
77
|
+
discard;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return color;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// if enabled and rendering outlines - Render depth to shadowmap
|
|
84
|
+
// if enabled and rendering colors - Return a darker color if in shadowmap
|
|
85
|
+
// if disabled, just return color
|
|
86
|
+
vec4 outline_filterColor(vec4 color) {
|
|
87
|
+
if (outline_uEnabled) {
|
|
88
|
+
return outline_uRenderOutlines ?
|
|
89
|
+
outline_filterShadowColor(color) :
|
|
90
|
+
outline_filterDarkenColor(color);
|
|
91
|
+
}
|
|
92
|
+
return color;
|
|
93
|
+
}
|
|
94
|
+
`;
|
|
95
|
+
|
|
96
|
+
export default {
|
|
97
|
+
name: 'outline',
|
|
98
|
+
vs,
|
|
99
|
+
fs,
|
|
100
|
+
getUniforms,
|
|
101
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// TODO - this module is a WIP
|
|
2
|
+
|
|
3
|
+
/* eslint-disable camelcase */
|
|
4
|
+
const INITIAL_STATE = {};
|
|
5
|
+
|
|
6
|
+
function getUniforms({} = INITIAL_STATE) {}
|
|
7
|
+
|
|
8
|
+
const vs = `\
|
|
9
|
+
// Note - fairly generic, move to a UV or screen package, or even project?
|
|
10
|
+
vec2 project_clipspace_to_uv(vec4 position) {
|
|
11
|
+
vec2 p = vec2(position.x / position.w, position.y / position.w);
|
|
12
|
+
return vec2((p.x + 1.0) / 2.0, (p.y + 1.0) / 2.0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
vec2 project_clipspace_to_projective_uv(vec4 position) {
|
|
16
|
+
// outline_vPosition = mat4(
|
|
17
|
+
// 0.5, 0.0, 0.0, 0.0,
|
|
18
|
+
// 0.0, 0.5, 0.0, 0.0,
|
|
19
|
+
// 0.0, 0.0, 0.5, 0.0,
|
|
20
|
+
// 0.5, 0.5, 0.5, 1.0
|
|
21
|
+
// ) * position;
|
|
22
|
+
return vec4(position.xyz * 0.5 + position.w * 0.5, position.w);
|
|
23
|
+
}
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const fs = vs;
|
|
27
|
+
|
|
28
|
+
export default {
|
|
29
|
+
name: 'outline',
|
|
30
|
+
vs,
|
|
31
|
+
fs,
|
|
32
|
+
getUniforms,
|
|
33
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import turfCenter from '@turf/center';
|
|
2
|
+
import turfRhumbBearing from '@turf/rhumb-bearing';
|
|
3
|
+
import turfRhumbDistance from '@turf/rhumb-distance';
|
|
4
|
+
import turfRhumbDestination from '@turf/rhumb-destination';
|
|
5
|
+
import type {
|
|
6
|
+
Position as TurfPosition,
|
|
7
|
+
Feature as TurfFeature,
|
|
8
|
+
Geometry as TurfGeometry,
|
|
9
|
+
} from '@turf/helpers';
|
|
10
|
+
import { mapCoords } from './edit-modes/utils';
|
|
11
|
+
import type { AnyCoordinates, Position } from './';
|
|
12
|
+
|
|
13
|
+
// This function takes feature's center, moves it,
|
|
14
|
+
// and builds new feature around it keeping the proportions
|
|
15
|
+
export function translateFromCenter(
|
|
16
|
+
feature: TurfFeature<TurfGeometry>,
|
|
17
|
+
distance: number,
|
|
18
|
+
direction: number
|
|
19
|
+
) {
|
|
20
|
+
const initialCenterPoint = turfCenter(feature as TurfFeature);
|
|
21
|
+
|
|
22
|
+
const movedCenterPoint = turfRhumbDestination(initialCenterPoint, distance, direction);
|
|
23
|
+
|
|
24
|
+
const movedCoordinates = mapCoords(
|
|
25
|
+
feature.geometry.coordinates as AnyCoordinates,
|
|
26
|
+
(coordinate) => {
|
|
27
|
+
const distance = turfRhumbDistance(
|
|
28
|
+
initialCenterPoint.geometry.coordinates,
|
|
29
|
+
coordinate as TurfPosition
|
|
30
|
+
);
|
|
31
|
+
const direction = turfRhumbBearing(
|
|
32
|
+
initialCenterPoint.geometry.coordinates,
|
|
33
|
+
coordinate as TurfPosition
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const movedPosition = turfRhumbDestination(
|
|
37
|
+
movedCenterPoint.geometry.coordinates,
|
|
38
|
+
distance,
|
|
39
|
+
direction
|
|
40
|
+
).geometry.coordinates;
|
|
41
|
+
return movedPosition as Position;
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
feature.geometry.coordinates = movedCoordinates;
|
|
46
|
+
|
|
47
|
+
return feature;
|
|
48
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type LayerMouseEventResult = {
|
|
2
|
+
eventConsumed?: boolean;
|
|
3
|
+
eventSoftConsumed?: boolean;
|
|
4
|
+
mousePointer?: string | null | undefined;
|
|
5
|
+
shouldRedraw?: boolean | string[];
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// [red, green, blue, alpha] in premultiplied alpha format
|
|
9
|
+
export type Color = [number, number, number, number];
|
|
10
|
+
|
|
11
|
+
export type Style = {
|
|
12
|
+
dashArray?: number[];
|
|
13
|
+
fillColor?: Color;
|
|
14
|
+
lineColor?: Color;
|
|
15
|
+
lineWidthMeters?: number;
|
|
16
|
+
pointRadiusMeters?: number;
|
|
17
|
+
outlineRadiusMeters?: number;
|
|
18
|
+
outlineColor?: Color;
|
|
19
|
+
mousePriority?: number;
|
|
20
|
+
arrowColor?: Color;
|
|
21
|
+
arrowStyle?: number;
|
|
22
|
+
arrowCount?: number;
|
|
23
|
+
iconNumber?: number;
|
|
24
|
+
text?: string;
|
|
25
|
+
tooltip?: string;
|
|
26
|
+
zLevel?: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type Viewport = {
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
longitude: number;
|
|
33
|
+
latitude: number;
|
|
34
|
+
zoom: number;
|
|
35
|
+
isDragging?: boolean;
|
|
36
|
+
isMoving?: boolean;
|
|
37
|
+
bearing?: number;
|
|
38
|
+
pitch?: number;
|
|
39
|
+
};
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import destination from '@turf/destination';
|
|
2
|
+
import bearing from '@turf/bearing';
|
|
3
|
+
import pointToLineDistance from '@turf/point-to-line-distance';
|
|
4
|
+
import { point } from '@turf/helpers';
|
|
5
|
+
import WebMercatorViewport from 'viewport-mercator-project';
|
|
6
|
+
import { FeatureOf, FeatureWithProps, LineString, Point, Position } from './geojson-types';
|
|
7
|
+
import { Viewport } from './types';
|
|
8
|
+
|
|
9
|
+
// TODO edit-modes: delete and use edit-modes/utils instead
|
|
10
|
+
|
|
11
|
+
export type NearestPointType = FeatureWithProps<Point, { dist: number; index: number }>;
|
|
12
|
+
|
|
13
|
+
export function toDeckColor(
|
|
14
|
+
color?: [number, number, number, number] | number,
|
|
15
|
+
defaultColor: [number, number, number, number] = [255, 0, 0, 255]
|
|
16
|
+
): [number, number, number, number] {
|
|
17
|
+
if (!Array.isArray(color)) {
|
|
18
|
+
return defaultColor;
|
|
19
|
+
}
|
|
20
|
+
return [color[0] * 255, color[1] * 255, color[2] * 255, color[3] * 255];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//
|
|
24
|
+
// a GeoJSON helper function that calls the provided function with
|
|
25
|
+
// an argument that is the most deeply-nested array having elements
|
|
26
|
+
// that are arrays of primitives as an argument, e.g.
|
|
27
|
+
//
|
|
28
|
+
// {
|
|
29
|
+
// "type": "MultiPolygon",
|
|
30
|
+
// "coordinates": [
|
|
31
|
+
// [
|
|
32
|
+
// [[30, 20], [45, 40], [10, 40], [30, 20]]
|
|
33
|
+
// ],
|
|
34
|
+
// [
|
|
35
|
+
// [[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]
|
|
36
|
+
// ]
|
|
37
|
+
// ]
|
|
38
|
+
// }
|
|
39
|
+
//
|
|
40
|
+
// the function would be called on:
|
|
41
|
+
//
|
|
42
|
+
// [[30, 20], [45, 40], [10, 40], [30, 20]]
|
|
43
|
+
//
|
|
44
|
+
// and
|
|
45
|
+
//
|
|
46
|
+
// [[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]
|
|
47
|
+
//
|
|
48
|
+
export function recursivelyTraverseNestedArrays(
|
|
49
|
+
array: Array<any>,
|
|
50
|
+
prefix: Array<number>,
|
|
51
|
+
fn: Function
|
|
52
|
+
) {
|
|
53
|
+
if (!Array.isArray(array[0])) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
for (let i = 0; i < array.length; i++) {
|
|
57
|
+
if (recursivelyTraverseNestedArrays(array[i], [...prefix, i], fn)) {
|
|
58
|
+
fn(array, prefix);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function generatePointsParallelToLinePoints(
|
|
66
|
+
p1: Position,
|
|
67
|
+
p2: Position,
|
|
68
|
+
mapCoords: Position
|
|
69
|
+
): Position[] {
|
|
70
|
+
const lineString: LineString = {
|
|
71
|
+
type: 'LineString',
|
|
72
|
+
coordinates: [p1, p2],
|
|
73
|
+
};
|
|
74
|
+
const pt = point(mapCoords);
|
|
75
|
+
const ddistance = pointToLineDistance(pt, lineString);
|
|
76
|
+
const lineBearing = bearing(p1, p2);
|
|
77
|
+
|
|
78
|
+
// Check if current point is to the left or right of line
|
|
79
|
+
// Line from A=(x1,y1) to B=(x2,y2) a point P=(x,y)
|
|
80
|
+
// then (x−x1)(y2−y1)−(y−y1)(x2−x1)
|
|
81
|
+
const isPointToLeftOfLine =
|
|
82
|
+
(mapCoords[0] - p1[0]) * (p2[1] - p1[1]) - (mapCoords[1] - p1[1]) * (p2[0] - p1[0]);
|
|
83
|
+
|
|
84
|
+
// Bearing to draw perpendicular to the line string
|
|
85
|
+
const orthogonalBearing = isPointToLeftOfLine < 0 ? lineBearing - 90 : lineBearing - 270;
|
|
86
|
+
|
|
87
|
+
// Get coordinates for the point p3 and p4 which are perpendicular to the lineString
|
|
88
|
+
// Add the distance as the current position moves away from the lineString
|
|
89
|
+
const p3 = destination(p2, ddistance, orthogonalBearing);
|
|
90
|
+
const p4 = destination(p1, ddistance, orthogonalBearing);
|
|
91
|
+
return [p3.geometry.coordinates, p4.geometry.coordinates] as [Position, Position];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function distance2d(x1: number, y1: number, x2: number, y2: number): number {
|
|
95
|
+
const dx = x1 - x2;
|
|
96
|
+
const dy = y1 - y2;
|
|
97
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function mix(a: number, b: number, ratio: number): number {
|
|
101
|
+
return b * ratio + a * (1 - ratio);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function nearestPointOnProjectedLine(
|
|
105
|
+
line: FeatureOf<LineString>,
|
|
106
|
+
inPoint: FeatureOf<Point>,
|
|
107
|
+
viewport: Viewport
|
|
108
|
+
): NearestPointType {
|
|
109
|
+
const wmViewport = new WebMercatorViewport(viewport);
|
|
110
|
+
// Project the line to viewport, then find the nearest point
|
|
111
|
+
const coordinates: Array<Array<number>> = line.geometry.coordinates as any;
|
|
112
|
+
const projectedCoords = coordinates.map(([x, y, z = 0]) => wmViewport.project([x, y, z]));
|
|
113
|
+
const [x, y] = wmViewport.project(inPoint.geometry.coordinates);
|
|
114
|
+
// console.log('projectedCoords', JSON.stringify(projectedCoords));
|
|
115
|
+
|
|
116
|
+
let minDistance = Infinity;
|
|
117
|
+
let minPointInfo = {};
|
|
118
|
+
|
|
119
|
+
projectedCoords.forEach(([x2, y2], index) => {
|
|
120
|
+
if (index === 0) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const [x1, y1] = projectedCoords[index - 1];
|
|
125
|
+
|
|
126
|
+
// line from projectedCoords[index - 1] to projectedCoords[index]
|
|
127
|
+
// convert to Ax + By + C = 0
|
|
128
|
+
const A = y1 - y2;
|
|
129
|
+
const B = x2 - x1;
|
|
130
|
+
const C = x1 * y2 - x2 * y1;
|
|
131
|
+
|
|
132
|
+
// https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
|
|
133
|
+
const div = A * A + B * B;
|
|
134
|
+
const distance = Math.abs(A * x + B * y + C) / Math.sqrt(div);
|
|
135
|
+
|
|
136
|
+
// TODO: Check if inside bounds
|
|
137
|
+
|
|
138
|
+
if (distance < minDistance) {
|
|
139
|
+
minDistance = distance;
|
|
140
|
+
minPointInfo = {
|
|
141
|
+
index,
|
|
142
|
+
x0: (B * (B * x - A * y) - A * C) / div,
|
|
143
|
+
y0: (A * (-B * x + A * y) - B * C) / div,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
const { index, x0, y0 } = minPointInfo as any;
|
|
148
|
+
const [x1, y1, z1 = 0] = projectedCoords[index - 1];
|
|
149
|
+
const [x2, y2, z2 = 0] = projectedCoords[index];
|
|
150
|
+
|
|
151
|
+
// calculate what ratio of the line we are on to find the proper z
|
|
152
|
+
const lineLength = distance2d(x1, y1, x2, y2);
|
|
153
|
+
const startToPointLength = distance2d(x1, y1, x0, y0);
|
|
154
|
+
const ratio = startToPointLength / lineLength;
|
|
155
|
+
const z0 = mix(z1, z2, ratio);
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
type: 'Feature',
|
|
159
|
+
geometry: {
|
|
160
|
+
type: 'Point',
|
|
161
|
+
// @ts-expect-error Position type diff
|
|
162
|
+
coordinates: wmViewport.unproject([x0, y0, z0]),
|
|
163
|
+
},
|
|
164
|
+
properties: {
|
|
165
|
+
// TODO: calculate the distance in proper units
|
|
166
|
+
dist: minDistance,
|
|
167
|
+
index: index - 1,
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Inserts toInsert string into base string before insertBefore string.
|
|
174
|
+
* @param base A string to insert into.
|
|
175
|
+
* @param insertBefore A sub string in `base` string to insert before.
|
|
176
|
+
* @param toInsert A string to insert.
|
|
177
|
+
* @returns Combined string. `base` string if `insertBefore` string isn't found.
|
|
178
|
+
*/
|
|
179
|
+
export function insertBefore(base: string, insertBefore: string, toInsert: string): string {
|
|
180
|
+
const at = base.indexOf(insertBefore);
|
|
181
|
+
if (at < 0) {
|
|
182
|
+
return base;
|
|
183
|
+
}
|
|
184
|
+
return base.slice(0, at) + toInsert + base.slice(at);
|
|
185
|
+
}
|