maplair 4.0.0-alpha.2 → 4.1.0

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 (284) hide show
  1. package/CHANGELOG.md +140 -38
  2. package/LICENSE +1 -1
  3. package/README.md +10 -9
  4. package/dist/esm/Maplair.js +14 -14
  5. package/dist/esm/control/AttributionControl.js +24 -14
  6. package/dist/esm/control/AttributionControl.js.map +1 -1
  7. package/dist/esm/control/ButtonControl.js +9 -11
  8. package/dist/esm/control/ButtonControl.js.map +1 -1
  9. package/dist/esm/control/ContextMenu.js +1 -3
  10. package/dist/esm/control/ContextMenu.js.map +1 -1
  11. package/dist/esm/control/Control.js +9 -16
  12. package/dist/esm/control/Control.js.map +1 -1
  13. package/dist/esm/control/FullscreenControl.js +5 -7
  14. package/dist/esm/control/FullscreenControl.js.map +1 -1
  15. package/dist/esm/control/LayersControl.js +144 -35
  16. package/dist/esm/control/LayersControl.js.map +1 -1
  17. package/dist/esm/control/LegendControl.js +7 -9
  18. package/dist/esm/control/LegendControl.js.map +1 -1
  19. package/dist/esm/control/MiniMapControl.js +14 -16
  20. package/dist/esm/control/MiniMapControl.js.map +1 -1
  21. package/dist/esm/control/RotateControl.js +1 -3
  22. package/dist/esm/control/RotateControl.js.map +1 -1
  23. package/dist/esm/control/ScaleControl.js +7 -9
  24. package/dist/esm/control/ScaleControl.js.map +1 -1
  25. package/dist/esm/control/SearchControl.js +100 -18
  26. package/dist/esm/control/SearchControl.js.map +1 -1
  27. package/dist/esm/control/SideBySideControl.js +1 -3
  28. package/dist/esm/control/SideBySideControl.js.map +1 -1
  29. package/dist/esm/control/TextControl.js +5 -7
  30. package/dist/esm/control/TextControl.js.map +1 -1
  31. package/dist/esm/control/TimeControl.js +408 -0
  32. package/dist/esm/control/TimeControl.js.map +1 -0
  33. package/dist/esm/control/ZoomControl.js +5 -7
  34. package/dist/esm/control/ZoomControl.js.map +1 -1
  35. package/dist/esm/core/Class.js +28 -37
  36. package/dist/esm/core/Class.js.map +1 -1
  37. package/dist/esm/core/Events.js +21 -8
  38. package/dist/esm/core/Events.js.map +1 -1
  39. package/dist/esm/core/Handler.js +13 -11
  40. package/dist/esm/core/Handler.js.map +1 -1
  41. package/dist/esm/core/Util.js +13 -1
  42. package/dist/esm/core/Util.js.map +1 -1
  43. package/dist/esm/dom/DomEvent.DoubleTap.js +25 -22
  44. package/dist/esm/dom/DomEvent.DoubleTap.js.map +1 -1
  45. package/dist/esm/dom/DomEvent.PointerEvents.js +2 -16
  46. package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -1
  47. package/dist/esm/dom/DomEvent.js +8 -42
  48. package/dist/esm/dom/DomEvent.js.map +1 -1
  49. package/dist/esm/dom/DomUtil.js +14 -68
  50. package/dist/esm/dom/DomUtil.js.map +1 -1
  51. package/dist/esm/dom/Draggable.js +17 -18
  52. package/dist/esm/dom/Draggable.js.map +1 -1
  53. package/dist/esm/dom/PosAnimation.js +3 -25
  54. package/dist/esm/dom/PosAnimation.js.map +1 -1
  55. package/dist/esm/geo/LatLng.js +34 -6
  56. package/dist/esm/geo/LatLng.js.map +1 -1
  57. package/dist/esm/geo/LatLngBounds.js +19 -7
  58. package/dist/esm/geo/LatLngBounds.js.map +1 -1
  59. package/dist/esm/geo/crs/CRS.js +4 -3
  60. package/dist/esm/geo/crs/CRS.js.map +1 -1
  61. package/dist/esm/geo/crs/EPSG3395.js +1 -1
  62. package/dist/esm/geo/crs/EPSG3395.js.map +1 -1
  63. package/dist/esm/geo/crs/EPSG3857.js +1 -1
  64. package/dist/esm/geo/crs/EPSG3857.js.map +1 -1
  65. package/dist/esm/geo/crs/EPSG4326.js +1 -1
  66. package/dist/esm/geo/crs/EPSG4326.js.map +1 -1
  67. package/dist/esm/geo/crs/EarthCRS.js +2 -2
  68. package/dist/esm/geo/crs/EarthCRS.js.map +1 -1
  69. package/dist/esm/geo/crs/SimpleCRS.js +1 -1
  70. package/dist/esm/geo/crs/SimpleCRS.js.map +1 -1
  71. package/dist/esm/geo/projection/Projection.LonLat.js +3 -3
  72. package/dist/esm/geo/projection/Projection.LonLat.js.map +1 -1
  73. package/dist/esm/geo/projection/Projection.Mercator.js +3 -3
  74. package/dist/esm/geo/projection/Projection.Mercator.js.map +1 -1
  75. package/dist/esm/geo/projection/Projection.SphericalMercator.js +3 -3
  76. package/dist/esm/geo/projection/Projection.SphericalMercator.js.map +1 -1
  77. package/dist/esm/geometry/Bounds.js +19 -7
  78. package/dist/esm/geometry/Bounds.js.map +1 -1
  79. package/dist/esm/geometry/LineUtil.js +36 -30
  80. package/dist/esm/geometry/LineUtil.js.map +1 -1
  81. package/dist/esm/geometry/Point.js +23 -5
  82. package/dist/esm/geometry/Point.js.map +1 -1
  83. package/dist/esm/geometry/PolyUtil.js +9 -28
  84. package/dist/esm/geometry/PolyUtil.js.map +1 -1
  85. package/dist/esm/layer/BlanketOverlay.js +6 -8
  86. package/dist/esm/layer/BlanketOverlay.js.map +1 -1
  87. package/dist/esm/layer/DivOverlay.js +15 -20
  88. package/dist/esm/layer/DivOverlay.js.map +1 -1
  89. package/dist/esm/layer/GeoJSON.js +9 -145
  90. package/dist/esm/layer/GeoJSON.js.map +1 -1
  91. package/dist/esm/layer/GeoJSONUtil.js +75 -0
  92. package/dist/esm/layer/GeoJSONUtil.js.map +1 -0
  93. package/dist/esm/layer/HeatLayer.js +15 -17
  94. package/dist/esm/layer/HeatLayer.js.map +1 -1
  95. package/dist/esm/layer/ImageOverlay.js +20 -26
  96. package/dist/esm/layer/ImageOverlay.js.map +1 -1
  97. package/dist/esm/layer/Layer.js +8 -10
  98. package/dist/esm/layer/Layer.js.map +1 -1
  99. package/dist/esm/layer/LayerGroup.js +51 -0
  100. package/dist/esm/layer/LayerGroup.js.map +1 -1
  101. package/dist/esm/layer/Popup.js +27 -43
  102. package/dist/esm/layer/Popup.js.map +1 -1
  103. package/dist/esm/layer/SVGOverlay.js +2 -5
  104. package/dist/esm/layer/SVGOverlay.js.map +1 -1
  105. package/dist/esm/layer/Tooltip.js +15 -17
  106. package/dist/esm/layer/Tooltip.js.map +1 -1
  107. package/dist/esm/layer/VideoOverlay.js +12 -15
  108. package/dist/esm/layer/VideoOverlay.js.map +1 -1
  109. package/dist/esm/layer/marker/DefaultIcon.js +15 -48
  110. package/dist/esm/layer/marker/DefaultIcon.js.map +1 -1
  111. package/dist/esm/layer/marker/DivIcon.js +8 -10
  112. package/dist/esm/layer/marker/DivIcon.js.map +1 -1
  113. package/dist/esm/layer/marker/Icon.js +9 -12
  114. package/dist/esm/layer/marker/Icon.js.map +1 -1
  115. package/dist/esm/layer/marker/Marker.Drag.js +1 -1
  116. package/dist/esm/layer/marker/Marker.Drag.js.map +1 -1
  117. package/dist/esm/layer/marker/Marker.Move.js +2 -3
  118. package/dist/esm/layer/marker/Marker.Move.js.map +1 -1
  119. package/dist/esm/layer/marker/Marker.js +63 -35
  120. package/dist/esm/layer/marker/Marker.js.map +1 -1
  121. package/dist/esm/layer/marker/MarkerClusterGroup.js +302 -0
  122. package/dist/esm/layer/marker/MarkerClusterGroup.js.map +1 -0
  123. package/dist/esm/layer/marker/SvgIcon.js +6 -8
  124. package/dist/esm/layer/marker/SvgIcon.js.map +1 -1
  125. package/dist/esm/layer/tile/GridLayer.js +86 -98
  126. package/dist/esm/layer/tile/GridLayer.js.map +1 -1
  127. package/dist/esm/layer/tile/TileLayer.js +28 -36
  128. package/dist/esm/layer/tile/TileLayer.js.map +1 -1
  129. package/dist/esm/layer/tile/WMSTileLayer.js +28 -23
  130. package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -1
  131. package/dist/esm/layer/vector/Canvas.js +1 -3
  132. package/dist/esm/layer/vector/Canvas.js.map +1 -1
  133. package/dist/esm/layer/vector/Circle.js +2 -2
  134. package/dist/esm/layer/vector/Circle.js.map +1 -1
  135. package/dist/esm/layer/vector/CircleMarker.js +17 -7
  136. package/dist/esm/layer/vector/CircleMarker.js.map +1 -1
  137. package/dist/esm/layer/vector/Path.DashAnimation.js +2 -3
  138. package/dist/esm/layer/vector/Path.DashAnimation.js.map +1 -1
  139. package/dist/esm/layer/vector/Path.js +27 -20
  140. package/dist/esm/layer/vector/Path.js.map +1 -1
  141. package/dist/esm/layer/vector/Polygon.js +20 -4
  142. package/dist/esm/layer/vector/Polygon.js.map +1 -1
  143. package/dist/esm/layer/vector/Polyline.Decorations.js +2 -2
  144. package/dist/esm/layer/vector/Polyline.js +24 -8
  145. package/dist/esm/layer/vector/Polyline.js.map +1 -1
  146. package/dist/esm/layer/vector/Renderer.getRenderer.js +14 -13
  147. package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -1
  148. package/dist/esm/layer/vector/SVG.js +5 -5
  149. package/dist/esm/layer/vector/SVG.js.map +1 -1
  150. package/dist/esm/map/Map.Rotation.js +15 -11
  151. package/dist/esm/map/Map.Rotation.js.map +1 -1
  152. package/dist/esm/map/Map.js +135 -129
  153. package/dist/esm/map/Map.js.map +1 -1
  154. package/dist/esm/map/handler/BoxZoomHandler.js +5 -7
  155. package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -1
  156. package/dist/esm/map/handler/DisplaceHandler.js +6 -8
  157. package/dist/esm/map/handler/DisplaceHandler.js.map +1 -1
  158. package/dist/esm/map/handler/DragHandler.js +20 -20
  159. package/dist/esm/map/handler/DragHandler.js.map +1 -1
  160. package/dist/esm/map/handler/KeyboardHandler.js +37 -37
  161. package/dist/esm/map/handler/KeyboardHandler.js.map +1 -1
  162. package/dist/esm/map/handler/PinchZoomHandler.js +12 -3
  163. package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -1
  164. package/dist/esm/map/handler/ScrollWheelZoomHandler.js +3 -4
  165. package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -1
  166. package/dist/esm/map/handler/TapHoldHandler.js +12 -5
  167. package/dist/esm/map/handler/TapHoldHandler.js.map +1 -1
  168. package/dist/esm/package.js +1 -1
  169. package/dist/maplair-global-src.js +6326 -5625
  170. package/dist/maplair-global-src.js.map +1 -1
  171. package/dist/maplair-global.js +3 -3
  172. package/dist/maplair-global.js.map +1 -1
  173. package/dist/maplair-src.js +9010 -8290
  174. package/dist/maplair-src.js.map +1 -1
  175. package/dist/maplair.css +148 -98
  176. package/dist/maplair.js +3 -3
  177. package/dist/maplair.js.map +1 -1
  178. package/i18n/cs.json +6 -0
  179. package/i18n/de.json +6 -0
  180. package/i18n/en.json +6 -0
  181. package/i18n/es.json +6 -0
  182. package/i18n/fr.json +6 -0
  183. package/i18n/hu.json +6 -0
  184. package/i18n/it.json +6 -0
  185. package/i18n/rm.json +6 -0
  186. package/i18n/sk.json +6 -0
  187. package/i18n/sl.json +6 -0
  188. package/package.json +12 -14
  189. package/src/control/AttributionControl.js +25 -18
  190. package/src/control/ButtonControl.js +9 -11
  191. package/src/control/ContextMenu.js +3 -5
  192. package/src/control/Control.js +9 -19
  193. package/src/control/FullscreenControl.js +5 -7
  194. package/src/control/LayersControl.js +159 -36
  195. package/src/control/LegendControl.js +7 -9
  196. package/src/control/MiniMapControl.js +9 -11
  197. package/src/control/RotateControl.js +3 -5
  198. package/src/control/ScaleControl.js +7 -9
  199. package/src/control/SearchControl.js +104 -21
  200. package/src/control/SideBySideControl.js +3 -5
  201. package/src/control/TextControl.js +5 -7
  202. package/src/control/TimeControl.js +471 -0
  203. package/src/control/ZoomControl.js +5 -7
  204. package/src/control/index.js +1 -0
  205. package/src/core/Class.js +35 -34
  206. package/src/core/Events.js +22 -3
  207. package/src/core/Handler.js +14 -12
  208. package/src/core/Util.js +19 -1
  209. package/src/core/index.js +0 -1
  210. package/src/dom/DomEvent.DoubleTap.js +18 -10
  211. package/src/dom/DomEvent.PointerEvents.js +3 -26
  212. package/src/dom/DomEvent.js +8 -66
  213. package/src/dom/DomUtil.js +22 -78
  214. package/src/dom/Draggable.js +27 -22
  215. package/src/dom/PosAnimation.js +3 -25
  216. package/src/dom/index.js +0 -1
  217. package/src/geo/LatLng.js +40 -4
  218. package/src/geo/LatLngBounds.js +20 -7
  219. package/src/geo/crs/CRS.js +4 -3
  220. package/src/geo/crs/EPSG3395.js +1 -1
  221. package/src/geo/crs/EPSG3857.js +1 -1
  222. package/src/geo/crs/EPSG4326.js +1 -1
  223. package/src/geo/crs/EarthCRS.js +2 -8
  224. package/src/geo/crs/SimpleCRS.js +1 -1
  225. package/src/geo/projection/Projection.LonLat.js +3 -3
  226. package/src/geo/projection/Projection.Mercator.js +3 -3
  227. package/src/geo/projection/Projection.SphericalMercator.js +3 -3
  228. package/src/geometry/Bounds.js +20 -7
  229. package/src/geometry/LineUtil.js +47 -48
  230. package/src/geometry/Point.js +24 -5
  231. package/src/geometry/PolyUtil.js +15 -44
  232. package/src/layer/BlanketOverlay.js +6 -10
  233. package/src/layer/DivOverlay.js +15 -26
  234. package/src/layer/GeoJSON.js +7 -205
  235. package/src/layer/GeoJSONUtil.js +85 -0
  236. package/src/layer/HeatLayer.js +9 -11
  237. package/src/layer/ImageOverlay.js +19 -30
  238. package/src/layer/Layer.js +8 -10
  239. package/src/layer/LayerGroup.js +71 -0
  240. package/src/layer/Popup.js +31 -46
  241. package/src/layer/SVGOverlay.js +1 -5
  242. package/src/layer/Tooltip.js +15 -19
  243. package/src/layer/VideoOverlay.js +12 -15
  244. package/src/layer/marker/DefaultIcon.js +15 -61
  245. package/src/layer/marker/DivIcon.js +10 -12
  246. package/src/layer/marker/Icon.js +9 -12
  247. package/src/layer/marker/Marker.Drag.js +1 -1
  248. package/src/layer/marker/Marker.Move.js +2 -3
  249. package/src/layer/marker/Marker.js +83 -41
  250. package/src/layer/marker/MarkerClusterGroup.js +370 -0
  251. package/src/layer/marker/SvgIcon.js +7 -9
  252. package/src/layer/marker/index.js +1 -0
  253. package/src/layer/tile/GridLayer.js +104 -134
  254. package/src/layer/tile/TileLayer.js +35 -43
  255. package/src/layer/tile/WMSTileLayer.js +26 -24
  256. package/src/layer/vector/Canvas.js +3 -5
  257. package/src/layer/vector/Circle.js +5 -2
  258. package/src/layer/vector/CircleMarker.js +17 -6
  259. package/src/layer/vector/Path.DashAnimation.js +1 -2
  260. package/src/layer/vector/Path.js +30 -21
  261. package/src/layer/vector/Polygon.js +28 -5
  262. package/src/layer/vector/Polyline.js +26 -7
  263. package/src/layer/vector/Renderer.getRenderer.js +17 -17
  264. package/src/layer/vector/SVG.js +5 -5
  265. package/src/map/Map.Rotation.js +19 -11
  266. package/src/map/Map.js +163 -154
  267. package/src/map/handler/BoxZoomHandler.js +9 -8
  268. package/src/map/handler/DisplaceHandler.js +6 -8
  269. package/src/map/handler/DragHandler.js +26 -29
  270. package/src/map/handler/KeyboardHandler.js +44 -50
  271. package/src/map/handler/PinchZoomHandler.js +16 -5
  272. package/src/map/handler/ScrollWheelZoomHandler.js +6 -4
  273. package/src/map/handler/TapHoldHandler.js +15 -4
  274. package/src/map/index.js +1 -1
  275. package/src/maplair.css +148 -98
  276. package/dist/esm/core/Browser.js +0 -100
  277. package/dist/esm/core/Browser.js.map +0 -1
  278. package/dist/images/layers.svg +0 -1
  279. package/dist/images/marker-icon.svg +0 -1
  280. package/dist/images/marker-shadow.svg +0 -1
  281. package/src/core/Browser.js +0 -110
  282. package/src/images/layers.svg +0 -1
  283. package/src/images/marker-icon.svg +0 -1
  284. package/src/images/marker-shadow.svg +0 -1
package/src/geo/LatLng.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import * as Util from '../core/Util.js';
2
- import {EarthCRS} from './crs/EarthCRS.js';
3
2
  import {LatLngBounds} from './LatLngBounds.js';
4
3
 
5
4
  // a coordinate in decimal degrees, with an optional degree sign
@@ -107,6 +106,25 @@ export class LatLng {
107
106
  }
108
107
  }
109
108
 
109
+ /**
110
+ * Returns the given `LatLng` object as it is, or else a new `LatLng` object created
111
+ * from the given arguments (the same as of the constructor) — unlike the
112
+ * constructor, which always creates a new object.
113
+ *
114
+ * @example
115
+ * ```js
116
+ * LatLng.from(latlng) === latlng; // true
117
+ * LatLng.from([50.5, 30.5]); // LatLng(50.5, 30.5)
118
+ * ```
119
+ * @param {number|LatLngExpression} lat Latitude in degrees, or the coordinates.
120
+ * @param {number} [lng] Longitude in degrees.
121
+ * @param {number} [alt] Altitude in meters.
122
+ * @returns {LatLng}
123
+ */
124
+ static from(lat, lng, alt) {
125
+ return lat instanceof LatLng ? lat : new LatLng(lat, lng, alt);
126
+ }
127
+
110
128
  /**
111
129
  * Returns `true` if the LatLng object can be properly initialized with the given
112
130
  * arguments (the same as of the constructor).
@@ -185,7 +203,7 @@ export class LatLng {
185
203
  equals(obj, maxMargin) {
186
204
  if (!obj) { return false; }
187
205
 
188
- obj = new LatLng(obj);
206
+ obj = LatLng.from(obj);
189
207
 
190
208
  const margin = Math.max(
191
209
  Math.abs(this.lat - obj.lat),
@@ -210,7 +228,7 @@ export class LatLng {
210
228
  * @returns {number}
211
229
  */
212
230
  distanceTo(other) {
213
- return EarthCRS.distance(this, new LatLng(other));
231
+ return haversineDistance(this, LatLng.from(other), 6371000);
214
232
  }
215
233
 
216
234
  /**
@@ -219,7 +237,7 @@ export class LatLng {
219
237
  * @returns {LatLng}
220
238
  */
221
239
  wrap() {
222
- return EarthCRS.wrapLatLng(this);
240
+ return new LatLng(this.lat, Util.wrapNum(this.lng, [-180, 180], true), this.alt);
223
241
  }
224
242
 
225
243
  /**
@@ -238,3 +256,21 @@ export class LatLng {
238
256
  }
239
257
 
240
258
  };
259
+
260
+ /**
261
+ * The great-circle distance on a sphere of radius `R`, shared with `EarthCRS.distance()`.
262
+ * @param {LatLng} latlng1
263
+ * @param {LatLng} latlng2
264
+ * @param {number} R
265
+ * @returns {number}
266
+ */
267
+ export function haversineDistance(latlng1, latlng2, R) {
268
+ const rad = Math.PI / 180,
269
+ lat1 = latlng1.lat * rad,
270
+ lat2 = latlng2.lat * rad,
271
+ sinDLat = Math.sin((latlng2.lat - latlng1.lat) * rad / 2),
272
+ sinDLon = Math.sin((latlng2.lng - latlng1.lng) * rad / 2),
273
+ a = sinDLat * sinDLat + Math.cos(lat1) * Math.cos(lat2) * sinDLon * sinDLon,
274
+ c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
275
+ return R * c;
276
+ }
@@ -68,6 +68,19 @@ export class LatLngBounds {
68
68
  }
69
69
  }
70
70
 
71
+ /**
72
+ * Returns the given `LatLngBounds` object as it is, or else a new `LatLngBounds`
73
+ * object created from the given arguments (the same as of the constructor) — unlike
74
+ * the constructor, which always creates a new object.
75
+ * @param {LatLngExpression|LatLngBoundsExpression} [corner1] The first corner, the
76
+ * points or the bounds.
77
+ * @param {LatLngExpression} [corner2] The opposite corner.
78
+ * @returns {LatLngBounds}
79
+ */
80
+ static from(corner1, corner2) {
81
+ return corner1 instanceof LatLngBounds ? corner1 : new LatLngBounds(corner1, corner2);
82
+ }
83
+
71
84
  /**
72
85
  * Extend the bounds to contain the given point or bounds.
73
86
  * @param {LatLngExpression|LatLngBoundsExpression} obj
@@ -93,9 +106,9 @@ export class LatLngBounds {
93
106
  return this;
94
107
  }
95
108
  if (LatLng.validate(obj)) {
96
- return this.extend(new LatLng(obj));
109
+ return this.extend(LatLng.from(obj));
97
110
  }
98
- return this.extend(new LatLngBounds(obj));
111
+ return this.extend(LatLngBounds.from(obj));
99
112
  }
100
113
 
101
114
  if (!sw && !ne) {
@@ -210,9 +223,9 @@ export class LatLngBounds {
210
223
  */
211
224
  contains(obj) { // (LatLngBounds) or (LatLng) -> Boolean
212
225
  if (LatLng.validate(obj)) {
213
- obj = new LatLng(obj);
226
+ obj = LatLng.from(obj);
214
227
  } else {
215
- obj = new LatLngBounds(obj);
228
+ obj = LatLngBounds.from(obj);
216
229
  }
217
230
 
218
231
  const sw = this._southWest,
@@ -237,7 +250,7 @@ export class LatLngBounds {
237
250
  * @returns {boolean}
238
251
  */
239
252
  intersects(bounds) {
240
- bounds = new LatLngBounds(bounds);
253
+ bounds = LatLngBounds.from(bounds);
241
254
 
242
255
  const sw = this._southWest,
243
256
  ne = this._northEast,
@@ -257,7 +270,7 @@ export class LatLngBounds {
257
270
  * @returns {boolean}
258
271
  */
259
272
  overlaps(bounds) {
260
- bounds = new LatLngBounds(bounds);
273
+ bounds = LatLngBounds.from(bounds);
261
274
 
262
275
  const sw = this._southWest,
263
276
  ne = this._northEast,
@@ -291,7 +304,7 @@ export class LatLngBounds {
291
304
  equals(bounds, maxMargin) {
292
305
  if (!bounds) { return false; }
293
306
 
294
- bounds = new LatLngBounds(bounds);
307
+ bounds = LatLngBounds.from(bounds);
295
308
 
296
309
  return this._southWest.equals(bounds.getSouthWest(), maxMargin) &&
297
310
  this._northEast.equals(bounds.getNorthEast(), maxMargin);
@@ -17,8 +17,9 @@ import * as Util from '../../core/Util.js';
17
17
  * [spatial reference system](https://en.wikipedia.org/wiki/Spatial_reference_system).
18
18
  *
19
19
  * Maplair defines the most usual CRSs by default. If you want to use a
20
- * CRS not defined by default, take a look at the
21
- * [Proj4Maplair](https://github.com/kartena/Proj4Leaflet) plugin.
20
+ * CRS not defined by default, extend `CRS` with a projection of your own, e.g.
21
+ * based on [Proj4js](https://github.com/proj4js/proj4js) (the Leaflet plugin
22
+ * [Proj4Leaflet](https://github.com/kartena/Proj4Leaflet) doesn't work with Maplair).
22
23
  *
23
24
  * The CRSs are classes with static properties and methods only; they are not
24
25
  * instantiated, the class itself is passed as the CRS, e.g. to the `crs` option of the
@@ -146,7 +147,7 @@ export class CRS {
146
147
  * @returns {LatLng}
147
148
  */
148
149
  static wrapLatLng(latlng) {
149
- latlng = new LatLng(latlng);
150
+ latlng = LatLng.from(latlng);
150
151
  const lng = this.wrapLng ? Util.wrapNum(latlng.lng, this.wrapLng, true) : latlng.lng,
151
152
  lat = this.wrapLat ? Util.wrapNum(latlng.lat, this.wrapLat, true) : latlng.lat,
152
153
  alt = latlng.alt;
@@ -12,7 +12,7 @@ export class EPSG3395 extends EarthCRS {
12
12
  /** @type {Projection} */
13
13
  static projection = Mercator;
14
14
  /** @type {Transformation} */
15
- static transformation = (() => {
15
+ static transformation = /* @__PURE__ */ (() => {
16
16
  const scale = 0.5 / (Math.PI * Mercator.R);
17
17
  return new Transformation(scale, 0.5, -scale, 0.5);
18
18
  })();
@@ -14,7 +14,7 @@ export class EPSG3857 extends EarthCRS {
14
14
  /** @type {Projection} */
15
15
  static projection = SphericalMercator;
16
16
  /** @type {Transformation} */
17
- static transformation = (() => {
17
+ static transformation = /* @__PURE__ */ (() => {
18
18
  const scale = 0.5 / (Math.PI * SphericalMercator.R);
19
19
  return new Transformation(scale, 0.5, -scale, 0.5);
20
20
  })();
@@ -19,5 +19,5 @@ export class EPSG4326 extends EarthCRS {
19
19
  /** @type {Projection} */
20
20
  static projection = LonLat;
21
21
  /** @type {Transformation} */
22
- static transformation = new Transformation(1 / 180, 1, -1 / 180, 0.5);
22
+ static transformation = /* @__PURE__ */ new Transformation(1 / 180, 1, -1 / 180, 0.5);
23
23
  }
@@ -1,4 +1,5 @@
1
1
  import {CRS} from './CRS.js';
2
+ import {haversineDistance} from '../LatLng.js';
2
3
  /** @import {LatLng} from '../LatLng.js' */
3
4
 
4
5
  /**
@@ -30,13 +31,6 @@ export class EarthCRS extends CRS {
30
31
  * @returns {number}
31
32
  */
32
33
  static distance(latlng1, latlng2) {
33
- const rad = Math.PI / 180,
34
- lat1 = latlng1.lat * rad,
35
- lat2 = latlng2.lat * rad,
36
- sinDLat = Math.sin((latlng2.lat - latlng1.lat) * rad / 2),
37
- sinDLon = Math.sin((latlng2.lng - latlng1.lng) * rad / 2),
38
- a = sinDLat * sinDLat + Math.cos(lat1) * Math.cos(lat2) * sinDLon * sinDLon,
39
- c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
40
- return this.R * c;
34
+ return haversineDistance(latlng1, latlng2, this.R);
41
35
  }
42
36
  }
@@ -14,7 +14,7 @@ export class SimpleCRS extends CRS {
14
14
  /** @type {Projection} */
15
15
  static projection = LonLat;
16
16
  /** @type {Transformation} */
17
- static transformation = new Transformation(1, 0, -1, 0);
17
+ static transformation = /* @__PURE__ */ new Transformation(1, 0, -1, 0);
18
18
 
19
19
  /**
20
20
  * Returns `2^zoom`.
@@ -13,14 +13,14 @@ import {Point} from '../../geometry/Point.js';
13
13
  */
14
14
  export const LonLat = {
15
15
  project(latlng) {
16
- latlng = new LatLng(latlng);
16
+ latlng = LatLng.from(latlng);
17
17
  return new Point(latlng.lng, latlng.lat);
18
18
  },
19
19
 
20
20
  unproject(point) {
21
- point = new Point(point);
21
+ point = Point.from(point);
22
22
  return new LatLng(point.y, point.x);
23
23
  },
24
24
 
25
- bounds: new Bounds([-180, -90], [180, 90])
25
+ bounds: /* @__PURE__ */ new Bounds([-180, -90], [180, 90])
26
26
  };
@@ -15,10 +15,10 @@ export const Mercator = {
15
15
  R: earthRadius,
16
16
  R_MINOR: 6356752.314245179,
17
17
 
18
- bounds: new Bounds([-20037508.34279, -15496570.73972], [20037508.34279, 18764656.23138]),
18
+ bounds: /* @__PURE__ */ new Bounds([-20037508.34279, -15496570.73972], [20037508.34279, 18764656.23138]),
19
19
 
20
20
  project(latlng) {
21
- latlng = new LatLng(latlng);
21
+ latlng = LatLng.from(latlng);
22
22
  const d = Math.PI / 180,
23
23
  r = this.R,
24
24
  tmp = this.R_MINOR / r,
@@ -33,7 +33,7 @@ export const Mercator = {
33
33
  },
34
34
 
35
35
  unproject(point) {
36
- point = new Point(point);
36
+ point = Point.from(point);
37
37
  const d = 180 / Math.PI,
38
38
  r = this.R,
39
39
  tmp = this.R_MINOR / r,
@@ -18,7 +18,7 @@ export const SphericalMercator = {
18
18
  MAX_LATITUDE: 85.0511287798,
19
19
 
20
20
  project(latlng) {
21
- latlng = new LatLng(latlng);
21
+ latlng = LatLng.from(latlng);
22
22
  const d = Math.PI / 180,
23
23
  max = this.MAX_LATITUDE,
24
24
  lat = Math.max(Math.min(max, latlng.lat), -max),
@@ -30,7 +30,7 @@ export const SphericalMercator = {
30
30
  },
31
31
 
32
32
  unproject(point) {
33
- point = new Point(point);
33
+ point = Point.from(point);
34
34
  const d = 180 / Math.PI;
35
35
 
36
36
  return new LatLng(
@@ -38,7 +38,7 @@ export const SphericalMercator = {
38
38
  point.x * d / this.R);
39
39
  },
40
40
 
41
- bounds: (() => {
41
+ bounds: /* @__PURE__ */ (() => {
42
42
  const d = earthRadius * Math.PI;
43
43
  return new Bounds([-d, -d], [d, d]);
44
44
  })()
@@ -62,6 +62,19 @@ export class Bounds {
62
62
  }
63
63
  }
64
64
 
65
+ /**
66
+ * Returns the given `Bounds` object as it is, or else a new `Bounds` object created
67
+ * from the given arguments (the same as of the constructor) — unlike the
68
+ * constructor, which always creates a new object.
69
+ * @param {PointExpression|BoundsExpression} [a] The first corner, the points or the
70
+ * bounds.
71
+ * @param {PointExpression} [b] The opposite corner.
72
+ * @returns {Bounds}
73
+ */
74
+ static from(a, b) {
75
+ return a instanceof Bounds ? a : new Bounds(a, b);
76
+ }
77
+
65
78
  /**
66
79
  * Extends the bounds to contain the given point or bounds.
67
80
  * @param {PointExpression|BoundsExpression} obj
@@ -72,9 +85,9 @@ export class Bounds {
72
85
  if (!obj) { return this; }
73
86
 
74
87
  if (obj instanceof Point || typeof obj[0] === 'number' || 'x' in obj) {
75
- min2 = max2 = new Point(obj);
88
+ min2 = max2 = Point.from(obj);
76
89
  } else {
77
- obj = new Bounds(obj);
90
+ obj = Bounds.from(obj);
78
91
  min2 = obj.min;
79
92
  max2 = obj.max;
80
93
 
@@ -153,9 +166,9 @@ export class Bounds {
153
166
  let min, max;
154
167
 
155
168
  if (typeof obj[0] === 'number' || obj instanceof Point) {
156
- obj = new Point(obj);
169
+ obj = Point.from(obj);
157
170
  } else {
158
- obj = new Bounds(obj);
171
+ obj = Bounds.from(obj);
159
172
  }
160
173
 
161
174
  if (obj instanceof Bounds) {
@@ -178,7 +191,7 @@ export class Bounds {
178
191
  * @returns {boolean}
179
192
  */
180
193
  intersects(bounds) { // (Bounds) -> Boolean
181
- bounds = new Bounds(bounds);
194
+ bounds = Bounds.from(bounds);
182
195
 
183
196
  const min = this.min,
184
197
  max = this.max,
@@ -197,7 +210,7 @@ export class Bounds {
197
210
  * @returns {boolean}
198
211
  */
199
212
  overlaps(bounds) { // (Bounds) -> Boolean
200
- bounds = new Bounds(bounds);
213
+ bounds = Bounds.from(bounds);
201
214
 
202
215
  const min = this.min,
203
216
  max = this.max,
@@ -246,7 +259,7 @@ export class Bounds {
246
259
  equals(bounds) {
247
260
  if (!bounds) { return false; }
248
261
 
249
- bounds = new Bounds(bounds);
262
+ bounds = Bounds.from(bounds);
250
263
 
251
264
  return this.min.equals(bounds.getTopLeft()) &&
252
265
  this.max.equals(bounds.getBottomRight());
@@ -1,7 +1,5 @@
1
1
  import {Point} from './Point.js';
2
2
  import {LatLng} from '../geo/LatLng.js';
3
- import {centroid} from './PolyUtil.js';
4
- import {LatLngBounds} from '../geo/LatLngBounds.js';
5
3
  /** @import {LatLngExpression} from '../geo/LatLng.js' */
6
4
  /** @import {Bounds} from './Bounds.js' */
7
5
  /** @import {CRS} from '../geo/crs/CRS.js' */
@@ -139,6 +137,7 @@ let _lastCode;
139
137
  * @returns {[Point, Point]|false}
140
138
  */
141
139
  export function clipSegment(a, b, bounds, useLastCode, round) {
140
+ const a0 = a, b0 = b;
142
141
  let codeA = useLastCode ? _lastCode : _getBitCode(a, bounds),
143
142
  codeB = _getBitCode(b, bounds),
144
143
 
@@ -150,6 +149,12 @@ export function clipSegment(a, b, bounds, useLastCode, round) {
150
149
  while (true) {
151
150
  // if a,b is inside the clip window (trivial accept)
152
151
  if (!(codeA | codeB)) {
152
+ // round only the endpoints that were replaced by a clipped intersection;
153
+ // rounding while iterating can push a point back outside and loop forever
154
+ if (round) {
155
+ if (a !== a0) { a = a.round(); }
156
+ if (b !== b0) { b = b.round(); }
157
+ }
153
158
  return [a, b];
154
159
  }
155
160
 
@@ -160,7 +165,7 @@ export function clipSegment(a, b, bounds, useLastCode, round) {
160
165
 
161
166
  // other cases
162
167
  codeOut = codeA || codeB;
163
- p = _getEdgeIntersection(a, b, codeOut, bounds, round);
168
+ p = _getEdgeIntersection(a, b, codeOut, bounds);
164
169
  newCode = _getBitCode(p, bounds);
165
170
 
166
171
  if (codeOut === codeA) {
@@ -270,8 +275,45 @@ export function isFlat(latlngs) {
270
275
  * @returns {LatLng}
271
276
  */
272
277
  export function polylineCenter(latlngs, crs) {
273
- let i, halfDist, segDist, dist, p1, p2, ratio, center;
278
+ const points = _projectRing(latlngs, crs),
279
+ len = points.length;
280
+
281
+ let halfDist = 0;
282
+ for (let i = 1; i < len; i++) {
283
+ halfDist += points[i - 1].distanceTo(points[i]);
284
+ }
285
+ halfDist /= 2;
286
+
287
+ // if the line has zero length, the loop below never finds a segment and the first point is the center
288
+ let center = points[0];
289
+
290
+ for (let i = 1, dist = 0; i < len; i++) {
291
+ const p1 = points[i - 1],
292
+ p2 = points[i],
293
+ segDist = p1.distanceTo(p2);
294
+ dist += segDist;
295
+
296
+ if (dist > halfDist) {
297
+ const ratio = (dist - halfDist) / segDist;
298
+ center = new Point(
299
+ p2.x - ratio * (p2.x - p1.x),
300
+ p2.y - ratio * (p2.y - p1.y)
301
+ );
302
+ break;
303
+ }
304
+ }
305
+
306
+ return crs.unproject(center);
307
+ }
274
308
 
309
+ /**
310
+ * Projects the first ring of the given latlngs.
311
+ * @internal
312
+ * @param {LatLngExpression[]|LatLngExpression[][]} latlngs
313
+ * @param {typeof CRS} crs
314
+ * @returns {Point[]}
315
+ */
316
+ export function _projectRing(latlngs, crs) {
275
317
  if (!latlngs || latlngs.length === 0) {
276
318
  throw new Error('latlngs not passed');
277
319
  }
@@ -281,48 +323,5 @@ export function polylineCenter(latlngs, crs) {
281
323
  latlngs = latlngs[0];
282
324
  }
283
325
 
284
- let centroidLatLng = new LatLng([0, 0]);
285
-
286
- const bounds = new LatLngBounds(latlngs);
287
- const areaBounds = bounds.getNorthWest().distanceTo(bounds.getSouthWest()) * bounds.getNorthEast().distanceTo(bounds.getNorthWest());
288
- // tests showed that below 1700 rounding errors are happening
289
- if (areaBounds < 1700) {
290
- // getting a inexact center, to move the latlngs near to [0, 0] to prevent rounding errors
291
- centroidLatLng = centroid(latlngs);
292
- }
293
-
294
- const len = latlngs.length;
295
- const points = [];
296
- for (i = 0; i < len; i++) {
297
- const latlng = new LatLng(latlngs[i]);
298
- points.push(crs.project(new LatLng([latlng.lat - centroidLatLng.lat, latlng.lng - centroidLatLng.lng])));
299
- }
300
-
301
- for (i = 0, halfDist = 0; i < len - 1; i++) {
302
- halfDist += points[i].distanceTo(points[i + 1]) / 2;
303
- }
304
-
305
- // The line is so small in the current view that all points are on the same pixel.
306
- if (halfDist === 0) {
307
- center = points[0];
308
- } else {
309
- for (i = 0, dist = 0; i < len - 1; i++) {
310
- p1 = points[i];
311
- p2 = points[i + 1];
312
- segDist = p1.distanceTo(p2);
313
- dist += segDist;
314
-
315
- if (dist > halfDist) {
316
- ratio = (dist - halfDist) / segDist;
317
- center = [
318
- p2.x - ratio * (p2.x - p1.x),
319
- p2.y - ratio * (p2.y - p1.y)
320
- ];
321
- break;
322
- }
323
- }
324
- }
325
-
326
- const latlngCenter = crs.unproject(new Point(center));
327
- return new LatLng([latlngCenter.lat + centroidLatLng.lat, latlngCenter.lng + centroidLatLng.lng]);
326
+ return latlngs.map(latlng => crs.project(LatLng.from(latlng)));
328
327
  }
@@ -71,6 +71,25 @@ export class Point {
71
71
  this.y = (round ? Math.round(_y) : _y);
72
72
  }
73
73
 
74
+ /**
75
+ * Returns the given `Point` object as it is (unless `round` is set), or else a new
76
+ * `Point` object created from the given arguments (the same as of the constructor) —
77
+ * unlike the constructor, which always creates a new object.
78
+ *
79
+ * @example
80
+ * ```js
81
+ * Point.from(point) === point; // true
82
+ * Point.from([200, 300]); // Point(200, 300)
83
+ * ```
84
+ * @param {number|PointExpression} x The `x` coordinate, or the coordinates.
85
+ * @param {number} [y] The `y` coordinate.
86
+ * @param {boolean} [round]
87
+ * @returns {Point}
88
+ */
89
+ static from(x, y, round) {
90
+ return x instanceof Point && !round ? x : new Point(x, y, round);
91
+ }
92
+
74
93
  /**
75
94
  * Returns `true` if the Point object can be properly initialized with the given
76
95
  * arguments (the same as of the constructor).
@@ -96,7 +115,7 @@ export class Point {
96
115
  */
97
116
  add(point) {
98
117
  // non-destructive, returns a new point
99
- return new Point(this)._add(new Point(point));
118
+ return new Point(this)._add(Point.from(point));
100
119
  }
101
120
 
102
121
  _add(point) {
@@ -112,7 +131,7 @@ export class Point {
112
131
  * @returns {Point}
113
132
  */
114
133
  subtract(point) {
115
- return new Point(this)._subtract(new Point(point));
134
+ return new Point(this)._subtract(Point.from(point));
116
135
  }
117
136
 
118
137
  _subtract(point) {
@@ -236,7 +255,7 @@ export class Point {
236
255
  * @returns {number}
237
256
  */
238
257
  distanceTo(point) {
239
- point = new Point(point);
258
+ point = Point.from(point);
240
259
 
241
260
  const x = point.x - this.x,
242
261
  y = point.y - this.y;
@@ -250,7 +269,7 @@ export class Point {
250
269
  * @returns {boolean}
251
270
  */
252
271
  equals(point) {
253
- point = new Point(point);
272
+ point = Point.from(point);
254
273
 
255
274
  return point.x === this.x &&
256
275
  point.y === this.y;
@@ -263,7 +282,7 @@ export class Point {
263
282
  * @returns {boolean}
264
283
  */
265
284
  contains(point) {
266
- point = new Point(point);
285
+ point = Point.from(point);
267
286
 
268
287
  return Math.abs(point.x) <= Math.abs(this.x) &&
269
288
  Math.abs(point.y) <= Math.abs(this.y);
@@ -1,7 +1,6 @@
1
1
  import * as LineUtil from './LineUtil.js';
2
2
  import {LatLng} from '../geo/LatLng.js';
3
3
  import {Point} from './Point.js';
4
- import {LatLngBounds} from '../geo/LatLngBounds.js';
5
4
  /** @import {LatLngExpression} from '../geo/LatLng.js' */
6
5
  /** @import {Bounds} from './Bounds.js' */
7
6
  /** @import {CRS} from '../geo/crs/CRS.js' */
@@ -70,56 +69,28 @@ export function clipPolygon(points, bounds, round) {
70
69
  * @returns {LatLng}
71
70
  */
72
71
  export function polygonCenter(latlngs, crs) {
73
- let i, j, p1, p2, f, area, x, y, center;
72
+ // points relative to the first one, to avoid floating point errors on tiny polygons with large projected coordinates
73
+ const points = LineUtil._projectRing(latlngs, crs),
74
+ origin = new Point(points[0]),
75
+ len = points.length;
74
76
 
75
- if (!latlngs || latlngs.length === 0) {
76
- throw new Error('latlngs not passed');
77
- }
78
-
79
- if (!LineUtil.isFlat(latlngs)) {
80
- console.warn('latlngs are not flat! Only the first ring will be used');
81
- latlngs = latlngs[0];
82
- }
83
-
84
- let centroidLatLng = new LatLng([0, 0]);
85
-
86
- const bounds = new LatLngBounds(latlngs);
87
- const areaBounds = bounds.getNorthWest().distanceTo(bounds.getSouthWest()) * bounds.getNorthEast().distanceTo(bounds.getNorthWest());
88
- // tests showed that below 1700 rounding errors are happening
89
- if (areaBounds < 1700) {
90
- // getting a inexact center, to move the latlngs near to [0, 0] to prevent rounding errors
91
- centroidLatLng = centroid(latlngs);
92
- }
77
+ for (const p of points) { p._subtract(origin); }
93
78
 
94
- const len = latlngs.length;
95
- const points = [];
96
- for (i = 0; i < len; i++) {
97
- const latlng = new LatLng(latlngs[i]);
98
- points.push(crs.project(new LatLng([latlng.lat - centroidLatLng.lat, latlng.lng - centroidLatLng.lng])));
99
- }
100
-
101
- area = x = y = 0;
102
-
103
- // polygon centroid algorithm;
104
- for (i = 0, j = len - 1; i < len; j = i++) {
105
- p1 = points[i];
106
- p2 = points[j];
79
+ let area = 0, x = 0, y = 0;
107
80
 
81
+ // polygon centroid algorithm
82
+ for (let i = 0, j = len - 1; i < len; j = i++) {
83
+ const p1 = points[i],
84
+ p2 = points[j],
108
85
  f = p1.y * p2.x - p2.y * p1.x;
86
+
109
87
  x += (p1.x + p2.x) * f;
110
88
  y += (p1.y + p2.y) * f;
111
- area += f * 3;
112
- }
113
-
114
- if (area === 0) {
115
- // Polygon is so small that all points are on same pixel.
116
- center = points[0];
117
- } else {
118
- center = [x / area, y / area];
89
+ area += f;
119
90
  }
120
91
 
121
- const latlngCenter = crs.unproject(new Point(center));
122
- return new LatLng([latlngCenter.lat + centroidLatLng.lat, latlngCenter.lng + centroidLatLng.lng]);
92
+ // area is 0 when the polygon is so small that all points are on the same pixel
93
+ return crs.unproject(area === 0 ? origin : new Point(x / (3 * area), y / (3 * area))._add(origin));
123
94
  }
124
95
 
125
96
  /**
@@ -132,7 +103,7 @@ export function centroid(coords) {
132
103
  let lngSum = 0;
133
104
  let len = 0;
134
105
  for (const coord of coords) {
135
- const latlng = new LatLng(coord);
106
+ const latlng = LatLng.from(coord);
136
107
  latSum += latlng.lat;
137
108
  lngSum += latlng.lng;
138
109
  len++;