Skip to content

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

9.4 KB
Raw Blame
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project

import {combineReducers} from 'redux';
import Task, {withTask} from '@kepler.gl/tasks';

import {aiAssistantReducer} from '@openassistant/kepler-assistant/integration';
import {EXPORT_MAP_FORMATS} from '@kepler.gl/constants';
import {processGeojson, processRowObject, processArrowTable} from '@kepler.gl/processors';
import keplerGlReducer, {combinedUpdaters, uiStateUpdaters} from '@kepler.gl/reducers';
import KeplerGlSchema from '@kepler.gl/schemas';
import {KeplerTable} from '@kepler.gl/table';
import {getApplicationConfig, initApplicationConfig} from '@kepler.gl/utils';
import keplerGlDuckdbPlugin, {KeplerGlDuckDbTable, DuckDBWasmAdapter} from '@kepler.gl/duckdb';
import {getSqlConnector} from '../components/sql-connector';

import {
  INIT,
  LOAD_MAP_SAMPLE_FILE,
  LOAD_REMOTE_RESOURCE_SUCCESS,
  LOAD_REMOTE_DATASET_PROCESSED_SUCCESS,
  LOAD_REMOTE_RESOURCE_ERROR,
  SET_SAMPLE_LOADING_STATUS,
  loadRemoteDatasetProcessedSuccessAction
} from '../actions';

import {CLOUD_PROVIDERS_CONFIGURATION} from '../constants/default-settings';
import {generateHashId} from '../utils/strings';

// Share DuckDB between map imports and the SQLRooms SQL editor.
initApplicationConfig({
  // Custom UI for DuckDB
  plugins: [keplerGlDuckdbPlugin],
  // async data ingestion to DuckDb
  table: KeplerGlDuckDbTable,
  // setup database for DuckDB plugin
  database: new DuckDBWasmAdapter(getSqlConnector().then(connector => connector.getDb())),
  // progressive loading is sync, doesn't wait properly for a dataset to be created in DuckDB
  useArrowProgressiveLoading: false
});

// Example: Register custom icons for the icon layer.
// These will be merged with the default icons fetched from CDN.
// Data values in the "icon" column matching these IDs will render the custom shapes.
// NOTE: Cell winding must be counter-clockwise (CCW) to match the CDN icon convention.
initApplicationConfig({
  enableA5Layer: true,
  enableGeohashLayer: true,
  customIcons: [
    {
      id: 'custom-star',
      mesh: {
        cells: [
          [5, 1, 0],
          [5, 2, 1],
          [5, 3, 2],
          [5, 4, 3],
          [5, 0, 4]
        ],
        positions: [
          [0, 1, 0],
          [0.95, 0.31, 0],
          [0.59, -0.81, 0],
          [-0.59, -0.81, 0],
          [-0.95, 0.31, 0],
          [0, 0, 0]
        ]
      }
    }
  ],
  customIconUrl:
    'https://raw.githubusercontent.com/keplergl/kepler.gl-data/refs/heads/master/layers/icon/custom-icons.json'
});

const {DEFAULT_MAP_CONTROLS} = uiStateUpdaters;

// INITIAL_APP_STATE
const initialAppState = {
  appName: 'example',
  loaded: false,
  sampleMaps: [], // this is used to store sample maps fetch from a remote json file
  isMapLoading: false, // determine whether we are loading a sample map,
  error: null // contains error when loading/retrieving data/configuration
  // {
  //   status: null,
  //   message: null
  // }
};

// App reducer
export const appReducer = (state = initialAppState, action) => {
  switch (action.type) {
    case INIT:
      return {...state, loaded: true};
    case LOAD_MAP_SAMPLE_FILE:
      return {...state, sampleMaps: action.samples};
    case SET_SAMPLE_LOADING_STATUS:
      return {...state, isMapLoading: action.isMapLoading};
    default:
      return state;
  }
};

const {DEFAULT_EXPORT_MAP} = uiStateUpdaters;

// combine app reducer and keplerGl reducer
// to mimic the reducer state of kepler.gl website
const demoReducer = combineReducers({
  // mount keplerGl reducer
  keplerGl: keplerGlReducer.initialState({
    // In order to provide single file export functionality
    // we are going to set the mapbox access token to be used
    // in the exported file
    uiState: {
      exportMap: {
        ...DEFAULT_EXPORT_MAP,
        [EXPORT_MAP_FORMATS.HTML]: {
          ...DEFAULT_EXPORT_MAP[EXPORT_MAP_FORMATS.HTML],
          exportMapboxAccessToken: CLOUD_PROVIDERS_CONFIGURATION.EXPORT_MAPBOX_TOKEN
        }
      },
      mapControls: {
        ...DEFAULT_MAP_CONTROLS,
        // TODO find a better way not to add extra controls optionally - from plugin?
        ...((getApplicationConfig().plugins || []).some(p => p.name === 'duckdb')
          ? {
              sqlPanel: {
                active: false,
                activeMapIndex: 0,
                disableClose: false,
                show: true
              }
            }
          : {}),
        aiAssistant: {
          active: false,
          show: true
        }
      }
    },
    visState: {
      loaders: [], // Add additional loaders.gl loaders here
      loadOptions: {} // Add additional loaders.gl loader options here
    }
  }),
  app: appReducer,
  aiAssistant: aiAssistantReducer
});

async function loadRemoteResourceSuccessTask({
  dataUrl,
  datasetId,
  datasetLabel,
  processorMethod,
  remoteDatasetConfig,
  unprocessedData
}) {
  if (dataUrl) {
    const data = await processorMethod(unprocessedData);
    return {
      info: {
        id: datasetId,
        label: datasetLabel
      },
      data
    };
  }

  // remote datasets like vector tile datasets
  return remoteDatasetConfig;
}

const LOAD_REMOTE_RESOURCE_SUCCESS_TASK = Task.fromPromise(
  loadRemoteResourceSuccessTask,
  'LOAD_REMOTE_RESOURCE_SUCCESS_TASK'
);

// this can be moved into a action and call kepler.gl action
/**
 * Used to load Kepler.gl demo examples
 * @param state
 * @param action {map: resultset, config, map}
 * @returns {{app: {isMapLoading: boolean}, keplerGl: {map: (state|*)}}}
 */
export const loadRemoteResourceSuccess = (state, action) => {
  // TODO: replace generate with a different function
  const datasetId = action.options.id || generateHashId(6);
  const {dataUrl} = action.options;

  const {shape} = dataUrl ? action.response : {};
  let processorMethod = processRowObject;
  let unprocessedData = action.response;
  unprocessedData = shape === 'object-row-table' ? action.response.data : unprocessedData;

  if (dataUrl) {
    const table = getApplicationConfig().table ?? KeplerTable;
    if (typeof table.getFileProcessor === 'function') {
      if (shape === 'arrow-table') {
        // arrow processor from table plugin expects batches
        unprocessedData = action.response.data.batches;
      }
      // use custom processors from table class
      const processorResult = table.getFileProcessor(unprocessedData);
      // TODO save processorResult.format here with the dataset
      processorMethod = processorResult.processor;
    } else {
      if (shape === 'arrow-table') {
        processorMethod = processArrowTable;
      } else if (shape === 'object-row-table') {
        processorMethod = processRowObject;
      } else if (dataUrl.includes('.json') || dataUrl.includes('.geojson')) {
        processorMethod = processGeojson;
      } else {
        throw new Error('Failed to select data processor');
      }
    }
  }

  // processorMethod can be async so create a task
  const task = LOAD_REMOTE_RESOURCE_SUCCESS_TASK({
    dataUrl,
    datasetId,
    datasetLabel: action.options.label || datasetId,
    processorMethod,
    remoteDatasetConfig: action.remoteDatasetConfig,
    unprocessedData
  }).bimap(
    datasets => loadRemoteDatasetProcessedSuccessAction({...action, datasets}),
    () => {
      throw new Error('loadRemoteResource data processor failed');
    }
  );

  return withTask(state, task);
};

const loadRemoteDatasetProcessedSuccess = (state, action) => {
  const {config, datasets, options} = action.payload;

  const parsedConfig = config ? KeplerGlSchema.parseSavedConfig(config) : undefined;

  // a hack to use minZoom and maxZoom from examples
  if (parsedConfig?.mapState) {
    if (typeof config?.config?.mapState?.maxZoom === 'number') {
      parsedConfig.mapState.maxZoom = config.config.mapState.maxZoom;
    }
    if (typeof config?.config?.mapState?.minZoom === 'number') {
      parsedConfig.mapState.minZoom = config.config.mapState.minZoom;
    }
  }

  const keplerGlInstance = combinedUpdaters.addDataToMapUpdater(
    state.keplerGl.map, // "map" is the id of your kepler.gl instance
    {
      payload: {
        datasets,
        config: parsedConfig,
        options: {
          centerMap: Boolean(!config)
        }
      }
    }
  );

  return {
    ...state,
    app: {
      ...state.app,
      currentSample: options,
      isMapLoading: false // we turn off the spinner
    },
    keplerGl: {
      ...state.keplerGl, // in case you keep multiple instances
      map: keplerGlInstance
    }
  };
};

export const loadRemoteResourceError = (state, action) => {
  const {error, url} = action;

  const errorNote = {
    type: 'error',
    message: error.message || `Error loading ${url}`
  };

  return {
    ...state,
    app: {
      ...state.app,
      isMapLoading: false // we turn of the spinner
    },
    keplerGl: {
      ...state.keplerGl, // in case you keep multiple instances
      map: {
        ...state.keplerGl.map,
        uiState: uiStateUpdaters.addNotificationUpdater(state.keplerGl.map.uiState, {
          payload: errorNote
        })
      }
    }
  };
};

const composedUpdaters = {
  [LOAD_REMOTE_RESOURCE_SUCCESS]: loadRemoteResourceSuccess,
  [LOAD_REMOTE_DATASET_PROCESSED_SUCCESS]: loadRemoteDatasetProcessedSuccess,
  [LOAD_REMOTE_RESOURCE_ERROR]: loadRemoteResourceError
};

const composedReducer = (state, action) => {
  if (composedUpdaters[action.type]) {
    return composedUpdaters[action.type](state, action);
  }
  return demoReducer(state, action);
};

// export demoReducer to be combined in website app
export default composedReducer;