Skip to content

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

fix: integrate Maplibre support and update dependencies (#3395)

Xun Li169cd8e9f7c6
diff --git a/src/components/package.json b/src/components/package.json
index 6199438cb..559264ecb 100644
--- a/src/components/package.json
+++ b/src/components/package.json
@@ -77,6 +77,7 @@
"@types/react-virtualized": "^9.21.30",
"@types/react-vis": "1.11.7",
"@types/styled-components": "^5.1.32",
+ "@vis.gl/react-maplibre": "8.1.1",
"classnames": "^2.2.1",
"copy-to-clipboard": "^3.3.1",
"d3-array": "^2.8.0",
diff --git a/src/components/src/map-container.tsx b/src/components/src/map-container.tsx
index fe945ad51..6fe270e54 100644
--- a/src/components/src/map-container.tsx
+++ b/src/components/src/map-container.tsx
@@ -4,7 +4,8 @@
// libraries
import React, {Component, createRef, useMemo} from 'react';
import styled, {withTheme, useTheme} from 'styled-components';
-import {Map, MapRef} from 'react-map-gl/mapbox-legacy';
+import {Map as MapboxLegacyMap, MapRef} from 'react-map-gl/mapbox-legacy';
+import {Map as MaplibreMap} from '@vis.gl/react-maplibre';
import {PickingInfo, MapView} from '@deck.gl/core';
import DeckGL from '@deck.gl/react';
import {createSelector, Selector} from 'reselect';
@@ -395,7 +396,7 @@ export interface MapContainerProps {
locale?: any;
theme?: any;
editor?: any;
- MapComponent?: typeof Map;
+ MapComponent?: typeof MapboxLegacyMap | typeof MaplibreMap;
deckGlProps?: any;
onDeckInitialized?: (a: any, b: any) => void;
onViewStateChange?: (viewport: Viewport) => void;
@@ -438,7 +439,6 @@ export default function MapContainerFactory(
declare context: React.ContextType<typeof MapViewStateContext>;
static defaultProps = {
- MapComponent: Map,
deckGlProps: {},
index: 0,
primary: true
@@ -1143,7 +1143,6 @@ export default function MapContainerFactory(
mapState,
mapStyle,
mapStateActions,
- MapComponent = Map,
mapboxApiAccessToken,
// mapboxApiUrl,
mapControls,
@@ -1172,27 +1171,38 @@ export default function MapContainerFactory(
// Current style can be a custom style, from which we pull the mapbox API acccess token
const currentStyle = mapStyle.mapStyles?.[mapStyle.styleType];
const baseMapLibraryName = getBaseMapLibrary(currentStyle);
- const baseMapLibraryConfig =
- getApplicationConfig().baseMapLibraryConfig?.[baseMapLibraryName];
+ const baseMapLibraryConfig = getApplicationConfig().baseMapLibraryConfig[baseMapLibraryName];
+
+ // Select the correct Map adapter based on the active base map library.
+ // Using the native adapter for each library avoids Transform API
+ // incompatibilities (e.g. mapbox-legacy's cloneTransform with MapLibre v5).
+ const ResolvedMapComponent =
+ this.props.MapComponent ??
+ (baseMapLibraryName === MAP_LIB_OPTIONS.MAPBOX ? MapboxLegacyMap : MaplibreMap);
+
+ const useMapboxAdapter = ResolvedMapComponent === MapboxLegacyMap;
const internalViewState = this.context?.getInternalViewState(index);
const configMaxPitch = mapState.maxPitch ?? getApplicationConfig().maxPitch;
- const effectiveMaxPitch =
- baseMapLibraryName === MAP_LIB_OPTIONS.MAPBOX
- ? Math.min(configMaxPitch, MAPBOX_MAX_PITCH)
- : configMaxPitch;
- const mapProps = {
+ const effectiveMaxPitch = useMapboxAdapter
+ ? Math.min(configMaxPitch, MAPBOX_MAX_PITCH)
+ : configMaxPitch;
+ const mapProps: Record<string, any> = {
...internalViewState,
maxPitch: effectiveMaxPitch,
preserveDrawingBuffer: this.props.isExport ?? false,
mapboxAccessToken: currentStyle?.accessToken || mapboxApiAccessToken,
// baseApiUrl: mapboxApiUrl,
- mapLib: baseMapLibraryConfig.getMapLib(),
transformRequest:
this.props.transformRequest ||
transformRequest(currentStyle?.accessToken || mapboxApiAccessToken)
};
+ if (useMapboxAdapter) {
+ const mapboxConfig = getApplicationConfig().baseMapLibraryConfig[MAP_LIB_OPTIONS.MAPBOX];
+ mapProps.mapLib = mapboxConfig.getMapLib();
+ }
+
const hasGeocoderLayer = Boolean(layers.find(l => l.id === GEOCODER_LAYER_ID));
const isSplit = Boolean(mapState.isSplit);
@@ -1200,7 +1210,7 @@ export default function MapContainerFactory(
primaryMap: true,
isInteractive: true,
children: (
- <MapComponent
+ <ResolvedMapComponent
key={`bottom-${baseMapLibraryName}`}
{...mapProps}
mapStyle={mapStyle.bottomMapStyle ?? EMPTY_MAPBOX_STYLE}
@@ -1274,7 +1284,7 @@ export default function MapContainerFactory(
/>
{this.props.children}
{mapStyle.topMapStyle ? (
- <MapComponent
+ <ResolvedMapComponent
key={`top-${baseMapLibraryName}`}
viewState={internalViewState}
maxPitch={effectiveMaxPitch}
@@ -1282,7 +1292,14 @@ export default function MapContainerFactory(
style={MAP_STYLE.top}
mapboxAccessToken={mapProps.mapboxAccessToken}
transformRequest={mapProps.transformRequest}
- mapLib={baseMapLibraryConfig.getMapLib()}
+ {...(useMapboxAdapter
+ ? {
+ mapLib:
+ getApplicationConfig().baseMapLibraryConfig[
+ MAP_LIB_OPTIONS.MAPBOX
+ ].getMapLib()
+ }
+ : {})}
{...topMapContainerProps}
/>
) : null}
@@ -1332,8 +1349,7 @@ export default function MapContainerFactory(
const currentStyle = mapStyle.mapStyles?.[mapStyle.styleType];
const baseMapLibraryName = getBaseMapLibrary(currentStyle);
- const baseMapLibraryConfig =
- getApplicationConfig().baseMapLibraryConfig?.[baseMapLibraryName];
+ const baseMapLibraryConfig = getApplicationConfig().baseMapLibraryConfig[baseMapLibraryName];
return (
<StyledMap
diff --git a/src/components/src/modals/add-map-style-modal.tsx b/src/components/src/modals/add-map-style-modal.tsx
index 03895d85d..08392be73 100644
--- a/src/components/src/modals/add-map-style-modal.tsx
+++ b/src/components/src/modals/add-map-style-modal.tsx
@@ -5,7 +5,8 @@ import React, {Component} from 'react';
import {polyfill} from 'react-lifecycles-compat';
import classnames from 'classnames';
import styled from 'styled-components';
-import {Map, MapInstance, MapRef} from 'react-map-gl/mapbox-legacy';
+import {Map as MapboxLegacyMap, MapInstance, MapRef} from 'react-map-gl/mapbox-legacy';
+import {Map as MaplibreMap} from '@vis.gl/react-maplibre';
import {
StyledModalContent,
InputLight,
@@ -19,7 +20,7 @@ import {media} from '@kepler.gl/styles';
import {getApplicationConfig, getBaseMapLibrary, transformRequest} from '@kepler.gl/utils';
import {injectIntl, IntlShape} from 'react-intl';
import {FormattedMessage} from '@kepler.gl/localization';
-import {NO_BASEMAP_ICON} from '@kepler.gl/constants';
+import {NO_BASEMAP_ICON, MAP_LIB_OPTIONS} from '@kepler.gl/constants';
import {InputStyle, MapState} from '@kepler.gl/types';
import {ActionHandler, inputMapStyle, loadCustomMapStyle} from '@kepler.gl/actions';
@@ -138,7 +139,7 @@ function AddMapStyleModalFactory() {
_map: MapInstance | undefined | null;
- _setMapRef = (mapRef: MapRef) => {
+ _setMapRef = (mapRef: MapRef | null) => {
// Handle change of the basemap library
if (this._map && mapRef) {
const map = mapRef.getMap();
@@ -179,18 +180,25 @@ function AddMapStyleModalFactory() {
const baseMapLibraryConfig = getApplicationConfig().baseMapLibraryConfig[baseMapLibraryName];
const mapboxApiAccessToken = inputStyle.accessToken || this.props.mapboxApiAccessToken;
- const mapProps = {
+ const mapProps: Record<string, any> = {
...mapState,
// TODO baseApiUrl should be taken into account in transformRequest as we use dynamic mapLib import
// baseApiUrl: mapboxApiUrl,
mapboxAccessToken: mapboxApiAccessToken,
- mapLib: baseMapLibraryConfig.getMapLib(),
preserveDrawingBuffer: true,
transformRequest:
this.props.transformRequest?.(mapboxApiAccessToken) ||
transformRequest(mapboxApiAccessToken)
};
+ // Only pass mapLib for the mapbox-legacy adapter
+ if (baseMapLibraryName === MAP_LIB_OPTIONS.MAPBOX) {
+ mapProps.mapLib = baseMapLibraryConfig.getMapLib();
+ }
+
+ const ResolvedMapComponent =
+ baseMapLibraryName === MAP_LIB_OPTIONS.MAPBOX ? MapboxLegacyMap : MaplibreMap;
+
return (
<div className="add-map-style-modal">
<StyledModalContent>
@@ -299,9 +307,9 @@ function AddMapStyleModalFactory() {
<div className="preview-image-spinner" />
) : (
<StyledMapContainer>
- <Map
+ <ResolvedMapComponent
{...mapProps}
- ref={this._setMapRef}
+ ref={this._setMapRef as any}
key={`${baseMapLibraryName}-${this.state.reRenderKey}-${inputStyle.url}-${mapboxApiAccessToken}`}
style={{
width: MapW,
diff --git a/src/components/src/plot-container.tsx b/src/components/src/plot-container.tsx
index 5eeceb531..e6033e5c1 100644
--- a/src/components/src/plot-container.tsx
+++ b/src/components/src/plot-container.tsx
@@ -4,7 +4,6 @@
// libraries
import React, {useRef, useEffect, useState, useCallback, useMemo} from 'react';
import styled from 'styled-components';
-import {Map} from 'react-map-gl/mapbox-legacy';
import debounce from 'lodash/debounce';
import {
exportImageError,
@@ -355,7 +354,6 @@ export default function PlotContainerFactory(
settings: mapFields.mapControls?.mapLegend?.settings
}
},
- MapComponent: Map,
onMapRender,
isExport: true,
deckGlProps: {
diff --git a/website/esbuild.config.mjs b/website/esbuild.config.mjs
index 170a81d04..0f31f5d93 100644
--- a/website/esbuild.config.mjs
+++ b/website/esbuild.config.mjs
@@ -6,7 +6,7 @@ import { replace } from 'esbuild-plugin-replace';
import process from 'node:process';
import { join } from 'node:path';
import { spawn } from 'node:child_process';
-import WebsitePackage from '../package.json' assert {type: 'json'};
+import WebsitePackage from '../package.json' with {type: 'json'};
const args = process.argv;
const LIB_DIR = '../';
diff --git a/yarn.lock b/yarn.lock
index aac61e85e..25c6836cd 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5095,6 +5095,7 @@ __metadata:
"@types/react-virtualized": "npm:^9.21.30"
"@types/react-vis": "npm:1.11.7"
"@types/styled-components": "npm:^5.1.32"
+ "@vis.gl/react-maplibre": "npm:8.1.1"
classnames: "npm:^2.2.1"
copy-to-clipboard: "npm:^3.3.1"
d3-array: "npm:^2.8.0"
@@ -10392,11 +10393,11 @@ __metadata:
linkType: hard
"@types/mapbox-gl@npm:>=1.0.0":
- version: 3.1.0
- resolution: "@types/mapbox-gl@npm:3.1.0"
+ version: 3.4.1
+ resolution: "@types/mapbox-gl@npm:3.4.1"
dependencies:
"@types/geojson": "npm:*"
- checksum: 10c0/952ff4cef4053d986c1b35b7137c84f3f86faaa6bfbb050fd919004114a78ff3a95d28a5325a2a34d24753d3e84c019a55f873891cb2b8d0467efede2f095a7d
+ checksum: 10c0/475f48063a39cc791a0efa099f4a62b84df56a52e1d111e32d11930fcee8e281f20b157f4069ff0ff4d5e13889ce9b452ca4a57e5c27c6d70a32fb3f5a184e72
languageName: node
linkType: hard