]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/leaflet/leaflet.osm.js
Merge remote-tracking branch 'upstream/pull/2994'
[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.OPNVKarte = L.OSM.TileLayer.extend({
48   options: {
49     url: 'https://tileserver.memomaps.de/tilegen/{z}/{x}/{y}.png',
50     maxZoom: 18,
51     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>'
52   }
53 });
54
55 L.OSM.HOT = L.OSM.TileLayer.extend({
56   options: {
57     url: 'https://tile-{s}.openstreetmap.fr/hot/{z}/{x}/{y}.png',
58     maxZoom: 20,
59     subdomains: 'abc',
60     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>'
61   }
62 });
63
64 L.OSM.GPS = L.OSM.TileLayer.extend({
65   options: {
66     url: 'https://gps.tile.openstreetmap.org/lines/{z}/{x}/{y}.png',
67     maxZoom: 21,
68     maxNativeZoom: 20,
69     subdomains: 'abc'
70   }
71 });
72
73 L.OSM.DataLayer = L.FeatureGroup.extend({
74   options: {
75     areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
76     uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
77     styles: {}
78   },
79
80   initialize: function (xml, options) {
81     L.Util.setOptions(this, options);
82
83     L.FeatureGroup.prototype.initialize.call(this);
84
85     if (xml) {
86       this.addData(xml);
87     }
88   },
89
90   addData: function (features) {
91     if (!(features instanceof Array)) {
92       features = this.buildFeatures(features);
93     }
94
95     for (var i = 0; i < features.length; i++) {
96       var feature = features[i], layer;
97
98       if (feature.type === "changeset") {
99         layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
100       } else if (feature.type === "node") {
101         layer = L.circleMarker(feature.latLng, this.options.styles.node);
102       } else {
103         var latLngs = new Array(feature.nodes.length);
104
105         for (var j = 0; j < feature.nodes.length; j++) {
106           latLngs[j] = feature.nodes[j].latLng;
107         }
108
109         if (this.isWayArea(feature)) {
110           latLngs.pop(); // Remove last == first.
111           layer = L.polygon(latLngs, this.options.styles.area);
112         } else {
113           layer = L.polyline(latLngs, this.options.styles.way);
114         }
115       }
116
117       layer.addTo(this);
118       layer.feature = feature;
119     }
120   },
121
122   buildFeatures: function (xml) {
123     var features = L.OSM.getChangesets(xml),
124       nodes = L.OSM.getNodes(xml),
125       ways = L.OSM.getWays(xml, nodes),
126       relations = L.OSM.getRelations(xml, nodes, ways);
127
128     for (var node_id in nodes) {
129       var node = nodes[node_id];
130       if (this.interestingNode(node, ways, relations)) {
131         features.push(node);
132       }
133     }
134
135     for (var i = 0; i < ways.length; i++) {
136       var way = ways[i];
137       features.push(way);
138     }
139
140     return features;
141   },
142
143   isWayArea: function (way) {
144     if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
145       return false;
146     }
147
148     for (var key in way.tags) {
149       if (~this.options.areaTags.indexOf(key)) {
150         return true;
151       }
152     }
153
154     return false;
155   },
156
157   interestingNode: function (node, ways, relations) {
158     var used = false;
159
160     for (var i = 0; i < ways.length; i++) {
161       if (ways[i].nodes.indexOf(node) >= 0) {
162         used = true;
163         break;
164       }
165     }
166
167     if (!used) {
168       return true;
169     }
170
171     for (var i = 0; i < relations.length; i++) {
172       if (relations[i].members.indexOf(node) >= 0)
173         return true;
174     }
175
176     for (var key in node.tags) {
177       if (this.options.uninterestingTags.indexOf(key) < 0) {
178         return true;
179       }
180     }
181
182     return false;
183   }
184 });
185
186 L.Util.extend(L.OSM, {
187   getChangesets: function (xml) {
188     var result = [];
189
190     var nodes = xml.getElementsByTagName("changeset");
191     for (var i = 0; i < nodes.length; i++) {
192       var node = nodes[i], id = node.getAttribute("id");
193       result.push({
194         id: id,
195         type: "changeset",
196         latLngBounds: L.latLngBounds(
197           [node.getAttribute("min_lat"), node.getAttribute("min_lon")],
198           [node.getAttribute("max_lat"), node.getAttribute("max_lon")]),
199         tags: this.getTags(node)
200       });
201     }
202
203     return result;
204   },
205
206   getNodes: function (xml) {
207     var result = {};
208
209     var nodes = xml.getElementsByTagName("node");
210     for (var i = 0; i < nodes.length; i++) {
211       var node = nodes[i], id = node.getAttribute("id");
212       result[id] = {
213         id: id,
214         type: "node",
215         latLng: L.latLng(node.getAttribute("lat"),
216                          node.getAttribute("lon"),
217                          true),
218         tags: this.getTags(node)
219       };
220     }
221
222     return result;
223   },
224
225   getWays: function (xml, nodes) {
226     var result = [];
227
228     var ways = xml.getElementsByTagName("way");
229     for (var i = 0; i < ways.length; i++) {
230       var way = ways[i], nds = way.getElementsByTagName("nd");
231
232       var way_object = {
233         id: way.getAttribute("id"),
234         type: "way",
235         nodes: new Array(nds.length),
236         tags: this.getTags(way)
237       };
238
239       for (var j = 0; j < nds.length; j++) {
240         way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
241       }
242
243       result.push(way_object);
244     }
245
246     return result;
247   },
248
249   getRelations: function (xml, nodes, ways) {
250     var result = [];
251
252     var rels = xml.getElementsByTagName("relation");
253     for (var i = 0; i < rels.length; i++) {
254       var rel = rels[i], members = rel.getElementsByTagName("member");
255
256       var rel_object = {
257         id: rel.getAttribute("id"),
258         type: "relation",
259         members: new Array(members.length),
260         tags: this.getTags(rel)
261       };
262
263       for (var j = 0; j < members.length; j++) {
264         if (members[j].getAttribute("type") === "node")
265           rel_object.members[j] = nodes[members[j].getAttribute("ref")];
266         else // relation-way and relation-relation membership not implemented
267           rel_object.members[j] = null;
268       }
269
270       result.push(rel_object);
271     }
272
273     return result;
274   },
275
276   getTags: function (xml) {
277     var result = {};
278
279     var tags = xml.getElementsByTagName("tag");
280     for (var j = 0; j < tags.length; j++) {
281       result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");
282     }
283
284     return result;
285   }
286 });