Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Feat] Hide Mapbox attribution when using non-Mapbox tiles (#1975)

Igor Dykhta9029b8ea3f05
diff --git a/src/components/src/map-container.tsx b/src/components/src/map-container.tsx
index 582f3fc0e..c6898546d 100644
--- a/src/components/src/map-container.tsx
+++ b/src/components/src/map-container.tsx
@@ -46,6 +46,7 @@ import {MapState, MapControls, Viewport, SplitMap, SplitMapLayers} from '@kepler
import {
errorNotification,
setLayerBlending,
+ isStyleUsingMapboxTiles,
transformRequest,
observeDimensions,
unobserveDimensions,
@@ -104,8 +105,19 @@ const MapboxLogo = () => (
/>
</div>
);
-export const Attribution = () => {
+
+export const Attribution = ({showMapboxLogo = true}) => {
const isPalm = hasMobileWidth(breakPointValues);
+ if (!showMapboxLogo) {
+ return (
+ <StyledAttrbution>
+ <a href="http://www.openstreetmap.org/copyright" target="_blank" rel="noopener noreferrer">
+ © OpenStreetMap
+ </a>
+ </StyledAttrbution>
+ );
+ }
+
return (
<StyledAttrbution>
{isPalm ? <MapboxLogo /> : null}
@@ -180,6 +192,11 @@ export default function MapContainerFactory(
primary: true
};
+ state = {
+ // Determines whether attribution should be visible based the result of loading the map style
+ showMapboxAttribution: true
+ };
+
constructor(props) {
super(props);
}
@@ -272,11 +289,16 @@ export default function MapContainerFactory(
visStateActions.toggleLayerForMap(mapIndex, layerId);
};
- _onMapboxStyleUpdate = () => {
+ _onMapboxStyleUpdate = update => {
// force refresh mapboxgl layers
this.previousLayers = {};
this._updateMapboxLayers();
+ if (update && update.style) {
+ // No attributions are needed if the style doesn't reference Mapbox sources
+ this.setState({showMapboxAttribution: isStyleUsingMapboxTiles(update.style)});
+ }
+
if (typeof this.props.onMapStyleLoaded === 'function') {
this.props.onMapStyleLoaded(this._map);
}
@@ -641,7 +663,9 @@ export default function MapContainerFactory(
</div>
) : null}
{this._renderMapPopover()}
- {!isSplit || index === 1 ? <Attribution /> : null}
+ {!isSplit || index === 1 ? (
+ <Attribution showMapboxLogo={this.state.showMapboxAttribution} />
+ ) : null}
</>
);
}
diff --git a/src/utils/src/index.ts b/src/utils/src/index.ts
index 9f4b710c4..00d1e192e 100644
--- a/src/utils/src/index.ts
+++ b/src/utils/src/index.ts
@@ -154,7 +154,7 @@ export {
} from './split-map-utils';
// Mapbox
-export {transformRequest} from './map-style-utils/mapbox-utils';
+export {transformRequest, isStyleUsingMapboxTiles} from './map-style-utils/mapbox-utils';
// Map
export {onViewPortChange, getMapLayersFromSplitMaps} from './map-utils';
diff --git a/src/utils/src/map-style-utils/mapbox-utils.ts b/src/utils/src/map-style-utils/mapbox-utils.ts
index 9651bb9d8..ee93173e7 100644
--- a/src/utils/src/map-style-utils/mapbox-utils.ts
+++ b/src/utils/src/map-style-utils/mapbox-utils.ts
@@ -18,6 +18,29 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
+/**
+ * Determines whether a Map Style is using Mapbox Tiles
+ * @param {any} mapStyle the mapStyle to check
+ * @returns true if the style is using Mapbox tiles
+ */
+export function isStyleUsingMapboxTiles(mapStyle) {
+ const sources = mapStyle.stylesheet?.sources || {};
+
+ return Object.keys(sources).some(sourceId => {
+ const {url, tiles} = sources[sourceId] || {};
+
+ if (url) {
+ return url.toLowerCase().startsWith('mapbox://');
+ }
+
+ if (tiles) {
+ return tiles.some(tileUrl => tileUrl.toLowerCase().startsWith('mapbox://'));
+ }
+
+ return false;
+ });
+}
+
export const transformRequest = (url: string): {url: string} => {
const isMapboxRequest =
url.slice(8, 22) === 'api.mapbox.com' || url.slice(10, 26) === 'tiles.mapbox.com';
diff --git a/test/node/utils/index.js b/test/node/utils/index.js
index 8b140f646..2ddd9b2d5 100644
--- a/test/node/utils/index.js
+++ b/test/node/utils/index.js
@@ -28,6 +28,7 @@ import './layer-utils-test';
import './data-scale-utils-test';
import './interaction-utils-test';
import './mapbox-gl-style-editor-test';
+import './mapbox-utils-test';
import './notifications-utils-test';
import './aggregate-utils-test';
import './color-util-test';
diff --git a/test/node/utils/mapbox-utils-test.js b/test/node/utils/mapbox-utils-test.js
new file mode 100644
index 000000000..8722d3c70
--- /dev/null
+++ b/test/node/utils/mapbox-utils-test.js
@@ -0,0 +1,53 @@
+// Copyright (c) 2022 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import test from 'tape';
+import {isStyleUsingMapboxTiles} from '../../../src/utils';
+
+test('mapbox-utils -> isStyleUsingMapboxTiles', t => {
+ t.notOk(isStyleUsingMapboxTiles({}), 'Empty style does not reference Mapbox');
+ t.notOk(
+ isStyleUsingMapboxTiles({stylesheet: {sources: {a: {}}}}),
+ 'Source does not reference Mapbox'
+ );
+ t.ok(
+ isStyleUsingMapboxTiles({
+ stylesheet: {
+ sources: {
+ a: {url: 'some/url'},
+ b: {url: 'mapbox://mapbox-style.json'}
+ }
+ }
+ }),
+ 'Source references Mapbox tiles using "url"'
+ );
+ t.ok(
+ isStyleUsingMapboxTiles({
+ stylesheet: {
+ sources: {
+ a: {url: 'some/url'},
+ b: {tiles: ['mapbox://mapbox-style.json']}
+ }
+ }
+ }),
+ 'Source references Mapbox tiles using "tiles"'
+ );
+ t.end();
+});