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