@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.
Files changed (295) hide show
  1. package/README.md +82 -0
  2. package/dist/constants.d.ts +14 -0
  3. package/dist/constants.js +14 -0
  4. package/dist/curve-utils.d.ts +2 -0
  5. package/dist/curve-utils.js +61 -0
  6. package/dist/edit-modes/composite-mode.d.ts +14 -0
  7. package/dist/edit-modes/composite-mode.js +47 -0
  8. package/dist/edit-modes/draw-90degree-polygon-mode.d.ts +11 -0
  9. package/dist/edit-modes/draw-90degree-polygon-mode.js +179 -0
  10. package/dist/edit-modes/draw-circle-by-diameter-mode.d.ts +24 -0
  11. package/dist/edit-modes/draw-circle-by-diameter-mode.js +78 -0
  12. package/dist/edit-modes/draw-circle-from-center-mode.d.ts +22 -0
  13. package/dist/edit-modes/draw-circle-from-center-mode.js +70 -0
  14. package/dist/edit-modes/draw-ellipse-by-bounding-box-mode.d.ts +5 -0
  15. package/dist/edit-modes/draw-ellipse-by-bounding-box-mode.js +20 -0
  16. package/dist/edit-modes/draw-ellipse-using-three-points-mode.d.ts +5 -0
  17. package/dist/edit-modes/draw-ellipse-using-three-points-mode.js +16 -0
  18. package/dist/edit-modes/draw-line-string-mode.d.ts +25 -0
  19. package/dist/edit-modes/draw-line-string-mode.js +170 -0
  20. package/dist/edit-modes/draw-point-mode.d.ts +8 -0
  21. package/dist/edit-modes/draw-point-mode.js +28 -0
  22. package/dist/edit-modes/draw-polygon-by-dragging-mode.d.ts +14 -0
  23. package/dist/edit-modes/draw-polygon-by-dragging-mode.js +87 -0
  24. package/dist/edit-modes/draw-polygon-mode.d.ts +10 -0
  25. package/dist/edit-modes/draw-polygon-mode.js +143 -0
  26. package/dist/edit-modes/draw-rectangle-from-center-mode.d.ts +5 -0
  27. package/dist/edit-modes/draw-rectangle-from-center-mode.js +17 -0
  28. package/dist/edit-modes/draw-rectangle-mode.d.ts +5 -0
  29. package/dist/edit-modes/draw-rectangle-mode.js +11 -0
  30. package/dist/edit-modes/draw-rectangle-using-three-points-mode.d.ts +5 -0
  31. package/dist/edit-modes/draw-rectangle-using-three-points-mode.js +28 -0
  32. package/dist/edit-modes/draw-square-from-center-mode.d.ts +5 -0
  33. package/dist/edit-modes/draw-square-from-center-mode.js +35 -0
  34. package/dist/edit-modes/draw-square-mode.d.ts +5 -0
  35. package/dist/edit-modes/draw-square-mode.js +28 -0
  36. package/dist/edit-modes/duplicate-mode.d.ts +7 -0
  37. package/dist/edit-modes/duplicate-mode.js +17 -0
  38. package/dist/edit-modes/edit-mode.d.ts +11 -0
  39. package/dist/edit-modes/edit-mode.js +2 -0
  40. package/dist/edit-modes/elevation-mode.d.ts +13 -0
  41. package/dist/edit-modes/elevation-mode.js +49 -0
  42. package/dist/edit-modes/extend-line-string-mode.d.ts +9 -0
  43. package/dist/edit-modes/extend-line-string-mode.js +72 -0
  44. package/dist/edit-modes/extrude-mode.d.ts +15 -0
  45. package/dist/edit-modes/extrude-mode.js +186 -0
  46. package/dist/edit-modes/geojson-edit-mode.d.ts +33 -0
  47. package/dist/edit-modes/geojson-edit-mode.js +208 -0
  48. package/dist/edit-modes/immutable-feature-collection.d.ts +43 -0
  49. package/dist/edit-modes/immutable-feature-collection.js +300 -0
  50. package/dist/edit-modes/measure-angle-mode.d.ts +11 -0
  51. package/dist/edit-modes/measure-angle-mode.js +99 -0
  52. package/dist/edit-modes/measure-area-mode.d.ts +8 -0
  53. package/dist/edit-modes/measure-area-mode.js +50 -0
  54. package/dist/edit-modes/measure-distance-mode.d.ts +19 -0
  55. package/dist/edit-modes/measure-distance-mode.js +171 -0
  56. package/dist/edit-modes/modify-mode.d.ts +15 -0
  57. package/dist/edit-modes/modify-mode.js +203 -0
  58. package/dist/edit-modes/resize-circle-mode.d.ts +16 -0
  59. package/dist/edit-modes/resize-circle-mode.js +142 -0
  60. package/dist/edit-modes/rotate-mode.d.ts +17 -0
  61. package/dist/edit-modes/rotate-mode.js +151 -0
  62. package/dist/edit-modes/scale-mode.d.ts +27 -0
  63. package/dist/edit-modes/scale-mode.js +173 -0
  64. package/dist/edit-modes/snappable-mode.d.ts +21 -0
  65. package/dist/edit-modes/snappable-mode.js +109 -0
  66. package/dist/edit-modes/split-polygon-mode.d.ts +10 -0
  67. package/dist/edit-modes/split-polygon-mode.js +164 -0
  68. package/dist/edit-modes/three-click-polygon-mode.d.ts +10 -0
  69. package/dist/edit-modes/three-click-polygon-mode.js +72 -0
  70. package/dist/edit-modes/transform-mode.d.ts +9 -0
  71. package/dist/edit-modes/transform-mode.js +63 -0
  72. package/dist/edit-modes/translate-mode.d.ts +13 -0
  73. package/dist/edit-modes/translate-mode.js +113 -0
  74. package/dist/edit-modes/two-click-polygon-mode.d.ts +13 -0
  75. package/dist/edit-modes/two-click-polygon-mode.js +93 -0
  76. package/dist/edit-modes/types.d.ts +86 -0
  77. package/dist/edit-modes/types.js +1 -0
  78. package/dist/edit-modes/utils.d.ts +36 -0
  79. package/dist/edit-modes/utils.js +381 -0
  80. package/dist/edit-modes/view-mode.d.ts +3 -0
  81. package/dist/edit-modes/view-mode.js +3 -0
  82. package/dist/editable-layers/editable-geojson-layer.d.ts +98 -0
  83. package/dist/editable-layers/editable-geojson-layer.js +450 -0
  84. package/dist/editable-layers/editable-h3-cluster-layer.d.ts +34 -0
  85. package/dist/editable-layers/editable-h3-cluster-layer.js +164 -0
  86. package/dist/editable-layers/editable-layer.d.ts +49 -0
  87. package/dist/editable-layers/editable-layer.js +195 -0
  88. package/dist/editable-layers/editable-path-layer.d.ts +9 -0
  89. package/dist/editable-layers/editable-path-layer.js +34 -0
  90. package/dist/editable-layers/elevated-edit-handle-layer.d.ts +7 -0
  91. package/dist/editable-layers/elevated-edit-handle-layer.js +24 -0
  92. package/dist/editable-layers/junction-scatterplot-layer.d.ts +14 -0
  93. package/dist/editable-layers/junction-scatterplot-layer.js +41 -0
  94. package/dist/editable-layers/path-marker-layer/arrow-2d-geometry.d.ts +4 -0
  95. package/dist/editable-layers/path-marker-layer/arrow-2d-geometry.js +55 -0
  96. package/dist/editable-layers/path-marker-layer/create-path-markers.d.ts +16 -0
  97. package/dist/editable-layers/path-marker-layer/create-path-markers.js +75 -0
  98. package/dist/editable-layers/path-marker-layer/path-marker-layer.d.ts +40 -0
  99. package/dist/editable-layers/path-marker-layer/path-marker-layer.js +121 -0
  100. package/dist/editable-layers/path-marker-layer/polyline.d.ts +18 -0
  101. package/dist/editable-layers/path-marker-layer/polyline.js +37 -0
  102. package/dist/editable-layers/path-outline-layer/path-outline-layer.d.ts +26 -0
  103. package/dist/editable-layers/path-outline-layer/path-outline-layer.js +106 -0
  104. package/dist/editable-layers/selection-layer.d.ts +26 -0
  105. package/dist/editable-layers/selection-layer.js +167 -0
  106. package/dist/geojson-types.d.ts +58 -0
  107. package/dist/geojson-types.js +2 -0
  108. package/dist/index.cjs +5825 -0
  109. package/dist/index.cjs.map +7 -0
  110. package/dist/index.d.ts +60 -0
  111. package/dist/index.js +62 -0
  112. package/dist/lib/constants.d.ts +6 -0
  113. package/dist/lib/constants.js +6 -0
  114. package/dist/lib/deck-renderer/deck-cache.d.ts +14 -0
  115. package/dist/lib/deck-renderer/deck-cache.js +51 -0
  116. package/dist/lib/deck-renderer/deck-drawer.d.ts +63 -0
  117. package/dist/lib/deck-renderer/deck-drawer.js +232 -0
  118. package/dist/lib/feature.d.ts +10 -0
  119. package/dist/lib/feature.js +16 -0
  120. package/dist/lib/layer-mouse-event.d.ts +11 -0
  121. package/dist/lib/layer-mouse-event.js +24 -0
  122. package/dist/lib/layers/junctions-layer.d.ts +8 -0
  123. package/dist/lib/layers/junctions-layer.js +33 -0
  124. package/dist/lib/layers/segments-layer.d.ts +18 -0
  125. package/dist/lib/layers/segments-layer.js +94 -0
  126. package/dist/lib/layers/texts-layer.d.ts +8 -0
  127. package/dist/lib/layers/texts-layer.js +32 -0
  128. package/dist/lib/math.d.ts +11 -0
  129. package/dist/lib/math.js +22 -0
  130. package/dist/lib/nebula-layer.d.ts +13 -0
  131. package/dist/lib/nebula-layer.js +26 -0
  132. package/dist/lib/nebula.d.ts +34 -0
  133. package/dist/lib/nebula.js +254 -0
  134. package/dist/lib/style.d.ts +19 -0
  135. package/dist/lib/style.js +20 -0
  136. package/dist/memoize.d.ts +6 -0
  137. package/dist/memoize.js +40 -0
  138. package/dist/mode-handlers/composite-mode-handler.d.ts +24 -0
  139. package/dist/mode-handlers/composite-mode-handler.js +55 -0
  140. package/dist/mode-handlers/draw-90degree-polygon-handler.d.ts +13 -0
  141. package/dist/mode-handlers/draw-90degree-polygon-handler.js +169 -0
  142. package/dist/mode-handlers/draw-circle-by-bounding-box-handler.d.ts +9 -0
  143. package/dist/mode-handlers/draw-circle-by-bounding-box-handler.js +29 -0
  144. package/dist/mode-handlers/draw-circle-from-center-handler.d.ts +9 -0
  145. package/dist/mode-handlers/draw-circle-from-center-handler.js +27 -0
  146. package/dist/mode-handlers/draw-ellipse-by-bounding-box-handler.d.ts +9 -0
  147. package/dist/mode-handlers/draw-ellipse-by-bounding-box-handler.js +30 -0
  148. package/dist/mode-handlers/draw-ellipse-using-three-points-handler.d.ts +9 -0
  149. package/dist/mode-handlers/draw-ellipse-using-three-points-handler.js +37 -0
  150. package/dist/mode-handlers/draw-line-string-handler.d.ts +9 -0
  151. package/dist/mode-handlers/draw-line-string-handler.js +83 -0
  152. package/dist/mode-handlers/draw-point-handler.d.ts +5 -0
  153. package/dist/mode-handlers/draw-point-handler.js +11 -0
  154. package/dist/mode-handlers/draw-polygon-handler.d.ts +11 -0
  155. package/dist/mode-handlers/draw-polygon-handler.js +92 -0
  156. package/dist/mode-handlers/draw-rectangle-handler.d.ts +9 -0
  157. package/dist/mode-handlers/draw-rectangle-handler.js +18 -0
  158. package/dist/mode-handlers/draw-rectangle-using-three-points-handler.d.ts +9 -0
  159. package/dist/mode-handlers/draw-rectangle-using-three-points-handler.js +49 -0
  160. package/dist/mode-handlers/duplicate-handler.d.ts +9 -0
  161. package/dist/mode-handlers/duplicate-handler.js +19 -0
  162. package/dist/mode-handlers/elevation-handler.d.ts +19 -0
  163. package/dist/mode-handlers/elevation-handler.js +48 -0
  164. package/dist/mode-handlers/extrude-handler.d.ts +18 -0
  165. package/dist/mode-handlers/extrude-handler.js +176 -0
  166. package/dist/mode-handlers/mode-handler.d.ts +61 -0
  167. package/dist/mode-handlers/mode-handler.js +286 -0
  168. package/dist/mode-handlers/modify-handler.d.ts +19 -0
  169. package/dist/mode-handlers/modify-handler.js +193 -0
  170. package/dist/mode-handlers/rotate-handler.d.ts +17 -0
  171. package/dist/mode-handlers/rotate-handler.js +74 -0
  172. package/dist/mode-handlers/scale-handler.d.ts +17 -0
  173. package/dist/mode-handlers/scale-handler.js +76 -0
  174. package/dist/mode-handlers/snappable-handler.d.ts +33 -0
  175. package/dist/mode-handlers/snappable-handler.js +133 -0
  176. package/dist/mode-handlers/split-polygon-handler.d.ts +11 -0
  177. package/dist/mode-handlers/split-polygon-handler.js +154 -0
  178. package/dist/mode-handlers/three-click-polygon-handler.d.ts +5 -0
  179. package/dist/mode-handlers/three-click-polygon-handler.js +18 -0
  180. package/dist/mode-handlers/translate-handler.d.ts +17 -0
  181. package/dist/mode-handlers/translate-handler.js +72 -0
  182. package/dist/mode-handlers/two-click-polygon-handler.d.ts +5 -0
  183. package/dist/mode-handlers/two-click-polygon-handler.js +18 -0
  184. package/dist/mode-handlers/view-handler.d.ts +8 -0
  185. package/dist/mode-handlers/view-handler.js +10 -0
  186. package/dist/shaderlib/color/color.d.ts +8 -0
  187. package/dist/shaderlib/color/color.js +51 -0
  188. package/dist/shaderlib/outline/outline.d.ts +8 -0
  189. package/dist/shaderlib/outline/outline.js +97 -0
  190. package/dist/shaderlib/utils/utils.d.ts +8 -0
  191. package/dist/shaderlib/utils/utils.js +28 -0
  192. package/dist/translateFromCenter.d.ts +4 -0
  193. package/dist/translateFromCenter.js +19 -0
  194. package/dist/types.d.ts +35 -0
  195. package/dist/types.js +1 -0
  196. package/dist/utils.d.ts +20 -0
  197. package/dist/utils.js +144 -0
  198. package/package.json +84 -0
  199. package/src/constants.ts +15 -0
  200. package/src/curve-utils.ts +77 -0
  201. package/src/edit-modes/composite-mode.ts +74 -0
  202. package/src/edit-modes/draw-90degree-polygon-mode.ts +220 -0
  203. package/src/edit-modes/draw-circle-by-diameter-mode.ts +88 -0
  204. package/src/edit-modes/draw-circle-from-center-mode.ts +79 -0
  205. package/src/edit-modes/draw-ellipse-by-bounding-box-mode.ts +25 -0
  206. package/src/edit-modes/draw-ellipse-using-three-points-mode.ts +23 -0
  207. package/src/edit-modes/draw-line-string-mode.ts +200 -0
  208. package/src/edit-modes/draw-point-mode.ts +35 -0
  209. package/src/edit-modes/draw-polygon-by-dragging-mode.ts +106 -0
  210. package/src/edit-modes/draw-polygon-mode.ts +171 -0
  211. package/src/edit-modes/draw-rectangle-from-center-mode.ts +23 -0
  212. package/src/edit-modes/draw-rectangle-mode.ts +14 -0
  213. package/src/edit-modes/draw-rectangle-using-three-points-mode.ts +36 -0
  214. package/src/edit-modes/draw-square-from-center-mode.ts +46 -0
  215. package/src/edit-modes/draw-square-mode.ts +36 -0
  216. package/src/edit-modes/duplicate-mode.ts +21 -0
  217. package/src/edit-modes/edit-mode.ts +30 -0
  218. package/src/edit-modes/elevation-mode.ts +86 -0
  219. package/src/edit-modes/extend-line-string-mode.ts +87 -0
  220. package/src/edit-modes/extrude-mode.ts +254 -0
  221. package/src/edit-modes/geojson-edit-mode.ts +283 -0
  222. package/src/edit-modes/immutable-feature-collection.ts +417 -0
  223. package/src/edit-modes/measure-angle-mode.ts +127 -0
  224. package/src/edit-modes/measure-area-mode.ts +62 -0
  225. package/src/edit-modes/measure-distance-mode.ts +215 -0
  226. package/src/edit-modes/modify-mode.ts +293 -0
  227. package/src/edit-modes/resize-circle-mode.ts +202 -0
  228. package/src/edit-modes/rotate-mode.ts +208 -0
  229. package/src/edit-modes/scale-mode.ts +231 -0
  230. package/src/edit-modes/snappable-mode.ts +174 -0
  231. package/src/edit-modes/split-polygon-mode.ts +201 -0
  232. package/src/edit-modes/three-click-polygon-mode.ts +111 -0
  233. package/src/edit-modes/transform-mode.ts +75 -0
  234. package/src/edit-modes/translate-mode.ts +161 -0
  235. package/src/edit-modes/two-click-polygon-mode.ts +132 -0
  236. package/src/edit-modes/types.ts +135 -0
  237. package/src/edit-modes/utils.ts +513 -0
  238. package/src/edit-modes/view-mode.ts +3 -0
  239. package/src/editable-layers/editable-geojson-layer.ts +603 -0
  240. package/src/editable-layers/editable-h3-cluster-layer.ts +226 -0
  241. package/src/editable-layers/editable-layer.ts +252 -0
  242. package/src/editable-layers/editable-path-layer.ts +51 -0
  243. package/src/editable-layers/elevated-edit-handle-layer.ts +33 -0
  244. package/src/editable-layers/junction-scatterplot-layer.ts +53 -0
  245. package/src/editable-layers/path-marker-layer/arrow-2d-geometry.ts +61 -0
  246. package/src/editable-layers/path-marker-layer/create-path-markers.ts +107 -0
  247. package/src/editable-layers/path-marker-layer/path-marker-layer.ts +179 -0
  248. package/src/editable-layers/path-marker-layer/polyline.ts +40 -0
  249. package/src/editable-layers/path-outline-layer/path-outline-layer.ts +147 -0
  250. package/src/editable-layers/selection-layer.ts +209 -0
  251. package/src/geojson-types.ts +89 -0
  252. package/src/index.ts +125 -0
  253. package/src/lib/constants.ts +6 -0
  254. package/src/lib/deck-renderer/deck-cache.ts +61 -0
  255. package/src/lib/deck-renderer/deck-drawer.ts +263 -0
  256. package/src/lib/feature.ts +27 -0
  257. package/src/lib/layer-mouse-event.ts +32 -0
  258. package/src/lib/layers/junctions-layer.ts +40 -0
  259. package/src/lib/layers/segments-layer.ts +108 -0
  260. package/src/lib/layers/texts-layer.ts +43 -0
  261. package/src/lib/math.ts +26 -0
  262. package/src/lib/nebula-layer.ts +33 -0
  263. package/src/lib/nebula.ts +323 -0
  264. package/src/lib/style.ts +22 -0
  265. package/src/memoize.ts +43 -0
  266. package/src/mode-handlers/composite-mode-handler.ts +89 -0
  267. package/src/mode-handlers/draw-90degree-polygon-handler.ts +201 -0
  268. package/src/mode-handlers/draw-circle-by-bounding-box-handler.ts +39 -0
  269. package/src/mode-handlers/draw-circle-from-center-handler.ts +38 -0
  270. package/src/mode-handlers/draw-ellipse-by-bounding-box-handler.ts +41 -0
  271. package/src/mode-handlers/draw-ellipse-using-three-points-handler.ts +46 -0
  272. package/src/mode-handlers/draw-line-string-handler.ts +108 -0
  273. package/src/mode-handlers/draw-point-handler.ts +15 -0
  274. package/src/mode-handlers/draw-polygon-handler.ts +121 -0
  275. package/src/mode-handlers/draw-rectangle-handler.ts +28 -0
  276. package/src/mode-handlers/draw-rectangle-using-three-points-handler.ts +60 -0
  277. package/src/mode-handlers/duplicate-handler.ts +25 -0
  278. package/src/mode-handlers/elevation-handler.ts +88 -0
  279. package/src/mode-handlers/extrude-handler.ts +245 -0
  280. package/src/mode-handlers/mode-handler.ts +394 -0
  281. package/src/mode-handlers/modify-handler.ts +263 -0
  282. package/src/mode-handlers/rotate-handler.ts +107 -0
  283. package/src/mode-handlers/scale-handler.ts +105 -0
  284. package/src/mode-handlers/snappable-handler.ts +184 -0
  285. package/src/mode-handlers/split-polygon-handler.ts +177 -0
  286. package/src/mode-handlers/three-click-polygon-handler.ts +25 -0
  287. package/src/mode-handlers/translate-handler.ts +110 -0
  288. package/src/mode-handlers/two-click-polygon-handler.ts +25 -0
  289. package/src/mode-handlers/view-handler.ts +13 -0
  290. package/src/shaderlib/color/color.ts +56 -0
  291. package/src/shaderlib/outline/outline.ts +101 -0
  292. package/src/shaderlib/utils/utils.ts +33 -0
  293. package/src/translateFromCenter.ts +48 -0
  294. package/src/types.ts +39 -0
  295. package/src/utils.ts +185 -0
@@ -0,0 +1,107 @@
1
+ import turfCentroid from '@turf/centroid';
2
+ import turfBearing from '@turf/bearing';
3
+ import turfTransformRotate from '@turf/transform-rotate';
4
+ import { FeatureCollection, Position } from '../geojson-types';
5
+ import { PointerMoveEvent, StartDraggingEvent, StopDraggingEvent } from '../edit-modes/types';
6
+ import { EditAction, ModeHandler } from './mode-handler';
7
+
8
+ // TODO edit-modes: delete handlers once EditMode fully implemented
9
+ export class RotateHandler extends ModeHandler {
10
+ _isRotatable: boolean = undefined!;
11
+ _geometryBeingRotated: FeatureCollection | null | undefined;
12
+
13
+ handlePointerMove(event: PointerMoveEvent): {
14
+ editAction: EditAction | null | undefined;
15
+ cancelMapPan: boolean;
16
+ } {
17
+ let editAction: EditAction | null | undefined = null;
18
+
19
+ this._isRotatable = Boolean(this._geometryBeingRotated) || this.isSelectionPicked(event.picks);
20
+
21
+ if (!this._isRotatable || !event.pointerDownMapCoords) {
22
+ // Nothing to do
23
+ return { editAction: null, cancelMapPan: false };
24
+ }
25
+
26
+ if (event.isDragging && this._geometryBeingRotated) {
27
+ // Rotate the geometry
28
+ editAction = this.getRotateAction(
29
+ event.pointerDownMapCoords,
30
+ event.mapCoords,
31
+ 'rotating'
32
+ );
33
+ }
34
+
35
+ return { editAction, cancelMapPan: true };
36
+ }
37
+
38
+ handleStartDragging(event: StartDraggingEvent): EditAction | null | undefined {
39
+ if (!this._isRotatable) {
40
+ return null;
41
+ }
42
+
43
+ this._geometryBeingRotated = this.getSelectedFeaturesAsFeatureCollection();
44
+ return null;
45
+ }
46
+
47
+ handleStopDragging(event: StopDraggingEvent): EditAction | null | undefined {
48
+ let editAction: EditAction | null | undefined = null;
49
+
50
+ if (this._geometryBeingRotated) {
51
+ // Rotate the geometry
52
+ editAction = this.getRotateAction(
53
+ event.pointerDownMapCoords,
54
+ event.mapCoords,
55
+ 'rotated'
56
+ );
57
+ this._geometryBeingRotated = null;
58
+ }
59
+
60
+ return editAction;
61
+ }
62
+
63
+ getCursor({ isDragging }: { isDragging: boolean }): string {
64
+ if (this._isRotatable) {
65
+ // TODO: look at doing SVG cursors to get a better "rotate" cursor
66
+ return 'move';
67
+ }
68
+ return isDragging ? 'grabbing' : 'grab';
69
+ }
70
+
71
+ getRotateAction(startDragPoint: Position, currentPoint: Position, editType: string): EditAction {
72
+ const startPosition = startDragPoint;
73
+ // @ts-expect-error turf types diff
74
+ const centroid = turfCentroid(this._geometryBeingRotated);
75
+ // @ts-expect-error turf types diff
76
+ const angle = getRotationAngle(centroid, startPosition, currentPoint);
77
+
78
+ // @ts-expect-error turf type diff
79
+ const rotatedFeatures: FeatureCollection = turfTransformRotate(
80
+ // @ts-expect-error turf type diff
81
+ this._geometryBeingRotated,
82
+ angle
83
+ );
84
+
85
+ let updatedData = this.getImmutableFeatureCollection();
86
+
87
+ const selectedIndexes = this.getSelectedFeatureIndexes();
88
+ for (let i = 0; i < selectedIndexes.length; i++) {
89
+ const selectedIndex = selectedIndexes[i];
90
+ const movedFeature = rotatedFeatures.features[i];
91
+ updatedData = updatedData.replaceGeometry(selectedIndex, movedFeature.geometry);
92
+ }
93
+
94
+ return {
95
+ updatedData: updatedData.getObject(),
96
+ editType,
97
+ featureIndexes: selectedIndexes,
98
+ editContext: null,
99
+ };
100
+ }
101
+ }
102
+
103
+ function getRotationAngle(centroid: Position, startDragPoint: Position, currentPoint: Position) {
104
+ const bearing1 = turfBearing(centroid, startDragPoint);
105
+ const bearing2 = turfBearing(centroid, currentPoint);
106
+ return bearing2 - bearing1;
107
+ }
@@ -0,0 +1,105 @@
1
+ import turfCentroid from '@turf/centroid';
2
+ import turfDistance from '@turf/distance';
3
+ import turfTransformScale from '@turf/transform-scale';
4
+ import { FeatureCollection, Position } from '../geojson-types';
5
+ import { PointerMoveEvent, StartDraggingEvent, StopDraggingEvent } from '../edit-modes/types';
6
+ import { EditAction, ModeHandler } from './mode-handler';
7
+
8
+ // TODO edit-modes: delete handlers once EditMode fully implemented
9
+ export class ScaleHandler extends ModeHandler {
10
+ _isScalable: boolean = undefined!;
11
+ _geometryBeingScaled: FeatureCollection | null | undefined;
12
+
13
+ handlePointerMove(event: PointerMoveEvent): {
14
+ editAction: EditAction | null | undefined;
15
+ cancelMapPan: boolean;
16
+ } {
17
+ let editAction: EditAction | null | undefined = null;
18
+
19
+ this._isScalable = Boolean(this._geometryBeingScaled) || this.isSelectionPicked(event.picks);
20
+
21
+ if (!this._isScalable || !event.pointerDownMapCoords) {
22
+ // Nothing to do
23
+ return { editAction: null, cancelMapPan: false };
24
+ }
25
+
26
+ if (event.isDragging && this._geometryBeingScaled) {
27
+ // Scale the geometry
28
+ editAction = this.getScaleAction(
29
+ event.pointerDownMapCoords,
30
+ event.mapCoords,
31
+ 'scaling'
32
+ );
33
+ }
34
+
35
+ return { editAction, cancelMapPan: true };
36
+ }
37
+
38
+ handleStartDragging(event: StartDraggingEvent): EditAction | null | undefined {
39
+ if (!this._isScalable) {
40
+ return null;
41
+ }
42
+
43
+ this._geometryBeingScaled = this.getSelectedFeaturesAsFeatureCollection();
44
+ return null;
45
+ }
46
+
47
+ handleStopDragging(event: StopDraggingEvent): EditAction | null | undefined {
48
+ let editAction: EditAction | null | undefined = null;
49
+
50
+ if (this._geometryBeingScaled) {
51
+ // Scale the geometry
52
+ editAction = this.getScaleAction(event.pointerDownMapCoords, event.mapCoords, 'scaled');
53
+ this._geometryBeingScaled = null;
54
+ }
55
+
56
+ return editAction;
57
+ }
58
+
59
+ getCursor({ isDragging }: { isDragging: boolean }): string {
60
+ if (this._isScalable) {
61
+ // TODO: look at doing SVG cursors to get a better "scale" cursor
62
+ return 'move';
63
+ }
64
+ return isDragging ? 'grabbing' : 'grab';
65
+ }
66
+
67
+ getScaleAction(startDragPoint: Position, currentPoint: Position, editType: string): EditAction {
68
+ const startPosition = startDragPoint;
69
+ // @ts-expect-error turf types diff
70
+ const centroid = turfCentroid(this._geometryBeingScaled);
71
+ // @ts-expect-error turf types diff
72
+ const factor = getScaleFactor(centroid, startPosition, currentPoint);
73
+ // @ts-expect-error turf type diff
74
+ const scaledFeatures: FeatureCollection = turfTransformScale(
75
+ // @ts-expect-error turf type diff
76
+ this._geometryBeingScaled,
77
+ factor,
78
+ {
79
+ origin: centroid,
80
+ }
81
+ );
82
+
83
+ let updatedData = this.getImmutableFeatureCollection();
84
+
85
+ const selectedIndexes = this.getSelectedFeatureIndexes();
86
+ for (let i = 0; i < selectedIndexes.length; i++) {
87
+ const selectedIndex = selectedIndexes[i];
88
+ const movedFeature = scaledFeatures.features[i];
89
+ updatedData = updatedData.replaceGeometry(selectedIndex, movedFeature.geometry);
90
+ }
91
+
92
+ return {
93
+ updatedData: updatedData.getObject(),
94
+ editType,
95
+ featureIndexes: selectedIndexes,
96
+ editContext: null,
97
+ };
98
+ }
99
+ }
100
+
101
+ function getScaleFactor(centroid: Position, startDragPoint: Position, currentPoint: Position) {
102
+ const startDistance = turfDistance(centroid, startDragPoint);
103
+ const endDistance = turfDistance(centroid, currentPoint);
104
+ return endDistance / startDistance;
105
+ }
@@ -0,0 +1,184 @@
1
+ import { Feature, FeatureCollection, Position } from '../geojson-types';
2
+ import { PointerMoveEvent, StartDraggingEvent, StopDraggingEvent } from '../edit-modes/types';
3
+ import {
4
+ EditHandle,
5
+ EditAction,
6
+ ModeHandler,
7
+ getPickedEditHandle,
8
+ getEditHandlesForGeometry,
9
+ } from './mode-handler';
10
+
11
+ type HandlePicks = { pickedHandle?: EditHandle; potentialSnapHandle?: EditHandle };
12
+
13
+ // TODO edit-modes: delete handlers once EditMode fully implemented
14
+ export class SnappableHandler extends ModeHandler {
15
+ _handler: ModeHandler;
16
+ _editHandlePicks: HandlePicks | null | undefined;
17
+ _startDragSnapHandlePosition: Position = undefined!;
18
+
19
+ constructor(handler: ModeHandler) {
20
+ super();
21
+ this._handler = handler;
22
+ }
23
+
24
+ setFeatureCollection(featureCollection: FeatureCollection): void {
25
+ this._handler.setFeatureCollection(featureCollection);
26
+ }
27
+
28
+ setModeConfig(modeConfig: any): void {
29
+ this._modeConfig = modeConfig;
30
+ this._handler.setModeConfig(modeConfig);
31
+ }
32
+
33
+ setSelectedFeatureIndexes(indexes: number[]): void {
34
+ this._handler.setSelectedFeatureIndexes(indexes);
35
+ }
36
+
37
+ _getSnappedMouseEvent(event: Record<string, any>, snapPoint: Position): PointerMoveEvent {
38
+ // @ts-expect-error narrow event type
39
+ return Object.assign({}, event, {
40
+ mapCoords: snapPoint,
41
+ pointerDownMapCoords: this._startDragSnapHandlePosition,
42
+ });
43
+ }
44
+
45
+ _getEditHandlePicks(event: PointerMoveEvent): HandlePicks {
46
+ const { picks } = event;
47
+
48
+ const potentialSnapHandle = picks.find(
49
+ (pick) => pick.object && pick.object.type === 'intermediate'
50
+ );
51
+ const handles = { potentialSnapHandle: potentialSnapHandle && potentialSnapHandle.object };
52
+
53
+ const pickedHandle = getPickedEditHandle(event.pointerDownPicks);
54
+ if (pickedHandle) {
55
+ return { ...handles, pickedHandle };
56
+ }
57
+
58
+ return handles;
59
+ }
60
+
61
+ _updatePickedHandlePosition(editAction: EditAction) {
62
+ const { pickedHandle = {} as EditHandle } = this._editHandlePicks || {};
63
+
64
+ if (pickedHandle && editAction) {
65
+ const { featureIndexes, updatedData } = editAction;
66
+
67
+ for (let i = 0; i < featureIndexes.length; i++) {
68
+ const selectedIndex = featureIndexes[i];
69
+ const updatedFeature = updatedData.features[selectedIndex];
70
+
71
+ const { positionIndexes, featureIndex } = pickedHandle;
72
+ if (selectedIndex >= 0 && featureIndex === selectedIndex) {
73
+ const { coordinates } = updatedFeature.geometry;
74
+ pickedHandle.position = positionIndexes.reduce(
75
+ (a: any[], b: number) => a[b],
76
+ coordinates
77
+ ) as Position;
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ // If additionalSnapTargets is present in modeConfig and is populated, this
84
+ // method will return those features along with the features
85
+ // that live in the current layer. Otherwise, this method will simply return the
86
+ // features from the current layer
87
+ _getSnapTargets(): Feature[] {
88
+ let { additionalSnapTargets } = this.getModeConfig() || {};
89
+ additionalSnapTargets = additionalSnapTargets || [];
90
+
91
+ const features = [
92
+ ...this._handler.featureCollection.getObject().features,
93
+ ...additionalSnapTargets,
94
+ ];
95
+ return features;
96
+ }
97
+
98
+ _getNonPickedIntermediateHandles(): EditHandle[] {
99
+ const handles: EditHandle[] = [];
100
+ const features = this._getSnapTargets();
101
+
102
+ for (let i = 0; i < features.length; i++) {
103
+ // Filter out the currently selected feature(s)
104
+ const isCurrentIndexFeatureNotSelected =
105
+ i < features.length && !this._handler.getSelectedFeatureIndexes().includes(i);
106
+
107
+ if (isCurrentIndexFeatureNotSelected) {
108
+ const { geometry } = features[i];
109
+ handles.push(...getEditHandlesForGeometry(geometry, i, 'intermediate'));
110
+ }
111
+ }
112
+ return handles;
113
+ }
114
+
115
+ // If no snap handle has been picked, only display the edit handles of the
116
+ // selected feature. If a snap handle has been picked, display said snap handle
117
+ // along with all snappable points on all non-selected features.
118
+ getEditHandles(picks?: Array<Record<string, any>>, mapCoords?: Position): any[] {
119
+ const { enableSnapping } = this._modeConfig || {};
120
+ const handles = this._handler.getEditHandles(picks, mapCoords);
121
+
122
+ if (!enableSnapping) return handles;
123
+ const { pickedHandle } = this._editHandlePicks || {};
124
+
125
+ if (pickedHandle) {
126
+ handles.push(...this._getNonPickedIntermediateHandles(), pickedHandle);
127
+ return handles;
128
+ }
129
+
130
+ const { features } = this._handler.featureCollection.getObject();
131
+ for (const index of this._handler.getSelectedFeatureIndexes()) {
132
+ if (index < features.length) {
133
+ const { geometry } = features[index];
134
+ handles.push(...getEditHandlesForGeometry(geometry, index, 'snap'));
135
+ }
136
+ }
137
+
138
+ return handles.filter(Boolean);
139
+ }
140
+
141
+ _getSnapAwareEvent(event: Record<string, any>): Record<string, any> {
142
+ const { potentialSnapHandle } = this._editHandlePicks || {};
143
+
144
+ return potentialSnapHandle && potentialSnapHandle.position
145
+ ? this._getSnappedMouseEvent(event, potentialSnapHandle.position)
146
+ : event;
147
+ }
148
+
149
+ handleStartDragging(event: StartDraggingEvent): EditAction | null | undefined {
150
+ this._startDragSnapHandlePosition = (getPickedEditHandle(event.picks) || {}).position as Position;
151
+ return this._handler.handleStartDragging(event);
152
+ }
153
+
154
+ handleStopDragging(event: StopDraggingEvent): EditAction | null | undefined {
155
+ // @ts-expect-error narrow event type
156
+ const modeActionSummary = this._handler.handleStopDragging(this._getSnapAwareEvent(event));
157
+
158
+ this._editHandlePicks = null;
159
+ return modeActionSummary;
160
+ }
161
+
162
+ getCursor(event: { isDragging: boolean }): string {
163
+ return this._handler.getCursor(event);
164
+ }
165
+
166
+ handlePointerMove(event: PointerMoveEvent): {
167
+ editAction: EditAction | null | undefined;
168
+ cancelMapPan: boolean;
169
+ } {
170
+ const { enableSnapping } = this._handler.getModeConfig() || {};
171
+
172
+ if (enableSnapping) {
173
+ this._editHandlePicks = this._getEditHandlePicks(event);
174
+ }
175
+ // @ts-expect-error narrow event type
176
+ const modeActionSummary = this._handler.handlePointerMove(this._getSnapAwareEvent(event));
177
+ const { editAction } = modeActionSummary;
178
+ if (editAction) {
179
+ this._updatePickedHandlePosition(editAction);
180
+ }
181
+
182
+ return modeActionSummary;
183
+ }
184
+ }
@@ -0,0 +1,177 @@
1
+ import booleanPointInPolygon from '@turf/boolean-point-in-polygon';
2
+ import turfDifference from '@turf/difference';
3
+ import turfBuffer from '@turf/buffer';
4
+ import lineIntersect from '@turf/line-intersect';
5
+ import { lineString } from '@turf/helpers';
6
+ import turfBearing from '@turf/bearing';
7
+ import turfDistance from '@turf/distance';
8
+ import turfDestination from '@turf/destination';
9
+ import turfPolygonToLine from '@turf/polygon-to-line';
10
+ import nearestPointOnLine, { NearestPointOnLine } from '@turf/nearest-point-on-line';
11
+ import { generatePointsParallelToLinePoints } from '../utils';
12
+ import { EditAction, ModeHandler } from './mode-handler';
13
+ import { ClickEvent, PointerMoveEvent } from '../edit-modes/types';
14
+
15
+ // TODO edit-modes: delete handlers once EditMode fully implemented
16
+ export class SplitPolygonHandler extends ModeHandler {
17
+ calculateMapCoords(clickSequence: any, mapCoords: any) {
18
+ const modeConfig = this.getModeConfig();
19
+ if (!modeConfig || !modeConfig.lock90Degree || !clickSequence.length) {
20
+ return mapCoords;
21
+ }
22
+ if (clickSequence.length === 1) {
23
+ // if first point is clicked, then find closest polygon point and build ~90deg vector
24
+ const firstPoint = clickSequence[0];
25
+ const selectedGeometry = this.getSelectedGeometry();
26
+ // @ts-expect-error turf type diff
27
+ const feature = turfPolygonToLine(selectedGeometry);
28
+
29
+ const lines = feature.type === 'FeatureCollection' ? feature.features : [feature];
30
+ let minDistance = Number.MAX_SAFE_INTEGER;
31
+ let closestPoint: NearestPointOnLine | null = null;
32
+ // If Multipolygon, then we should find nearest polygon line and stick split to it.
33
+ lines.forEach((line) => {
34
+ const snapPoint = nearestPointOnLine(line, firstPoint);
35
+ const distanceFromOrigin = turfDistance(snapPoint, firstPoint);
36
+ if (minDistance > distanceFromOrigin) {
37
+ minDistance = distanceFromOrigin;
38
+ closestPoint = snapPoint;
39
+ }
40
+ });
41
+
42
+ if (closestPoint) {
43
+ // closest point is used as 90degree entry to the polygon
44
+ const lastBearing = turfBearing(firstPoint, closestPoint);
45
+ const currentDistance = turfDistance(firstPoint, mapCoords, { units: 'meters' });
46
+ return turfDestination(firstPoint, currentDistance, lastBearing, {
47
+ units: 'meters',
48
+ }).geometry.coordinates;
49
+ }
50
+ return mapCoords;
51
+ }
52
+ // Allow only 90 degree turns
53
+ const lastPoint = clickSequence[clickSequence.length - 1];
54
+ const [approximatePoint] = generatePointsParallelToLinePoints(
55
+ clickSequence[clickSequence.length - 2],
56
+ lastPoint,
57
+ mapCoords
58
+ );
59
+ // align point with current ground
60
+ const nearestPt = nearestPointOnLine(lineString([lastPoint, approximatePoint]), mapCoords)
61
+ .geometry.coordinates;
62
+ return nearestPt;
63
+ }
64
+
65
+ handleClick(event: ClickEvent): EditAction | null | undefined {
66
+ super.handleClick({
67
+ ...event,
68
+ mapCoords: this.calculateMapCoords(this.getClickSequence(), event.mapCoords),
69
+ });
70
+ const editAction: EditAction | null | undefined = null;
71
+ const tentativeFeature = this.getTentativeFeature();
72
+ const selectedGeometry = this.getSelectedGeometry();
73
+ const clickSequence = this.getClickSequence();
74
+
75
+ if (!selectedGeometry) {
76
+ // eslint-disable-next-line no-console,no-undef
77
+ console.warn('A polygon must be selected for splitting');
78
+ this._setTentativeFeature(null);
79
+ return editAction;
80
+ }
81
+ const pt = {
82
+ type: 'Point',
83
+ coordinates: clickSequence[clickSequence.length - 1],
84
+ };
85
+ // @ts-expect-error turf type diff
86
+ const isPointInPolygon = booleanPointInPolygon(pt, selectedGeometry);
87
+ if (clickSequence.length > 1 && tentativeFeature && !isPointInPolygon) {
88
+ this.resetClickSequence();
89
+ // @ts-expect-error turf type diff
90
+ const isLineInterectingWithPolygon = lineIntersect(tentativeFeature, selectedGeometry);
91
+ if (isLineInterectingWithPolygon.features.length === 0) {
92
+ this._setTentativeFeature(null);
93
+ return editAction;
94
+ }
95
+ return this.splitPolygon();
96
+ }
97
+
98
+ return editAction;
99
+ }
100
+
101
+ handlePointerMove({ mapCoords }: PointerMoveEvent): {
102
+ editAction: EditAction | null | undefined;
103
+ cancelMapPan: boolean;
104
+ } {
105
+ const clickSequence = this.getClickSequence();
106
+ const result = { editAction: null, cancelMapPan: false };
107
+
108
+ if (clickSequence.length === 0) {
109
+ // nothing to do yet
110
+ return result;
111
+ }
112
+
113
+ this._setTentativeFeature({
114
+ type: 'Feature',
115
+ geometry: {
116
+ type: 'LineString',
117
+ coordinates: [...clickSequence, this.calculateMapCoords(clickSequence, mapCoords)],
118
+ },
119
+ });
120
+
121
+ return result;
122
+ }
123
+
124
+ splitPolygon() {
125
+ const selectedGeometry = this.getSelectedGeometry();
126
+ const tentativeFeature = this.getTentativeFeature();
127
+ const featureIndex = this.getSelectedFeatureIndexes()[0];
128
+ const modeConfig = this.getModeConfig() || {};
129
+
130
+ // Default gap in between the polygon
131
+ let { gap = 0.1, units = 'centimeters' } = modeConfig;
132
+ if (gap === 0) {
133
+ gap = 0.1;
134
+ units = 'centimeters';
135
+ }
136
+ // @ts-expect-error turf type diff
137
+ const buffer = turfBuffer(tentativeFeature, gap, { units });
138
+ // @ts-expect-error turf type diff
139
+ const updatedGeometry = turfDifference(selectedGeometry, buffer);
140
+ this._setTentativeFeature(null);
141
+ if (!updatedGeometry) {
142
+ // eslint-disable-next-line no-console,no-undef
143
+ console.warn('Canceling edit. Split Polygon erased');
144
+ return null;
145
+ }
146
+
147
+ const { type, coordinates } = updatedGeometry.geometry;
148
+ let updatedCoordinates: any[] = []; // TODO
149
+ if (type === 'Polygon') {
150
+ // Update the coordinates as per Multipolygon
151
+ updatedCoordinates = coordinates.map((c) => [c]);
152
+ } else {
153
+ // Handle Case when Multipolygon has holes
154
+ updatedCoordinates = coordinates.reduce((agg, prev) => {
155
+ prev.forEach((p) => {
156
+ agg.push([p]);
157
+ });
158
+ return agg;
159
+ }, [] as any);
160
+ }
161
+
162
+ // Update the type to Mulitpolygon
163
+ const updatedData = this.getImmutableFeatureCollection().replaceGeometry(featureIndex, {
164
+ type: 'MultiPolygon',
165
+ coordinates: updatedCoordinates,
166
+ });
167
+
168
+ const editAction: EditAction = {
169
+ updatedData: updatedData.getObject(),
170
+ editType: 'split',
171
+ featureIndexes: [featureIndex],
172
+ editContext: null,
173
+ };
174
+
175
+ return editAction;
176
+ }
177
+ }
@@ -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 ThreeClickPolygonHandler 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 > 2 &&
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
+ }