From ab54580820189ca1b3e5433e53f0719f6794b613 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Thu, 28 Dec 2023 11:27:25 +0100 Subject: [PATCH] Fix preview of TMS TileLayer preview This is not the ideal fix, instead we'd prefer to use `TileLayer.getTileUrl`, but this is not possible yet. cf https://github.com/Leaflet/Leaflet/pull/9201 cf https://lists.openstreetmap.org/pipermail/umap/2023-December/000557.html --- umap/static/umap/js/umap.controls.js | 20 +++++++++++++++----- umap/static/umap/js/umap.js | 7 ++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index ab25569b..d6c4cb90 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -1199,11 +1199,21 @@ L.U.TileLayerControl = L.Control.IconLayers.extend({ if (!layers) { layers = [] this.map.eachTileLayer((layer) => { - layers.push({ - title: layer.options.name, - layer: layer, - icon: L.Util.template(layer.options.url_template, this.map.demoTileInfos), - }) + try { + // We'd like to use layer.getTileUrl, but this method will only work + // when the tilelayer is actually added to the map (needs this._tileZoom + // to be defined) + // Fixme when https://github.com/Leaflet/Leaflet/pull/9201 is released + const icon = L.Util.template(layer.options.url_template, this.map.demoTileInfos) + layers.push({ + title: layer.options.name, + layer: layer, + icon: icon, + }) + } catch (e) { + // Skip this tilelayer + console.error(e) + } }) } const maxShown = 10 diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index add161cf..be0766f7 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -29,7 +29,12 @@ L.Map.mergeOptions({ name: '', description: '', displayPopupFooter: false, - demoTileInfos: { s: 'a', z: 9, x: 265, y: 181, r: '' }, + // When a TileLayer is in TMS mode, it needs -y instead of y. + // This is usually handled by the TileLayer instance itself, but + // we cannot rely on this because of the y is overriden by Leaflet + // See https://github.com/Leaflet/Leaflet/pull/9201 + // And let's remove this -y when this PR is merged and released. + demoTileInfos: { s: 'a', z: 9, x: 265, y: 181, '-y': 181, r: '' }, licences: [], licence: '', enableMarkerDraw: true,