Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
import {loadFiles, toggleModal} from '@kepler.gl/actions';
import {parseUri} from '@kepler.gl/common-utils';
import {load} from '@loaders.gl/core';
import {CSVLoader} from '@loaders.gl/csv';
import {GeoArrowLoader} from '@loaders.gl/arrow';
import {_GeoJSONLoader as GeoJSONLoader} from '@loaders.gl/json';
import {ParquetWasmLoader} from '@loaders.gl/parquet';
import {
DEMO_BASE_PATH,
LOADING_SAMPLE_ERROR_MESSAGE,
LOADING_SAMPLE_LIST_ERROR_MESSAGE,
MAP_CONFIG_URL
} from './constants/default-settings';
// CONSTANTS
export const INIT = 'INIT';
export const LOAD_REMOTE_RESOURCE_SUCCESS = 'LOAD_REMOTE_RESOURCE_SUCCESS';
export const LOAD_REMOTE_DATASET_PROCESSED_SUCCESS = 'LOAD_REMOTE_DATASET_PROCESSED_SUCCESS';
export const LOAD_REMOTE_RESOURCE_ERROR = 'LOAD_REMOTE_RESOURCE_ERROR';
export const LOAD_MAP_SAMPLE_FILE = 'LOAD_MAP_SAMPLE_FILE';
export const SET_SAMPLE_LOADING_STATUS = 'SET_SAMPLE_LOADING_STATUS';
// Sharing
export const PUSHING_FILE = 'PUSHING_FILE';
export const CLOUD_LOGIN_SUCCESS = 'CLOUD_LOGIN_SUCCESS';
// ACTIONS
export function initApp() {
return {
type: INIT
};
}
export function loadRemoteResourceSuccess(response, config, options, remoteDatasetConfig) {
return {
type: LOAD_REMOTE_RESOURCE_SUCCESS,
response,
config,
options,
remoteDatasetConfig
};
}
export function loadRemoteDatasetProcessedSuccessAction(result) {
return {
type: LOAD_REMOTE_DATASET_PROCESSED_SUCCESS,
payload: result
};
}
export function loadRemoteResourceError(error, url) {
return {
type: LOAD_REMOTE_RESOURCE_ERROR,
error,
url
};
}
export function loadMapSampleFile(samples) {
return {
type: LOAD_MAP_SAMPLE_FILE,
samples
};
}
export function setLoadingMapStatus(isMapLoading) {
return {
type: SET_SAMPLE_LOADING_STATUS,
isMapLoading
};
}
/**
* Actions passed to kepler.gl, called
*
* Note: exportFile is called on both saving and sharing
*
* @param {*} param0
*/
export function onExportFileSuccess({provider, options}) {
return () => {
if (options.isPublic && provider.getShareUrl) {
window.history.pushState(null, '', provider.getShareUrl(false));
} else if (!options.isPublic && provider.getMapUrl) {
window.history.pushState(null, '', provider.getMapUrl(false));
}
};
}
export function onLoadCloudMapSuccess({provider, loadParams}) {
return () => {
const mapUrl = provider?.getMapUrl(loadParams);
if (mapUrl) {
const url = `${DEMO_BASE_PATH}/map/${provider.name}?path=${mapUrl}`;
window.history.pushState(null, '', url);
}
};
}
// This can be moved into Kepler.gl to provide ability to load data from remote URLs
/**
* The method is able to load both data and kepler.gl files.
* It uses loadFile action to dispatch and add new datasets/configs
* to the kepler.gl instance
* @param options
* @param {string} options.dataUrl the URL to fetch data from. Current supported file type json,csv, kepler.json
* @returns {Function}
*/
export function loadRemoteMap(options) {
return dispatch => {
dispatch(setLoadingMapStatus(true));
// breakdown url into url+query params
loadRemoteRawData(options.dataUrl).then(
// In this part we turn the response into a FileBlob
// so we can use it to call loadFiles
([file, url]) => {
const {file: filename} = parseUri(url);
dispatch(loadFiles([new File([file], filename)])).then(() =>
dispatch(setLoadingMapStatus(false))
);
},
error => {
const {target = {}} = error;
const {status, responseText} = target;
dispatch(loadRemoteResourceError({status, message: responseText}, options.dataUrl));
}
);
};
}
/**
* Load a file from a remote URL
* @param url
* @returns {Promise<any>}
*/
function loadRemoteRawData(url) {
if (!url) {
// TODO: we should return reject with an appropriate error
return Promise.resolve(null);
}
return fetch(url)
.then(resp => {
if (!resp.ok) {
return resp.text().then(text => {
throw new Error(text);
});
}
return resp.blob();
})
.then(data => {
return [data, url];
});
}
// The following methods are only used to load SAMPLES
/**
*
* @param {Object} options
* @param {string} [options.dataUrl] the URL to fetch data from, e.g. https://raw.githubusercontent.com/keplergl/kepler.gl-data/master/earthquakes/data.csv
* @param {string} [options.configUrl] the URL string to fetch kepler config from, e.g. https://raw.githubusercontent.com/keplergl/kepler.gl-data/master/earthquakes/config.json
* @param {string} [options.id] the id used as dataset unique identifier, e.g. earthquakes
* @param {string} [options.label] the label used to describe the new dataset, e.g. California Earthquakes
* @param {string} [options.queryType] the type of query to execute to load data/config, e.g. sample
* @param {string} [options.imageUrl] the URL to fetch the dataset image to use in sample gallery
* @param {string} [options.description] the description used in sample galley to define the current example
* @param {string} [options.size] the number of entries displayed in the current sample
* @param {string} [keplergl] url to fetch the full data/config generated by kepler
* @returns {Function}
*/
export function loadSample(options, pushRoute = true) {
return dispatch => {
if (options.id && pushRoute) {
const search = window.location.search || '';
window.history.pushState(null, '', `${DEMO_BASE_PATH}/${options.id}${search}`);
}
// if the sample has a kepler.gl config file url we load it
if (options.keplergl) {
dispatch(loadRemoteMap({dataUrl: options.keplergl}));
} else {
dispatch(loadRemoteSampleMap(options));
}
dispatch(setLoadingMapStatus(true));
};
}
/**
* Load remote map with config and data
* @param options {configUrl, dataUrl}
* @returns {Function}
*/
function loadRemoteSampleMap(options) {
return dispatch => {
// Load configuration first
const {configUrl, dataUrl, remoteDatasetConfigUrl} = options;
const toLoad = [loadRemoteConfig(configUrl)];
toLoad.push(dataUrl ? loadRemoteData(dataUrl) : null);
// Load remote dataset config for tiled layers
toLoad.push(remoteDatasetConfigUrl ? loadRemoteConfig(remoteDatasetConfigUrl) : null);
Promise.all(toLoad).then(
([config, data, remoteDatasetConfig]) => {
// TODO: these two actions can be merged
dispatch(loadRemoteResourceSuccess(data, config, options, remoteDatasetConfig));
// TODO: toggleModal when async dataset task is done, show the spinner until then
dispatch(toggleModal(null));
},
error => {
if (error) {
const {target = {}} = error;
const {status, responseText} = target;
dispatch(
loadRemoteResourceError(
{
status,
message: `${responseText} - ${LOADING_SAMPLE_ERROR_MESSAGE} ${options.id} (${configUrl})`
},
configUrl
)
);
}
}
);
};
}
/**
*
* @param url
* @returns {Promise<any>}
*/
function loadRemoteConfig(url) {
if (!url) {
// TODO: we should return reject with an appropriate error
return Promise.resolve(null);
}
return fetch(url).then(resp => {
if (!resp.ok) {
return resp.text().then(text => {
throw new Error(text);
});
}
return resp.json();
});
}
/**
*
* @param url to fetch data from (csv, json, geojson)
* @returns {Promise<any>}
*/
function loadRemoteData(url) {
if (!url) {
// TODO: we should return reject with an appropriate error
return Promise.resolve(null);
}
// Load data
return new Promise(resolve => {
const loaders = [CSVLoader, GeoArrowLoader, ParquetWasmLoader, GeoJSONLoader];
const loadOptions = {
csv: {
shape: 'object-row-table'
},
arrow: {
shape: 'arrow-table'
},
parquet: {
shape: 'arrow-table'
},
metadata: true
};
const data = load(url, loaders, loadOptions);
resolve(data);
});
}
/**
*
* @param sampleMapId optional if we pass the sampleMapId, after fetching
* map sample configurations we are going to load the actual map data if it exists
* @returns {function(*)}
*/
export function loadSampleConfigurations(sampleMapId = null) {
return dispatch => {
fetch(MAP_CONFIG_URL)
.then(response => {
if (!response.ok) {
return response.text().then(text => {
throw new Error(text);
});
} else {
return response.json();
}
})
.then(samples => {
dispatch(loadMapSampleFile(samples));
// Load the specified map
const map = sampleMapId && samples.find(s => s.id === sampleMapId);
if (map) {
dispatch(loadSample(map, false));
}
})
.catch(error => {
dispatch(
loadRemoteResourceError(
{message: `${error} - ${LOADING_SAMPLE_LIST_ERROR_MESSAGE}`},
MAP_CONFIG_URL
)
);
});
};
}