]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/leaflet/leaflet.osm.js
Merge remote-tracking branch 'upstream/pull/5064'
[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   addData: function (features) {
108     if (!(features instanceof Array)) {
109       features = this.buildFeatures(features);
110     }
111
112     for (var i = 0; i < features.length; i++) {
113       let feature = features[i], layer;
114
115       if (feature.type === "changeset") {
116         layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
117       } else if (feature.type === "node") {
118         layer = L.circleMarker(feature.latLng, this.options.styles.node);
119       } else {
120         var latLngs = new Array(feature.nodes.length);
121
122         for (var j = 0; j < feature.nodes.length; j++) {
123           latLngs[j] = feature.nodes[j].latLng;
124         }
125
126         if (this.isWayArea(feature)) {
127           latLngs.pop(); // Remove last == first.
128           layer = L.polygon(latLngs, this.options.styles.area);
129         } else {
130           layer = L.polyline(latLngs, this.options.styles.way);
131         }
132       }
133
134       if (this.options.asynchronous) {
135         setTimeout(() => layer.addTo(this));
136       } else {
137         layer.addTo(this);
138       }
139
140       layer.feature = feature;
141     }
142   },
143
144   buildFeatures: function (data) {
145
146     const parser = data instanceof Document ? L.OSM.XMLParser : L.OSM.JSONParser;
147
148     var features = parser.getChangesets(data),
149         nodes = parser.getNodes(data),
150         ways = parser.getWays(data, nodes),
151         relations = parser.getRelations(data, nodes, ways);
152
153     var wayNodes = {}
154     for (var i = 0; i < ways.length; i++) {
155       var way = ways[i];
156       for (var j = 0; j < way.nodes.length; j++) {
157         wayNodes[way.nodes[j].id] = true
158       }
159     }
160
161     var relationNodes = {}
162     for (var i = 0; i < relations.length; i++){
163       var relation = relations[i];
164       for (var j = 0; j < relation.members.length; j++) {
165         relationNodes[relation.members[j].id] = true
166       }
167     }
168
169     for (var node_id in nodes) {
170       var node = nodes[node_id];
171       if (this.interestingNode(node, wayNodes, relationNodes)) {
172         features.push(node);
173       }
174     }
175
176     for (var i = 0; i < ways.length; i++) {
177       var way = ways[i];
178       features.push(way);
179     }
180
181     return features;
182   },
183
184   isWayArea: function (way) {
185     if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
186       return false;
187     }
188
189     for (var key in way.tags) {
190       if (~this.options.areaTags.indexOf(key)) {
191         return true;
192       }
193     }
194
195     return false;
196   },
197
198   interestingNode: function (node, wayNodes, relationNodes) {
199     if (!wayNodes[node.id] || relationNodes[node.id]) {
200       return true
201     }
202
203     for (var key in node.tags) {
204       if (this.options.uninterestingTags.indexOf(key) < 0) {
205         return true;
206       }
207     }
208
209     return false;
210   },
211
212   onRemove: function(map) {
213     this.eachLayer(map.removeLayer, map, this.options.asynchronous);
214   },
215
216   onAdd: function(map) {
217     this.eachLayer(map.addLayer, map, this.options.asynchronous);
218   },
219
220   eachLayer: function (method, context, asynchronous = false) {
221     for (let i in this._layers) {
222       if (asynchronous) {
223         setTimeout(() => {
224           method.call(context, this._layers[i]);
225         });
226       } else {
227         method.call(context, this._layers[i]);
228       }
229     }
230     return this;
231   },
232 });
233
234 L.OSM.XMLParser = {
235   getChangesets: function (xml) {
236     var result = [];
237
238     var nodes = xml.getElementsByTagName("changeset");
239     for (var i = 0; i < nodes.length; i++) {
240       var node = nodes[i], id = node.getAttribute("id");
241       result.push({
242         id: id,
243         type: "changeset",
244         latLngBounds: L.latLngBounds(
245           [node.getAttribute("min_lat"), node.getAttribute("min_lon")],
246           [node.getAttribute("max_lat"), node.getAttribute("max_lon")]),
247         tags: this.getTags(node)
248       });
249     }
250
251     return result;
252   },
253
254   getNodes: function (xml) {
255     var result = {};
256
257     var nodes = xml.getElementsByTagName("node");
258     for (var i = 0; i < nodes.length; i++) {
259       var node = nodes[i], id = node.getAttribute("id");
260       result[id] = {
261         id: id,
262         type: "node",
263         latLng: L.latLng(node.getAttribute("lat"),
264                          node.getAttribute("lon"),
265                          true),
266         tags: this.getTags(node)
267       };
268     }
269
270     return result;
271   },
272
273   getWays: function (xml, nodes) {
274     var result = [];
275
276     var ways = xml.getElementsByTagName("way");
277     for (var i = 0; i < ways.length; i++) {
278       var way = ways[i], nds = way.getElementsByTagName("nd");
279
280       var way_object = {
281         id: way.getAttribute("id"),
282         type: "way",
283         nodes: new Array(nds.length),
284         tags: this.getTags(way)
285       };
286
287       for (var j = 0; j < nds.length; j++) {
288         way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
289       }
290
291       result.push(way_object);
292     }
293
294     return result;
295   },
296
297   getRelations: function (xml, nodes, ways) {
298     var result = [];
299
300     var rels = xml.getElementsByTagName("relation");
301     for (var i = 0; i < rels.length; i++) {
302       var rel = rels[i], members = rel.getElementsByTagName("member");
303
304       var rel_object = {
305         id: rel.getAttribute("id"),
306         type: "relation",
307         members: new Array(members.length),
308         tags: this.getTags(rel)
309       };
310
311       for (var j = 0; j < members.length; j++) {
312         if (members[j].getAttribute("type") === "node")
313           rel_object.members[j] = nodes[members[j].getAttribute("ref")];
314         else // relation-way and relation-relation membership not implemented
315           rel_object.members[j] = null;
316       }
317       rel_object.members = rel_object.members.filter(i => i !== null && i !== undefined)
318       result.push(rel_object);
319     }
320
321     return result;
322   },
323
324   getTags: function (xml) {
325     var result = {};
326
327     var tags = xml.getElementsByTagName("tag");
328     for (var j = 0; j < tags.length; j++) {
329       result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");
330     }
331
332     return result;
333   }
334 }
335
336 L.OSM.JSONParser = {
337   getChangesets(json) {
338     const changesets = json.changeset ? [json.changeset] : [];
339
340     return changesets.map(cs => ({
341       id: String(cs.id),
342       type: "changeset",
343       latLngBounds: L.latLngBounds(
344         [cs.min_lat, cs.min_lon],
345         [cs.max_lat, cs.max_lon]
346       ),
347       tags: this.getTags(cs)
348     }));
349   },
350
351   getNodes(json) {
352     const nodes = json.elements?.filter(el => el.type === "node") ?? [];
353     let result = {};
354
355     for (const node of nodes) {
356       result[node.id] = {
357         id: String(node.id),
358         type: "node",
359         latLng: L.latLng(node.lat, node.lon, true),
360         tags: this.getTags(node)
361       };
362     }
363
364     return result;
365   },
366
367   getWays(json, nodes) {
368     const ways = json.elements?.filter(el => el.type === "way") ?? [];
369
370     return ways.map(way => ({
371       id: String(way.id),
372       type: "way",
373       nodes: way.nodes.map(nodeId => nodes[nodeId]),
374       tags: this.getTags(way)
375     }));
376   },
377
378   getRelations(json, nodes, ways) {
379     const relations = json.elements?.filter(el => el.type === "relation") ?? [];
380
381     return relations.map(rel => ({
382       id: String(rel.id),
383       type: "relation",
384       members: (rel.members ?? [])   // relation-way and relation-relation membership not implemented
385         .map(member => member.type === "node" ? nodes[member.ref] : null)
386         .filter(Boolean),     // filter out null and undefined
387       tags: this.getTags(rel)
388     }));
389   },
390
391   getTags(json) {
392     return json.tags ?? {};
393   }
394 };