Skip to content

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

4.7 KB
Raw Blame

Get Started

Installation

Use Node v20 and above, older node versions have not been tested

npm install --save kepler.gl @kepler.gl/components @kepler.gl/reducers maplibre-gl

The default basemaps use MapLibre, so load its stylesheet once in your application entry point:

import 'maplibre-gl/dist/maplibre-gl.css';

Mapbox-only applications should load mapbox-gl/dist/mapbox-gl.css instead. Applications that allow both MapLibre and Mapbox styles should load both stylesheets.

For a Mapbox-only application, replace maplibre-gl in the installation command:

npm install --save kepler.gl @kepler.gl/components @kepler.gl/reducers mapbox-gl
import 'mapbox-gl/dist/mapbox-gl.css';

Get a Mapbox Token (Mapbox styles only)

The default MapLibre-backed basemaps do not require a Mapbox token. If your application uses Mapbox-hosted styles, create a Mapbox account and get a Mapbox Access Token.

Basic Usage

Basic Usage

0. Working Template

Check full example on Github.

import * as React from "react";
import ReactDOM from "react-dom/client";
import document from "global/document";

import { applyMiddleware, combineReducers, compose, createStore } from "redux";
import { connect, Provider } from "react-redux";

import keplerGlReducer, { enhanceReduxMiddleware } from "@kepler.gl/reducers";
import KeplerGl from "@kepler.gl/components";
import "maplibre-gl/dist/maplibre-gl.css";

import AutoSizer from "react-virtualized/dist/commonjs/AutoSizer";

const reducers = combineReducers({
  keplerGl: keplerGlReducer.initialState({
    uiState: {
      readOnly: false,
      currentModal: null,
    },
  }),
});

const middleWares = enhanceReduxMiddleware([
  // Add other middlewares here
]);

const enhancers = applyMiddleware(...middleWares);

const initialState = {};
const store = createStore(reducers, initialState, compose(enhancers));

const App = () => (
  <div
    style={{
      position: "absolute",
      top: "0px",
      left: "0px",
      width: "100%",
      height: "100%",
    }}
  >
    <AutoSizer>
      {({ height, width }) => (
        <KeplerGl
          mapboxApiAccessToken="xxx" // Replace with your mapbox token
          id="map"
          width={width}
          height={height}
        />
      )}
    </AutoSizer>
  </div>
);

const mapStateToProps = (state) => state;
const dispatchToProps = (dispatch) => ({ dispatch });
const ConnectedApp = connect(mapStateToProps, dispatchToProps)(App);
const Root = () => (
  <Provider store={store}>
    <ConnectedApp />
  </Provider>
);

export default Root;

1. Mount reducer

Kepler.gl uses Redux to manage its internal state, along with a built-in task middleware to handle async side effects. Mount kepler.gl reducer in your store and apply the middleware using enhanceReduxMiddleware:

import keplerGlReducer, {enhanceReduxMiddleware} from '@kepler.gl/reducers';
import {createStore, combineReducers, applyMiddleware} from 'redux';

const reducer = combineReducers({
  // <-- mount kepler.gl reducer in your app
  keplerGl: keplerGlReducer,

  // Your other reducers here
  app: appReducer
});

const middlewares = enhanceReduxMiddleware([
  // Add other middlewares here
]);

// create store
const store = createStore(reducer, {}, applyMiddleware(...middlewares));

If you mount keplerGlReducer in another address instead of keplerGl, or it is not mounted at root of your reducer, you will need to specify the path to it when you mount the component with the getState prop.

2. Mount component

import KeplerGl from '@kepler.gl/components';

const Map = props => (
  <KeplerGl
      id="foo"
      mapboxApiAccessToken={token}
      width={width}
      height={height}/>
);

3. Add data to map

In order to interact with a kepler.gl instance and add new data to it, you can dispatch the addDataToMap action from anywhere inside your app. It adds dataset(s) to a kepler.gl instance and updates the full configuration (mapState, mapStyle, visState).

Read more about addDataToMap

import {addDataToMap} from '@kepler.gl/actions';

this.props.dispatch(
  addDataToMap({
    // datasets
    datasets: {
      info: {
        label: 'Sample Taxi Trips in New York City',
        id: 'test_trip_data'
      },
      data: sampleTripData
    },
    // option
    option: {
      centerMap: true,
      readOnly: false
    },
    // config
    config: {
      mapStyle: {styleType: 'light'}
    }
  })
);