Skip to content

Commit

Permalink
Merge pull request #6778 from plotly/more-stamen-setup
Browse files Browse the repository at this point in the history
additional setup needed to pass api key for `stamen` styles
  • Loading branch information
archmoj authored Nov 8, 2023
2 parents 9156bd7 + 7f0d263 commit 0312f51
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion draftlogs/6776_change.md
Original file line number Diff line number Diff line change
@@ -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)]
6 changes: 3 additions & 3 deletions src/plots/mapbox/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
Expand All @@ -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
}
},
Expand All @@ -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
}
},
Expand Down
9 changes: 5 additions & 4 deletions src/plots/mapbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
);
}

Expand Down
16 changes: 13 additions & 3 deletions src/plots/mapbox/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -768,7 +768,7 @@ proto.getViewEditsWithDerived = function(cont) {
return obj;
};

function getStyleObj(val) {
function getStyleObj(val, fullLayout) {
var styleObj = {};

if(Lib.isPlainObject(val)) {
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion test/image/make_baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 0312f51

Please sign in to comment.