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: 'http://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png',
29 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>'
33 L.OSM.TransportMap = L.OSM.TileLayer.extend({
35 url: 'http://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}.png',
36 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>'
40 L.OSM.MapQuestOpen = L.OSM.TileLayer.extend({
42 url: document.location.protocol === 'https:' ?
43 'https://otile{s}-s.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png' :
44 'http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png',
46 attribution: '© <a href="http://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. ' + document.location.protocol === 'https:' ?
47 'Tiles courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="https://developer.mapquest.com/content/osm/mq_logo.png">' :
48 'Tiles courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png">'
52 L.OSM.HOT = L.OSM.TileLayer.extend({
54 url: 'http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png',
57 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>'
61 L.OSM.DataLayer = L.FeatureGroup.extend({
63 areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
64 uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
68 initialize: function (xml, options) {
69 L.Util.setOptions(this, options);
71 L.FeatureGroup.prototype.initialize.call(this);
78 addData: function (features) {
79 if (!(features instanceof Array)) {
80 features = this.buildFeatures(features);
83 for (var i = 0; i < features.length; i++) {
84 var feature = features[i], layer;
86 if (feature.type === "changeset") {
87 layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
88 } else if (feature.type === "node") {
89 layer = L.circleMarker(feature.latLng, this.options.styles.node);
91 var latLngs = new Array(feature.nodes.length);
93 for (var j = 0; j < feature.nodes.length; j++) {
94 latLngs[j] = feature.nodes[j].latLng;
97 if (this.isWayArea(feature)) {
98 latLngs.pop(); // Remove last == first.
99 layer = L.polygon(latLngs, this.options.styles.area);
101 layer = L.polyline(latLngs, this.options.styles.way);
106 layer.feature = feature;
110 buildFeatures: function (xml) {
111 var features = L.OSM.getChangesets(xml),
112 nodes = L.OSM.getNodes(xml),
113 ways = L.OSM.getWays(xml, nodes),
114 relations = L.OSM.getRelations(xml, nodes, ways);
116 for (var node_id in nodes) {
117 var node = nodes[node_id];
118 if (this.interestingNode(node, ways, relations)) {
123 for (var i = 0; i < ways.length; i++) {
131 isWayArea: function (way) {
132 if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
136 for (var key in way.tags) {
137 if (~this.options.areaTags.indexOf(key)) {
145 interestingNode: function (node, ways, relations) {
148 for (var i = 0; i < ways.length; i++) {
149 if (ways[i].nodes.indexOf(node) >= 0) {
159 for (var i = 0; i < relations.length; i++) {
160 if (relations[i].members.indexOf(node) >= 0)
164 for (var key in node.tags) {
165 if (this.options.uninterestingTags.indexOf(key) < 0) {
174 L.Util.extend(L.OSM, {
175 getChangesets: function (xml) {
178 var nodes = xml.getElementsByTagName("changeset");
179 for (var i = 0; i < nodes.length; i++) {
180 var node = nodes[i], id = node.getAttribute("id");
184 latLngBounds: L.latLngBounds(
185 [node.getAttribute("min_lat"), node.getAttribute("min_lon")],
186 [node.getAttribute("max_lat"), node.getAttribute("max_lon")]),
187 tags: this.getTags(node)
194 getNodes: function (xml) {
197 var nodes = xml.getElementsByTagName("node");
198 for (var i = 0; i < nodes.length; i++) {
199 var node = nodes[i], id = node.getAttribute("id");
203 latLng: L.latLng(node.getAttribute("lat"),
204 node.getAttribute("lon"),
206 tags: this.getTags(node)
213 getWays: function (xml, nodes) {
216 var ways = xml.getElementsByTagName("way");
217 for (var i = 0; i < ways.length; i++) {
218 var way = ways[i], nds = way.getElementsByTagName("nd");
221 id: way.getAttribute("id"),
223 nodes: new Array(nds.length),
224 tags: this.getTags(way)
227 for (var j = 0; j < nds.length; j++) {
228 way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
231 result.push(way_object);
237 getRelations: function (xml, nodes, ways) {
240 var rels = xml.getElementsByTagName("relation");
241 for (var i = 0; i < rels.length; i++) {
242 var rel = rels[i], members = rel.getElementsByTagName("member");
245 id: rel.getAttribute("id"),
247 members: new Array(members.length),
248 tags: this.getTags(rel)
251 for (var j = 0; j < members.length; j++) {
252 if (members[j].getAttribute("type") === "node")
253 rel_object.members[j] = nodes[members[j].getAttribute("ref")];
254 else // relation-way and relation-relation membership not implemented
255 rel_object.members[j] = null;
258 result.push(rel_object);
264 getTags: function (xml) {
267 var tags = xml.getElementsByTagName("tag");
268 for (var j = 0; j < tags.length; j++) {
269 result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");