Skip to content

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

Load data and kepler.gl file using URLs (#260)

Giuseppe Macrì1fe81d2c88e2
diff --git a/examples/demo-app/src/actions.js b/examples/demo-app/src/actions.js
index c54c95395..6d3ace6f2 100644
--- a/examples/demo-app/src/actions.js
+++ b/examples/demo-app/src/actions.js
@@ -19,19 +19,34 @@
// THE SOFTWARE.
import {push} from 'react-router-redux';
-import {text as requestText, json as requestJson} from 'd3-request';
-import {toggleModal} from 'kepler.gl/actions';
-import {console as Console} from 'global/window';
-import {MAP_CONFIG_URL} from './constants/sample-maps';
+import {request, text as requestText, json as requestJson} from 'd3-request';
+import {loadFiles, toggleModal} from 'kepler.gl/actions';
+
+import {
+ LOADING_SAMPLE_ERROR_MESSAGE,
+ LOADING_SAMPLE_LIST_ERROR_MESSAGE,
+ MAP_CONFIG_URL
+} from './constants/default-settings';
+import {LOADING_METHODS_NAMES} from './constants/default-settings';
// CONSTANTS
export const INIT = 'INIT';
export const SET_LOADING_METHOD = 'SET_LOADING_METHOD';
-export const LOAD_REMOTE_FILE_DATA_SUCCESS = 'LOAD_DATA_SUCCESS';
+export const LOAD_REMOTE_RESOURCE_SUCCESS = 'LOAD_REMOTE_RESOURCE_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';
// ACTIONS
+export function switchToLoadingMethod(method) {
+ return dispatch => {
+ dispatch(setLoadingMethod(method));
+ if (method === LOADING_METHODS_NAMES.sample) {
+ dispatch(loadSampleConfigurations());
+ }
+ };
+}
+
export function setLoadingMethod(method) {
return {
type: SET_LOADING_METHOD,
@@ -39,15 +54,23 @@ export function setLoadingMethod(method) {
};
}
-export function loadResponseFromRemoteFile(response, config, map) {
+export function loadRemoteResourceSuccess(response, config, options) {
return {
- type: LOAD_REMOTE_FILE_DATA_SUCCESS,
+ type: LOAD_REMOTE_RESOURCE_SUCCESS,
response,
config,
- map
+ options
};
}
+export function loadRemoteResourceError(error, url) {
+ return {
+ type: LOAD_REMOTE_RESOURCE_ERROR,
+ error,
+ url
+ }
+}
+
export function loadMapSampleFile(samples) {
return {
type: LOAD_MAP_SAMPLE_FILE,
@@ -62,46 +85,193 @@ export function setLoadingMapStatus(isMapLoading) {
};
}
-export function loadSampleMap(sample) {
+/**
+ * this method detects whther the response status is < 200 or > 300 in case the error
+ * is not caught by the actualy request framework
+ * @param response the response
+ * @returns {{status: *, message: (*|{statusCode}|Object)}}
+ */
+function detectResponseError(response) {
+ if (response.statusCode && (response.statusCode < 200 || response.statusCode >= 300)) {
+ return {
+ status: response.statusCode,
+ message: response.body || response.message || response
+ }
+ }
+}
+
+// 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 dispatcha and add new datasets/configs
+ * to the kepler.gl instance
+ * @param options
+ * @param {string} options.dataUrl the URL to fetch data from. Current supoprted file type json,csv, kepler.json
+ * @returns {Function}
+ */
+export function loadRemoteMap(options) {
+ return dispatch => {
+ loadRemoteRawData(options.dataUrl).then(
+ // In this part we turn the response into a FileBlob
+ // so we can use it to call loadFiles
+ file => dispatch(loadFiles([
+ /* eslint-disable no-undef */
+ new File([file], options.dataUrl)
+ /* eslint-enable no-undef */
+ ])),
+ error => {
+ const {target = {}} = error;
+ const {status, responseText} = target;
+ dispatch(loadRemoteResourceError({status, message: responseText}, options.dataUrl));
+ }
+ );
+ dispatch(setLoadingMapStatus(true));
+ }
+}
+
+/**
+ * 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 new Promise((resolve, reject) => {
+ request(url, (error, result) => {
+ if (error) {
+ reject(error);
+ }
+ const responseError = detectResponseError(result);
+ if (responseError) {
+ reject(responseError);
+ return;
+ }
+ resolve(result.response)
+ })
+ });
+}
+
+// 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/uber-web/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/uber-web/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, getState) => {
const {routing} = getState();
- dispatch(push(`/demo/${sample.id}${routing.locationBeforeTransitions.search}`));
- dispatch(loadRemoteMap(sample));
+ if (options.id && pushRoute) {
+ dispatch(push(`/demo/${options.id}${routing.locationBeforeTransitions.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));
};
}
-function loadMapCallback(dispatch, error, result, sample, config) {
- if (error) {
- Console.warn(`Error loading datafile ${sample.dataUrl}`);
- // dispatch(ERROR)
- } else {
- dispatch(loadResponseFromRemoteFile(result, config, sample));
- dispatch(toggleModal(null));
+/**
+ * Load remote map with config and data
+ * @param options {configUrl, dataUrl}
+ * @returns {Function}
+ */
+function loadRemoteSampleMap(options) {
+ return (dispatch) => {
+ // Load configuration first
+ const {configUrl, dataUrl} = options;
+
+ Promise
+ .all([loadRemoteConfig(configUrl), loadRemoteData(dataUrl)])
+ .then(
+ ([config, data]) => {
+ // TODO: these two actions can be merged
+ dispatch(loadRemoteResourceSuccess(data, config, options));
+ 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));
+ }
+ }
+ );
}
}
-function loadRemoteMap(sample) {
- return (dispatch) => {
- // Load configuration first
- requestJson(sample.configUrl, (confError, config) => {
- if (confError) {
- Console.warn(`Error loading config file ${sample.configUrl}`);
- // dispatch(error)
+/**
+ *
+ * @param url
+ * @returns {Promise<any>}
+ */
+function loadRemoteConfig(url) {
+ if (!url) {
+ // TODO: we should return reject with an appropriate error
+ return Promise.resolve(null)
+ }
+
+ return new Promise((resolve, reject) => {
+ requestJson(url, (error, config) => {
+ if (error) {
+ reject(error);
+ }
+ const responseError = detectResponseError(config);
+ if (responseError) {
+ reject(responseError);
return;
}
+ resolve(config);
+ })
+ })
+}
- let requestMethod = requestText;
- if (sample.dataUrl.includes('.json') || sample.dataUrl.includes('.geojson')) {
- requestMethod = requestJson;
- }
+/**
+ *
+ * @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
- requestMethod(sample.dataUrl, (dataError, result) => {
- loadMapCallback(dispatch, dataError, result, sample, config);
- });
- })
+ let requestMethod = requestText;
+ if (url.includes('.json') || url.includes('.geojson')) {
+ requestMethod = requestJson;
}
+
+ // Load data
+ return new Promise((resolve, reject) => {
+ requestMethod(url, (error, result) => {
+ if (error) {
+ reject(error);
+ }
+ const responseError = detectResponseError(result);
+ if (responseError) {
+ reject(responseError);
+ return;
+ }
+ resolve(result);
+ })
+ });
}
/**
@@ -111,21 +281,27 @@ function loadRemoteMap(sample) {
* @returns {function(*)}
*/
export function loadSampleConfigurations(sampleMapId = null) {
- return (dispatch) => {
+ return dispatch => {
requestJson(MAP_CONFIG_URL, (error, samples) => {
if (error) {
- Console.warn(`Error loading sample configuration file ${MAP_CONFIG_URL}`);
+ const {target = {}} = error;
+ const {status, responseText} = target;
+ dispatch(loadRemoteResourceError({status, message: `${responseText} - ${LOADING_SAMPLE_LIST_ERROR_MESSAGE}`}, MAP_CONFIG_URL));
} else {
+ const responseError = detectResponseError(samples);
+ if (responseError) {
+ dispatch(loadRemoteResourceError(responseError, MAP_CONFIG_URL));
+ return;
+ }
+
dispatch(loadMapSampleFile(samples));
// Load the specified map
- if (sampleMapId) {
- const map = samples.find(s => s.id === sampleMapId);
- if (map) {
- dispatch(loadRemoteMap(map));
- dispatch(setLoadingMapStatus(true));
- }
+ const map = sampleMapId && samples.find(s => s.id === sampleMapId);
+ if (map) {
+ dispatch(loadSample(map, false));
}
}
});
}
}
+
diff --git a/examples/demo-app/src/app.js b/examples/demo-app/src/app.js
index c2e982057..356e1ce3a 100644
--- a/examples/demo-app/src/app.js
+++ b/examples/demo-app/src/app.js
@@ -25,7 +25,7 @@ import {connect} from 'react-redux';
import Banner from './components/banner';
import Announcement from './components/announcement';
-import {loadSampleConfigurations} from './actions';
+import {loadRemoteMap, loadSampleConfigurations} from './actions';
import {replaceLoadDataModal} from './factories/load-data-modal';
const KeplerGl = require('kepler.gl/components').injectComponents([
@@ -72,8 +72,18 @@ class App extends Component {
componentWillMount() {
// if we pass an id as part of the url
// we ry to fetch along map configurations
- const {params: {id: sampleMapId} = {}} = this.props;
- this.props.dispatch(loadSampleConfigurations(sampleMapId));
+ const {params: {id} = {}, location: {query = {}}} = this.props;
+
+ // Load sample using its id
+ if (id) {
+ this.props.dispatch(loadSampleConfigurations(id));
+ }
+
+ // Load map using a custom
+ if (query.mapUrl) {
+ this.props.dispatch(loadRemoteMap({dataUrl: query.mapUrl}));
+ }
+
window.addEventListener('resize', this._onResize);
this._onResize();
}
diff --git a/examples/demo-app/src/components/load-data-modal/load-data-modal.js b/examples/demo-app/src/components/load-data-modal/load-data-modal.js
index 970d5b8b5..2f3a766c2 100644
--- a/examples/demo-app/src/components/load-data-modal/load-data-modal.js
+++ b/examples/demo-app/src/components/load-data-modal/load-data-modal.js
@@ -27,9 +27,10 @@ import {LoadingSpinner} from 'kepler.gl/components';
import {themeLT} from 'kepler.gl/styles';
import {Icons} from 'kepler.gl/components/';
-import {LOADING_METHODS, QUERY_TYPES, ASSETS_URL} from '../../constants/default-settings';
+import {LOADING_METHODS, ASSETS_URL, LOADING_METHODS_NAMES} from '../../constants/default-settings';
import SampleMapGallery from './sample-data-viewer';
+import LoadRemoteMap from './load-remote-map';
const propTypes = {
// query options
@@ -39,8 +40,9 @@ const propTypes = {
// call backs
onFileUpload: PropTypes.func.isRequired,
- onLoadSampleData: PropTypes.func.isRequired,
- onSetLoadingMethod: PropTypes.func.isRequired
+ onLoadRemoteMap: PropTypes.func.isRequired,
+ onLoadSample: PropTypes.func.isRequired,
+ onSwitchToLoadingMethod: PropTypes.func.isRequired
};
const ModalTab = styled.div`
@@ -134,7 +136,11 @@ const StyledSpinner = styled.div`
class LoadDataModal extends Component {
render() {
- const {loadingMethod, currentOption, previousMethod, sampleMaps, isMapLoading} = this.props;
+ const {
+ loadingMethod, currentOption, previousMethod,
+ sampleMaps, isMapLoading, onSwitchToLoadingMethod,
+ error
+ } = this.props;
return (
<ThemeProvider theme={themeLT}>
@@ -148,18 +154,26 @@ class LoadDataModal extends Component {
{loadingMethod.id !== 'sample' ? (
<Tabs
method={loadingMethod.id}
- toggleMethod={this.props.onSetLoadingMethod}
+ toggleMethod={this.props.onSwitchToLoadingMethod}
/>
) : null}
{loadingMethod.id === 'upload' ? (
<FileUpload onFileUpload={this.props.onFileUpload} />
) : null}
+ {loadingMethod.id === 'remote' ? (
+ <LoadRemoteMap
+ onLoadRemoteMap={this.props.onLoadRemoteMap}
+ option={this.props.currentOption}
+ error={this.props.error}
+ />
+ ) : null}
{loadingMethod.id === 'sample' ? (
<SampleMapGallery
sampleData={currentOption}
sampleMaps={sampleMaps}
- back={() => this.props.onSetLoadingMethod(previousMethod.id)}
- onLoadSampleData={this.props.onLoadSampleData}/>
+ back={() => onSwitchToLoadingMethod(previousMethod.id)}
+ onLoadSample={this.props.onLoadSample}
+ error={error} />
) : null}
</div>)
}
@@ -187,7 +201,7 @@ const Tabs = ({method, toggleMethod}) => (
) : null
)}
</div>
- <TrySampleData onClick={() => toggleMethod(QUERY_TYPES.sample)} />
+ <TrySampleData onClick={() => toggleMethod(LOADING_METHODS_NAMES.sample)} />
</ModalTab>
);
diff --git a/examples/demo-app/src/components/load-data-modal/load-remote-map.js b/examples/demo-app/src/components/load-data-modal/load-remote-map.js
new file mode 100644
index 000000000..bbb271f0f
--- /dev/null
+++ b/examples/demo-app/src/components/load-data-modal/load-remote-map.js
@@ -0,0 +1,160 @@
+// Copyright (c) 2018 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import React, {Component} from 'react';
+import PropTypes from 'prop-types';
+import styled from 'styled-components';
+import {CORS_LINK, LOADING_URL_MESSAGE} from '../../constants/default-settings';
+
+const propTypes = {
+ onLoadRemoteMap: PropTypes.func.isRequired
+};
+
+const StyledDescription = styled.div`
+ font-size: 14px;
+ color: ${props => props.theme.labelColorLT};
+ line-height: 18px;
+ margin-bottom: 12px;
+`;
+
+const InputForm = styled.div`
+ flex-grow: 1;
+ padding: 32px;
+ background-color: ${props => props.theme.panelBackgroundLT};
+`;
+
+const StyledInput = styled.input`
+ width: 100%;
+ padding: ${props => props.theme.inputPadding};
+ color: ${props => props.error ? 'red' : props.theme.titleColorLT};
+ height: ${props => props.theme.inputBoxHeight};
+ border: 0;
+ outline: 0;
+ font-size: ${props => props.theme.inputFontSize};
+
+ :active,
+ :focus,
+ &.focus,
+ &.active {
+ outline: 0;
+ }
+`;
+
+const StyledFromGroup = styled.div`
+ margin-top: 30px;
+ display: flex;
+ flex-direction: row;
+`;
+
+export const StyledInputLabel = styled.div`
+ font-size: 11px;
+ color: ${props => props.theme.textColorLT};
+ letter-spacing: 0.2px;
+ ul {
+ padding-left: 12px;
+ }
+`;
+
+export const StyledBtn = styled.button`
+ background-color: ${props => props.theme.primaryBtnActBgd};
+ color: ${props => props.theme.primaryBtnActColor};
+`;
+
+export const StyledError = styled.div`
+ color: red;
+`;
+
+export const StyledErrorDescription = styled.div`
+ font-size: 14px;
+`;
+
+const Error = ({error, url}) => (
+ <StyledError>
+ <StyledErrorDescription>{url}</StyledErrorDescription>
+ <StyledErrorDescription>{error.message}</StyledErrorDescription>
+ </StyledError>
+);
+
+class LoadRemoteMap extends Component {
+
+ constructor(props) {
+ super(props);
+
+ this.state = {
+ dataUrl: ''
+ };
+ }
+ onMapUrlChange = (e) => {
+ // TODO: validate url
+ this.setState({
+ dataUrl: e.target.value
+ });
+ };
+
+ onLoadRemoteMap = () => {
+ const {dataUrl} = this.state;
+ if (!dataUrl) {
+ return;
+ }
+
+ this.props.onLoadRemoteMap({dataUrl});
+ };
+
+ render() {
+ return (
+ <div>
+ <InputForm>
+ <StyledDescription>Load your map using your custom URL</StyledDescription>
+ <StyledInputLabel>
+ {LOADING_URL_MESSAGE}
+ </StyledInputLabel>
+ <StyledInputLabel>
+ Examples:
+ <ul>
+ <li>https://your.map.url/map.json</li>
+ <li>http://your.map.url/data.csv</li>
+ </ul>
+ </StyledInputLabel>
+ <StyledInputLabel>
+ * CORS policy must be defined on your custom url domain in order to be accessible.
+ For more info <a rel="noopener noreferrer" target="_blank" href={`${CORS_LINK}`}>click here</a>
+ </StyledInputLabel>
+ <StyledFromGroup>
+ <StyledInput
+ onChange={this.onMapUrlChange}
+ type="text"
+ placeholder="File Url"
+ value={this.state.dataUrl}
+ error={this.props.error}
+ />
+ <StyledBtn type="submit" onClick={this.onLoadRemoteMap}>
+ Fetch
+ </StyledBtn>
+ </StyledFromGroup>
+ {this.props.error && (<Error error={this.props.error} url={this.props.option.dataUrl} />)}
+ </InputForm>
+ </div>
+ );
+ }
+}
+
+LoadRemoteMap.propTypes = propTypes;
+
+export default LoadRemoteMap;
diff --git a/examples/demo-app/src/components/load-data-modal/sample-data-viewer.js b/examples/demo-app/src/components/load-data-modal/sample-data-viewer.js
index 60e953df1..da334de18 100644
--- a/examples/demo-app/src/components/load-data-modal/sample-data-viewer.js
+++ b/examples/demo-app/src/components/load-data-modal/sample-data-viewer.js
@@ -28,7 +28,7 @@ const numFormat = format(',');
const propTypes = {
sampleData: PropTypes.object.isRequired,
- onLoadSampleData: PropTypes.func.isRequired,
+ onLoadSample: PropTypes.func.isRequired,
back: PropTypes.func.isRequired
};
@@ -108,11 +108,17 @@ const StyledImageCaption = styled.div`
opacity: 0;
`;
+const StyledError = styled.div`
+ color: red;
+ font-size: 14px;
+ margin-bottom: 16px;
+`;
+
const SampleMap = ({sample, onClick}) => (
<StyledSampleMap className="sample-map-gallery__item">
<div className="sample-map">
<div className="sample-map__image" onClick={onClick}>
- <img src={sample.imageUrl} />
+ {sample.imageUrl && <img src={sample.imageUrl} />}
</div>
<div className="sample-map__title">{sample.label}</div>
<div className="sample-map__size">{`${numFormat(sample.size)} rows`}</div>
@@ -123,18 +129,23 @@ const SampleMap = ({sample, onClick}) => (
</StyledSampleMap>
);
-const SampleMapGallery = ({sampleData, sampleMaps, onLoadSampleData, back}) => (
+const SampleMapGallery = ({sampleData, sampleMaps, onLoadSample, back, error}) => (
<div className="sample-data-modal">
<BackLink onClick={back}>
<Icons.LeftArrow height="12px" />
<span>Back</span>
</BackLink>
+ {error && (
+ <StyledError>
+ {error.message}
+ </StyledError>
+ )}
<StyledSampleGallery className="sample-map-gallery">
- {sampleMaps.map(sp => (
+ {sampleMaps.filter(sp => sp.visible).map(sp => (
<SampleMap
sample={sp}
key={sp.id}
- onClick={() => onLoadSampleData(sp)}
+ onClick={() => onLoadSample(sp)}
/>
))}
</StyledSampleGallery>
diff --git a/examples/demo-app/src/constants/default-settings.js b/examples/demo-app/src/constants/default-settings.js
index 68c19e877..28cecc66b 100644
--- a/examples/demo-app/src/constants/default-settings.js
+++ b/examples/demo-app/src/constants/default-settings.js
@@ -26,6 +26,11 @@ export const ASSETS_URL = 'https://d1a3f4spazzrp4.cloudfront.net/kepler.gl/';
export const DATA_URL = 'https://raw.githubusercontent.com/uber-web/kepler.gl-data/master/';
+/*
+ * If you want to add more samples, feel free to edit the json file on github kepler.gl data repo
+ */
+export const MAP_CONFIG_URL = `${DATA_URL}samples.json?nocache=${(new Date()).getTime()}`;
+
export const QUERY_TYPES = keyMirror({
file: null,
sample: null
@@ -36,9 +41,15 @@ export const QUERY_OPTIONS = keyMirror({
geojson: null
});
+export const LOADING_METHODS_NAMES = keyMirror({
+ upload: null,
+ remote: null,
+ sample: null
+});
+
export const LOADING_METHODS = [
{
- id: 'upload',
+ id: LOADING_METHODS_NAMES.upload,
label: 'Load Your Data',
options: [
{
@@ -61,7 +72,11 @@ export const LOADING_METHODS = [
]
},
{
- id: 'sample',
+ id: LOADING_METHODS_NAMES.remote,
+ label: 'Load Map using URL'
+ },
+ {
+ id: LOADING_METHODS_NAMES.sample,
label: 'Sample Data',
options: [
// Dynamically populated
@@ -69,4 +84,9 @@ export const LOADING_METHODS = [
}
];
+export const LOADING_SAMPLE_LIST_ERROR_MESSAGE = 'Not able to load sample gallery';
+export const LOADING_SAMPLE_ERROR_MESSAGE = 'Not able to load sample';
+export const LOADING_URL_MESSAGE = 'You can use the following formats: CSV | JSON | Kepler.gl config json. Make sure the url contains the file extension.';
+export const CORS_LINK = 'https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS';
+
export const DEFAULT_LOADING_METHOD = LOADING_METHODS[0];
diff --git a/examples/demo-app/src/factories/load-data-modal.js b/examples/demo-app/src/factories/load-data-modal.js
index d12ce569d..6e59d4996 100644
--- a/examples/demo-app/src/factories/load-data-modal.js
+++ b/examples/demo-app/src/factories/load-data-modal.js
@@ -23,8 +23,9 @@ import LoadDataModal from '../components/load-data-modal/load-data-modal';
import {withState} from 'kepler.gl/components';
import {
- loadSampleMap,
- setLoadingMethod
+ loadRemoteMap,
+ loadSample,
+ switchToLoadingMethod
} from '../actions';
export const CustomLoadDataModalFactory = () =>
@@ -32,8 +33,9 @@ export const CustomLoadDataModalFactory = () =>
[],
state => ({...state.demo.app}),
{
- onSetLoadingMethod: setLoadingMethod,
- onLoadSampleData: loadSampleMap
+ onSwitchToLoadingMethod: switchToLoadingMethod,
+ onLoadSample: loadSample,
+ onLoadRemoteMap: loadRemoteMap
}
)(LoadDataModal);
diff --git a/examples/demo-app/src/main.js b/examples/demo-app/src/main.js
index 36b4791e2..a33de863b 100644
--- a/examples/demo-app/src/main.js
+++ b/examples/demo-app/src/main.js
@@ -35,10 +35,11 @@ const history = syncHistoryWithStore(hashHistory, store);
const Root = () => (
<Provider store={store}>
<Router history={history}>
- <Route path="/(:id)" component={App} />
- <Route path="/demo/(:id)" component={App} />
+ <Route name="map" path="/" component={App} />
+ <Route name="demo" path="/demo/(:id)" component={App} />
+ <Route name="map" path="/map" component={App} />
</Router>
</Provider>
);
-render(<Root />, document.body.appendChild(document.createElement('div')));
\ No newline at end of file
+render(<Root />, document.body.appendChild(document.createElement('div')));
diff --git a/examples/demo-app/src/reducers.js b/examples/demo-app/src/reducers.js
index 11da51477..c7c6fccdc 100644
--- a/examples/demo-app/src/reducers.js
+++ b/examples/demo-app/src/reducers.js
@@ -29,11 +29,12 @@ import {
INIT,
SET_LOADING_METHOD,
LOAD_MAP_SAMPLE_FILE,
- LOAD_REMOTE_FILE_DATA_SUCCESS,
- SET_SAMPLE_LOADING_STATUS
+ LOAD_REMOTE_RESOURCE_SUCCESS,
+ SET_SAMPLE_LOADING_STATUS, LOAD_REMOTE_RESOURCE_ERROR
} from './actions';
import {DEFAULT_LOADING_METHOD, LOADING_METHODS} from './constants/default-settings';
+import {generateHashId} from 'kepler.gl/utils/utils';
// INITIAL_APP_STATE
const initialAppState = {
@@ -43,7 +44,12 @@ const initialAppState = {
currentOption: DEFAULT_LOADING_METHOD.options[0],
previousMethod: null,
sampleMaps: [], // this is used to store sample maps fetch from a remote json file
- isMapLoading: false // determine whether we are loading a sample map
+ 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
@@ -55,7 +61,8 @@ export const appReducer = handleActions({
[SET_LOADING_METHOD]: (state, action) => ({
...state,
previousMethod: state.loadingMethod,
- loadingMethod: LOADING_METHODS.find(({id}) => id === action.method)
+ loadingMethod: LOADING_METHODS.find(({id}) => id === action.method),
+ error: null
}),
[LOAD_MAP_SAMPLE_FILE]: (state, action) => ({
...state,
@@ -64,6 +71,12 @@ export const appReducer = handleActions({
[SET_SAMPLE_LOADING_STATUS]: (state, action) => ({
...state,
isMapLoading: action.isMapLoading
+ }),
+ [LOAD_REMOTE_RESOURCE_ERROR]: (state, action) => ({
+ ...state,
+ error: action.error,
+ currentOption: {dataUrl: action.url},
+ isMapLoading: false
})
}, initialAppState);
@@ -76,11 +89,18 @@ const demoReducer = combineReducers({
});
// this can be moved into a action and call kepler.gl action
+/**
+ *
+ * @param state
+ * @param action {map: resultset, config, map}
+ * @returns {{app: {isMapLoading: boolean}, keplerGl: {map: (state|*)}}}
+ */
export const loadRemoteFileDataSuccess = (state, action) => {
- const datasetId = action.map.id;
- const {dataUrl} = action.map;
+ // TODO: replace generate with a different function
+ const datasetId = action.options.id || generateHashId(6);
+ const {dataUrl} = action.options;
let processorMethod = Processor.processCsvData;
-
+ // TODO: create helper to determine file ext eligibility
if (dataUrl.includes('.json') || dataUrl.includes('.geojson')) {
processorMethod = Processor.processGeojson;
}
@@ -92,7 +112,8 @@ export const loadRemoteFileDataSuccess = (state, action) => {
data: processorMethod(action.response)
};
- const config = KeplerGlSchema.parseSavedConfig(action.config);
+ const config = action.config ?
+ KeplerGlSchema.parseSavedConfig(action.config) : null;
const keplerGlInstance = combineUpdaters.addDataToMapComposed(
state.keplerGl.map, // "map" is the id of your kepler.gl instance
@@ -118,7 +139,7 @@ export const loadRemoteFileDataSuccess = (state, action) => {
};
const composedUpdaters = {
- [LOAD_REMOTE_FILE_DATA_SUCCESS]: loadRemoteFileDataSuccess
+ [LOAD_REMOTE_RESOURCE_SUCCESS]: loadRemoteFileDataSuccess
};
const composedReducer = (state, action) => {
diff --git a/package.json b/package.json
index 5e2e5d184..6acf11274 100644
--- a/package.json
+++ b/package.json
@@ -28,13 +28,12 @@
"fast-test": "NODE_ENV=test babel-tape-runner ./test/node.js && NODE_ENV=test babel-tape-runner -r ./test/setup-browser-env.js ./test/browser.js",
"test-browser": "NODE_ENV=test babel-tape-runner -r ./test/setup-browser-env.js ./test/browser.js",
"test-node": "NODE_ENV=test babel-tape-runner ./test/node.js | tap-spec",
- "test": "npm run test-node && npm run test-browser",
+ "test": "npm run lint && npm run test-node && npm run test-browser",
"cover": "nyc --reporter html --reporter cobertura --reporter json-summary npm test && nyc report",
"start": "npm run install-and-start -- examples/demo-app start-local",
"start:custom-layer": "npm run install-and-start -- examples/custom-layer start-local",
"start:open-modal": "npm run install-and-start -- examples/open-modal start-local",
"start:custom-reducer": "npm run install-and-start -- examples/custom-reducer start-local",
- "start:": "npm run install-and-start -- examples/open-modal start-local",
"build": "rm -fr dist && uber-licence && babel src --out-dir dist --plugins=transform-es2015-modules-commonjs --source-maps inline",
"lint": "eslint src examples/**/src website/src",
"check-licence": "uber-licence --dry",
diff --git a/src/reducers/vis-state-updaters.js b/src/reducers/vis-state-updaters.js
index 80ad41d98..81d30dc4d 100644
--- a/src/reducers/vis-state-updaters.js
+++ b/src/reducers/vis-state-updaters.js
@@ -29,7 +29,6 @@ import {loadFilesErr} from 'actions/vis-state-actions';
import {addDataToMap} from 'actions';
// Utils
-import {generateHashId} from 'utils/utils';
import {
getDefaultInteraction,
findFieldsToShow
@@ -48,8 +47,6 @@ import {
calculateLayerData
} from 'utils/layer-utils/layer-utils';
-import {getFileHandler} from 'processors/file-handler';
-
import {
mergeFilters,
mergeLayers,
@@ -64,6 +61,7 @@ import {
// };
import {Layer, LayerClasses} from 'layers';
+import {processFileToLoad} from '/utils/file-utils';
// react-palm
// disable capture exception for react-palm call to withTask
@@ -934,15 +932,7 @@ function closeSpecificMapAtIndex(state, action) {
export const loadFilesUpdater = (state, action) => {
const {files} = action;
- const filesToLoad = files.map(fileBlob => ({
- fileBlob,
- info: {
- id: generateHashId(4),
- label: fileBlob.name,
- size: fileBlob.size
- },
- handler: getFileHandler(fileBlob)
- }));
+ const filesToLoad = files.map(fileBlob => processFileToLoad(fileBlob));
// reader -> parser -> augment -> receiveVisData
const loadFileTasks = [
diff --git a/examples/demo-app/src/constants/sample-maps.js b/src/utils/file-utils.js
similarity index 78%
rename from examples/demo-app/src/constants/sample-maps.js
rename to src/utils/file-utils.js
index 5d3352faa..aa5725af4 100644
--- a/examples/demo-app/src/constants/sample-maps.js
+++ b/src/utils/file-utils.js
@@ -18,9 +18,17 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import {DATA_URL} from './default-settings';
+import {generateHashId} from 'utils/utils';
+import {getFileHandler} from 'processors/file-handler';
-/*
- * If you want to add more samples, feel free to edit the json file on github kepler.gl data repo
- */
-export const MAP_CONFIG_URL = `${DATA_URL}samples.json?nocache=${(new Date()).getTime()}`;
+export const processFileToLoad = (file) => {
+ return {
+ fileBlob: file,
+ info: {
+ id: generateHashId(4),
+ label: file.name,
+ size: file.size
+ },
+ handler: getFileHandler(file)
+ }
+};
diff --git a/website/src/reducers/analytics.js b/website/src/reducers/analytics.js
index cb063418c..07774b03a 100644
--- a/website/src/reducers/analytics.js
+++ b/website/src/reducers/analytics.js
@@ -60,8 +60,7 @@ const getFilterType = (store, idx, value) => {
};
const trackingInformation = {
- [ActionTypes.LOAD_FILES]: ({files}) =>
- files.map(({size, type}) => ({size, type})),
+ [ActionTypes.LOAD_FILES]: payload => payload.files.map(({size, type}) => ({size, type})),
[ActionTypes.LAYER_TYPE_CHANGE]: ({newType}) => ({
newType
}),
@@ -91,12 +90,13 @@ const EXCLUDED_ACTIONS = [ActionTypes.LAYER_HOVER, ActionTypes.UPDATE_MAP];
const analyticsMiddleware = store => next => action => {
if (window.gtag && !EXCLUDED_ACTIONS.includes(action.type)) {
+ const payload = action.paylod || action;
// eslint-disable-next-line no-undef
window.gtag('event', 'action', {
event_category: action.type,
event_label: trackingInformation[action.type]
? JSON.stringify(
- trackingInformation[action.type](action.payload, store)
+ trackingInformation[action.type](payload, store)
)
: undefined
});
diff --git a/yarn.lock b/yarn.lock
index c733dc0af..aad0ae748 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -8,7 +8,7 @@
dependencies:
regenerator-runtime "^0.12.0"
-"@deck.gl/core@^6.2.3", "@deck.gl/core@^6.2.4":
+"@deck.gl/core@^6.2.4":
version "6.2.4"
resolved "https://registry.yarnpkg.com/@deck.gl/core/-/core-6.2.4.tgz#c569854633f3087dea9866276ad68812671487d7"
dependencies:
@@ -19,17 +19,17 @@
seer "^0.2.4"
viewport-mercator-project "^6.0.0"
-"@deck.gl/layers@^6.2.3":
+"@deck.gl/layers@^6.2.4":
version "6.2.4"
- resolved "https://registry.yarnpkg.com/@deck.gl/layers/-/layers-6.2.4.tgz#c5c9473a174cd45d01cd404177cbff20c9da1f56"
+ resolved "https://unpm.uberinternal.com/@deck.gl%2flayers/-/layers-6.2.4.tgz#c5c9473a174cd45d01cd404177cbff20c9da1f56"
dependencies:
"@deck.gl/core" "^6.2.4"
d3-hexbin "^0.2.1"
earcut "^2.0.6"
-"@deck.gl/react@^6.2.3":
+"@deck.gl/react@^6.2.4":
version "6.2.4"
- resolved "https://registry.yarnpkg.com/@deck.gl/react/-/react-6.2.4.tgz#fab68aaac23a1429c4ad84dd09879ca764543f2f"
+ resolved "https://unpm.uberinternal.com/@deck.gl%2freact/-/react-6.2.4.tgz#fab68aaac23a1429c4ad84dd09879ca764543f2f"
dependencies:
"@deck.gl/core" "^6.2.4"
prop-types "^15.6.0"
@@ -2294,13 +2294,13 @@ decamelize@^1.0.0, decamelize@^1.1.1, decamelize@^1.1.2:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
- version "6.2.3"
- resolved "https://registry.yarnpkg.com/deck.gl/-/deck.gl-6.2.3.tgz#7f32b9225d06148ed3f7e945ef51b970f4caa302"
+deck.gl@^6.2.3:
+ version "6.2.4"
+ resolved "https://unpm.uberinternal.com/deck.gl/-/deck.gl-6.2.4.tgz#8652f492a6d6e30dc32925355d6f8f820967a70c"
dependencies:
- "@deck.gl/core" "^6.2.3"
- "@deck.gl/layers" "^6.2.3"
- "@deck.gl/react" "^6.2.3"
+ "@deck.gl/core" "^6.2.4"
+ "@deck.gl/layers" "^6.2.4"
+ "@deck.gl/react" "^6.2.4"
decode-uri-component@^0.2.0:
version "0.2.0"