3 L.OSM.TileLayer = L.TileLayer.extend({
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'
11 initialize: function (options) {
12 options = L.Util.setOptions(this, options);
13 L.TileLayer.prototype.initialize.call(this, options.url);
17 L.OSM.Mapnik = L.OSM.TileLayer.extend({
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',
26 L.OSM.CycleMap = L.OSM.TileLayer.extend({
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>'
35 L.OSM.TransportMap = L.OSM.TileLayer.extend({
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>'
44 L.OSM.MapQuestOpen = L.OSM.TileLayer.extend({
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',
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">'
56 L.OSM.HOT = L.OSM.TileLayer.extend({
58 url: 'http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png',
61 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>'
65 L.OSM.DataLayer = L.FeatureGroup.extend({
67 areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
68 uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
72 initialize: function (xml, options) {
73 L.Util.setOptions(this, options);
75 L.FeatureGroup.prototype.initialize.call(this);
82 addData: function (features) {
83 if (!(features instanceof Array)) {
84 features = this.buildFeatures(features);
87 for (var i = 0; i < features.length; i++) {
88 var feature = features[i], layer;
90 if (feature.type === "changeset") {
91 layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
92 } else if (feature.type === "node") {
93 layer = L.circleMarker(feature.latLng, this.options.styles.node);
95 var latLngs = new Array(feature.nodes.length);
97 for (var j = 0; j < feature.nodes.length; j++) {
98 latLngs[j] = feature.nodes[j].latLng;
101 if (this.isWayArea(feature)) {
102 latLngs.pop(); // Remove last == first.
103 layer = L.polygon(latLngs, this.options.styles.area);
105 layer = L.polyline(latLngs, this.options.styles.way);
110 layer.feature = feature;
114 buildFeatures: function (xml) {
115 var features = L.OSM.getChangesets(xml),
116 nodes = L.OSM.getNodes(xml),
117 ways = L.OSM.getWays(xml, nodes),
118 relations = L.OSM.getRelations(xml, nodes, ways);
120 for (var node_id in nodes) {
121 var node = nodes[node_id];
122 if (this.interestingNode(node, ways, relations)) {
127 for (var i = 0; i < ways.length; i++) {
135 isWayArea: function (way) {
136 if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
140 for (var key in way.tags) {
141 if (~this.options.areaTags.indexOf(key)) {
149 interestingNode: function (node, ways, relations) {
152 for (var i = 0; i < ways.length; i++) {
153 if (ways[i].nodes.indexOf(node) >= 0) {
163 for (var i = 0; i < relations.length; i++) {
164 if (relations[i].members.indexOf(node) >= 0)
168 for (var key in node.tags) {
169 if (this.options.uninterestingTags.indexOf(key) < 0) {
178 L.Util.extend(L.OSM, {
179 getChangesets: function (xml) {
182 var nodes = xml.getElementsByTagName("changeset");
183 for (var i = 0; i < nodes.length; i++) {
184 var node = nodes[i], id = node.getAttribute("id");
188 latLngBounds: L.latLngBounds(
189 [node.getAttribute("min_lat"), node.getAttribute("min_lon")],
190 [node.getAttribute("max_lat"), node.getAttribute("max_lon")]),
191 tags: this.getTags(node)
198 getNodes: function (xml) {
201 var nodes = xml.getElementsByTagName("node");
202 for (var i = 0; i < nodes.length; i++) {
203 var node = nodes[i], id = node.getAttribute("id");
207 latLng: L.latLng(node.getAttribute("lat"),
208 node.getAttribute("lon"),
210 tags: this.getTags(node)
217 getWays: function (xml, nodes) {
220 var ways = xml.getElementsByTagName("way");
221 for (var i = 0; i < ways.length; i++) {
222 var way = ways[i], nds = way.getElementsByTagName("nd");
225 id: way.getAttribute("id"),
227 nodes: new Array(nds.length),
228 tags: this.getTags(way)
231 for (var j = 0; j < nds.length; j++) {
232 way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
235 result.push(way_object);
241 getRelations: function (xml, nodes, ways) {
244 var rels = xml.getElementsByTagName("relation");
245 for (var i = 0; i < rels.length; i++) {
246 var rel = rels[i], members = rel.getElementsByTagName("member");
249 id: rel.getAttribute("id"),
251 members: new Array(members.length),
252 tags: this.getTags(rel)
255 for (var j = 0; j < members.length; j++) {
256 if (members[j].getAttribute("type") === "node")
257 rel_object.members[j] = nodes[members[j].getAttribute("ref")];
258 else // relation-way and relation-relation membership not implemented
259 rel_object.members[j] = null;
262 result.push(rel_object);
268 getTags: function (xml) {
271 var tags = xml.getElementsByTagName("tag");
272 for (var j = 0; j < tags.length; j++) {
273 result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");