3 L.OSM.TileLayer = L.TileLayer.extend({
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>'
9 initialize: function (options) {
10 options = L.Util.setOptions(this, options);
11 L.TileLayer.prototype.initialize.call(this, options.url);
15 L.OSM.Mapnik = L.OSM.TileLayer.extend({
17 url: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
22 L.OSM.CycleMap = L.OSM.TileLayer.extend({
24 url: 'http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png'
28 L.OSM.TransportMap = L.OSM.TileLayer.extend({
30 url: 'http://{s}.tile2.opencyclemap.org/transport/{z}/{x}/{y}.png'
34 L.OSM.MapQuestOpen = L.OSM.TileLayer.extend({
36 url: 'http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png',
38 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'>"
42 L.OSM.HOT = L.OSM.TileLayer.extend({
44 url: 'http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png',
47 attribution: "Tiles courtesy of <a href='http://hot.openstreetmap.org/' target='_blank'>Humanitarian OpenStreetMap Team</a>"
51 L.OSM.DataLayer = L.FeatureGroup.extend({
53 areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
54 uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
58 initialize: function (xml, options) {
59 L.Util.setOptions(this, options);
61 L.FeatureGroup.prototype.initialize.call(this);
68 addData: function (features) {
69 if (!(features instanceof Array)) {
70 features = this.buildFeatures(features);
73 for (var i = 0; i < features.length; i++) {
74 var feature = features[i], layer;
76 if (feature.type === "node") {
77 layer = L.circleMarker(feature.latLng, this.options.styles.node);
79 var latLngs = new Array(feature.nodes.length);
81 for (var j = 0; j < feature.nodes.length; j++) {
82 latLngs[j] = feature.nodes[j].latLng;
85 if (this.isWayArea(feature)) {
86 latLngs.pop(); // Remove last == first.
87 layer = L.polygon(latLngs, this.options.styles.area);
89 layer = L.polyline(latLngs, this.options.styles.way);
94 layer.feature = feature;
98 buildFeatures: function (xml) {
100 nodes = L.OSM.getNodes(xml),
101 ways = L.OSM.getWays(xml, nodes),
102 relations = L.OSM.getRelations(xml, nodes, ways);
104 for (var node_id in nodes) {
105 var node = nodes[node_id];
106 if (this.interestingNode(node, ways, relations)) {
111 for (var i = 0; i < ways.length; i++) {
119 isWayArea: function (way) {
120 if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
124 for (var key in way.tags) {
125 if (~this.options.areaTags.indexOf(key)) {
133 interestingNode: function (node, ways, relations) {
136 for (var i = 0; i < ways.length; i++) {
137 if (ways[i].nodes.indexOf(node) >= 0) {
147 for (var i = 0; i < relations.length; i++) {
148 if (relations[i].members.indexOf(node) >= 0)
152 for (var key in node.tags) {
153 if (this.options.uninterestingTags.indexOf(key) < 0) {
162 L.Util.extend(L.OSM, {
163 getNodes: function (xml) {
166 var nodes = xml.getElementsByTagName("node");
167 for (var i = 0; i < nodes.length; i++) {
168 var node = nodes[i], id = node.getAttribute("id");
172 latLng: L.latLng(node.getAttribute("lat"),
173 node.getAttribute("lon"),
175 tags: this.getTags(node)
182 getWays: function (xml, nodes) {
185 var ways = xml.getElementsByTagName("way");
186 for (var i = 0; i < ways.length; i++) {
187 var way = ways[i], nds = way.getElementsByTagName("nd");
190 id: way.getAttribute("id"),
192 nodes: new Array(nds.length),
193 tags: this.getTags(way)
196 for (var j = 0; j < nds.length; j++) {
197 way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
200 result.push(way_object);
206 getRelations: function (xml, nodes, ways) {
209 var rels = xml.getElementsByTagName("relation");
210 for (var i = 0; i < rels.length; i++) {
211 var rel = rels[i], members = rel.getElementsByTagName("member");
214 id: rel.getAttribute("id"),
216 members: new Array(members.length),
217 tags: this.getTags(rel)
220 for (var j = 0; j < members.length; j++) {
221 if (members[j].getAttribute("type") === "node")
222 rel_object.members[j] = nodes[members[j].getAttribute("ref")];
223 else // relation-way and relation-relation membership not implemented
224 rel_object.members[j] = null;
227 result.push(rel_object);
233 getTags: function (xml) {
236 var tags = xml.getElementsByTagName("tag");
237 for (var j = 0; j < tags.length; j++) {
238 result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");