UNPKG

13.3 kBMarkdownView Raw
1# Leaflet.Geodesic
2[![Build Status](https://app.travis-ci.com/henrythasler/Leaflet.Geodesic.svg?branch=master)](https://app.travis-ci.com/github/henrythasler/Leaflet.Geodesic) [![npm](https://img.shields.io/npm/v/leaflet.geodesic)](https://www.npmjs.com/package/leaflet.geodesic) [![Coverage Status](https://coveralls.io/repos/github/henrythasler/Leaflet.Geodesic/badge.svg?branch=master)](https://coveralls.io/github/henrythasler/Leaflet.Geodesic?branch=master) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=henrythasler_Leaflet.Geodesic&metric=alert_status)](https://sonarcloud.io/dashboard?id=henrythasler_Leaflet.Geodesic)
3
4Add-on for [Leaflet](http://leafletjs.com/) to draw [geodesic](http://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid) lines and circles. A geodesic line is the shortest path between two given positions on the earth surface. It's based on [Vincenty's formulae](https://en.wikipedia.org/wiki/Vincenty%27s_formulae) implemented by [Chris Veness](https://github.com/chrisveness/geodesy) for highest precision.
5
6[![demo](docs/img/demo.png)](https://blog.cyclemap.link/Leaflet.Geodesic/basic-interactive.html)
7
8[Live Demos and Tutorials](https://blog.cyclemap.link/Leaflet.Geodesic/)
9
10[Observable-Notebook](https://observablehq.com/@henrythasler/leaflet-geodesic)
11
12[API-Documentation](https://blog.cyclemap.link/Leaflet.Geodesic/api)
13
14## Add the plugin to your project
15
16Leaflet.Geodesic is available via CDN. Add the following snippet to your html-file after you have [included leaflet.js](https://leafletjs.com/examples/quick-start/).
17
18```html
19<!-- Make sure you put this AFTER leaflet.js -->
20<script src="https://cdn.jsdelivr.net/npm/leaflet.geodesic">
21 integrity="see-release-page-for-current-checksum"
22 crossorigin=""></script>
23```
24
25Leaflet.Geodesic is available via the following CDNs:
26
27 - [unpkg](https://unpkg.com/browse/leaflet.geodesic/)
28 - [jsDelivr](https://www.jsdelivr.com/package/npm/leaflet.geodesic)
29 - [npmjs](https://www.npmjs.com/package/leaflet.geodesic)
30
31Add it in your nodejs-project with `npm i leaflet.geodesic`.
32
33It is good practice, to pin the plug-in to a specific version and use [Subresource Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity). Check the [release page](https://github.com/henrythasler/Leaflet.Geodesic/releases) for the latest version, links and checksum. A checksum can by verified with `npm run build`, is stored in `dist/leaflet.geodesic.umd.min.js.sha512` on [jsDelivr](https://www.jsdelivr.com/package/npm/leaflet.geodesic?path=dist) and [unpkg](https://unpkg.com/browse/leaflet.geodesic/dist/leaflet.geodesic.umd.min.js.sha512) and is shown in the [build-log](https://app.travis-ci.com/github/henrythasler/Leaflet.Geodesic/builds) for a tagged version.
34
35## Basic usage
36
37- `L.Geodesic` draws geodesic lines between all points of a given line- or multiline-string.
38- `L.GeodesicCircle` draws a circle with a specific radius around a given point.
39
40The Objects can be created as follows:
41
42```JavaScript
43const geodesicLine = new L.Geodesic().addTo(map); // creates a blank geodesic-line-object and adds it to the map
44const geodesicCircle = new L.GeodesicCircle().addTo(map); // creates a blank geodesic-circle-object and adds it to the map
45```
46
47Alternative method:
48
49```JavaScript
50const geodesicLine = L.geodesic().addTo(map); // lower-case, w/o new-keyword
51const geodesicCircle = L.geodesiccircle().addTo(map); // lower-case, w/o new-keyword
52```
53
54Make sure you add the geodesic-object to the map (`.addTo(map)`). It won't display otherwise.
55
56Each constructor is defined as:
57```JavaScript
58Geodesic(latlngs?: L.LatLngExpression[] | L.LatLngExpression[][], options?: GeodesicOptions)
59GeodesicCircle(center?: L.LatLngExpression, options?: GeodesicOptions)
60```
61
62Both classes are extended from [L.Polyline](http://leafletjs.com/reference.html#polyline), so all methods, events and options for `L.Polyline` can be used with `L.Geodesic` and `L.GeodesicCircle` here as well. Any [alt-properties](https://leafletjs.com/reference.html#latlng-l-latlng) given with any points are preserved by `L.Geodesic`.
63
64## Geodesic Lines
65
66This draws a line. The geometry (points) to use can be given during creation as:
67
68### Objects (Literals)
69
70```JavaScript
71const Berlin = {lat: 52.5, lng: 13.35};
72const LosAngeles = {lat: 33.82, lng: -118.38};
73const geodesic = new L.Geodesic([Berlin, LosAngeles]).addTo(map);
74```
75
76### LatLng-Class
77
78```JavaScript
79const Berlin = new L.LatLng(52.5, 13.35);
80const LosAngeles = new L.LatLng(33.82, -118.38);
81const geodesic = new L.Geodesic([Berlin, LosAngeles]).addTo(map);
82```
83
84### Tuples
85
86```JavaScript
87const Berlin = [52.5, 13.35];
88const LosAngeles = [33.82, -118.38];
89const geodesic = new L.Geodesic([Berlin, LosAngeles]).addTo(map);
90```
91
92![line](docs/img/line.png)
93
94### Line-strings
95
96Multiple consecutive points can be given as an array (linestring):
97
98```JavaScript
99const places = [
100 new L.LatLng(52.5, 13.35), // Berlin
101 new L.LatLng(33.82, -118.38), // Los Angeles
102 new L.LatLng(-33.44, -70.71), // Santiago
103 new L.LatLng(-33.94, 18.39), // Capetown
104];
105const geodesic = new L.Geodesic(places).addTo(map);
106```
107
108![linestring](docs/img/linestring.png)
109
110### Multi-line-strings
111
112Multiple independent linestrings can be defined as a 2-dimensional array of points:
113
114```JavaScript
115const places = [
116 [ // 1st line
117 new L.LatLng(52.5, 13.35), // Berlin
118 new L.LatLng(33.82, -118.38), // Los Angeles
119 ],
120 [ // 2nd line
121 new L.LatLng(-33.44, -70.71), // Santiago
122 new L.LatLng(-33.94, 18.39), // Capetown
123 ]
124];
125const geodesic = new L.Geodesic(places).addTo(map);
126```
127
128![multilinestring](docs/img/multilinestring.png)
129
130### GeoJSON-Support
131
132GeoJSON-data can be used to create geodesic lines with the `fromGeoJson()` method:
133
134```JavaScript
135const geojson = {
136 "type": "LineString",
137 "coordinates": [
138 [13.35, 52.5], [-122.33, 47.56], [18.39, -33.94], [116.39, 39.92], [13.35, 52.5]
139 ]
140};
141const geodesic = new L.Geodesic().addTo(map);
142geodesic.fromGeoJson(geojson);
143```
144
145![geojson](docs/img/geojson.png)
146
147### Updating the geometry
148
149#### Set new geometry
150
151The Geodesic-Class provides a `setLatLngs()`-Method, that can be used to update the geometry of an existing `L.Geodesic`-object:
152
153```Javascript
154const geodesic = new L.Geodesic().addTo(map); // add empty object to the map
155
156const Berlin = new L.LatLng(52.5, 13.35);
157const LosAngeles = new L.LatLng(33.82, -118.38);
158
159geodesic.setLatLngs([Berlin, LosAngeles]) // update in-place
160```
161
162The `setLatLngs()`-Method accepts the same types (Literal, Tuple, LatLang-Class, Linstring, Multilinestring) as the L.Geodesic-constructor itself. Please refer to the section about geodesic circles below, on how to update a circle geometry.
163
164#### Delete geometry
165
166Delete the existing geometry by setting an empty array `geodesic.setLatLngs([])`.
167
168#### adding points
169
170Points can be added to existing geodesic lines with `addLatLng()`:
171
172```Javascript
173const Berlin = new L.LatLng(52.5, 13.35);
174const LosAngeles = new L.LatLng(33.82, -118.38);
175const Beijing = new L.LatLng(39.92, 116.39);
176
177const geodesic = new L.Geodesic([Berlin, LosAngeles]).addTo(map);
178geodesic.addLatLng(Beijing); // results in [[Berlin, LosAngeles, Beijing]
179```
180
181The new point will always be added to the last linestring of a multiline. You can define a specific linestring to add to by reading the `points` property before and hand over a specific linestring as second parameter:
182
183```Javascript
184const Berlin = new L.LatLng(52.5, 13.35);
185const LosAngeles = new L.LatLng(33.82, -118.38);
186const Beijing = new L.LatLng(39.92, 116.39 );
187const Capetown = new L.LatLng(-33.94, 18.39 );
188const Santiago = new L.LatLng(-33.44, -70.71);
189
190const geodesic = new L.Geodesic([[Berlin, LosAngeles], [Santiago, Capetown]]).addTo(map);
191geodesic.addLatLng(Beijing, geodesic.points[0]); // results in [[Berlin, LosAngeles, Beijing], [Santiago, Capetown]]
192```
193
194### Drawing over the antimeridian
195
196In some cases it is required to draw over the antimeridian (dateline) to show a continuous path. This is possible by setting the `wrap`-option to false. Leaflet.Geodesic will make sure to shift the individual points to draw a continuous line, even if the coordinates are not properly aligned to a map section. See [interactive example](https://blog.cyclemap.link/Leaflet.Geodesic/multiline-nosplit.html)
197
198```Javascript
199const Berlin = new L.LatLng(52.5, 13.35);
200const LosAngeles = new L.LatLng(33.82, -118.38);
201const Capetown = new L.LatLng(-33.94, 18.39 );
202const Santiago = new L.LatLng(-33.44, -70.71);
203const Tokyo = new L.LatLng(35.47, 139.15 + 360); // these points are in another map section
204const Sydney = new L.LatLng(-33.91, 151.08 + 10 * 360); // but will get shifted accordingly
205
206const geodesic = L.geodesic(
207 [ Santiago, Tokyo, Capetown, Sydney, LosAngeles, Berlin],
208 { wrap: false
209}).addTo(map);
210```
211
212![nowrap](docs/img/nowrap.png)
213
214### Line Options
215All options defined for [Polyline](http://leafletjs.com/reference.html#polyline) and [Path](https://leafletjs.com/reference.html#path) for can be used Leaflet.Geodesic.
216
217The most important options are:
218
219Option | Type | Default | Description
220---|---|---|---
221`color` | `String` | "#3388ff" | Stroke color
222`weight` | `Number` | 3 | Stroke width in pixels
223`opacity` | `Number` | 1.0 | Stroke opacity (0=transparent, 1=opaque)
224`steps` | `Number` | 3 | Level of detail (vertices = 1+2**(steps+1)) for the geodesic line. More steps result in a smoother line. Range: 0..8
225`wrap` | `Boolean` | true | Wrap geodesic line at antimeridian. Set to `false`, to draw a line over the antimeridian. See [no-wrap demo](https://blog.cyclemap.link/Leaflet.Geodesic/nowrap-interactive.html) for example.
226
227Example:
228
229```Javascript
230const Berlin = new L.LatLng(52.5, 13.35);
231const LosAngeles = new L.LatLng(33.82, -118.38);
232const options = {
233 weight: 20,
234 opacity: 0.5,
235 color: 'red',
236};
237const geodesic = new L.Geodesic([Berlin, LosAngeles], options).addTo(map);
238```
239
240![lineoptions](docs/img/lineoptions.png)
241
242## Geodesic Circles
243
244Circles can be added with another class called `L.GeodesicCircle` as follows:
245
246```Javascript
247const Seattle = new L.LatLng(47.56, -122.33);
248const geodesiccircle = new L.GeodesicCircle(Seattle, {
249 radius: 3000*1000, // 3000km in meters
250}).addTo(map);
251```
252
253![circle](docs/img/circle.png)
254
255The geometry of a circle can be updated with the following methods:
256
257- `setLatLng(latlng: L.LatLngExpression)` - set a new center
258- `setRadius(radius: number)` - update the radius
259
260Handling of **filled** circles crossing the antimeridian (wrapping) is not yet supported. Set `fill: false` in these cases to avoid display artefacts.
261
262### Circle Options
263
264Option | Type | Default | Description
265---|---|---|---
266`radius` | `Number` | 1000*1000 | Radius in **meters**
267`steps` | `Number` | 24 | Number of segments that are used to approximate the circle.
268`fill` | `boolean` | true | Draws a filled circle.
269`color` | `String` | "#3388ff" | Stroke color
270`weight` | `Number` | 3 | Stroke width in pixels
271`opacity` | `Number` | 1.0 | Stroke opacity (0=transparent, 1=opaque)
272
273Please refer to the options for [Polyline](http://leafletjs.com/reference.html#polyline) and [Path](https://leafletjs.com/reference.html#path) for additional settings.
274
275## Statistics
276
277The `L.Geodesic` and `L.GeodesicCircle`-class provide a `statistics`-Object with the following properties:
278
279Property | Type | Description
280---|---|---
281`totalDistance` | `Number` | The total distance of all geodesic lines in meters. (Circumfence for `L.GeodesicCircle`)
282`distanceArray` | `Number[]` | The distance for each separate linestring in meters
283`points` | `Number` | Number of points that were given on creation or with `setLatLngs()`
284`vertices` | `Number` | Number of vertices of all geodesic lines that were calculated
285
286## Distance Calculation
287
288The `L.Geodesic` provides a `distance`-function to calculate the precise distance between two points:
289
290```Javascript
291const Berlin = new L.LatLng(52.5, 13.35);
292const Beijing = new L.LatLng(39.92, 116.39);
293
294const line = new L.Geodesic();
295const distance = line.distance(Berlin, Beijing);
296console.log(`${Math.floor(distance/1000)} km`) // prints: 7379 km
297```
298
299The `L.GeodesicCircle`-class provides a `distanceTo`-function to calculate the distance between the current center and any given point:
300
301```Javascript
302const Berlin = new L.LatLng(52.5, 13.35);
303const Beijing = new L.LatLng(39.92, 116.39);
304
305const circle = new L.GeodesicCircle(Berlin);
306const distance = circle.distanceTo(Beijing);
307console.log(`${Math.floor(distance/1000)} km`) // prints: 7379 km
308```
309
310## Scientific background
311
312All calculations are based on the [WGS84-Ellipsoid](https://en.wikipedia.org/wiki/World_Geodetic_System#WGS84) (EPSG:4326) using [Vincenty's formulae](https://en.wikipedia.org/wiki/Vincenty%27s_formulae). This method leads to very precise calculations but may fail for some corner-cases (e.g. [Antipodes](https://en.wikipedia.org/wiki/Antipodes)). I use some workarounds to mitigate these convergence errors. This may lead to reduced precision (a.k.a. slightly wrong results) in these cases. This is good enough for a web mapping application but you shouldn't plan a space mission based on this data. OMG, this section has just become a disclaimer...