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