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