Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
add checkbox to export map config as json
Shan Hec7240e740ebc
diff --git a/examples/demo-app/src/app.js b/examples/demo-app/src/app.jsindex d030b1f59..3998586c6 100644--- a/examples/demo-app/src/app.js+++ b/examples/demo-app/src/app.js@@ -36,6 +36,7 @@ const MAPBOX_TOKEN = process.env.MapboxAccessToken; // eslint-disable-lineimport sampleTripData from './data/sample-trip-data';import sampleGeojson from './data/sample-geojson.json';import {updateVisData, addDataToMap} from 'kepler.gl/actions';+import Processors from 'kepler.gl/processors';/* eslint-enable no-unused-vars */class App extends Component {@@ -46,14 +47,8 @@ class App extends Component {// if (sampleMapId) {this.props.dispatch(loadSampleConfigurations(sampleMapId));}- componentDidMount() {- // if we pass an id as part f the url- // we ry to fetch along map configurations- // const {params: {id: sampleMapId} = {}} = this.props;- // if (sampleMapId) {- // this.props.dispatch(loadSampleConfigurations(sampleMapId));- // }+ componentDidMount() {/*** ENABLE THE FOLLOWING CODE TO PRE-POLUATE KEPLER.GL INSTANCE*/@@ -102,7 +97,7 @@ class App extends Component {// this.props.dispatch(// updateVisData({// info: {label: 'SF Zip Geo'},- // data: Processor.processGeojson(sampleGeojson)+ // data: Processors.processGeojson(sampleGeojson)// })// );}diff --git a/src/actions/ui-state-actions.js b/src/actions/ui-state-actions.jsindex 3b809c986..be366cb9f 100644--- a/src/actions/ui-state-actions.js+++ b/src/actions/ui-state-actions.js@@ -27,6 +27,7 @@ const {SET_EXPORT_DATA_TYPE,SET_EXPORT_FILTERED,SET_EXPORT_IMAGE_DATA_URI,+ SET_EXPORT_CONFIG,SET_EXPORT_SELECTED_DATASET,SET_RATIO,SET_RESOLUTION,@@ -51,7 +52,8 @@ const [// export datasetExportSelectedDataset,setExportDataType,- setExportFiltered+ setExportFiltered,+ setExportConfig] = [TOGGLE_SIDE_PANEL,TOGGLE_MODAL,@@ -64,11 +66,12 @@ const [CLEANUP_EXPORT_IMAGE,SET_EXPORT_SELECTED_DATASET,SET_EXPORT_DATA_TYPE,- SET_EXPORT_FILTERED+ SET_EXPORT_FILTERED,+ SET_EXPORT_CONFIG].map(a => createAction(a));export {- toggleSidePanel, toggleModal, openDeleteModal,+ toggleSidePanel, toggleModal, openDeleteModal, setExportConfig,setRatio, setResolution, toggleLegend, startExportingImage, setExportImageDataUri, cleanupExportImage,setExportSelectedDataset, setExportDataType, setExportFiltered};diff --git a/src/components/common/checkbox.js b/src/components/common/checkbox.jsindex a77ddf12a..cf4948ae4 100644--- a/src/components/common/checkbox.js+++ b/src/components/common/checkbox.js@@ -50,11 +50,15 @@ const defaultProps = {label: ''};-const StyledLabel = styled.label`+const StyledSwitchInput = styled.label`${props =>props.secondary ? props.theme.secondarySwitch : props.theme.inputSwitch};`;+const StyledCheckboxInput = styled.label`+ ${props => props.theme.inputCheckbox}+`;+const HiddenInput = styled.input`position: absolute;display: none;@@ -93,12 +97,14 @@ class Checkbox extends React.Component {...pick(this.props, ['checked', 'disabled', 'secondary']),htmlFor: this.props.id};++ const LabelElement = this.props.type === 'checkbox' ? StyledCheckboxInput : StyledSwitchInput;return (<StyledCheckbox className="kg-checkbox"><HiddenInput {...inputProps} />- <StyledLabel chassName="kg-checkbox__label" {...labelProps}>+ <LabelElement chassName="kg-checkbox__label" {...labelProps}>{this.props.label}- </StyledLabel>+ </LabelElement></StyledCheckbox>);}diff --git a/src/components/common/switch.js b/src/components/common/switch.jsindex 7d60587ff..fe8612576 100644--- a/src/components/common/switch.js+++ b/src/components/common/switch.js@@ -36,7 +36,7 @@ const propTypes = {const Switch = (props) => {const switchProps = {...props,- switch: true+ switch: props.type !== 'checkbox'};return <Checkbox {...switchProps}/>;diff --git a/src/components/modal-container.js b/src/components/modal-container.jsindex 7c3a07235..bbb3a4314 100644--- a/src/components/modal-container.js+++ b/src/components/modal-container.js@@ -27,6 +27,7 @@ import document from 'global/document';import ModalDialog from './common/modal';import {formatCsv} from 'processors/data-processor';+import KeplerGlSchema from '@uber/kepler.gl/schemas';// modalsimport DeleteDatasetModalFactory from './modals/delete-data-modal';@@ -129,36 +130,59 @@ export default function ModalContainerFactory(this._closeModal();};+ _downloadFile(data, type, filename) {+ const fileBlob = new Blob([data], {type});+ const url = URL.createObjectURL(fileBlob);+ const link = document.createElement('a');++ link.setAttribute('href', url);+ link.setAttribute('download', filename);+ link.click();+ URL.revokeObjectURL(url);+ }+_onExportData = () => {- const {datasets} = this.props.visState;- const {selectedDataset, dataType, filtered} = this.props.uiState.exportData;+ const {mapStyle, visState, mapState, uiState} = this.props;+ const {datasets} = visState;+ const {selectedDataset, dataType, filtered, config} = uiState.exportData;// get the selected dataconst filename = 'kepler-gl';- const selectedData = datasets[selectedDataset];- if (!selectedData) {+ const selectedDatasets = datasets[selectedDataset] ? [datasets[selectedDataset]] : Object.values(datasets);+ if (!selectedDatasets.length) {// error: selected dataset not found.this._closeModal();}- const {allData, data, fields, label} = selectedData;- const exportData = filtered ? data : allData;- // start to export data according to selected data type- switch (dataType) {- case EXPORT_DATA_TYPE.CSV: {- const type = 'text/csv';- const csv = formatCsv(exportData, fields);- const csvFile = new Blob([csv], {type});- const url = URL.createObjectURL(csvFile);- const link = document.createElement('a');- link.setAttribute('href', url);- link.setAttribute('download', `${filename}_${label}.csv`);- link.click();- URL.revokeObjectURL(url);- break;++ selectedDatasets.forEach(selectedData => {+ const {allData, data, fields, label} = selectedData;+ const exportData = filtered ? data : allData;+ // start to export data according to selected data type+ switch (dataType) {+ case EXPORT_DATA_TYPE.CSV: {+ const type = 'text/csv';+ const csv = formatCsv(exportData, fields);+ this._downloadFile(csv, type, `${filename}_${label}.csv`);+ break;+ }+ // TODO: support more different data type later.+ default:+ break;}- // TODO: support more different data type later.- default:- break;++ });++ if (config) {+ const keplerGlConfig = KeplerGlSchema.getConfigToSave(+ {mapStyle, visState, mapState, uiState}+ );++ this._downloadFile(+ JSON.stringify(keplerGlConfig, null, 2),+ 'application/json',+ 'kepler-gl_config.json'+ );}+this._closeModal();};@@ -265,17 +289,15 @@ export default function ModalContainerFactory(break;case EXPORT_DATA_ID:- const {selectedDataset, dataType, filtered} = uiState.exportData;template = (<ExportDataModal+ {...uiState.exportData}datasets={datasets}- selectedDataset={selectedDataset}- dataType={dataType}- filtered={filtered}onClose={this._closeModal}onChangeExportDataType={this.props.uiStateActions.setExportDataType}onChangeExportSelectedDataset={this.props.uiStateActions.setExportSelectedDataset}onChangeExportFiltered={this.props.uiStateActions.setExportFiltered}+ onChangeExportConfig={this.props.uiStateActions.setExportConfig}/>);modalProps = {diff --git a/src/components/modals/export-data-modal.js b/src/components/modals/export-data-modal.jsindex 162d10fee..18cc77f3e 100644--- a/src/components/modals/export-data-modal.js+++ b/src/components/modals/export-data-modal.js@@ -25,6 +25,7 @@ import styled from 'styled-components';import {EXPORT_DATA_TYPE_OPTIONS} from 'constants/default-settings';import {FileType} from 'components/common/icons';import {StyledModalContent} from 'components/common/styled-components';+import Switch from 'components/common/switch';const StyledExportDataSection = styled.div`display: flex;@@ -160,11 +161,11 @@ const propTypes = {const getDataRowCount = (datasets, selectedDataset, filtered) => {const selectedData = datasets[selectedDataset];if (!selectedData) {- return 0;+ return `${Object.keys(datasets).length} Files ` ;}const {allData, data} = selectedData;const rowCount = filtered ? data.length : allData.length;- return rowCount.toLocaleString('en');+ return `${rowCount.toLocaleString('en')} Rows`;};const ExportDataModal = ({@@ -172,75 +173,97 @@ const ExportDataModal = ({selectedDataset,dataType,filtered,+ config,// callbacks:onChangeExportDataType,onChangeExportSelectedDataset,onChangeExportFiltered,+ onChangeExportConfig,onClose}) => (<div className="export-data-modal"><StyledModalContent>- <StyledExportDataSection>- <div className="description">- <div className="title">- Dataset+ <div>+ <StyledExportDataSection>+ <div className="description">+ <div className="title">+ Dataset+ </div>+ <div className="subtitle">+ Choose the datasets you want to export+ </div></div>- <div className="subtitle">- Choose the datasets you want to export+ <div className="selection">+ <select value={selectedDataset} onChange={e => onChangeExportSelectedDataset({dataset: e.target.value})}>+ {['All'].concat(Object.keys(datasets)).map(d => (+ <option key={d} value={d}>{(datasets[d] && datasets[d].label) || d}</option>+ ))}+ </select></div>- </div>- <div className="selection">- <select value={selectedDataset} onChange={e => onChangeExportSelectedDataset({dataset: e.target.value})}>- {['Select dataset'].concat(Object.keys(datasets)).map(d => (- <option key={d} value={d}>{d}</option>- ))}- </select>- </div>- </StyledExportDataSection>-- <StyledExportDataSection>- <div className="description">- <div className="title">- Data Type+ </StyledExportDataSection>++ <StyledExportDataSection>+ <div className="description">+ <div className="title">+ Data Type+ </div>+ <div className="subtitle">+ Choose the type of data you want to export+ </div>+ </div>+ <div className="selection">+ {EXPORT_DATA_TYPE_OPTIONS.map(op =>+ <StyledDataType+ key={op.id}+ selected={dataType === op.id}+ available={op.available}+ onClick={() => op.available && onChangeExportDataType({dataType: op.id})}+ >+ <FileType ext={op.label} height="80px" fontSize="11px" />+ </StyledDataType>+ )}+ </div>+ </StyledExportDataSection>++ <StyledExportDataSection>+ <div className="description">+ <div className="title">+ Filter Data+ </div>+ <div className="subtitle">+ You can choose exporting original data or filtered data+ </div></div>- <div className="subtitle">- Choose the type of data you want to export+ <div className="selection">+ <StyledFilteredDataOption selected={!filtered} onClick={() => onChangeExportFiltered({filtered: false})}>+ <div className="filtered-title">Unfiltered Data</div>+ <div className="filtered-subtitle">{getDataRowCount(datasets, selectedDataset, false)}</div>+ </StyledFilteredDataOption>+ <StyledFilteredDataOption selected={filtered} onClick={() => onChangeExportFiltered({filtered: true})}>+ <div className="filtered-title">Filtered Data</div>+ <div className="filtered-subtitle">{getDataRowCount(datasets, selectedDataset, true)}</div>+ </StyledFilteredDataOption></div>- </div>- <div className="selection">- {EXPORT_DATA_TYPE_OPTIONS.map(op =>- <StyledDataType- key={op.id}- selected={dataType === op.id}- available={op.available}- onClick={() => op.available && onChangeExportDataType({dataType: op.id})}- >- <FileType ext={op.label} height="80px" fontSize="11px" />- </StyledDataType>- )}- </div>- </StyledExportDataSection>-- <StyledExportDataSection>- <div className="description">- <div className="title">- Filter Data+ </StyledExportDataSection>++ <StyledExportDataSection>+ <div className="description">+ <div className="title">+ Include Map Config+ </div>+ <div className="subtitle">+ Export current map config as a Json file+ </div></div>- <div className="subtitle">- You can choose exporting original data or filtered data+ <div className="selection">+ <Switch type="checkbox"+ id="export-map-config"+ checked={config}+ onChange={onChangeExportConfig}/></div>- </div>- <div className="selection">- <StyledFilteredDataOption selected={!filtered} onClick={() => onChangeExportFiltered({filtered: false})}>- <div className="filtered-title">Unfiltered Data</div>- <div className="filtered-subtitle">{`${getDataRowCount(datasets, selectedDataset, false)} Rows`}</div>- </StyledFilteredDataOption>- <StyledFilteredDataOption selected={filtered} onClick={() => onChangeExportFiltered({filtered: true})}>- <div className="filtered-title">Filtered Data</div>- <div className="filtered-subtitle">{`${getDataRowCount(datasets, selectedDataset, true)} Rows`}</div>- </StyledFilteredDataOption>- </div>- </StyledExportDataSection>+ </StyledExportDataSection>++ </div></StyledModalContent></div>);diff --git a/src/components/side-panel/panel-header.js b/src/components/side-panel/panel-header.jsindex d20bce4f5..460d8bdfb 100644--- a/src/components/side-panel/panel-header.js+++ b/src/components/side-panel/panel-header.js@@ -88,6 +88,7 @@ const StyledPanelDropdown = styled.div`opacity: ${props => props.show ? 1 : 0};transform: translateX(calc(-50% + 20px));pointer-events: ${props => props.show ? 'all' : 'none'};+ z-index: 1000;.save-export-dropdown__inner {box-shadow: none;@@ -221,7 +222,8 @@ function PanelHeaderFactory() {actionItems,onSaveMap,onExportImage,- onExportData+ onExportData,+ onExportConfig} = this.props;return (<StyledPanelHeader className="side-panel__panel-header">@@ -248,6 +250,7 @@ function PanelHeaderFactory() {onSaveMap={onSaveMap}onExportData={onExportData}onExportImage={onExportImage}+ onExportConfig={onExportConfig}/>) : null}</div>diff --git a/src/constants/action-types.js b/src/constants/action-types.jsindex c54761597..0bfd2a34d 100644--- a/src/constants/action-types.js+++ b/src/constants/action-types.js@@ -86,6 +86,7 @@ const ActionTypes = keyMirror({SET_EXPORT_SELECTED_DATASET: null,SET_EXPORT_DATA_TYPE: null,SET_EXPORT_FILTERED: null,+ SET_EXPORT_CONFIG: null,// allINIT: null,diff --git a/src/constants/default-settings.js b/src/constants/default-settings.jsindex 4caf4b842..2f99a5584 100644--- a/src/constants/default-settings.js+++ b/src/constants/default-settings.js@@ -475,11 +475,11 @@ export const DEFAULT_EXPORT_IMAGE_NAME = 'kepler-gl.png';export const WAIT_FOR_LOADING = 1200;export const EXPORT_DATA_TYPE = keyMirror({- CSV: null,- SHAPEFILE: null,- JSON: null,- GEOJSON: null,- TOPOJSON: null+ CSV: null+ // SHAPEFILE: null,+ // JSON: null,+ // GEOJSON: null,+ // TOPOJSON: null});export const EXPORT_DATA_TYPE_OPTIONS = [@@ -487,25 +487,25 @@ export const EXPORT_DATA_TYPE_OPTIONS = [id: EXPORT_DATA_TYPE.CSV,label: 'csv',available: true- },- {- id: EXPORT_DATA_TYPE.SHAPEFILE,- label: 'shapefile',- available: false- },- {- id: EXPORT_DATA_TYPE.JSON,- label: 'json',- available: false- },- {- id: EXPORT_DATA_TYPE.GEOJSON,- label: 'geojson',- available: false- },- {- id: EXPORT_DATA_TYPE.TOPOJSON,- label: 'topojson',- available: false}+ // {+ // id: EXPORT_DATA_TYPE.SHAPEFILE,+ // label: 'shapefile',+ // available: false+ // },+ // {+ // id: EXPORT_DATA_TYPE.JSON,+ // label: 'json',+ // available: false+ // },+ // {+ // id: EXPORT_DATA_TYPE.GEOJSON,+ // label: 'geojson',+ // available: false+ // },+ // {+ // id: EXPORT_DATA_TYPE.TOPOJSON,+ // label: 'topojson',+ // available: false+ // }];diff --git a/src/processors/data-processor.js b/src/processors/data-processor.jsindex a72fb0813..ba616c92e 100644--- a/src/processors/data-processor.js+++ b/src/processors/data-processor.js@@ -24,7 +24,7 @@ import {console as globalConsole} from 'global/window';import assert from 'assert';import {Analyzer, DATA_TYPES as AnalyzerDATA_TYPES} from 'type-analyzer';import normalize from 'geojson-normalize';-import {ALL_FIELD_TYPES} from 'constants/default-settings';+import {ALL_FIELD_TYPES, GEOJSON_FIELDS} from 'constants/default-settings';import {notNullorUndefined} from 'utils/data-utils';// if any of these value occurs in csv, parse it to null;@@ -325,9 +325,26 @@ export function processGeojson(rawData) {return processRowObject(allData);}+/**+ * On export data to csv+ * @param data+ * @param fields+ */export function formatCsv(data, fields) {const columns = fields.map(f => f.name);- return csvFormatRows([columns, ...data]);+ const formattedData = [columns];++ // parse geojson object as string+ data.forEach(row => {+ formattedData.push(+ row.map(+ (d, i) => d && GEOJSON_FIELDS.geojson.includes(fields[i].name) ?+ JSON.stringify(d) : d+ )+ )+ });++ return csvFormatRows(formattedData);}/**diff --git a/src/reducers/ui-state-updaters.js b/src/reducers/ui-state-updaters.jsindex 5cc5b1626..b4992ba2d 100644--- a/src/reducers/ui-state-updaters.js+++ b/src/reducers/ui-state-updaters.js@@ -127,3 +127,11 @@ export const setExportFilteredUpdater = (state, {payload}) => ({filtered: payload.filtered}});++export const setExportConfigUpdater = (state, action) => ({+ ...state,+ exportData: {+ ...state.exportData,+ config: !state.exportData.config+ }+});diff --git a/src/reducers/ui-state.js b/src/reducers/ui-state.jsindex 184730461..a8b104648 100644--- a/src/reducers/ui-state.js+++ b/src/reducers/ui-state.js@@ -40,7 +40,8 @@ import {// export datasetExportSelectedDatasetUpdater,setExportDataTypeUpdater,- setExportFilteredUpdater+ setExportFilteredUpdater,+ setExportConfigUpdater} from './ui-state-updaters';export const INITIAL_UI_STATE = {@@ -63,7 +64,8 @@ export const INITIAL_UI_STATE = {exportData: {selectedDataset: '',dataType: EXPORT_DATA_TYPE.CSV,- filtered: true+ filtered: true,+ config: true}};@@ -81,7 +83,8 @@ const actionHandler = {[ActionTypes.SET_EXPORT_SELECTED_DATASET]: setExportSelectedDatasetUpdater,[ActionTypes.SET_EXPORT_DATA_TYPE]: setExportDataTypeUpdater,- [ActionTypes.SET_EXPORT_FILTERED]: setExportFilteredUpdater+ [ActionTypes.SET_EXPORT_FILTERED]: setExportFilteredUpdater,+ [ActionTypes.SET_EXPORT_CONFIG]: setExportConfigUpdater};/* Reducer */diff --git a/src/styles/base.js b/src/styles/base.jsindex 3b4d831e6..1b7c29f36 100644--- a/src/styles/base.js+++ b/src/styles/base.js@@ -101,23 +101,6 @@ export const secondaryInputColor = '#A0A7B4';export const secondaryInputBorderColor = '#242730';export const secondaryInputBorderActiveColor = '#D3D8E0';-export const switchWidth = 24;-export const switchHeight = 12;-export const switchLabelMargin = 12;--export const switchTrackBgd = '#29323C';-export const switchTrackBgdActive = activeColor;-export const switchTrackBorderRadius = '1px';-export const switchBtnBgd = '#6A7485';-export const switchBtnBgdActive = '#D3D8E0';-export const switchBtnBoxShadow = '0 2px 4px 0 rgba(0,0,0,0.40)';-export const switchBtnBorderRadius = '1px';-export const switchBtnWidth = '12px';-export const switchBtnHeight = '12px';--export const secondarySwitchTrackBgd = '#242730';-export const secondarySwitchBtnBgd = '#3A414C';-// Selectexport const selectColor = inputColor;export const selectColorLT = titleColorLT;@@ -142,6 +125,34 @@ export const dropdownListShadow = '0 6px 12px 0 rgba(0,0,0,0.16)';export const dropdownListBgd = '#3A414C';export const dropdownListBorderTop = '#242730';+// Switch+export const switchWidth = 24;+export const switchHeight = 12;+export const switchLabelMargin = 12;++export const switchTrackBgd = '#29323C';+export const switchTrackBgdActive = activeColor;+export const switchTrackBorderRadius = '1px';+export const switchBtnBgd = '#6A7485';+export const switchBtnBgdActive = '#D3D8E0';+export const switchBtnBoxShadow = '0 2px 4px 0 rgba(0,0,0,0.40)';+export const switchBtnBorderRadius = '1px';+export const switchBtnWidth = '12px';+export const switchBtnHeight = '12px';++export const secondarySwitchTrackBgd = '#242730';+export const secondarySwitchBtnBgd = '#3A414C';++// Checkbox+export const checkboxWidth = 16;+export const checkboxHeight = 16;+export const checkboxMargin = 12;+export const checkboxBorderColor = selectBorderColor;+export const checkboxBorderRadius = '2px';+export const checkboxBorderColorLT = selectBorderColorLT;+export const checkboxBoxBgd = 'white';+export const checkboxBoxBgdChecked = primaryBtnBgd;+// Side Panelexport const sidePanelHeaderBg = '#29323C';export const sidePanelBg = '#242730';@@ -414,6 +425,55 @@ const inputSwitch = css`}`;+// This is a light version checkbox+const checkboxBox = css`+ display: block;+ position: absolute;+ top: 0;+ left: 0;+ width: ${props => props.theme.checkboxWidth}px;+ height: ${props => props.theme.checkboxHeight}px;+ background: ${props => props.checked ? props.theme.checkboxBoxBgdChecked : props.theme.checkboxBoxBgd};+ border: 1px solid ${props => props.checked ? props.theme.checkboxBoxBgdChecked : props.theme.checkboxBorderColor};+ border-radius: 2px;+ content: '';+`;++const checkboxCheck = css`+ width: 10px;+ height: 5px;+ border-bottom: 2px solid white;+ border-left: 2px solid white;+ top: 4px;+ left: 3px;+ transform: rotate(-45deg);+ display: block;+ position: absolute;+ opacity: ${props => props.checked ? 1 : 0};+ content: "";+`;++const inputCheckbox = css`+ display: inline-block;+ position: relative;+ padding-left: 32px;+ margin-bottom: 24px;+ line-height: 20px;+ vertical-align: middle;+ cursor: pointer;+ font-size: 12px;+ color: ${props => props.theme.labelColor};+ margin-left: -${props => props.theme.switchLabelMargin}px;++ :before {+ ${props => props.theme.checkboxBox};+ }++ :after {+ ${props => props.theme.checkboxCheck};+ }+`;+const secondarySwitch = css`${props => props.theme.inputSwitch}:before {@@ -653,6 +713,9 @@ export const theme = {secondarySwitch,switchTrack,switchButton,+ inputCheckbox,+ checkboxBox,+ checkboxCheck,// Transitionstransition,@@ -715,6 +778,7 @@ export const theme = {secondaryInputBorderColor,secondaryInputBorderActiveColor,+ // SwitchswitchWidth,switchHeight,switchTrackBgd,@@ -731,6 +795,16 @@ export const theme = {secondarySwitchTrackBgd,secondarySwitchBtnBgd,+ // Checkbox+ checkboxWidth,+ checkboxHeight,+ checkboxMargin,+ checkboxBorderColor,+ checkboxBorderRadius,+ checkboxBorderColorLT,+ checkboxBoxBgd,+ checkboxBoxBgdChecked,+// ButtonprimaryBtnBgd,primaryBtnActBgd,