Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(deckgl-map): add tile server layer support #27476

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ import { t } from '../translation';

/**
* Validate a [Mapbox styles URL](https://docs.mapbox.com/help/glossary/style-url/)
* or a tile server URL
* @param v
*/
export default function validateMapboxStylesUrl(v: unknown) {
if (
typeof v === 'string' &&
v.trim().length > 0 &&
v.trim().startsWith('mapbox://styles/')
(v.trim().startsWith('mapbox://styles/') ||
v.trim().startsWith('tile://http'))
) {
return false;
}

return t('is expected to be a Mapbox URL');
return t(
'is expected to be a Mapbox URL (eg. mapbox://styles/...) or a tile server URL (eg. tile://http...)',
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ describe('validateMapboxStylesUrl', () => {
'mapbox://styles/foobar/clp2dr5r4008a01pcg4ad45m8',
),
).toEqual(false);
expect(
validateMapboxStylesUrl(
'tile://https://c.tile.openstreetmap.org/{z}/{x}/{y}.png',
),
).toEqual(false);
});

[
Expand All @@ -40,7 +45,7 @@ describe('validateMapboxStylesUrl', () => {
].forEach(value => {
it(`should not validate ${value}`, () => {
expect(validateMapboxStylesUrl(value)).toEqual(
'is expected to be a Mapbox URL',
'is expected to be a Mapbox URL (eg. mapbox://styles/...) or a tile server URL (eg. tile://http...)',
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"lib"
],
"dependencies": {
"@deck.gl/geo-layers": "^8.8.27",
"@deck.gl/layers": "^8.8.27",
"@mapbox/geojson-extent": "^1.0.1",
"@math.gl/web-mercator": "^3.2.2",
"@types/d3-array": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ import { JsonObject, JsonValue, styled, usePrevious } from '@superset-ui/core';
import Tooltip, { TooltipProps } from './components/Tooltip';
import 'mapbox-gl/dist/mapbox-gl.css';
import { Viewport } from './utils/fitViewport';
import {
buildTileLayer,
TILE_LAYER_PREFIX,
MAPBOX_LAYER_PREFIX,
} from './utils';

const TICK = 250; // milliseconds

Expand Down Expand Up @@ -98,8 +103,13 @@ export const DeckGLContainer = memo(
) as Layer[];
}

if (props.mapStyle?.startsWith(TILE_LAYER_PREFIX)) {
props.layers.unshift(
buildTileLayer(props.mapStyle.replace(TILE_LAYER_PREFIX, '')),
);
}
return props.layers as Layer[];
}, [props.layers]);
}, [props.layers, props.mapStyle]);

const { children = null, height, width } = props;

Expand All @@ -115,11 +125,28 @@ export const DeckGLContainer = memo(
glOptions={{ preserveDrawingBuffer: true }}
onViewStateChange={onViewStateChange}
>
<StaticMap
preserveDrawingBuffer
mapStyle={props.mapStyle || 'light'}
mapboxApiAccessToken={props.mapboxApiAccessToken}
/>
{props.mapStyle?.startsWith(MAPBOX_LAYER_PREFIX) && (
<StaticMap
preserveDrawingBuffer
mapStyle={props.mapStyle || 'light'}
mapboxApiAccessToken={props.mapboxApiAccessToken}
/>
)}
{props.mapStyle?.indexOf('openstreetmap') !== -1 && (
<div
style={{
position: 'absolute',
left: 0,
bottom: 0,
backgroundColor: 'hsla(0,0%,100%,.5)',
padding: '0 5px',
}}
>
<a href="http://www.openstreetmap.org/copyright">
© OpenStreetMap contributors
</a>
</div>
)}
</DeckGL>
{children}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,14 @@ export const mapboxStyle = {
['mapbox://styles/mapbox/satellite-streets-v9', t('Satellite Streets')],
['mapbox://styles/mapbox/satellite-v9', t('Satellite')],
['mapbox://styles/mapbox/outdoors-v9', t('Outdoors')],
[
'tile://https://c.tile.openstreetmap.org/{z}/{x}/{y}.png',
t('OpenStreetMap'),
],
],
default: 'mapbox://styles/mapbox/light-v9',
description: t(
'Base layer map style. See Mapbox documentation: %s',
'Mapbox base layer map style (see Mapbox documentation: %s) or tile server URL.',
'https://docs.mapbox.com/help/glossary/style-url/',
),
},
Expand Down
26 changes: 26 additions & 0 deletions superset-frontend/plugins/legacy-preset-chart-deckgl/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ import {
SequentialScheme,
} from '@superset-ui/core';
import { isNumber } from 'lodash';
import { TileLayer, BitmapLayer } from 'deck.gl/typed';
import { GeoBoundingBox } from '@deck.gl/geo-layers/typed';
import { hexToRGB } from './utils/colors';

const DEFAULT_NUM_BUCKETS = 10;

export const TILE_LAYER_PREFIX = 'tile://';
export const MAPBOX_LAYER_PREFIX = 'mapbox://';

export type Buckets = {
break_points: string[];
num_buckets: string;
Expand Down Expand Up @@ -188,3 +193,24 @@ export function getBuckets(

return buckets;
}

export function buildTileLayer(url: string) {
return new TileLayer({
data: url,

minZoom: 0,
maxZoom: 19,
tileSize: 256,

renderSubLayers: props => {
const { west, north, east, south } = props.tile.bbox as GeoBoundingBox;
return [
new BitmapLayer(props, {
data: undefined,
image: props.data,
bounds: [west, south, east, north],
}),
];
},
});
}
Loading