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}{r}.png?apikey={apikey}' :
30 'http://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}{r}.png?apikey={apikey}',
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}{r}.png?apikey={apikey}' :
39 'http://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}{r}.png?apikey={apikey}',
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: document.location.protocol === 'https:' ?
59 'https://tile-{s}.openstreetmap.fr/hot/{z}/{x}/{y}.png' :
60 'http://tile-{s}.openstreetmap.fr/hot/{z}/{x}/{y}.png',
63 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>'
67 L.OSM.GPS = L.OSM.TileLayer.extend({
69 url: document.location.protocol === 'https:' ?
70 'https://gps-{s}.tile.openstreetmap.org/lines/{z}/{x}/{y}.png' :
71 'http://gps-{s}.tile.openstreetmap.org/lines/{z}/{x}/{y}.png',
77 L.OSM.DataLayer = L.FeatureGroup.extend({
79 areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
80 uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
84 initialize: function (xml, options) {
85 L.Util.setOptions(this, options);
87 L.FeatureGroup.prototype.initialize.call(this);
94 addData: function (features) {
95 if (!(features instanceof Array)) {
96 features = this.buildFeatures(features);
99 for (var i = 0; i < features.length; i++) {
100 var feature = features[i], layer;
102 if (feature.type === "changeset") {
103 layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
104 } else if (feature.type === "node") {
105 layer = L.circleMarker(feature.latLng, this.options.styles.node);
107 var latLngs = new Array(feature.nodes.length);
109 for (var j = 0; j < feature.nodes.length; j++) {
110 latLngs[j] = feature.nodes[j].latLng;
113 if (this.isWayArea(feature)) {
114 latLngs.pop(); // Remove last == first.
115 layer = L.polygon(latLngs, this.options.styles.area);
117 layer = L.polyline(latLngs, this.options.styles.way);
122 layer.feature = feature;
126 buildFeatures: function (xml) {
127 var features = L.OSM.getChangesets(xml),
128 nodes = L.OSM.getNodes(xml),
129 ways = L.OSM.getWays(xml, nodes),
130 relations = L.OSM.getRelations(xml, nodes, ways);
132 for (var node_id in nodes) {
133 var node = nodes[node_id];
134 if (this.interestingNode(node, ways, relations)) {
139 for (var i = 0; i < ways.length; i++) {
147 isWayArea: function (way) {
148 if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
152 for (var key in way.tags) {
153 if (~this.options.areaTags.indexOf(key)) {
161 interestingNode: function (node, ways, relations) {
164 for (var i = 0; i < ways.length; i++) {
165 if (ways[i].nodes.indexOf(node) >= 0) {
175 for (var i = 0; i < relations.length; i++) {
176 if (relations[i].members.indexOf(node) >= 0)
180 for (var key in node.tags) {
181 if (this.options.uninterestingTags.indexOf(key) < 0) {
190 L.Util.extend(L.OSM, {
191 getChangesets: function (xml) {
194 var nodes = xml.getElementsByTagName("changeset");
195 for (var i = 0; i < nodes.length; i++) {
196 var node = nodes[i], id = node.getAttribute("id");
200 latLngBounds: L.latLngBounds(
201 [node.getAttribute("min_lat"), node.getAttribute("min_lon")],
202 [node.getAttribute("max_lat"), node.getAttribute("max_lon")]),
203 tags: this.getTags(node)
210 getNodes: function (xml) {
213 var nodes = xml.getElementsByTagName("node");
214 for (var i = 0; i < nodes.length; i++) {
215 var node = nodes[i], id = node.getAttribute("id");
219 latLng: L.latLng(node.getAttribute("lat"),
220 node.getAttribute("lon"),
222 tags: this.getTags(node)
229 getWays: function (xml, nodes) {
232 var ways = xml.getElementsByTagName("way");
233 for (var i = 0; i < ways.length; i++) {
234 var way = ways[i], nds = way.getElementsByTagName("nd");
237 id: way.getAttribute("id"),
239 nodes: new Array(nds.length),
240 tags: this.getTags(way)
243 for (var j = 0; j < nds.length; j++) {
244 way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
247 result.push(way_object);
253 getRelations: function (xml, nodes, ways) {
256 var rels = xml.getElementsByTagName("relation");
257 for (var i = 0; i < rels.length; i++) {
258 var rel = rels[i], members = rel.getElementsByTagName("member");
261 id: rel.getAttribute("id"),
263 members: new Array(members.length),
264 tags: this.getTags(rel)
267 for (var j = 0; j < members.length; j++) {
268 if (members[j].getAttribute("type") === "node")
269 rel_object.members[j] = nodes[members[j].getAttribute("ref")];
270 else // relation-way and relation-relation membership not implemented
271 rel_object.members[j] = null;
274 result.push(rel_object);
280 getTags: function (xml) {
283 var tags = xml.getElementsByTagName("tag");
284 for (var j = 0; j < tags.length; j++) {
285 result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");