maplair 4.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 (262) hide show
  1. package/CHANGELOG.md +2804 -0
  2. package/LICENSE +27 -0
  3. package/README.md +40 -0
  4. package/dist/esm/Maplair.js +71 -0
  5. package/dist/esm/Maplair.js.map +1 -0
  6. package/dist/esm/_virtual/_rolldown/runtime.js +13 -0
  7. package/dist/esm/control/AttributionControl.js +79 -0
  8. package/dist/esm/control/AttributionControl.js.map +1 -0
  9. package/dist/esm/control/Control.js +81 -0
  10. package/dist/esm/control/Control.js.map +1 -0
  11. package/dist/esm/control/LayersControl.js +220 -0
  12. package/dist/esm/control/LayersControl.js.map +1 -0
  13. package/dist/esm/control/ScaleControl.js +72 -0
  14. package/dist/esm/control/ScaleControl.js.map +1 -0
  15. package/dist/esm/control/ZoomControl.js +86 -0
  16. package/dist/esm/control/ZoomControl.js.map +1 -0
  17. package/dist/esm/core/Browser.js +31 -0
  18. package/dist/esm/core/Browser.js.map +1 -0
  19. package/dist/esm/core/Class.js +52 -0
  20. package/dist/esm/core/Class.js.map +1 -0
  21. package/dist/esm/core/Events.js +149 -0
  22. package/dist/esm/core/Events.js.map +1 -0
  23. package/dist/esm/core/Handler.js +32 -0
  24. package/dist/esm/core/Handler.js.map +1 -0
  25. package/dist/esm/core/I18n.js +36 -0
  26. package/dist/esm/core/I18n.js.map +1 -0
  27. package/dist/esm/core/Util.js +71 -0
  28. package/dist/esm/core/Util.js.map +1 -0
  29. package/dist/esm/dom/DomEvent.DoubleTap.js +74 -0
  30. package/dist/esm/dom/DomEvent.DoubleTap.js.map +1 -0
  31. package/dist/esm/dom/DomEvent.PointerEvents.js +46 -0
  32. package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -0
  33. package/dist/esm/dom/DomEvent.js +135 -0
  34. package/dist/esm/dom/DomEvent.js.map +1 -0
  35. package/dist/esm/dom/DomUtil.js +107 -0
  36. package/dist/esm/dom/DomUtil.js.map +1 -0
  37. package/dist/esm/dom/Draggable.js +107 -0
  38. package/dist/esm/dom/Draggable.js.map +1 -0
  39. package/dist/esm/dom/PosAnimation.js +56 -0
  40. package/dist/esm/dom/PosAnimation.js.map +1 -0
  41. package/dist/esm/geo/LatLng.js +65 -0
  42. package/dist/esm/geo/LatLng.js.map +1 -0
  43. package/dist/esm/geo/LatLngBounds.js +114 -0
  44. package/dist/esm/geo/LatLngBounds.js.map +1 -0
  45. package/dist/esm/geo/crs/CRS.js +51 -0
  46. package/dist/esm/geo/crs/CRS.js.map +1 -0
  47. package/dist/esm/geo/crs/EPSG3395.js +16 -0
  48. package/dist/esm/geo/crs/EPSG3395.js.map +1 -0
  49. package/dist/esm/geo/crs/EPSG3857.js +19 -0
  50. package/dist/esm/geo/crs/EPSG3857.js.map +1 -0
  51. package/dist/esm/geo/crs/EPSG4326.js +13 -0
  52. package/dist/esm/geo/crs/EPSG4326.js.map +1 -0
  53. package/dist/esm/geo/crs/EarthCRS.js +14 -0
  54. package/dist/esm/geo/crs/EarthCRS.js.map +1 -0
  55. package/dist/esm/geo/crs/SimpleCRS.js +23 -0
  56. package/dist/esm/geo/crs/SimpleCRS.js.map +1 -0
  57. package/dist/esm/geo/projection/Projection.LonLat.js +19 -0
  58. package/dist/esm/geo/projection/Projection.LonLat.js.map +1 -0
  59. package/dist/esm/geo/projection/Projection.Mercator.js +33 -0
  60. package/dist/esm/geo/projection/Projection.Mercator.js.map +1 -0
  61. package/dist/esm/geo/projection/Projection.SphericalMercator.js +27 -0
  62. package/dist/esm/geo/projection/Projection.SphericalMercator.js.map +1 -0
  63. package/dist/esm/geo/projection/index.js +14 -0
  64. package/dist/esm/geo/projection/index.js.map +1 -0
  65. package/dist/esm/geometry/Bounds.js +91 -0
  66. package/dist/esm/geometry/Bounds.js.map +1 -0
  67. package/dist/esm/geometry/LineUtil.js +169 -0
  68. package/dist/esm/geometry/LineUtil.js.map +1 -0
  69. package/dist/esm/geometry/Point.js +119 -0
  70. package/dist/esm/geometry/Point.js.map +1 -0
  71. package/dist/esm/geometry/PolyUtil.js +89 -0
  72. package/dist/esm/geometry/PolyUtil.js.map +1 -0
  73. package/dist/esm/geometry/Transformation.js +34 -0
  74. package/dist/esm/geometry/Transformation.js.map +1 -0
  75. package/dist/esm/layer/BlanketOverlay.js +87 -0
  76. package/dist/esm/layer/BlanketOverlay.js.map +1 -0
  77. package/dist/esm/layer/DivOverlay.js +192 -0
  78. package/dist/esm/layer/DivOverlay.js.map +1 -0
  79. package/dist/esm/layer/FeatureGroup.js +40 -0
  80. package/dist/esm/layer/FeatureGroup.js.map +1 -0
  81. package/dist/esm/layer/GeoJSON.js +192 -0
  82. package/dist/esm/layer/GeoJSON.js.map +1 -0
  83. package/dist/esm/layer/ImageOverlay.js +140 -0
  84. package/dist/esm/layer/ImageOverlay.js.map +1 -0
  85. package/dist/esm/layer/Layer.js +119 -0
  86. package/dist/esm/layer/Layer.js.map +1 -0
  87. package/dist/esm/layer/LayerGroup.js +58 -0
  88. package/dist/esm/layer/LayerGroup.js.map +1 -0
  89. package/dist/esm/layer/Popup.js +224 -0
  90. package/dist/esm/layer/Popup.js.map +1 -0
  91. package/dist/esm/layer/SVGOverlay.js +21 -0
  92. package/dist/esm/layer/SVGOverlay.js.map +1 -0
  93. package/dist/esm/layer/Tooltip.js +228 -0
  94. package/dist/esm/layer/Tooltip.js.map +1 -0
  95. package/dist/esm/layer/VideoOverlay.js +54 -0
  96. package/dist/esm/layer/VideoOverlay.js.map +1 -0
  97. package/dist/esm/layer/marker/DefaultIcon.js +50 -0
  98. package/dist/esm/layer/marker/DefaultIcon.js.map +1 -0
  99. package/dist/esm/layer/marker/DivIcon.js +37 -0
  100. package/dist/esm/layer/marker/DivIcon.js.map +1 -0
  101. package/dist/esm/layer/marker/Icon.js +65 -0
  102. package/dist/esm/layer/marker/Icon.js.map +1 -0
  103. package/dist/esm/layer/marker/Marker.Drag.js +77 -0
  104. package/dist/esm/layer/marker/Marker.Drag.js.map +1 -0
  105. package/dist/esm/layer/marker/Marker.js +235 -0
  106. package/dist/esm/layer/marker/Marker.js.map +1 -0
  107. package/dist/esm/layer/tile/GridLayer.js +447 -0
  108. package/dist/esm/layer/tile/GridLayer.js.map +1 -0
  109. package/dist/esm/layer/tile/TileLayer.js +139 -0
  110. package/dist/esm/layer/tile/TileLayer.js.map +1 -0
  111. package/dist/esm/layer/tile/WMSTileLayer.js +68 -0
  112. package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -0
  113. package/dist/esm/layer/vector/Canvas.js +292 -0
  114. package/dist/esm/layer/vector/Canvas.js.map +1 -0
  115. package/dist/esm/layer/vector/Circle.js +35 -0
  116. package/dist/esm/layer/vector/Circle.js.map +1 -0
  117. package/dist/esm/layer/vector/CircleMarker.js +69 -0
  118. package/dist/esm/layer/vector/CircleMarker.js.map +1 -0
  119. package/dist/esm/layer/vector/Path.js +68 -0
  120. package/dist/esm/layer/vector/Path.js.map +1 -0
  121. package/dist/esm/layer/vector/Polygon.js +70 -0
  122. package/dist/esm/layer/vector/Polygon.js.map +1 -0
  123. package/dist/esm/layer/vector/Polyline.js +152 -0
  124. package/dist/esm/layer/vector/Polyline.js.map +1 -0
  125. package/dist/esm/layer/vector/Rectangle.js +25 -0
  126. package/dist/esm/layer/vector/Rectangle.js.map +1 -0
  127. package/dist/esm/layer/vector/Renderer.getRenderer.js +31 -0
  128. package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -0
  129. package/dist/esm/layer/vector/Renderer.js +39 -0
  130. package/dist/esm/layer/vector/Renderer.js.map +1 -0
  131. package/dist/esm/layer/vector/SVG.js +115 -0
  132. package/dist/esm/layer/vector/SVG.js.map +1 -0
  133. package/dist/esm/map/Map.js +844 -0
  134. package/dist/esm/map/Map.js.map +1 -0
  135. package/dist/esm/map/handler/BoxZoomHandler.js +105 -0
  136. package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -0
  137. package/dist/esm/map/handler/DoubleClickZoomHandler.js +27 -0
  138. package/dist/esm/map/handler/DoubleClickZoomHandler.js.map +1 -0
  139. package/dist/esm/map/handler/DragHandler.js +133 -0
  140. package/dist/esm/map/handler/DragHandler.js.map +1 -0
  141. package/dist/esm/map/handler/KeyboardHandler.js +117 -0
  142. package/dist/esm/map/handler/KeyboardHandler.js.map +1 -0
  143. package/dist/esm/map/handler/PinchZoomHandler.js +96 -0
  144. package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -0
  145. package/dist/esm/map/handler/ScrollWheelZoomHandler.js +50 -0
  146. package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -0
  147. package/dist/esm/map/handler/TapHoldHandler.js +77 -0
  148. package/dist/esm/map/handler/TapHoldHandler.js.map +1 -0
  149. package/dist/esm/package.js +6 -0
  150. package/dist/esm/package.js.map +1 -0
  151. package/dist/images/layers.svg +1 -0
  152. package/dist/images/logo.svg +1 -0
  153. package/dist/images/marker-icon.svg +1 -0
  154. package/dist/images/marker-shadow.svg +1 -0
  155. package/dist/maplair-global-src.js +6810 -0
  156. package/dist/maplair-global-src.js.map +1 -0
  157. package/dist/maplair-global.js +12 -0
  158. package/dist/maplair-global.js.map +1 -0
  159. package/dist/maplair-src.js +6704 -0
  160. package/dist/maplair-src.js.map +1 -0
  161. package/dist/maplair.css +730 -0
  162. package/dist/maplair.js +7 -0
  163. package/dist/maplair.js.map +1 -0
  164. package/i18n/cs.json +12 -0
  165. package/i18n/de.json +12 -0
  166. package/i18n/en.json +12 -0
  167. package/i18n/es.json +12 -0
  168. package/i18n/fr.json +12 -0
  169. package/i18n/hu.json +12 -0
  170. package/i18n/it.json +12 -0
  171. package/i18n/rm.json +12 -0
  172. package/i18n/sk.json +12 -0
  173. package/i18n/sl.json +12 -0
  174. package/package.json +87 -0
  175. package/src/Maplair.js +24 -0
  176. package/src/control/AttributionControl.js +147 -0
  177. package/src/control/Control.js +174 -0
  178. package/src/control/LayersControl.js +444 -0
  179. package/src/control/ScaleControl.js +141 -0
  180. package/src/control/ZoomControl.js +147 -0
  181. package/src/control/index.js +5 -0
  182. package/src/core/Browser.js +75 -0
  183. package/src/core/Class.js +97 -0
  184. package/src/core/Events.js +334 -0
  185. package/src/core/Handler.js +59 -0
  186. package/src/core/I18n.js +76 -0
  187. package/src/core/Util.js +122 -0
  188. package/src/core/index.js +8 -0
  189. package/src/dom/DomEvent.DoubleTap.js +120 -0
  190. package/src/dom/DomEvent.PointerEvents.js +75 -0
  191. package/src/dom/DomEvent.js +282 -0
  192. package/src/dom/DomUtil.js +175 -0
  193. package/src/dom/Draggable.js +204 -0
  194. package/src/dom/PosAnimation.js +115 -0
  195. package/src/dom/index.js +9 -0
  196. package/src/geo/LatLng.js +163 -0
  197. package/src/geo/LatLngBounds.js +260 -0
  198. package/src/geo/crs/CRS.js +143 -0
  199. package/src/geo/crs/EPSG3395.js +19 -0
  200. package/src/geo/crs/EPSG3857.js +26 -0
  201. package/src/geo/crs/EPSG4326.js +21 -0
  202. package/src/geo/crs/EarthCRS.js +32 -0
  203. package/src/geo/crs/SimpleCRS.js +35 -0
  204. package/src/geo/crs/index.js +6 -0
  205. package/src/geo/index.js +7 -0
  206. package/src/geo/projection/Projection.LonLat.js +30 -0
  207. package/src/geo/projection/Projection.Mercator.js +53 -0
  208. package/src/geo/projection/Projection.SphericalMercator.js +46 -0
  209. package/src/geo/projection/index.js +26 -0
  210. package/src/geometry/Bounds.js +222 -0
  211. package/src/geometry/LineUtil.js +299 -0
  212. package/src/geometry/Point.js +239 -0
  213. package/src/geometry/PolyUtil.js +129 -0
  214. package/src/geometry/Transformation.js +67 -0
  215. package/src/geometry/index.js +8 -0
  216. package/src/images/layers.svg +1 -0
  217. package/src/images/logo.svg +1 -0
  218. package/src/images/marker-icon.svg +1 -0
  219. package/src/images/marker-shadow.svg +1 -0
  220. package/src/layer/BlanketOverlay.js +171 -0
  221. package/src/layer/DivOverlay.js +353 -0
  222. package/src/layer/FeatureGroup.js +93 -0
  223. package/src/layer/GeoJSON.js +432 -0
  224. package/src/layer/ImageOverlay.js +279 -0
  225. package/src/layer/Layer.js +273 -0
  226. package/src/layer/LayerGroup.js +129 -0
  227. package/src/layer/Popup.js +528 -0
  228. package/src/layer/SVGOverlay.js +47 -0
  229. package/src/layer/Tooltip.js +469 -0
  230. package/src/layer/VideoOverlay.js +111 -0
  231. package/src/layer/index.js +18 -0
  232. package/src/layer/marker/DefaultIcon.js +79 -0
  233. package/src/layer/marker/DivIcon.js +76 -0
  234. package/src/layer/marker/Icon.js +164 -0
  235. package/src/layer/marker/Marker.Drag.js +161 -0
  236. package/src/layer/marker/Marker.js +465 -0
  237. package/src/layer/marker/index.js +5 -0
  238. package/src/layer/tile/GridLayer.js +901 -0
  239. package/src/layer/tile/TileLayer.js +299 -0
  240. package/src/layer/tile/WMSTileLayer.js +133 -0
  241. package/src/layer/tile/index.js +3 -0
  242. package/src/layer/vector/Canvas.js +479 -0
  243. package/src/layer/vector/Circle.js +87 -0
  244. package/src/layer/vector/CircleMarker.js +110 -0
  245. package/src/layer/vector/Path.js +145 -0
  246. package/src/layer/vector/Polygon.js +160 -0
  247. package/src/layer/vector/Polyline.js +296 -0
  248. package/src/layer/vector/Rectangle.js +52 -0
  249. package/src/layer/vector/Renderer.getRenderer.js +48 -0
  250. package/src/layer/vector/Renderer.js +75 -0
  251. package/src/layer/vector/SVG.js +216 -0
  252. package/src/layer/vector/index.js +10 -0
  253. package/src/map/Map.js +1830 -0
  254. package/src/map/handler/BoxZoomHandler.js +156 -0
  255. package/src/map/handler/DoubleClickZoomHandler.js +62 -0
  256. package/src/map/handler/DragHandler.js +240 -0
  257. package/src/map/handler/KeyboardHandler.js +188 -0
  258. package/src/map/handler/PinchZoomHandler.js +157 -0
  259. package/src/map/handler/ScrollWheelZoomHandler.js +98 -0
  260. package/src/map/handler/TapHoldHandler.js +111 -0
  261. package/src/map/index.js +8 -0
  262. package/src/maplair.css +730 -0
@@ -0,0 +1,299 @@
1
+ import {Point} from './Point.js';
2
+ import {LatLng} from '../geo/LatLng.js';
3
+ import {centroid} from './PolyUtil.js';
4
+ import {LatLngBounds} from '../geo/LatLngBounds.js';
5
+
6
+
7
+ /*
8
+ * @namespace LineUtil
9
+ *
10
+ * Various utility functions for polyline points processing, used by Maplair internally to make polylines lightning-fast.
11
+ */
12
+
13
+ // Simplify polyline with vertex reduction and Douglas-Peucker simplification.
14
+ // Improves rendering performance dramatically by lessening the number of points to draw.
15
+
16
+ // @function simplify(points: Point[], tolerance: Number): Point[]
17
+ // Dramatically reduces the number of points in a polyline while retaining
18
+ // its shape and returns a new array of simplified points, using the
19
+ // [Ramer-Douglas-Peucker algorithm](https://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm).
20
+ // Used for a huge performance boost when processing/displaying Maplair polylines for
21
+ // each zoom level and also reducing visual noise. tolerance affects the amount of
22
+ // simplification (lesser value means higher quality but slower and with more points).
23
+ // Also released as a separated micro-library [Simplify.js](https://mourner.github.io/simplify-js/).
24
+ export function simplify(points, tolerance) {
25
+ if (!tolerance || !points.length) {
26
+ return points.slice();
27
+ }
28
+
29
+ const sqTolerance = tolerance * tolerance;
30
+
31
+ // stage 1: vertex reduction
32
+ points = _reducePoints(points, sqTolerance);
33
+
34
+ // stage 2: Douglas-Peucker simplification
35
+ points = _simplifyDP(points, sqTolerance);
36
+
37
+ return points;
38
+ }
39
+
40
+ // @function pointToSegmentDistance(p: Point, p1: Point, p2: Point): Number
41
+ // Returns the distance between point `p` and segment `p1` to `p2`.
42
+ export function pointToSegmentDistance(p, p1, p2) {
43
+ return Math.sqrt(_sqClosestPointOnSegment(p, p1, p2, true));
44
+ }
45
+
46
+ // @function closestPointOnSegment(p: Point, p1: Point, p2: Point): Number
47
+ // Returns the closest point from a point `p` on a segment `p1` to `p2`.
48
+ export function closestPointOnSegment(p, p1, p2) {
49
+ return _sqClosestPointOnSegment(p, p1, p2);
50
+ }
51
+
52
+ // Ramer-Douglas-Peucker simplification, see https://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
53
+ function _simplifyDP(points, sqTolerance) {
54
+
55
+ const len = points.length,
56
+ markers = new Uint8Array(len);
57
+ markers[0] = markers[len - 1] = 1;
58
+
59
+ _simplifyDPStep(points, markers, sqTolerance, 0, len - 1);
60
+
61
+ let i;
62
+ const newPoints = [];
63
+
64
+ for (i = 0; i < len; i++) {
65
+ if (markers[i]) {
66
+ newPoints.push(points[i]);
67
+ }
68
+ }
69
+
70
+ return newPoints;
71
+ }
72
+
73
+ function _simplifyDPStep(points, markers, sqTolerance, first, last) {
74
+
75
+ let maxSqDist = 0,
76
+ index, i, sqDist;
77
+
78
+ for (i = first + 1; i <= last - 1; i++) {
79
+ sqDist = _sqClosestPointOnSegment(points[i], points[first], points[last], true);
80
+
81
+ if (sqDist > maxSqDist) {
82
+ index = i;
83
+ maxSqDist = sqDist;
84
+ }
85
+ }
86
+
87
+ if (maxSqDist > sqTolerance) {
88
+ markers[index] = 1;
89
+
90
+ _simplifyDPStep(points, markers, sqTolerance, first, index);
91
+ _simplifyDPStep(points, markers, sqTolerance, index, last);
92
+ }
93
+ }
94
+
95
+ // reduce points that are too close to each other to a single point
96
+ function _reducePoints(points, sqTolerance) {
97
+ const reducedPoints = [points[0]];
98
+ let prev = 0;
99
+
100
+ for (let i = 1; i < points.length; i++) {
101
+ if (_sqDist(points[i], points[prev]) > sqTolerance) {
102
+ reducedPoints.push(points[i]);
103
+ prev = i;
104
+ }
105
+ }
106
+ if (prev < points.length - 1) {
107
+ reducedPoints.push(points[points.length - 1]);
108
+ }
109
+ return reducedPoints;
110
+ }
111
+
112
+ let _lastCode;
113
+
114
+ // @function clipSegment(a: Point, b: Point, bounds: Bounds, useLastCode?: Boolean, round?: Boolean): Point[]|Boolean
115
+ // Clips the segment a to b by rectangular bounds with the
116
+ // [Cohen-Sutherland algorithm](https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm)
117
+ // (modifying the segment points directly!). Used by Maplair to only show polyline
118
+ // points that are on the screen or near, increasing performance.
119
+ export function clipSegment(a, b, bounds, useLastCode, round) {
120
+ let codeA = useLastCode ? _lastCode : _getBitCode(a, bounds),
121
+ codeB = _getBitCode(b, bounds),
122
+
123
+ codeOut, p, newCode;
124
+
125
+ // save 2nd code to avoid calculating it on the next segment
126
+ _lastCode = codeB;
127
+
128
+ while (true) {
129
+ // if a,b is inside the clip window (trivial accept)
130
+ if (!(codeA | codeB)) {
131
+ return [a, b];
132
+ }
133
+
134
+ // if a,b is outside the clip window (trivial reject)
135
+ if (codeA & codeB) {
136
+ return false;
137
+ }
138
+
139
+ // other cases
140
+ codeOut = codeA || codeB;
141
+ p = _getEdgeIntersection(a, b, codeOut, bounds, round);
142
+ newCode = _getBitCode(p, bounds);
143
+
144
+ if (codeOut === codeA) {
145
+ a = p;
146
+ codeA = newCode;
147
+ } else {
148
+ b = p;
149
+ codeB = newCode;
150
+ }
151
+ }
152
+ }
153
+
154
+ export function _getEdgeIntersection(a, b, code, bounds, round) {
155
+ const dx = b.x - a.x,
156
+ dy = b.y - a.y,
157
+ min = bounds.min,
158
+ max = bounds.max;
159
+ let x, y;
160
+
161
+ if (code & 8) { // top
162
+ x = a.x + dx * (max.y - a.y) / dy;
163
+ y = max.y;
164
+
165
+ } else if (code & 4) { // bottom
166
+ x = a.x + dx * (min.y - a.y) / dy;
167
+ y = min.y;
168
+
169
+ } else if (code & 2) { // right
170
+ x = max.x;
171
+ y = a.y + dy * (max.x - a.x) / dx;
172
+
173
+ } else if (code & 1) { // left
174
+ x = min.x;
175
+ y = a.y + dy * (min.x - a.x) / dx;
176
+ }
177
+
178
+ return new Point(x, y, round);
179
+ }
180
+
181
+ export function _getBitCode(p, bounds) {
182
+ let code = 0;
183
+
184
+ if (p.x < bounds.min.x) { // left
185
+ code |= 1;
186
+ } else if (p.x > bounds.max.x) { // right
187
+ code |= 2;
188
+ }
189
+
190
+ if (p.y < bounds.min.y) { // bottom
191
+ code |= 4;
192
+ } else if (p.y > bounds.max.y) { // top
193
+ code |= 8;
194
+ }
195
+
196
+ return code;
197
+ }
198
+
199
+ // square distance (to avoid unnecessary Math.sqrt calls)
200
+ function _sqDist(p1, p2) {
201
+ const dx = p2.x - p1.x,
202
+ dy = p2.y - p1.y;
203
+ return dx * dx + dy * dy;
204
+ }
205
+
206
+ // return closest point on segment or distance to that point
207
+ export function _sqClosestPointOnSegment(p, p1, p2, sqDist) {
208
+ let x = p1.x,
209
+ y = p1.y,
210
+ dx = p2.x - x,
211
+ dy = p2.y - y,
212
+ t;
213
+ const dot = dx * dx + dy * dy;
214
+
215
+ if (dot > 0) {
216
+ t = ((p.x - x) * dx + (p.y - y) * dy) / dot;
217
+
218
+ if (t > 1) {
219
+ x = p2.x;
220
+ y = p2.y;
221
+ } else if (t > 0) {
222
+ x += dx * t;
223
+ y += dy * t;
224
+ }
225
+ }
226
+
227
+ dx = p.x - x;
228
+ dy = p.y - y;
229
+
230
+ return sqDist ? dx * dx + dy * dy : new Point(x, y);
231
+ }
232
+
233
+
234
+ // @function isFlat(latlngs: LatLng[]): Boolean
235
+ // Returns true if `latlngs` is a flat array, false is nested.
236
+ export function isFlat(latlngs) {
237
+ return !Array.isArray(latlngs[0]) || (typeof latlngs[0][0] !== 'object' && typeof latlngs[0][0] !== 'undefined');
238
+ }
239
+
240
+ /* @function polylineCenter(latlngs: LatLng[], crs: CRS): LatLng
241
+ * Returns the center ([centroid](http://en.wikipedia.org/wiki/Centroid)) of the passed LatLngs (first ring) from a polyline.
242
+ */
243
+ export function polylineCenter(latlngs, crs) {
244
+ let i, halfDist, segDist, dist, p1, p2, ratio, center;
245
+
246
+ if (!latlngs || latlngs.length === 0) {
247
+ throw new Error('latlngs not passed');
248
+ }
249
+
250
+ if (!isFlat(latlngs)) {
251
+ console.warn('latlngs are not flat! Only the first ring will be used');
252
+ latlngs = latlngs[0];
253
+ }
254
+
255
+ let centroidLatLng = new LatLng([0, 0]);
256
+
257
+ const bounds = new LatLngBounds(latlngs);
258
+ const areaBounds = bounds.getNorthWest().distanceTo(bounds.getSouthWest()) * bounds.getNorthEast().distanceTo(bounds.getNorthWest());
259
+ // tests showed that below 1700 rounding errors are happening
260
+ if (areaBounds < 1700) {
261
+ // getting a inexact center, to move the latlngs near to [0, 0] to prevent rounding errors
262
+ centroidLatLng = centroid(latlngs);
263
+ }
264
+
265
+ const len = latlngs.length;
266
+ const points = [];
267
+ for (i = 0; i < len; i++) {
268
+ const latlng = new LatLng(latlngs[i]);
269
+ points.push(crs.project(new LatLng([latlng.lat - centroidLatLng.lat, latlng.lng - centroidLatLng.lng])));
270
+ }
271
+
272
+ for (i = 0, halfDist = 0; i < len - 1; i++) {
273
+ halfDist += points[i].distanceTo(points[i + 1]) / 2;
274
+ }
275
+
276
+ // The line is so small in the current view that all points are on the same pixel.
277
+ if (halfDist === 0) {
278
+ center = points[0];
279
+ } else {
280
+ for (i = 0, dist = 0; i < len - 1; i++) {
281
+ p1 = points[i];
282
+ p2 = points[i + 1];
283
+ segDist = p1.distanceTo(p2);
284
+ dist += segDist;
285
+
286
+ if (dist > halfDist) {
287
+ ratio = (dist - halfDist) / segDist;
288
+ center = [
289
+ p2.x - ratio * (p2.x - p1.x),
290
+ p2.y - ratio * (p2.y - p1.y)
291
+ ];
292
+ break;
293
+ }
294
+ }
295
+ }
296
+
297
+ const latlngCenter = crs.unproject(new Point(center));
298
+ return new LatLng([latlngCenter.lat + centroidLatLng.lat, latlngCenter.lng + centroidLatLng.lng]);
299
+ }
@@ -0,0 +1,239 @@
1
+ import {formatNum} from '../core/Util.js';
2
+
3
+ /*
4
+ * @class Point
5
+ *
6
+ * Represents a point with `x` and `y` coordinates in pixels.
7
+ *
8
+ * @example
9
+ *
10
+ * ```js
11
+ * const point = new Point(200, 300);
12
+ * ```
13
+ *
14
+ * All Maplair methods and options that accept `Point` objects also accept them in a simple Array form (unless noted otherwise), so these lines are equivalent:
15
+ *
16
+ * ```js
17
+ * map.panBy([200, 300]);
18
+ * map.panBy(new Point(200, 300));
19
+ * ```
20
+ *
21
+ * Note that `Point` does not inherit from Maplair's `Class` object,
22
+ * which means new classes can't inherit from it, and new methods
23
+ * can't be added to it with the `include` function.
24
+ */
25
+
26
+ // @constructor Point(x: Number, y: Number, round?: Boolean)
27
+ // Creates a Point object with the given `x` and `y` coordinates. If optional `round` is set to true, rounds the `x` and `y` values.
28
+
29
+ // @alternative
30
+ // @constructor Point(coords: Point)
31
+ // Expects a Point object instead.
32
+
33
+ // @alternative
34
+ // @constructor Point(coords: Number[])
35
+ // Expects an array of the form `[x, y]` instead.
36
+
37
+ // @alternative
38
+ // @constructor Point(coords: Object)
39
+ // Expects a plain object of the form `{x: Number, y: Number}` instead.
40
+ export class Point {
41
+ constructor(x, y, round) {
42
+
43
+ const valid = Point.validate(x, y);
44
+ if (!valid) {
45
+ throw new Error(`Invalid Point object: (${x}, ${y})`);
46
+ }
47
+
48
+ let _x, _y;
49
+ if (x instanceof Point) {
50
+ _x = x.x;
51
+ _y = x.y;
52
+ } else if (Array.isArray(x)) {
53
+ _x = x[0];
54
+ _y = x[1];
55
+ } else if (typeof x === 'object' && 'x' in x && 'y' in x) {
56
+ _x = x.x;
57
+ _y = x.y;
58
+ } else {
59
+ _x = x;
60
+ _y = y;
61
+ }
62
+
63
+ // @property x: Number; The `x` coordinate of the point
64
+ this.x = (round ? Math.round(_x) : _x);
65
+ // @property y: Number; The `y` coordinate of the point
66
+ this.y = (round ? Math.round(_y) : _y);
67
+ }
68
+
69
+ // @section
70
+ // There are several static functions which can be called without instantiating Point:
71
+
72
+ // @function validate(x: Number, y: Number): Boolean
73
+ // Returns `true` if the Point object can be properly initialized.
74
+
75
+ // @alternative
76
+ // @function validate(coords: Number[]): Boolean
77
+ // Expects an array of the form `[x, y]`. Returns `true` if the Point object can be properly initialized.
78
+
79
+ // @alternative
80
+ // @function validate(coords: Object): Boolean
81
+ // Returns `true` if the Point object can be properly initialized.
82
+ static validate(x, y) {
83
+ if (x instanceof Point || Array.isArray(x)) {
84
+ return true;
85
+ } else if (x && typeof x === 'object' && 'x' in x && 'y' in x) {
86
+ return true;
87
+ } else if ((x || x === 0) && (y || y === 0)) {
88
+ return true;
89
+ }
90
+ return false;
91
+ }
92
+
93
+ // @method add(otherPoint: Point): Point
94
+ // Returns the result of addition of the current and the given points.
95
+ add(point) {
96
+ // non-destructive, returns a new point
97
+ return new Point(this)._add(new Point(point));
98
+ }
99
+
100
+ _add(point) {
101
+ // destructive, used directly for performance in situations where it's safe to modify existing point
102
+ this.x += point.x;
103
+ this.y += point.y;
104
+ return this;
105
+ }
106
+
107
+ // @method subtract(otherPoint: Point): Point
108
+ // Returns the result of subtraction of the given point from the current.
109
+ subtract(point) {
110
+ return new Point(this)._subtract(new Point(point));
111
+ }
112
+
113
+ _subtract(point) {
114
+ this.x -= point.x;
115
+ this.y -= point.y;
116
+ return this;
117
+ }
118
+
119
+ // @method divideBy(num: Number): Point
120
+ // Returns the result of division of the current point by the given number.
121
+ divideBy(num) {
122
+ return new Point(this)._divideBy(num);
123
+ }
124
+
125
+ _divideBy(num) {
126
+ this.x /= num;
127
+ this.y /= num;
128
+ return this;
129
+ }
130
+
131
+ // @method multiplyBy(num: Number): Point
132
+ // Returns the result of multiplication of the current point by the given number.
133
+ multiplyBy(num) {
134
+ return new Point(this)._multiplyBy(num);
135
+ }
136
+
137
+ _multiplyBy(num) {
138
+ this.x *= num;
139
+ this.y *= num;
140
+ return this;
141
+ }
142
+
143
+ // @method scaleBy(scale: Point): Point
144
+ // Multiply each coordinate of the current point by each coordinate of
145
+ // `scale`. In linear algebra terms, multiply the point by the
146
+ // [scaling matrix](https://en.wikipedia.org/wiki/Scaling_%28geometry%29#Matrix_representation)
147
+ // defined by `scale`.
148
+ scaleBy(point) {
149
+ return new Point(this.x * point.x, this.y * point.y);
150
+ }
151
+
152
+ // @method unscaleBy(scale: Point): Point
153
+ // Inverse of `scaleBy`. Divide each coordinate of the current point by
154
+ // each coordinate of `scale`.
155
+ unscaleBy(point) {
156
+ return new Point(this.x / point.x, this.y / point.y);
157
+ }
158
+
159
+ // Returns a copy of the current point with rounded coordinates.
160
+ round() {
161
+ return new Point(this)._round();
162
+ }
163
+
164
+ _round() {
165
+ this.x = Math.round(this.x);
166
+ this.y = Math.round(this.y);
167
+ return this;
168
+ }
169
+
170
+ // @method floor(): Point
171
+ // Returns a copy of the current point with floored coordinates (rounded down).
172
+ floor() {
173
+ return new Point(this)._floor();
174
+ }
175
+
176
+ _floor() {
177
+ this.x = Math.floor(this.x);
178
+ this.y = Math.floor(this.y);
179
+ return this;
180
+ }
181
+
182
+ // @method ceil(): Point
183
+ // Returns a copy of the current point with ceiled coordinates (rounded up).
184
+ ceil() {
185
+ return new Point(this)._ceil();
186
+ }
187
+
188
+ _ceil() {
189
+ this.x = Math.ceil(this.x);
190
+ this.y = Math.ceil(this.y);
191
+ return this;
192
+ }
193
+
194
+ // Returns a copy of the current point with truncated coordinates (rounded towards zero).
195
+ trunc() {
196
+ return new Point(this)._trunc();
197
+ }
198
+
199
+ _trunc() {
200
+ this.x = Math.trunc(this.x);
201
+ this.y = Math.trunc(this.y);
202
+ return this;
203
+ }
204
+
205
+ // @method distanceTo(otherPoint: Point): Number
206
+ // Returns the cartesian distance between the current and the given points.
207
+ distanceTo(point) {
208
+ point = new Point(point);
209
+
210
+ const x = point.x - this.x,
211
+ y = point.y - this.y;
212
+
213
+ return Math.sqrt(x * x + y * y);
214
+ }
215
+
216
+ // @method equals(otherPoint: Point): Boolean
217
+ // Returns `true` if the given point has the same coordinates.
218
+ equals(point) {
219
+ point = new Point(point);
220
+
221
+ return point.x === this.x &&
222
+ point.y === this.y;
223
+ }
224
+
225
+ // @method contains(otherPoint: Point): Boolean
226
+ // Returns `true` if both coordinates of the given point are less than the corresponding current point coordinates (in absolute values).
227
+ contains(point) {
228
+ point = new Point(point);
229
+
230
+ return Math.abs(point.x) <= Math.abs(this.x) &&
231
+ Math.abs(point.y) <= Math.abs(this.y);
232
+ }
233
+
234
+ // @method toString(): String
235
+ // Returns a string representation of the point for debugging purposes.
236
+ toString() {
237
+ return `Point(${formatNum(this.x)}, ${formatNum(this.y)})`;
238
+ }
239
+ }
@@ -0,0 +1,129 @@
1
+ import * as LineUtil from './LineUtil.js';
2
+ import {LatLng} from '../geo/LatLng.js';
3
+ import {Point} from './Point.js';
4
+ import {LatLngBounds} from '../geo/LatLngBounds.js';
5
+ /*
6
+ * @namespace PolyUtil
7
+ * Various utility functions for polygon geometries.
8
+ */
9
+
10
+ /* @function clipPolygon(points: Point[], bounds: Bounds, round?: Boolean): Point[]
11
+ * Clips the polygon geometry defined by the given `points` by the given bounds (using the [Sutherland-Hodgman algorithm](https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm)).
12
+ * Used by Maplair to only show polygon points that are on the screen or near, increasing
13
+ * performance. Note that polygon points needs different algorithm for clipping
14
+ * than polyline, so there's a separate method for it.
15
+ */
16
+ export function clipPolygon(points, bounds, round) {
17
+ let clippedPoints,
18
+ i, j, k,
19
+ a, b,
20
+ len, edge, p;
21
+ const edges = [1, 4, 2, 8];
22
+
23
+ for (i = 0, len = points.length; i < len; i++) {
24
+ points[i]._code = LineUtil._getBitCode(points[i], bounds);
25
+ }
26
+
27
+ // for each edge (left, bottom, right, top)
28
+ for (k = 0; k < 4; k++) {
29
+ edge = edges[k];
30
+ clippedPoints = [];
31
+
32
+ for (i = 0, len = points.length, j = len - 1; i < len; j = i++) {
33
+ a = points[i];
34
+ b = points[j];
35
+
36
+ // if a is inside the clip window
37
+ if (!(a._code & edge)) {
38
+ // if b is outside the clip window (a->b goes out of screen)
39
+ if (b._code & edge) {
40
+ p = LineUtil._getEdgeIntersection(b, a, edge, bounds, round);
41
+ p._code = LineUtil._getBitCode(p, bounds);
42
+ clippedPoints.push(p);
43
+ }
44
+ clippedPoints.push(a);
45
+
46
+ // else if b is inside the clip window (a->b enters the screen)
47
+ } else if (!(b._code & edge)) {
48
+ p = LineUtil._getEdgeIntersection(b, a, edge, bounds, round);
49
+ p._code = LineUtil._getBitCode(p, bounds);
50
+ clippedPoints.push(p);
51
+ }
52
+ }
53
+ points = clippedPoints;
54
+ }
55
+
56
+ return points;
57
+ }
58
+
59
+ /* @function polygonCenter(latlngs: LatLng[], crs: CRS): LatLng
60
+ * Returns the center ([centroid](http://en.wikipedia.org/wiki/Centroid)) of the passed LatLngs (first ring) from a polygon.
61
+ */
62
+ export function polygonCenter(latlngs, crs) {
63
+ let i, j, p1, p2, f, area, x, y, center;
64
+
65
+ if (!latlngs || latlngs.length === 0) {
66
+ throw new Error('latlngs not passed');
67
+ }
68
+
69
+ if (!LineUtil.isFlat(latlngs)) {
70
+ console.warn('latlngs are not flat! Only the first ring will be used');
71
+ latlngs = latlngs[0];
72
+ }
73
+
74
+ let centroidLatLng = new LatLng([0, 0]);
75
+
76
+ const bounds = new LatLngBounds(latlngs);
77
+ const areaBounds = bounds.getNorthWest().distanceTo(bounds.getSouthWest()) * bounds.getNorthEast().distanceTo(bounds.getNorthWest());
78
+ // tests showed that below 1700 rounding errors are happening
79
+ if (areaBounds < 1700) {
80
+ // getting a inexact center, to move the latlngs near to [0, 0] to prevent rounding errors
81
+ centroidLatLng = centroid(latlngs);
82
+ }
83
+
84
+ const len = latlngs.length;
85
+ const points = [];
86
+ for (i = 0; i < len; i++) {
87
+ const latlng = new LatLng(latlngs[i]);
88
+ points.push(crs.project(new LatLng([latlng.lat - centroidLatLng.lat, latlng.lng - centroidLatLng.lng])));
89
+ }
90
+
91
+ area = x = y = 0;
92
+
93
+ // polygon centroid algorithm;
94
+ for (i = 0, j = len - 1; i < len; j = i++) {
95
+ p1 = points[i];
96
+ p2 = points[j];
97
+
98
+ f = p1.y * p2.x - p2.y * p1.x;
99
+ x += (p1.x + p2.x) * f;
100
+ y += (p1.y + p2.y) * f;
101
+ area += f * 3;
102
+ }
103
+
104
+ if (area === 0) {
105
+ // Polygon is so small that all points are on same pixel.
106
+ center = points[0];
107
+ } else {
108
+ center = [x / area, y / area];
109
+ }
110
+
111
+ const latlngCenter = crs.unproject(new Point(center));
112
+ return new LatLng([latlngCenter.lat + centroidLatLng.lat, latlngCenter.lng + centroidLatLng.lng]);
113
+ }
114
+
115
+ /* @function centroid(latlngs: LatLng[]): LatLng
116
+ * Returns the 'center of mass' of the passed LatLngs.
117
+ */
118
+ export function centroid(coords) {
119
+ let latSum = 0;
120
+ let lngSum = 0;
121
+ let len = 0;
122
+ for (const coord of coords) {
123
+ const latlng = new LatLng(coord);
124
+ latSum += latlng.lat;
125
+ lngSum += latlng.lng;
126
+ len++;
127
+ }
128
+ return new LatLng([latSum / len, lngSum / len]);
129
+ }