3 L.OSM.TileLayer = L.TileLayer.extend({
5 url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
6 attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
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: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
22 L.OSM.CycleMap = L.OSM.TileLayer.extend({
24 url: 'https://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}{r}.png?apikey={apikey}',
25 attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://www.thunderforest.com/" target="_blank">Andy Allan</a>'
29 L.OSM.TransportMap = L.OSM.TileLayer.extend({
31 url: 'https://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}{r}.png?apikey={apikey}',
32 attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://www.thunderforest.com/" target="_blank">Andy Allan</a>'
36 L.OSM.HOT = L.OSM.TileLayer.extend({
38 url: 'https://tile-{s}.openstreetmap.fr/hot/{z}/{x}/{y}.png',
41 attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://hot.openstreetmap.org/" target="_blank">Humanitarian OpenStreetMap Team</a>'
45 L.OSM.GPS = L.OSM.TileLayer.extend({
47 url: 'https://gps-{s}.tile.openstreetmap.org/lines/{z}/{x}/{y}.png',
53 L.OSM.DataLayer = L.FeatureGroup.extend({
55 areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
56 uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
60 initialize: function (xml, options) {
61 L.Util.setOptions(this, options);
63 L.FeatureGroup.prototype.initialize.call(this);
70 addData: function (features) {
71 if (!(features instanceof Array)) {
72 features = this.buildFeatures(features);
75 for (var i = 0; i < features.length; i++) {
76 var feature = features[i], layer;
78 if (feature.type === "changeset") {
79 layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
80 } else if (feature.type === "node") {
81 layer = L.circleMarker(feature.latLng, this.options.styles.node);
83 var latLngs = new Array(feature.nodes.length);
85 for (var j = 0; j < feature.nodes.length; j++) {
86 latLngs[j] = feature.nodes[j].latLng;
89 if (this.isWayArea(feature)) {
90 latLngs.pop(); // Remove last == first.
91 layer = L.polygon(latLngs, this.options.styles.area);
93 layer = L.polyline(latLngs, this.options.styles.way);
98 layer.feature = feature;
102 buildFeatures: function (xml) {
103 var features = L.OSM.getChangesets(xml),
104 nodes = L.OSM.getNodes(xml),
105 ways = L.OSM.getWays(xml, nodes),
106 relations = L.OSM.getRelations(xml, nodes, ways);
108 for (var node_id in nodes) {
109 var node = nodes[node_id];
110 if (this.interestingNode(node, ways, relations)) {
115 for (var i = 0; i < ways.length; i++) {
123 isWayArea: function (way) {
124 if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
128 for (var key in way.tags) {
129 if (~this.options.areaTags.indexOf(key)) {
137 interestingNode: function (node, ways, relations) {
140 for (var i = 0; i < ways.length; i++) {
141 if (ways[i].nodes.indexOf(node) >= 0) {
151 for (var i = 0; i < relations.length; i++) {
152 if (relations[i].members.indexOf(node) >= 0)
156 for (var key in node.tags) {
157 if (this.options.uninterestingTags.indexOf(key) < 0) {
166 L.Util.extend(L.OSM, {
167 getChangesets: function (xml) {
170 var nodes = xml.getElementsByTagName("changeset");
171 for (var i = 0; i < nodes.length; i++) {
172 var node = nodes[i], id = node.getAttribute("id");
176 latLngBounds: L.latLngBounds(
177 [node.getAttribute("min_lat"), node.getAttribute("min_lon")],
178 [node.getAttribute("max_lat"), node.getAttribute("max_lon")]),
179 tags: this.getTags(node)
186 getNodes: function (xml) {
189 var nodes = xml.getElementsByTagName("node");
190 for (var i = 0; i < nodes.length; i++) {
191 var node = nodes[i], id = node.getAttribute("id");
195 latLng: L.latLng(node.getAttribute("lat"),
196 node.getAttribute("lon"),
198 tags: this.getTags(node)
205 getWays: function (xml, nodes) {
208 var ways = xml.getElementsByTagName("way");
209 for (var i = 0; i < ways.length; i++) {
210 var way = ways[i], nds = way.getElementsByTagName("nd");
213 id: way.getAttribute("id"),
215 nodes: new Array(nds.length),
216 tags: this.getTags(way)
219 for (var j = 0; j < nds.length; j++) {
220 way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
223 result.push(way_object);
229 getRelations: function (xml, nodes, ways) {
232 var rels = xml.getElementsByTagName("relation");
233 for (var i = 0; i < rels.length; i++) {
234 var rel = rels[i], members = rel.getElementsByTagName("member");
237 id: rel.getAttribute("id"),
239 members: new Array(members.length),
240 tags: this.getTags(rel)
243 for (var j = 0; j < members.length; j++) {
244 if (members[j].getAttribute("type") === "node")
245 rel_object.members[j] = nodes[members[j].getAttribute("ref")];
246 else // relation-way and relation-relation membership not implemented
247 rel_object.members[j] = null;
250 result.push(rel_object);
256 getTags: function (xml) {
259 var tags = xml.getElementsByTagName("tag");
260 for (var j = 0; j < tags.length; j++) {
261 result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");