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',
 
  22 L.OSM.CyclOSM = L.OSM.TileLayer.extend({
 
  24     url: 'https://{s}.tile-cyclosm.openstreetmap.fr/cyclosm/{z}/{x}/{y}.png',
 
  27     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>'
 
  31 L.OSM.CycleMap = L.OSM.TileLayer.extend({
 
  33     url: 'https://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}{r}.png?apikey={apikey}',
 
  35     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>'
 
  39 L.OSM.TransportMap = L.OSM.TileLayer.extend({
 
  41     url: 'https://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}{r}.png?apikey={apikey}',
 
  43     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>'
 
  47 L.OSM.TransportDarkMap = L.OSM.TileLayer.extend({
 
  49     url: 'https://{s}.tile.thunderforest.com/transport-dark/{z}/{x}/{y}{r}.png?apikey={apikey}',
 
  51     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>'
 
  55 L.OSM.OPNVKarte = L.OSM.TileLayer.extend({
 
  57     url: 'https://tileserver.memomaps.de/tilegen/{z}/{x}/{y}.png',
 
  59     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>'
 
  63 L.OSM.HOT = L.OSM.TileLayer.extend({
 
  65     url: 'https://tile-{s}.openstreetmap.fr/hot/{z}/{x}/{y}.png',
 
  68     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>'
 
  72 L.OSM.TracestrackTopo = L.OSM.TileLayer.extend({
 
  74     url: 'https://tile.tracestrack.com/topo__/{z}/{x}/{y}.webp?key={apikey}',
 
  76     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>'
 
  80 L.OSM.GPS = L.OSM.TileLayer.extend({
 
  82     url: 'https://gps.tile.openstreetmap.org/lines/{z}/{x}/{y}.png',
 
  89 L.OSM.DataLayer = L.FeatureGroup.extend({
 
  91     areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
 
  92     uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
 
  97   initialize: function (xml, options) {
 
  98     L.Util.setOptions(this, options);
 
 100     L.FeatureGroup.prototype.initialize.call(this);
 
 109   cancelLoading: function () {
 
 110     this.loadingLayers.forEach(layer => clearTimeout(layer));
 
 111     this.loadingLayers = [];
 
 114   addData: function (features) {
 
 115     if (!(features instanceof Array)) {
 
 116       features = this.buildFeatures(features);
 
 119     for (var i = 0; i < features.length; i++) {
 
 120       let feature = features[i], layer;
 
 122       if (feature.type === "changeset") {
 
 123         layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
 
 124       } else if (feature.type === "node") {
 
 125         layer = L.circleMarker(feature.latLng, this.options.styles.node);
 
 127         var latLngs = new Array(feature.nodes.length);
 
 129         for (var j = 0; j < feature.nodes.length; j++) {
 
 130           latLngs[j] = feature.nodes[j].latLng;
 
 133         if (this.isWayArea(feature)) {
 
 134           latLngs.pop(); // Remove last == first.
 
 135           layer = L.polygon(latLngs, this.options.styles.area);
 
 137           layer = L.polyline(latLngs, this.options.styles.way);
 
 141       if (this.options.asynchronous) {
 
 142         setTimeout(() => layer.addTo(this));
 
 147       layer.feature = feature;
 
 151   buildFeatures: function (data) {
 
 153     const parser = data instanceof Document ? L.OSM.XMLParser : L.OSM.JSONParser;
 
 155     var features = parser.getChangesets(data),
 
 156         nodes = parser.getNodes(data),
 
 157         ways = parser.getWays(data, nodes),
 
 158         relations = parser.getRelations(data, nodes, ways);
 
 161     for (var i = 0; i < ways.length; i++) {
 
 163       for (var j = 0; j < way.nodes.length; j++) {
 
 164         wayNodes[way.nodes[j].id] = true
 
 168     var relationNodes = {}
 
 169     for (var i = 0; i < relations.length; i++){
 
 170       var relation = relations[i];
 
 171       for (var j = 0; j < relation.members.length; j++) {
 
 172         relationNodes[relation.members[j].id] = true
 
 176     for (var node_id in nodes) {
 
 177       var node = nodes[node_id];
 
 178       if (this.interestingNode(node, wayNodes, relationNodes)) {
 
 183     for (var i = 0; i < ways.length; i++) {
 
 191   isWayArea: function (way) {
 
 192     if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
 
 196     for (var key in way.tags) {
 
 197       if (~this.options.areaTags.indexOf(key)) {
 
 205   interestingNode: function (node, wayNodes, relationNodes) {
 
 206     if (!wayNodes[node.id] || relationNodes[node.id]) {
 
 210     for (var key in node.tags) {
 
 211       if (this.options.uninterestingTags.indexOf(key) < 0) {
 
 219   onRemove: function(map) {
 
 220     this.eachLayer(map.removeLayer, map, this.options.asynchronous);
 
 223   onAdd: function(map) {
 
 224     this.eachLayer(map.addLayer, map, this.options.asynchronous);
 
 227   eachLayer: function (method, context, asynchronous = false) {
 
 228     for (let i in this._layers) {
 
 230         this.loadingLayers.push(
 
 232             method.call(context, this._layers[i]);
 
 236         method.call(context, this._layers[i]);
 
 245     const changesets = [...xml.getElementsByTagName("changeset")];
 
 246     return changesets.map(cs => ({
 
 247       id: String(cs.getAttribute("id")),
 
 249       latLngBounds: L.latLngBounds(
 
 250         [cs.getAttribute("min_lat"), cs.getAttribute("min_lon")],
 
 251         [cs.getAttribute("max_lat"), cs.getAttribute("max_lon")]
 
 253       tags: this.getTags(cs)
 
 259     const nodes = [...xml.getElementsByTagName("node")];
 
 261     for (const node of nodes) {
 
 262       const id = node.getAttribute("id");
 
 267           node.getAttribute("lat"),
 
 268           node.getAttribute("lon"),
 
 271         tags: this.getTags(node)
 
 278   getWays(xml, nodes) {
 
 279     const ways = [...xml.getElementsByTagName("way")];
 
 280     return ways.map(way => {
 
 281       const nds = [...way.getElementsByTagName("nd")];
 
 283         id: String(way.getAttribute("id")),
 
 285         nodes: nds.map(nd => nodes[nd.getAttribute("ref")]),
 
 286         tags: this.getTags(way)
 
 291   getRelations(xml, nodes, ways) {
 
 292     const rels = [...xml.getElementsByTagName("relation")];
 
 293     return rels.map(rel => {
 
 294       const members = [...rel.getElementsByTagName("member")];
 
 296         id: String(rel.getAttribute("id")),
 
 298         members: members    // relation-way and relation-relation membership not implemented
 
 299           .map(member => member.getAttribute("type") === "node" ? nodes[member.getAttribute("ref")] : null)
 
 300           .filter(Boolean),    // filter out null and undefined
 
 301         tags: this.getTags(rel)
 
 308     const tags = [...xml.getElementsByTagName("tag")];
 
 310     for (const tag of tags) {
 
 311       result[tag.getAttribute("k")] = tag.getAttribute("v");
 
 319   getChangesets(json) {
 
 320     const changesets = json.changeset ? [json.changeset] : [];
 
 322     return changesets.map(cs => ({
 
 325       latLngBounds: L.latLngBounds(
 
 326         [cs.min_lat, cs.min_lon],
 
 327         [cs.max_lat, cs.max_lon]
 
 329       tags: this.getTags(cs)
 
 334     const nodes = json.elements?.filter(el => el.type === "node") ?? [];
 
 337     for (const node of nodes) {
 
 341         latLng: L.latLng(node.lat, node.lon, true),
 
 342         tags: this.getTags(node)
 
 349   getWays(json, nodes) {
 
 350     const ways = json.elements?.filter(el => el.type === "way") ?? [];
 
 352     return ways.map(way => ({
 
 355       nodes: way.nodes.map(nodeId => nodes[nodeId]),
 
 356       tags: this.getTags(way)
 
 360   getRelations(json, nodes, ways) {
 
 361     const relations = json.elements?.filter(el => el.type === "relation") ?? [];
 
 363     return relations.map(rel => ({
 
 366       members: (rel.members ?? [])   // relation-way and relation-relation membership not implemented
 
 367         .map(member => member.type === "node" ? nodes[member.ref] : null)
 
 368         .filter(Boolean),     // filter out null and undefined
 
 369       tags: this.getTags(rel)
 
 374     return json.tags ?? {};