]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/leaflet/leaflet.osm.js
Replace leaflet.draw with leaflet-locationfilter
[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 (xml) {
59     var nodes = L.OSM.getNodes(xml),
60          ways = L.OSM.getWays(xml);
61
62     for (var i = 0; i < ways.length; i++) {
63       var way = ways[i],
64         latLngs = new Array(way.nodes.length);
65
66       for (var j = 0; j < way.nodes.length; j++) {
67         latLngs[j] = nodes[way.nodes[j]].latLng;
68       }
69
70       var layer;
71
72       if (this.isWayArea(way)) {
73         latLngs.pop(); // Remove last == first.
74         layer = L.polygon(latLngs, this.options.styles.area);
75       } else {
76         layer = L.polyline(latLngs, this.options.styles.way);
77       }
78
79       layer.addTo(this);
80       layer.feature = way;
81     }
82
83     for (var node_id in nodes) {
84       var node = nodes[node_id];
85       if (this.interestingNode(node)) {
86         var layer = L.circleMarker(node.latLng, this.options.styles.node);
87
88         layer.addTo(this);
89         layer.feature = node;
90       }
91     }
92   },
93
94   isWayArea: function (way) {
95     if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
96       return false;
97     }
98
99     for (var key in way.tags) {
100       if (~this.options.areaTags.indexOf(key)) {
101         return true;
102       }
103     }
104
105     return false;
106   },
107
108   interestingNode: function (node) {
109     for (var key in node.tags) {
110       if (!~this.options.uninterestingTags.indexOf(key)) {
111         return true;
112       }
113     }
114
115     return false;
116   }
117 });
118
119 L.Util.extend(L.OSM, {
120   getNodes: function (xml) {
121     var result = {};
122
123     var nodes = xml.getElementsByTagName("node");
124     for (var i = 0; i < nodes.length; i++) {
125       var node = nodes[i], id = node.getAttribute("id");
126       result[id] = {
127         id: id,
128         type: "node",
129         latLng: L.latLng(node.getAttribute("lat"),
130                          node.getAttribute("lon"),
131                          true),
132         tags: this.getTags(node)
133       };
134     }
135
136     return result;
137   },
138
139   getWays: function (xml) {
140     var result = [];
141
142     var ways = xml.getElementsByTagName("way");
143     for (var i = 0; i < ways.length; i++) {
144       var way = ways[i], nds = way.getElementsByTagName("nd");
145
146       var way_object = {
147         id: way.getAttribute("id"),
148         type: "way",
149         nodes: new Array(nds.length),
150         tags: this.getTags(way)
151       };
152
153       for (var j = 0; j < nds.length; j++) {
154         way_object.nodes[j] = nds[j].getAttribute("ref");
155       }
156
157       result.push(way_object);
158     }
159
160     return result;
161   },
162
163   getTags: function (xml) {
164     var result = {};
165
166     var tags = xml.getElementsByTagName("tag");
167     for (var j = 0; j < tags.length; j++) {
168       result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");
169     }
170
171     return result;
172   }
173 });