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