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://tile.openstreetmap.org/{z}/{x}/{y}.png',
19 referrerPolicy: 'strict-origin-when-cross-origin'
23 L.OSM.CyclOSM = L.OSM.TileLayer.extend({
25 url: 'https://{s}.tile-cyclosm.openstreetmap.fr/cyclosm/{z}/{x}/{y}.png',
28 attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="https://www.openstreetmap.fr" target="_blank">OpenStreetMap France</a>'
32 L.OSM.CycleMap = L.OSM.TileLayer.extend({
34 url: 'https://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}{r}.png?apikey={apikey}',
36 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>'
40 L.OSM.TransportMap = L.OSM.TileLayer.extend({
42 url: 'https://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}{r}.png?apikey={apikey}',
44 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>'
48 L.OSM.TransportDarkMap = L.OSM.TileLayer.extend({
50 url: 'https://{s}.tile.thunderforest.com/transport-dark/{z}/{x}/{y}{r}.png?apikey={apikey}',
52 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>'
56 L.OSM.OPNVKarte = L.OSM.TileLayer.extend({
58 url: 'https://tileserver.memomaps.de/tilegen/{z}/{x}/{y}.png',
60 attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://memomaps.de/" target="_blank">MeMoMaps</a>'
64 L.OSM.HOT = L.OSM.TileLayer.extend({
66 url: 'https://tile-{s}.openstreetmap.fr/hot/{z}/{x}/{y}.png',
69 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>'
73 L.OSM.TracestrackTopo = L.OSM.TileLayer.extend({
75 url: 'https://tile.tracestrack.com/topo__/{z}/{x}/{y}.webp?key={apikey}',
77 attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="https://www.tracestrack.com/" target="_blank">Tracestrack Maps</a>'
81 L.OSM.GPS = L.OSM.TileLayer.extend({
83 url: 'https://gps.tile.openstreetmap.org/lines/{z}/{x}/{y}.png',
90 L.OSM.DataLayer = L.FeatureGroup.extend({
92 areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
93 uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
98 initialize: function (xml, options) {
99 L.Util.setOptions(this, options);
101 L.FeatureGroup.prototype.initialize.call(this);
110 cancelLoading: function () {
111 this.loadingLayers.forEach(layer => clearTimeout(layer));
112 this.loadingLayers = [];
115 addData: function (features) {
116 if (!(features instanceof Array)) {
117 features = this.buildFeatures(features);
120 for (var i = 0; i < features.length; i++) {
121 let feature = features[i], layer;
123 if (feature.type === "changeset") {
124 layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
125 } else if (feature.type === "node") {
126 layer = L.circleMarker(feature.latLng, this.options.styles.node);
128 var latLngs = new Array(feature.nodes.length);
130 for (var j = 0; j < feature.nodes.length; j++) {
131 latLngs[j] = feature.nodes[j].latLng;
134 if (this.isWayArea(feature)) {
135 latLngs.pop(); // Remove last == first.
136 layer = L.polygon(latLngs, this.options.styles.area);
138 layer = L.polyline(latLngs, this.options.styles.way);
142 if (this.options.asynchronous) {
143 setTimeout(() => layer.addTo(this));
148 layer.feature = feature;
152 buildFeatures: function (data) {
154 const parser = data instanceof Document ? L.OSM.XMLParser : L.OSM.JSONParser;
156 var features = parser.getChangesets(data),
157 nodes = parser.getNodes(data),
158 ways = parser.getWays(data, nodes),
159 relations = parser.getRelations(data, nodes, ways);
162 for (var i = 0; i < ways.length; i++) {
164 for (var j = 0; j < way.nodes.length; j++) {
165 wayNodes[way.nodes[j].id] = true
169 var relationNodes = {}
170 for (var i = 0; i < relations.length; i++){
171 var relation = relations[i];
172 for (var j = 0; j < relation.members.length; j++) {
173 relationNodes[relation.members[j].id] = true
177 for (var node_id in nodes) {
178 var node = nodes[node_id];
179 if (this.interestingNode(node, wayNodes, relationNodes)) {
184 for (var i = 0; i < ways.length; i++) {
192 isWayArea: function (way) {
193 if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
197 for (var key in way.tags) {
198 if (~this.options.areaTags.indexOf(key)) {
206 interestingNode: function (node, wayNodes, relationNodes) {
207 if (!wayNodes[node.id] || relationNodes[node.id]) {
211 for (var key in node.tags) {
212 if (this.options.uninterestingTags.indexOf(key) < 0) {
220 onRemove: function(map) {
221 this.eachLayer(map.removeLayer, map, this.options.asynchronous);
224 onAdd: function(map) {
225 this.eachLayer(map.addLayer, map, this.options.asynchronous);
228 eachLayer: function (method, context, asynchronous = false) {
229 for (let i in this._layers) {
231 this.loadingLayers.push(
233 method.call(context, this._layers[i]);
237 method.call(context, this._layers[i]);
246 const changesets = [...xml.getElementsByTagName("changeset")];
247 return changesets.map(cs => ({
248 id: String(cs.getAttribute("id")),
250 latLngBounds: L.latLngBounds(
251 [cs.getAttribute("min_lat"), cs.getAttribute("min_lon")],
252 [cs.getAttribute("max_lat"), cs.getAttribute("max_lon")]
254 tags: this.getTags(cs)
260 const nodes = [...xml.getElementsByTagName("node")];
262 for (const node of nodes) {
263 const id = node.getAttribute("id");
268 node.getAttribute("lat"),
269 node.getAttribute("lon"),
272 tags: this.getTags(node)
279 getWays(xml, nodes) {
280 const ways = [...xml.getElementsByTagName("way")];
281 return ways.map(way => {
282 const nds = [...way.getElementsByTagName("nd")];
284 id: String(way.getAttribute("id")),
286 nodes: nds.map(nd => nodes[nd.getAttribute("ref")]),
287 tags: this.getTags(way)
292 getRelations(xml, nodes, ways) {
293 const rels = [...xml.getElementsByTagName("relation")];
294 return rels.map(rel => {
295 const members = [...rel.getElementsByTagName("member")];
297 id: String(rel.getAttribute("id")),
299 members: members // relation-way and relation-relation membership not implemented
300 .map(member => member.getAttribute("type") === "node" ? nodes[member.getAttribute("ref")] : null)
301 .filter(Boolean), // filter out null and undefined
302 tags: this.getTags(rel)
309 const tags = [...xml.getElementsByTagName("tag")];
311 for (const tag of tags) {
312 result[tag.getAttribute("k")] = tag.getAttribute("v");
320 getChangesets(json) {
321 const changesets = json.changeset ? [json.changeset] : [];
323 return changesets.map(cs => ({
326 latLngBounds: L.latLngBounds(
327 [cs.min_lat, cs.min_lon],
328 [cs.max_lat, cs.max_lon]
330 tags: this.getTags(cs)
335 const nodes = json.elements?.filter(el => el.type === "node") ?? [];
338 for (const node of nodes) {
342 latLng: L.latLng(node.lat, node.lon, true),
343 tags: this.getTags(node)
350 getWays(json, nodes) {
351 const ways = json.elements?.filter(el => el.type === "way") ?? [];
353 return ways.map(way => ({
356 nodes: way.nodes.map(nodeId => nodes[nodeId]),
357 tags: this.getTags(way)
361 getRelations(json, nodes, ways) {
362 const relations = json.elements?.filter(el => el.type === "relation") ?? [];
364 return relations.map(rel => ({
367 members: (rel.members ?? []) // relation-way and relation-relation membership not implemented
368 .map(member => member.type === "node" ? nodes[member.ref] : null)
369 .filter(Boolean), // filter out null and undefined
370 tags: this.getTags(rel)
375 return json.tags ?? {};