3 L.OSM.TileLayer = L.TileLayer.extend({
5 url: 'https://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}',
26 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>'
30 L.OSM.TransportMap = L.OSM.TileLayer.extend({
32 url: 'https://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}{r}.png?apikey={apikey}',
34 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>'
38 L.OSM.HOT = L.OSM.TileLayer.extend({
40 url: 'https://tile-{s}.openstreetmap.fr/hot/{z}/{x}/{y}.png',
43 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>'
47 L.OSM.GPS = L.OSM.TileLayer.extend({
49 url: 'https://gps.tile.openstreetmap.org/lines/{z}/{x}/{y}.png',
56 L.OSM.DataLayer = L.FeatureGroup.extend({
58 areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
59 uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
63 initialize: function (xml, options) {
64 L.Util.setOptions(this, options);
66 L.FeatureGroup.prototype.initialize.call(this);
73 addData: function (features) {
74 if (!(features instanceof Array)) {
75 features = this.buildFeatures(features);
78 for (var i = 0; i < features.length; i++) {
79 var feature = features[i], layer;
81 if (feature.type === "changeset") {
82 layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
83 } else if (feature.type === "node") {
84 layer = L.circleMarker(feature.latLng, this.options.styles.node);
86 var latLngs = new Array(feature.nodes.length);
88 for (var j = 0; j < feature.nodes.length; j++) {
89 latLngs[j] = feature.nodes[j].latLng;
92 if (this.isWayArea(feature)) {
93 latLngs.pop(); // Remove last == first.
94 layer = L.polygon(latLngs, this.options.styles.area);
96 layer = L.polyline(latLngs, this.options.styles.way);
101 layer.feature = feature;
105 buildFeatures: function (xml) {
106 var features = L.OSM.getChangesets(xml),
107 nodes = L.OSM.getNodes(xml),
108 ways = L.OSM.getWays(xml, nodes),
109 relations = L.OSM.getRelations(xml, nodes, ways);
111 for (var node_id in nodes) {
112 var node = nodes[node_id];
113 if (this.interestingNode(node, ways, relations)) {
118 for (var i = 0; i < ways.length; i++) {
126 isWayArea: function (way) {
127 if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
131 for (var key in way.tags) {
132 if (~this.options.areaTags.indexOf(key)) {
140 interestingNode: function (node, ways, relations) {
143 for (var i = 0; i < ways.length; i++) {
144 if (ways[i].nodes.indexOf(node) >= 0) {
154 for (var i = 0; i < relations.length; i++) {
155 if (relations[i].members.indexOf(node) >= 0)
159 for (var key in node.tags) {
160 if (this.options.uninterestingTags.indexOf(key) < 0) {
169 L.Util.extend(L.OSM, {
170 getChangesets: function (xml) {
173 var nodes = xml.getElementsByTagName("changeset");
174 for (var i = 0; i < nodes.length; i++) {
175 var node = nodes[i], id = node.getAttribute("id");
179 latLngBounds: L.latLngBounds(
180 [node.getAttribute("min_lat"), node.getAttribute("min_lon")],
181 [node.getAttribute("max_lat"), node.getAttribute("max_lon")]),
182 tags: this.getTags(node)
189 getNodes: function (xml) {
192 var nodes = xml.getElementsByTagName("node");
193 for (var i = 0; i < nodes.length; i++) {
194 var node = nodes[i], id = node.getAttribute("id");
198 latLng: L.latLng(node.getAttribute("lat"),
199 node.getAttribute("lon"),
201 tags: this.getTags(node)
208 getWays: function (xml, nodes) {
211 var ways = xml.getElementsByTagName("way");
212 for (var i = 0; i < ways.length; i++) {
213 var way = ways[i], nds = way.getElementsByTagName("nd");
216 id: way.getAttribute("id"),
218 nodes: new Array(nds.length),
219 tags: this.getTags(way)
222 for (var j = 0; j < nds.length; j++) {
223 way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
226 result.push(way_object);
232 getRelations: function (xml, nodes, ways) {
235 var rels = xml.getElementsByTagName("relation");
236 for (var i = 0; i < rels.length; i++) {
237 var rel = rels[i], members = rel.getElementsByTagName("member");
240 id: rel.getAttribute("id"),
242 members: new Array(members.length),
243 tags: this.getTags(rel)
246 for (var j = 0; j < members.length; j++) {
247 if (members[j].getAttribute("type") === "node")
248 rel_object.members[j] = nodes[members[j].getAttribute("ref")];
249 else // relation-way and relation-relation membership not implemented
250 rel_object.members[j] = null;
253 result.push(rel_object);
259 getTags: function (xml) {
262 var tags = xml.getElementsByTagName("tag");
263 for (var j = 0; j < tags.length; j++) {
264 result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");