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