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