]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/leaflet/leaflet.osm.js
Update leaflet-osm to latest upstream version
[rails.git] / vendor / assets / leaflet / leaflet.osm.js
1 L.OSM = {};
2
3 L.OSM.TileLayer = L.TileLayer.extend({
4   options: {
5     url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
6     attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
7   },
8
9   initialize: function (options) {
10     options = L.Util.setOptions(this, options);
11     L.TileLayer.prototype.initialize.call(this, options.url);
12   }
13 });
14
15 L.OSM.Mapnik = L.OSM.TileLayer.extend({
16   options: {
17     url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
18     maxZoom: 19,
19     referrerPolicy: 'strict-origin-when-cross-origin'
20   }
21 });
22
23 L.OSM.CyclOSM = L.OSM.TileLayer.extend({
24   options: {
25     url: 'https://{s}.tile-cyclosm.openstreetmap.fr/cyclosm/{z}/{x}/{y}.png',
26     maxZoom: 20,
27     subdomains: 'abc',
28     attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="https://www.openstreetmap.fr" target="_blank">OpenStreetMap France</a>'
29   }
30 });
31
32 L.OSM.CycleMap = L.OSM.TileLayer.extend({
33   options: {
34     url: 'https://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}{r}.png?apikey={apikey}',
35     maxZoom: 21,
36     attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://www.thunderforest.com/" target="_blank">Andy Allan</a>'
37   }
38 });
39
40 L.OSM.TransportMap = L.OSM.TileLayer.extend({
41   options: {
42     url: 'https://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}{r}.png?apikey={apikey}',
43     maxZoom: 21,
44     attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://www.thunderforest.com/" target="_blank">Andy Allan</a>'
45   }
46 });
47
48 L.OSM.TransportDarkMap = L.OSM.TileLayer.extend({
49   options: {
50     url: 'https://{s}.tile.thunderforest.com/transport-dark/{z}/{x}/{y}{r}.png?apikey={apikey}',
51     maxZoom: 21,
52     attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://www.thunderforest.com/" target="_blank">Andy Allan</a>'
53   }
54 });
55
56 L.OSM.OPNVKarte = L.OSM.TileLayer.extend({
57   options: {
58     url: 'https://tileserver.memomaps.de/tilegen/{z}/{x}/{y}.png',
59     maxZoom: 18,
60     attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://memomaps.de/" target="_blank">MeMoMaps</a>'
61   }
62 });
63
64 L.OSM.HOT = L.OSM.TileLayer.extend({
65   options: {
66     url: 'https://tile-{s}.openstreetmap.fr/hot/{z}/{x}/{y}.png',
67     maxZoom: 20,
68     subdomains: 'abc',
69     attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://hot.openstreetmap.org/" target="_blank">Humanitarian OpenStreetMap Team</a>'
70   }
71 });
72
73 L.OSM.TracestrackTopo = L.OSM.TileLayer.extend({
74   options: {
75     url: 'https://tile.tracestrack.com/topo__/{z}/{x}/{y}.webp?key={apikey}',
76     maxZoom: 19,
77     attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="https://www.tracestrack.com/" target="_blank">Tracestrack Maps</a>'
78   }
79 });
80
81 L.OSM.GPS = L.OSM.TileLayer.extend({
82   options: {
83     url: 'https://gps.tile.openstreetmap.org/lines/{z}/{x}/{y}.png',
84     maxZoom: 21,
85     maxNativeZoom: 20,
86     subdomains: 'abc'
87   }
88 });
89
90 L.OSM.DataLayer = L.FeatureGroup.extend({
91   options: {
92     areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
93     uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
94     styles: {},
95     asynchronous: false,
96   },
97
98   initialize: function (xml, options) {
99     L.Util.setOptions(this, options);
100
101     L.FeatureGroup.prototype.initialize.call(this);
102
103     if (xml) {
104       this.addData(xml);
105     }
106   },
107
108   loadingLayers: [],
109
110   cancelLoading: function () {
111     this.loadingLayers.forEach(layer => clearTimeout(layer));
112     this.loadingLayers = [];
113   },
114
115   addData: function (features) {
116     if (!(features instanceof Array)) {
117       features = this.buildFeatures(features);
118     }
119
120     for (var i = 0; i < features.length; i++) {
121       let feature = features[i], layer;
122
123       if (feature.type === "changeset") {
124         layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
125       } else if (feature.type === "node") {
126         layer = L.circleMarker(feature.latLng, this.options.styles.node);
127       } else {
128         var latLngs = new Array(feature.nodes.length);
129
130         for (var j = 0; j < feature.nodes.length; j++) {
131           latLngs[j] = feature.nodes[j].latLng;
132         }
133
134         if (this.isWayArea(feature)) {
135           latLngs.pop(); // Remove last == first.
136           layer = L.polygon(latLngs, this.options.styles.area);
137         } else {
138           layer = L.polyline(latLngs, this.options.styles.way);
139         }
140       }
141
142       if (this.options.asynchronous) {
143         setTimeout(() => layer.addTo(this));
144       } else {
145         layer.addTo(this);
146       }
147
148       layer.feature = feature;
149     }
150   },
151
152   buildFeatures: function (data) {
153
154     const parser = data instanceof Document ? L.OSM.XMLParser : L.OSM.JSONParser;
155
156     var features = parser.getChangesets(data),
157         nodes = parser.getNodes(data),
158         ways = parser.getWays(data, nodes),
159         relations = parser.getRelations(data, nodes, ways);
160
161     var wayNodes = {}
162     for (var i = 0; i < ways.length; i++) {
163       var way = ways[i];
164       for (var j = 0; j < way.nodes.length; j++) {
165         wayNodes[way.nodes[j].id] = true
166       }
167     }
168
169     var relationNodes = {}
170     for (var i = 0; i < relations.length; i++){
171       var relation = relations[i];
172       for (var j = 0; j < relation.members.length; j++) {
173         relationNodes[relation.members[j].id] = true
174       }
175     }
176
177     for (var node_id in nodes) {
178       var node = nodes[node_id];
179       if (this.interestingNode(node, wayNodes, relationNodes)) {
180         features.push(node);
181       }
182     }
183
184     for (var i = 0; i < ways.length; i++) {
185       var way = ways[i];
186       features.push(way);
187     }
188
189     return features;
190   },
191
192   isWayArea: function (way) {
193     if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
194       return false;
195     }
196
197     for (var key in way.tags) {
198       if (~this.options.areaTags.indexOf(key)) {
199         return true;
200       }
201     }
202
203     return false;
204   },
205
206   interestingNode: function (node, wayNodes, relationNodes) {
207     if (!wayNodes[node.id] || relationNodes[node.id]) {
208       return true
209     }
210
211     for (var key in node.tags) {
212       if (this.options.uninterestingTags.indexOf(key) < 0) {
213         return true;
214       }
215     }
216
217     return false;
218   },
219
220   onRemove: function(map) {
221     this.eachLayer(map.removeLayer, map, this.options.asynchronous);
222   },
223
224   onAdd: function(map) {
225     this.eachLayer(map.addLayer, map, this.options.asynchronous);
226   },
227
228   eachLayer: function (method, context, asynchronous = false) {
229     for (let i in this._layers) {
230       if (asynchronous) {
231         this.loadingLayers.push(
232           setTimeout(() => {
233             method.call(context, this._layers[i]);
234           })
235         );
236       } else {
237         method.call(context, this._layers[i]);
238       }
239     }
240     return this;
241   },
242 });
243
244 L.OSM.XMLParser = {
245   getChangesets(xml) {
246     const changesets = [...xml.getElementsByTagName("changeset")];
247     return changesets.map(cs => ({
248       id: String(cs.getAttribute("id")),
249       type: "changeset",
250       latLngBounds: L.latLngBounds(
251         [cs.getAttribute("min_lat"), cs.getAttribute("min_lon")],
252         [cs.getAttribute("max_lat"), cs.getAttribute("max_lon")]
253       ),
254       tags: this.getTags(cs)
255     }));
256   },
257
258   getNodes(xml) {
259     const result = {};
260     const nodes = [...xml.getElementsByTagName("node")];
261
262     for (const node of nodes) {
263       const id = node.getAttribute("id");
264       result[id] = {
265         id: String(id),
266         type: "node",
267         latLng: L.latLng(
268           node.getAttribute("lat"),
269           node.getAttribute("lon"),
270           true
271         ),
272         tags: this.getTags(node)
273       };
274     }
275
276     return result;
277   },
278
279   getWays(xml, nodes) {
280     const ways = [...xml.getElementsByTagName("way")];
281     return ways.map(way => {
282       const nds = [...way.getElementsByTagName("nd")];
283       return {
284         id: String(way.getAttribute("id")),
285         type: "way",
286         nodes: nds.map(nd => nodes[nd.getAttribute("ref")]),
287         tags: this.getTags(way)
288       };
289     });
290   },
291
292   getRelations(xml, nodes, ways) {
293     const rels = [...xml.getElementsByTagName("relation")];
294     return rels.map(rel => {
295       const members = [...rel.getElementsByTagName("member")];
296       return {
297         id: String(rel.getAttribute("id")),
298         type: "relation",
299         members: members    // relation-way and relation-relation membership not implemented
300           .map(member => member.getAttribute("type") === "node" ? nodes[member.getAttribute("ref")] : null)
301           .filter(Boolean),    // filter out null and undefined
302         tags: this.getTags(rel)
303       };
304     });
305   },
306
307   getTags(xml) {
308     const result = {};
309     const tags = [...xml.getElementsByTagName("tag")];
310
311     for (const tag of tags) {
312       result[tag.getAttribute("k")] = tag.getAttribute("v");
313     }
314
315     return result;
316   }
317 }
318
319 L.OSM.JSONParser = {
320   getChangesets(json) {
321     const changesets = json.changeset ? [json.changeset] : [];
322
323     return changesets.map(cs => ({
324       id: String(cs.id),
325       type: "changeset",
326       latLngBounds: L.latLngBounds(
327         [cs.min_lat, cs.min_lon],
328         [cs.max_lat, cs.max_lon]
329       ),
330       tags: this.getTags(cs)
331     }));
332   },
333
334   getNodes(json) {
335     const nodes = json.elements?.filter(el => el.type === "node") ?? [];
336     let result = {};
337
338     for (const node of nodes) {
339       result[node.id] = {
340         id: String(node.id),
341         type: "node",
342         latLng: L.latLng(node.lat, node.lon, true),
343         tags: this.getTags(node)
344       };
345     }
346
347     return result;
348   },
349
350   getWays(json, nodes) {
351     const ways = json.elements?.filter(el => el.type === "way") ?? [];
352
353     return ways.map(way => ({
354       id: String(way.id),
355       type: "way",
356       nodes: way.nodes.map(nodeId => nodes[nodeId]),
357       tags: this.getTags(way)
358     }));
359   },
360
361   getRelations(json, nodes, ways) {
362     const relations = json.elements?.filter(el => el.type === "relation") ?? [];
363
364     return relations.map(rel => ({
365       id: String(rel.id),
366       type: "relation",
367       members: (rel.members ?? [])   // relation-way and relation-relation membership not implemented
368         .map(member => member.type === "node" ? nodes[member.ref] : null)
369         .filter(Boolean),     // filter out null and undefined
370       tags: this.getTags(rel)
371     }));
372   },
373
374   getTags(json) {
375     return json.tags ?? {};
376   }
377 };