From 306732e973a815ed6913231cbce64c511ef67525 Mon Sep 17 00:00:00 2001 From: Mojtaba Samimi Date: Wed, 8 Nov 2023 11:51:39 -0500 Subject: [PATCH 1/2] additional setup needed to pass api key for stamen styles --- src/plots/mapbox/constants.js | 6 +++--- src/plots/mapbox/index.js | 9 +++++---- src/plots/mapbox/mapbox.js | 16 +++++++++++++--- test/image/make_baseline.py | 2 +- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/plots/mapbox/constants.js b/src/plots/mapbox/constants.js index 6b6ab1ae8fe..d26430d721f 100644 --- a/src/plots/mapbox/constants.js +++ b/src/plots/mapbox/constants.js @@ -110,7 +110,7 @@ var stylesNonMapbox = { 'plotly-stamen-terrain': { type: 'raster', attribution: stamenTerrainOrToner, - tiles: ['https://tiles.stadiamaps.com/tiles/stamen_terrain/{z}/{x}/{y}.png'], + tiles: ['https://tiles.stadiamaps.com/tiles/stamen_terrain/{z}/{x}/{y}.png?api_key='], tileSize: 256 } }, @@ -130,7 +130,7 @@ var stylesNonMapbox = { 'plotly-stamen-toner': { type: 'raster', attribution: stamenTerrainOrToner, - tiles: ['https://tiles.stadiamaps.com/tiles/stamen_toner/{z}/{x}/{y}.png'], + tiles: ['https://tiles.stadiamaps.com/tiles/stamen_toner/{z}/{x}/{y}.png?api_key='], tileSize: 256 } }, @@ -150,7 +150,7 @@ var stylesNonMapbox = { 'plotly-stamen-watercolor': { type: 'raster', attribution: stamenWaterColor, - tiles: ['https://tiles.stadiamaps.com/tiles/stamen_watercolor/{z}/{x}/{y}.jpg'], + tiles: ['https://tiles.stadiamaps.com/tiles/stamen_watercolor/{z}/{x}/{y}.jpg?api_key='], tileSize: 256 } }, diff --git a/src/plots/mapbox/index.js b/src/plots/mapbox/index.js index 3048a40e334..15430dfaac9 100644 --- a/src/plots/mapbox/index.js +++ b/src/plots/mapbox/index.js @@ -222,11 +222,11 @@ function findAccessToken(gd, mapboxIds) { var opts = fullLayout[mapboxIds[i]]; var token = opts.accesstoken; - if(isMapboxStyle(opts.style)) { + if(isStyleRequireAccessToken(opts.style)) { if(token) { Lib.pushUnique(tokensUseful, token); } else { - if(isMapboxStyle(opts._input.style)) { + if(isStyleRequireAccessToken(opts._input.style)) { Lib.error('Uses Mapbox map style, but did not set an access token.'); hasOneSetMapboxStyle = true; } @@ -263,10 +263,11 @@ function findAccessToken(gd, mapboxIds) { } } -function isMapboxStyle(s) { +function isStyleRequireAccessToken(s) { return typeof s === 'string' && ( constants.styleValuesMapbox.indexOf(s) !== -1 || - s.indexOf('mapbox://') === 0 + s.indexOf('mapbox://') === 0 || + s.indexOf('stamen') === 0 ); } diff --git a/src/plots/mapbox/mapbox.js b/src/plots/mapbox/mapbox.js index 061495d0702..f1e3bb7106f 100644 --- a/src/plots/mapbox/mapbox.js +++ b/src/plots/mapbox/mapbox.js @@ -86,7 +86,7 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) { var opts = fullLayout[self.id]; // store style id and URL or object - var styleObj = self.styleObj = getStyleObj(opts.style); + var styleObj = self.styleObj = getStyleObj(opts.style, fullLayout); // store access token associated with this map self.accessToken = opts.accesstoken; @@ -152,7 +152,7 @@ proto.updateMap = function(calcData, fullLayout, resolve, reject) { self.rejectOnError(reject); var promises = []; - var styleObj = getStyleObj(opts.style); + var styleObj = getStyleObj(opts.style, fullLayout); if(JSON.stringify(self.styleObj) !== JSON.stringify(styleObj)) { self.styleObj = styleObj; @@ -768,7 +768,7 @@ proto.getViewEditsWithDerived = function(cont) { return obj; }; -function getStyleObj(val) { +function getStyleObj(val, fullLayout) { var styleObj = {}; if(Lib.isPlainObject(val)) { @@ -781,6 +781,16 @@ function getStyleObj(val) { styleObj.style = convertStyleVal(val); } else if(constants.stylesNonMapbox[val]) { styleObj.style = constants.stylesNonMapbox[val]; + var spec = styleObj.style.sources['plotly-' + val]; + var tiles = spec ? spec.tiles : undefined; + if( + tiles && + tiles[0] && + tiles[0].slice(-9) === '?api_key=' + ) { + // provide api_key for stamen styles + tiles[0] += fullLayout._mapboxAccessToken; + } } else { styleObj.style = val; } diff --git a/test/image/make_baseline.py b/test/image/make_baseline.py index 43fa4d39334..3d12090832b 100644 --- a/test/image/make_baseline.py +++ b/test/image/make_baseline.py @@ -78,7 +78,7 @@ blacklist = [ 'mapbox_density0-legend', 'mapbox_osm-style', - 'mapbox_stamen-style', # used to pass before 2023 Jun 20 + 'mapbox_stamen-style', # Could pass by setting mapboxAccessToken to a stadiamaps.com token 'mapbox_custom-style' # Figure out why needed this in https://github.com/plotly/plotly.js/pull/6610 ] allNames = [a for a in allNames if a not in blacklist] From 7f0d263b7827c697d9362a218dd6e8caf8a54493 Mon Sep 17 00:00:00 2001 From: Mojtaba Samimi Date: Wed, 8 Nov 2023 12:41:06 -0500 Subject: [PATCH 2/2] update draft log --- draftlogs/6776_change.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/draftlogs/6776_change.md b/draftlogs/6776_change.md index a3d78e4a41d..c9d4ab04f54 100644 --- a/draftlogs/6776_change.md +++ b/draftlogs/6776_change.md @@ -1,2 +1,2 @@ - Change stamen styles to point to stadiamaps.com, please note that in addition - the users now need to provide their own API_KEY via MAPBOX_ACCESS_TOKEN + the users now need to provide their own API_KEY via MAPBOX_ACCESS_TOKEN [[#6776](https://github.com/plotly/plotly.js/pull/6776), [#6778](https://github.com/plotly/plotly.js/pull/6778)]