Skip to content

Commit 0312f51

Browse files
authored
Merge pull request #6778 from plotly/more-stamen-setup
additional setup needed to pass api key for `stamen` styles
2 parents 9156bd7 + 7f0d263 commit 0312f51

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

Diff for: draftlogs/6776_change.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
- Change stamen styles to point to stadiamaps.com, please note that in addition
2-
the users now need to provide their own API_KEY via MAPBOX_ACCESS_TOKEN
2+
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)]

Diff for: src/plots/mapbox/constants.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ var stylesNonMapbox = {
110110
'plotly-stamen-terrain': {
111111
type: 'raster',
112112
attribution: stamenTerrainOrToner,
113-
tiles: ['https://tiles.stadiamaps.com/tiles/stamen_terrain/{z}/{x}/{y}.png'],
113+
tiles: ['https://tiles.stadiamaps.com/tiles/stamen_terrain/{z}/{x}/{y}.png?api_key='],
114114
tileSize: 256
115115
}
116116
},
@@ -130,7 +130,7 @@ var stylesNonMapbox = {
130130
'plotly-stamen-toner': {
131131
type: 'raster',
132132
attribution: stamenTerrainOrToner,
133-
tiles: ['https://tiles.stadiamaps.com/tiles/stamen_toner/{z}/{x}/{y}.png'],
133+
tiles: ['https://tiles.stadiamaps.com/tiles/stamen_toner/{z}/{x}/{y}.png?api_key='],
134134
tileSize: 256
135135
}
136136
},
@@ -150,7 +150,7 @@ var stylesNonMapbox = {
150150
'plotly-stamen-watercolor': {
151151
type: 'raster',
152152
attribution: stamenWaterColor,
153-
tiles: ['https://tiles.stadiamaps.com/tiles/stamen_watercolor/{z}/{x}/{y}.jpg'],
153+
tiles: ['https://tiles.stadiamaps.com/tiles/stamen_watercolor/{z}/{x}/{y}.jpg?api_key='],
154154
tileSize: 256
155155
}
156156
},

Diff for: src/plots/mapbox/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ function findAccessToken(gd, mapboxIds) {
222222
var opts = fullLayout[mapboxIds[i]];
223223
var token = opts.accesstoken;
224224

225-
if(isMapboxStyle(opts.style)) {
225+
if(isStyleRequireAccessToken(opts.style)) {
226226
if(token) {
227227
Lib.pushUnique(tokensUseful, token);
228228
} else {
229-
if(isMapboxStyle(opts._input.style)) {
229+
if(isStyleRequireAccessToken(opts._input.style)) {
230230
Lib.error('Uses Mapbox map style, but did not set an access token.');
231231
hasOneSetMapboxStyle = true;
232232
}
@@ -263,10 +263,11 @@ function findAccessToken(gd, mapboxIds) {
263263
}
264264
}
265265

266-
function isMapboxStyle(s) {
266+
function isStyleRequireAccessToken(s) {
267267
return typeof s === 'string' && (
268268
constants.styleValuesMapbox.indexOf(s) !== -1 ||
269-
s.indexOf('mapbox://') === 0
269+
s.indexOf('mapbox://') === 0 ||
270+
s.indexOf('stamen') === 0
270271
);
271272
}
272273

Diff for: src/plots/mapbox/mapbox.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ proto.createMap = function(calcData, fullLayout, resolve, reject) {
8686
var opts = fullLayout[self.id];
8787

8888
// store style id and URL or object
89-
var styleObj = self.styleObj = getStyleObj(opts.style);
89+
var styleObj = self.styleObj = getStyleObj(opts.style, fullLayout);
9090

9191
// store access token associated with this map
9292
self.accessToken = opts.accesstoken;
@@ -152,7 +152,7 @@ proto.updateMap = function(calcData, fullLayout, resolve, reject) {
152152
self.rejectOnError(reject);
153153

154154
var promises = [];
155-
var styleObj = getStyleObj(opts.style);
155+
var styleObj = getStyleObj(opts.style, fullLayout);
156156

157157
if(JSON.stringify(self.styleObj) !== JSON.stringify(styleObj)) {
158158
self.styleObj = styleObj;
@@ -768,7 +768,7 @@ proto.getViewEditsWithDerived = function(cont) {
768768
return obj;
769769
};
770770

771-
function getStyleObj(val) {
771+
function getStyleObj(val, fullLayout) {
772772
var styleObj = {};
773773

774774
if(Lib.isPlainObject(val)) {
@@ -781,6 +781,16 @@ function getStyleObj(val) {
781781
styleObj.style = convertStyleVal(val);
782782
} else if(constants.stylesNonMapbox[val]) {
783783
styleObj.style = constants.stylesNonMapbox[val];
784+
var spec = styleObj.style.sources['plotly-' + val];
785+
var tiles = spec ? spec.tiles : undefined;
786+
if(
787+
tiles &&
788+
tiles[0] &&
789+
tiles[0].slice(-9) === '?api_key='
790+
) {
791+
// provide api_key for stamen styles
792+
tiles[0] += fullLayout._mapboxAccessToken;
793+
}
784794
} else {
785795
styleObj.style = val;
786796
}

Diff for: test/image/make_baseline.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
blacklist = [
7979
'mapbox_density0-legend',
8080
'mapbox_osm-style',
81-
'mapbox_stamen-style', # used to pass before 2023 Jun 20
81+
'mapbox_stamen-style', # Could pass by setting mapboxAccessToken to a stadiamaps.com token
8282
'mapbox_custom-style' # Figure out why needed this in https://github.com/plotly/plotly.js/pull/6610
8383
]
8484
allNames = [a for a in allNames if a not in blacklist]

0 commit comments

Comments
 (0)