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,93 @@
1
+ import {LayerGroup} from './LayerGroup.js';
2
+ import {LatLngBounds} from '../geo/LatLngBounds.js';
3
+
4
+ /*
5
+ * @class FeatureGroup
6
+ * @inherits LayerGroup
7
+ *
8
+ * Extended `LayerGroup` that makes it easier to do the same thing to all its member layers:
9
+ * * [`bindPopup`](#layer-bindpopup) binds a popup to all of the layers at once (likewise with [`bindTooltip`](#layer-bindtooltip))
10
+ * * Events are propagated to the `FeatureGroup`, so if the group has an event
11
+ * handler, it will handle events from any of the layers. This includes pointer events
12
+ * and custom events.
13
+ * * Has `layeradd` and `layerremove` events
14
+ *
15
+ * @example
16
+ *
17
+ * ```js
18
+ * new FeatureGroup([marker1, marker2, polyline])
19
+ * .bindPopup('Hello world!')
20
+ * .on('click', function() { alert('Clicked on a member of the group!'); })
21
+ * .addTo(map);
22
+ * ```
23
+ */
24
+
25
+ // @constructor FeatureGroup(layers?: Layer[], options?: Object)
26
+ // Create a feature group, optionally given an initial set of layers and an `options` object.
27
+ export class FeatureGroup extends LayerGroup {
28
+ constructor(...args) {
29
+ super(...args);
30
+ this.callInitHooks(FeatureGroup);
31
+ }
32
+
33
+
34
+ addLayer(layer) {
35
+ if (this.hasLayer(layer)) {
36
+ return this;
37
+ }
38
+
39
+ layer.addEventParent(this);
40
+
41
+ super.addLayer(layer);
42
+
43
+ // @event layeradd: LayerEvent
44
+ // Fired when a layer is added to this `FeatureGroup`
45
+ return this.fire('layeradd', {layer});
46
+ }
47
+
48
+ removeLayer(layer) {
49
+ if (!this.hasLayer(layer)) {
50
+ return this;
51
+ }
52
+ if (layer in this._layers) {
53
+ layer = this._layers[layer];
54
+ }
55
+
56
+ layer.removeEventParent(this);
57
+
58
+ super.removeLayer(layer);
59
+
60
+ // @event layerremove: LayerEvent
61
+ // Fired when a layer is removed from this `FeatureGroup`
62
+ return this.fire('layerremove', {layer});
63
+ }
64
+
65
+ // @method setStyle(style: Path options): this
66
+ // Sets the given path options to each layer of the group that has a `setStyle` method.
67
+ setStyle(style) {
68
+ return this.eachLayer(l => l.setStyle?.(style));
69
+ }
70
+
71
+ // @method bringToFront(): this
72
+ // Brings the layer group to the top of all other layers
73
+ bringToFront() {
74
+ return this.eachLayer(l => l.bringToFront?.());
75
+ }
76
+
77
+ // @method bringToBack(): this
78
+ // Brings the layer group to the back of all other layers
79
+ bringToBack() {
80
+ return this.eachLayer(l => l.bringToBack?.());
81
+ }
82
+
83
+ // @method getBounds(): LatLngBounds
84
+ // Returns the LatLngBounds of the Feature Group (created from bounds and coordinates of its children).
85
+ getBounds() {
86
+ const bounds = new LatLngBounds();
87
+
88
+ for (const layer of Object.values(this._layers)) {
89
+ bounds.extend(layer.getBounds ? layer.getBounds() : layer.getLatLng());
90
+ }
91
+ return bounds;
92
+ }
93
+ }
@@ -0,0 +1,432 @@
1
+ import {LayerGroup} from './LayerGroup.js';
2
+ import {FeatureGroup} from './FeatureGroup.js';
3
+ import * as Util from '../core/Util.js';
4
+ import {Marker} from './marker/Marker.js';
5
+ import {Circle} from './vector/Circle.js';
6
+ import {CircleMarker} from './vector/CircleMarker.js';
7
+ import {Polyline} from './vector/Polyline.js';
8
+ import {Polygon} from './vector/Polygon.js';
9
+ import {LatLng} from '../geo/LatLng.js';
10
+ import * as LineUtil from '../geometry/LineUtil.js';
11
+
12
+
13
+ /*
14
+ * @class GeoJSON
15
+ * @inherits FeatureGroup
16
+ *
17
+ * Represents a GeoJSON object or an array of GeoJSON objects. Allows you to parse
18
+ * GeoJSON data and display it on the map. Extends `FeatureGroup`.
19
+ *
20
+ * @example
21
+ *
22
+ * ```js
23
+ * new GeoJSON(data, {
24
+ * style: function (feature) {
25
+ * return {color: feature.properties.color};
26
+ * }
27
+ * }).bindPopup(function (layer) {
28
+ * return layer.feature.properties.description;
29
+ * }).addTo(map);
30
+ * ```
31
+ */
32
+
33
+ // @namespace GeoJSON
34
+ // @constructor GeoJSON(geojson?: Object, options?: GeoJSON options)
35
+ // Creates a GeoJSON layer. Optionally accepts an object in
36
+ // [GeoJSON format](https://tools.ietf.org/html/rfc7946) to display on the map
37
+ // (you can alternatively add it later with `addData` method) and an `options` object.
38
+ export class GeoJSON extends FeatureGroup {
39
+
40
+ /* @section
41
+ * @aka GeoJSON options
42
+ *
43
+ * @option pointToLayer: Function = *
44
+ * A `Function` defining how GeoJSON points spawn Maplair layers. It is internally
45
+ * called when data is added, passing the GeoJSON point feature and its `LatLng`.
46
+ * The default is to spawn a default `Marker`:
47
+ * ```js
48
+ * function(geoJsonPoint, latlng) {
49
+ * return new Marker(latlng);
50
+ * }
51
+ * ```
52
+ *
53
+ * @option style: Function = *
54
+ * A `Function` defining the `Path options` for styling GeoJSON lines and polygons,
55
+ * called internally when data is added.
56
+ * The default value is to not override any defaults:
57
+ * ```js
58
+ * function (geoJsonFeature) {
59
+ * return {}
60
+ * }
61
+ * ```
62
+ *
63
+ * @option onEachFeature: Function = *
64
+ * A `Function` that will be called once for each created `Feature`, after it has
65
+ * been created and styled. Useful for attaching events and popups to features.
66
+ * It will receive the `feature` as the first argument and its corresponding
67
+ * spawned `layer` from [`asFeature`](#geojson-asFeature) as the second.
68
+ * The default is to do nothing with the newly created layers:
69
+ * ```js
70
+ * function (feature, layer) {}
71
+ * ```
72
+ *
73
+ * @option filter: Function = *
74
+ * A `Function` that will be used to decide whether to include a feature or not.
75
+ * The default is to include all features:
76
+ * ```js
77
+ * function (geoJsonFeature) {
78
+ * return true;
79
+ * }
80
+ * ```
81
+ * Note: dynamically changing the `filter` option will have effect only on newly
82
+ * added data. It will _not_ re-evaluate already included features.
83
+ *
84
+ * @option coordsToLatLng: Function = *
85
+ * A `Function` that will be used for converting GeoJSON coordinates to `LatLng`s.
86
+ * The default is the `coordsToLatLng` static method.
87
+ *
88
+ * @option markersInheritOptions: Boolean = false
89
+ * Whether default Markers for "Point" type Features inherit from group options.
90
+ */
91
+
92
+ constructor(geojson, options) {
93
+ super(undefined, options);
94
+
95
+ if (geojson) {
96
+ this.addData(geojson);
97
+ }
98
+
99
+ this.callInitHooks(GeoJSON);
100
+ }
101
+
102
+ // @method addData( <GeoJSON> data ): this
103
+ // Adds a GeoJSON object to the layer.
104
+ addData(geojson) {
105
+ const features = Array.isArray(geojson) ? geojson : geojson.features;
106
+
107
+ if (features) {
108
+ for (const feature of features) {
109
+ // only add this if geometry or geometries are set and not null
110
+ if (feature.geometries || feature.geometry || feature.features || feature.coordinates) {
111
+ this.addData(feature);
112
+ }
113
+ }
114
+ return this;
115
+ }
116
+
117
+ const options = this.options;
118
+
119
+ if (options.filter && !options.filter(geojson)) { return this; }
120
+
121
+ const layer = GeoJSON.geometryToLayer(geojson, options);
122
+ if (!layer) {
123
+ return this;
124
+ }
125
+ layer.feature = GeoJSON.asFeature(geojson);
126
+
127
+ layer.defaultOptions = layer.options;
128
+ this.resetStyle(layer);
129
+
130
+ if (options.onEachFeature) {
131
+ options.onEachFeature(geojson, layer);
132
+ }
133
+
134
+ return this.addLayer(layer);
135
+ }
136
+
137
+ // @method resetStyle( <Path> layer? ): this
138
+ // Resets the given vector layer's style to the original GeoJSON style, useful for resetting style after hover events.
139
+ // If `layer` is omitted, the style of all features in the current layer is reset.
140
+ resetStyle(layer) {
141
+ if (layer === undefined) {
142
+ return this.eachLayer(this.resetStyle, this);
143
+ }
144
+ // reset any custom styles
145
+ layer.options = Object.create(layer.defaultOptions);
146
+ this._setLayerStyle(layer, this.options.style);
147
+ return this;
148
+ }
149
+
150
+ // @method setStyle( <Function> style ): this
151
+ // Changes styles of GeoJSON vector layers with the given style function.
152
+ setStyle(style) {
153
+ return this.eachLayer(layer => this._setLayerStyle(layer, style));
154
+ }
155
+
156
+ _setLayerStyle(layer, style) {
157
+ if (layer.setStyle) {
158
+ if (typeof style === 'function') {
159
+ style = style(layer.feature);
160
+ }
161
+ layer.setStyle(style);
162
+ }
163
+ }
164
+
165
+ // @section
166
+ // There are several static functions which can be called without instantiating GeoJSON:
167
+
168
+ // @function geometryToLayer(featureData: Object, options?: GeoJSON options): Layer
169
+ // Creates a `Layer` from a given GeoJSON feature. Can use a custom
170
+ // [`pointToLayer`](#geojson-pointtolayer) and/or [`coordsToLatLng`](#geojson-coordstolatlng)
171
+ // functions if provided as options.
172
+ static geometryToLayer(geojson, options) {
173
+
174
+ const geometry = geojson.type === 'Feature' ? geojson.geometry : geojson,
175
+ coords = geometry?.coordinates,
176
+ layers = [],
177
+ pointToLayer = options?.pointToLayer,
178
+ _coordsToLatLng = options?.coordsToLatLng ?? GeoJSON.coordsToLatLng;
179
+ let latlng, latlngs;
180
+
181
+ if (!coords && !geometry) {
182
+ return null;
183
+ }
184
+
185
+ switch (geometry.type) {
186
+ case 'Point':
187
+ latlng = _coordsToLatLng(coords);
188
+ return GeoJSON._pointToLayer(pointToLayer, geojson, latlng, options);
189
+
190
+ case 'MultiPoint':
191
+ for (const coord of coords) {
192
+ latlng = _coordsToLatLng(coord);
193
+ layers.push(GeoJSON._pointToLayer(pointToLayer, geojson, latlng, options));
194
+ }
195
+ return new FeatureGroup(layers);
196
+
197
+ case 'LineString':
198
+ case 'MultiLineString':
199
+ latlngs = GeoJSON.coordsToLatLngs(coords, geometry.type === 'LineString' ? 0 : 1, _coordsToLatLng);
200
+ return new Polyline(latlngs, options);
201
+
202
+ case 'Polygon':
203
+ case 'MultiPolygon':
204
+ latlngs = GeoJSON.coordsToLatLngs(coords, geometry.type === 'Polygon' ? 1 : 2, _coordsToLatLng);
205
+ return new Polygon(latlngs, options);
206
+
207
+ case 'GeometryCollection':
208
+ for (const g of geometry.geometries) {
209
+ const geoLayer = GeoJSON.geometryToLayer({
210
+ geometry: g,
211
+ type: 'Feature',
212
+ properties: geojson.properties
213
+ }, options);
214
+
215
+ if (geoLayer) {
216
+ layers.push(geoLayer);
217
+ }
218
+ }
219
+ return new FeatureGroup(layers);
220
+
221
+ case 'FeatureCollection':
222
+ for (const f of geometry.features) {
223
+ const featureLayer = GeoJSON.geometryToLayer(f, options);
224
+
225
+ if (featureLayer) {
226
+ layers.push(featureLayer);
227
+ }
228
+ }
229
+ return new FeatureGroup(layers);
230
+
231
+ default:
232
+ throw new Error('Invalid GeoJSON object.');
233
+ }
234
+ }
235
+
236
+ static _pointToLayer(pointToLayerFn, geojson, latlng, options) {
237
+ return pointToLayerFn ?
238
+ pointToLayerFn(geojson, latlng) :
239
+ new Marker(latlng, options?.markersInheritOptions && options);
240
+ }
241
+
242
+ // @function coordsToLatLng(coords: Array): LatLng
243
+ // Creates a `LatLng` object from an array of 2 numbers (longitude, latitude)
244
+ // or 3 numbers (longitude, latitude, altitude) used in GeoJSON for points.
245
+ static coordsToLatLng(coords) {
246
+ return new LatLng(coords[1], coords[0], coords[2]);
247
+ }
248
+
249
+ // @function coordsToLatLngs(coords: Array, levelsDeep?: Number, coordsToLatLng?: Function): Array
250
+ // Creates a multidimensional array of `LatLng`s from a GeoJSON coordinates array.
251
+ // `levelsDeep` specifies the nesting level (0 is for an array of points, 1 for an array of arrays of points, etc., 0 by default).
252
+ // Can use a custom [`coordsToLatLng`](#geojson-coordstolatlng) function.
253
+ static coordsToLatLngs(coords, levelsDeep, _coordsToLatLng) {
254
+ return coords.map(coord => (levelsDeep ?
255
+ GeoJSON.coordsToLatLngs(coord, levelsDeep - 1, _coordsToLatLng) :
256
+ (_coordsToLatLng || GeoJSON.coordsToLatLng)(coord)));
257
+ }
258
+
259
+ // @function latLngToCoords(latlng: LatLng, precision?: Number|false): Array
260
+ // Reverse of [`coordsToLatLng`](#geojson-coordstolatlng)
261
+ // Coordinates values are rounded with [`formatNum`](#util-formatnum) function.
262
+ static latLngToCoords(latlng, precision) {
263
+ latlng = new LatLng(latlng);
264
+ return latlng.alt !== undefined ?
265
+ [Util.formatNum(latlng.lng, precision), Util.formatNum(latlng.lat, precision), Util.formatNum(latlng.alt, precision)] :
266
+ [Util.formatNum(latlng.lng, precision), Util.formatNum(latlng.lat, precision)];
267
+ }
268
+
269
+ // @function latLngsToCoords(latlngs: Array, levelsDeep?: Number, close?: Boolean, precision?: Number|false): Array
270
+ // Reverse of [`coordsToLatLngs`](#geojson-coordstolatlngs)
271
+ // `close` determines whether the first point should be appended to the end of the array to close the feature, only used when `levelsDeep` is 0. False by default.
272
+ // Coordinates values are rounded with [`formatNum`](#util-formatnum) function.
273
+ static latLngsToCoords(latlngs, levelsDeep, close, precision) {
274
+ // Check for flat arrays required to ensure unbalanced arrays are correctly converted in recursion
275
+ const coords = latlngs.map(latlng => (levelsDeep ?
276
+ GeoJSON.latLngsToCoords(latlng, LineUtil.isFlat(latlng) ? 0 : levelsDeep - 1, close, precision) :
277
+ GeoJSON.latLngToCoords(latlng, precision)));
278
+
279
+ if (!levelsDeep && close && coords.length > 0) {
280
+ coords.push(coords[0].slice());
281
+ }
282
+
283
+ return coords;
284
+ }
285
+
286
+ // @function getFeature(layer: Layer, newGeometry: Object): Object
287
+ // Returns GeoJSON geometries/features of layer with new GeoJSON geometry.
288
+ static getFeature(layer, newGeometry) {
289
+ return layer.feature ?
290
+ {...layer.feature, geometry: newGeometry} :
291
+ GeoJSON.asFeature(newGeometry);
292
+ }
293
+
294
+ // @function asFeature(geojson: Object): Object
295
+ // Normalize GeoJSON geometries/features into GeoJSON features.
296
+ static asFeature(geojson) {
297
+ if (geojson.type === 'Feature' || geojson.type === 'FeatureCollection') {
298
+ return geojson;
299
+ }
300
+
301
+ return {
302
+ type: 'Feature',
303
+ properties: {},
304
+ geometry: geojson
305
+ };
306
+ }
307
+
308
+ }
309
+
310
+ const PointToGeoJSON = {
311
+ toGeoJSON(precision) {
312
+ return GeoJSON.getFeature(this, {
313
+ type: 'Point',
314
+ coordinates: GeoJSON.latLngToCoords(this.getLatLng(), precision)
315
+ });
316
+ }
317
+ };
318
+
319
+ // @namespace Marker
320
+ // @section Other methods
321
+ // @method toGeoJSON(precision?: Number|false): Object
322
+ // Coordinates values are rounded with [`formatNum`](#util-formatnum) function with given `precision`.
323
+ // Returns a [`GeoJSON`](https://en.wikipedia.org/wiki/GeoJSON) representation of the marker (as a GeoJSON `Point` Feature).
324
+ Marker.include(PointToGeoJSON);
325
+
326
+ // @namespace CircleMarker
327
+ // @method toGeoJSON(precision?: Number|false): Object
328
+ // Coordinates values are rounded with [`formatNum`](#util-formatnum) function with given `precision`.
329
+ // Returns a [`GeoJSON`](https://en.wikipedia.org/wiki/GeoJSON) representation of the circle marker (as a GeoJSON `Point` Feature).
330
+ Circle.include(PointToGeoJSON);
331
+ CircleMarker.include(PointToGeoJSON);
332
+
333
+
334
+ // @namespace Polyline
335
+ // @method toGeoJSON(precision?: Number|false): Object
336
+ // Coordinates values are rounded with [`formatNum`](#util-formatnum) function with given `precision`.
337
+ // Returns a [`GeoJSON`](https://en.wikipedia.org/wiki/GeoJSON) representation of the polyline (as a GeoJSON `LineString` or `MultiLineString` Feature).
338
+ Polyline.include({
339
+ toGeoJSON(precision) {
340
+ const multi = !LineUtil.isFlat(this._latlngs);
341
+
342
+ const coords = GeoJSON.latLngsToCoords(this._latlngs, multi ? 1 : 0, false, precision);
343
+
344
+ return GeoJSON.getFeature(this, {
345
+ type: `${multi ? 'Multi' : ''}LineString`,
346
+ coordinates: coords
347
+ });
348
+ }
349
+ });
350
+
351
+ // @namespace Polygon
352
+ // @method toGeoJSON(precision?: Number|false): Object
353
+ // Coordinates values are rounded with [`formatNum`](#util-formatnum) function with given `precision`.
354
+ // Returns a [`GeoJSON`](https://en.wikipedia.org/wiki/GeoJSON) representation of the polygon (as a GeoJSON `Polygon` or `MultiPolygon` Feature).
355
+ Polygon.include({
356
+ toGeoJSON(precision) {
357
+ const holes = !LineUtil.isFlat(this._latlngs),
358
+ multi = holes && !LineUtil.isFlat(this._latlngs[0]);
359
+
360
+ let coords = GeoJSON.latLngsToCoords(this._latlngs, multi ? 2 : holes ? 1 : 0, true, precision);
361
+
362
+ if (!holes) {
363
+ coords = [coords];
364
+ }
365
+
366
+ return GeoJSON.getFeature(this, {
367
+ type: `${multi ? 'Multi' : ''}Polygon`,
368
+ coordinates: coords
369
+ });
370
+ }
371
+ });
372
+
373
+
374
+ // @namespace LayerGroup
375
+ LayerGroup.include({
376
+ toMultiPoint(precision) {
377
+ const coords = [];
378
+
379
+ this.eachLayer((layer) => {
380
+ coords.push(layer.toGeoJSON(precision).geometry.coordinates);
381
+ });
382
+
383
+ return GeoJSON.getFeature(this, {
384
+ type: 'MultiPoint',
385
+ coordinates: coords
386
+ });
387
+ },
388
+
389
+ // @method toGeoJSON(precision?: Number|false): Object
390
+ // Coordinates values are rounded with [`formatNum`](#util-formatnum) function with given `precision`.
391
+ // Returns a [`GeoJSON`](https://en.wikipedia.org/wiki/GeoJSON) representation of the layer group (as a GeoJSON `FeatureCollection`, `GeometryCollection`, or `MultiPoint`).
392
+ toGeoJSON(precision) {
393
+
394
+ const type = this.feature?.geometry?.type;
395
+
396
+ if (type === 'MultiPoint') {
397
+ return this.toMultiPoint(precision);
398
+ }
399
+
400
+ const isGeometryCollection = type === 'GeometryCollection',
401
+ jsons = [];
402
+
403
+ this.eachLayer((layer) => {
404
+ if (layer.toGeoJSON) {
405
+ const json = layer.toGeoJSON(precision);
406
+ if (isGeometryCollection) {
407
+ jsons.push(json.geometry);
408
+ } else {
409
+ const feature = GeoJSON.asFeature(json);
410
+ // Squash nested feature collections
411
+ if (feature.type === 'FeatureCollection') {
412
+ jsons.push(...feature.features);
413
+ } else {
414
+ jsons.push(feature);
415
+ }
416
+ }
417
+ }
418
+ });
419
+
420
+ if (isGeometryCollection) {
421
+ return GeoJSON.getFeature(this, {
422
+ geometries: jsons,
423
+ type: 'GeometryCollection'
424
+ });
425
+ }
426
+
427
+ return {
428
+ type: 'FeatureCollection',
429
+ features: jsons
430
+ };
431
+ }
432
+ });