]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/leaflet/leaflet.osm.js
Update Leaflet
[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   }
19 });
20
21 L.OSM.CycleMap = L.OSM.TileLayer.extend({
22   options: {
23     url: 'http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png'
24   }
25 });
26
27 L.OSM.TransportMap = L.OSM.TileLayer.extend({
28   options: {
29     url: 'http://{s}.tile2.opencyclemap.org/transport/{z}/{x}/{y}.png'
30   }
31 });
32
33 L.OSM.MapQuestOpen = L.OSM.TileLayer.extend({
34   options: {
35     url: 'http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png',
36     subdomains: '1234',
37     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'>"
38   }
39 });
40
41 L.OSM.DataLayer = L.FeatureGroup.extend({
42   options: {
43     areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
44     uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
45     styles: {}
46   },
47
48   initialize: function (xml, options) {
49     L.Util.setOptions(this, options);
50
51     L.FeatureGroup.prototype.initialize.call(this);
52
53     if (xml) {
54       this.addData(xml);
55     }
56   },
57
58   addData: function (features) {
59     if (!(features instanceof Array)) {
60       features = this.buildFeatures(features);
61     }
62
63     for (var i = 0; i < features.length; i++) {
64       var feature = features[i], layer;
65
66       if (feature.type === "node") {
67         layer = L.circleMarker(feature.latLng, this.options.styles.node);
68       } else {
69         var latLngs = new Array(feature.nodes.length);
70
71         for (var j = 0; j < feature.nodes.length; j++) {
72           latLngs[j] = feature.nodes[j].latLng;
73         }
74
75         if (this.isWayArea(feature)) {
76           latLngs.pop(); // Remove last == first.
77           layer = L.polygon(latLngs, this.options.styles.area);
78         } else {
79           layer = L.polyline(latLngs, this.options.styles.way);
80         }
81       }
82
83       layer.addTo(this);
84       layer.feature = feature;
85     }
86   },
87
88   buildFeatures: function (xml) {
89     var features = [],
90       nodes = L.OSM.getNodes(xml),
91       ways = L.OSM.getWays(xml, nodes);
92
93     for (var node_id in nodes) {
94       var node = nodes[node_id];
95       if (this.interestingNode(node)) {
96         features.push(node);
97       }
98     }
99
100     for (var i = 0; i < ways.length; i++) {
101       var way = ways[i];
102       features.push(way);
103     }
104
105     return features;
106   },
107
108   isWayArea: function (way) {
109     if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
110       return false;
111     }
112
113     for (var key in way.tags) {
114       if (~this.options.areaTags.indexOf(key)) {
115         return true;
116       }
117     }
118
119     return false;
120   },
121
122   interestingNode: function (node) {
123     for (var key in node.tags) {
124       if (!~this.options.uninterestingTags.indexOf(key)) {
125         return true;
126       }
127     }
128
129     return false;
130   }
131 });
132
133 L.Util.extend(L.OSM, {
134   getNodes: function (xml) {
135     var result = {};
136
137     var nodes = xml.getElementsByTagName("node");
138     for (var i = 0; i < nodes.length; i++) {
139       var node = nodes[i], id = node.getAttribute("id");
140       result[id] = {
141         id: id,
142         type: "node",
143         latLng: L.latLng(node.getAttribute("lat"),
144                          node.getAttribute("lon"),
145                          true),
146         tags: this.getTags(node)
147       };
148     }
149
150     return result;
151   },
152
153   getWays: function (xml, nodes) {
154     var result = [];
155
156     var ways = xml.getElementsByTagName("way");
157     for (var i = 0; i < ways.length; i++) {
158       var way = ways[i], nds = way.getElementsByTagName("nd");
159
160       var way_object = {
161         id: way.getAttribute("id"),
162         type: "way",
163         nodes: new Array(nds.length),
164         tags: this.getTags(way)
165       };
166
167       for (var j = 0; j < nds.length; j++) {
168         way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
169       }
170
171       result.push(way_object);
172     }
173
174     return result;
175   },
176
177   getTags: function (xml) {
178     var result = {};
179
180     var tags = xml.getElementsByTagName("tag");
181     for (var j = 0; j < tags.length; j++) {
182       result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");
183     }
184
185     return result;
186   }
187 });