Skip to content

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

[Enhancement] Add 3d building color editor (#633)

Shan He972aabbe44e2
diff --git a/src/actions/map-style-actions.js b/src/actions/map-style-actions.js
index 0f341cc49..510aa3e50 100644
--- a/src/actions/map-style-actions.js
+++ b/src/actions/map-style-actions.js
@@ -118,6 +118,18 @@ export const loadCustomMapStyle = createAction(
customMapStyle => customMapStyle
);
+// SET_3D_BUILDING_COLOR
+/**
+ * Set 3d building layer group color
+ * @memberof mapStyleActions
+ * @param {Array} color - [r, g, b]
+ * @public
+ */
+export const set3dBuildingColor = createAction(
+ ActionTypes.SET_3D_BUILDING_COLOR,
+ color => color
+);
+
/**
* Actions handled mostly by `mapStyle` reducer.
* They manage the display of base map, such as loading and receiving base map styles,
diff --git a/src/components/map-container.js b/src/components/map-container.js
index 33ca86737..db7688de4 100644
--- a/src/components/map-container.js
+++ b/src/components/map-container.js
@@ -325,7 +325,6 @@ export default function MapContainerFactory(MapPopover, MapControl) {
} = this.props;
let deckGlLayers = [];
-
// wait until data is ready before render data layers
if (layerData && layerData.length) {
// last layer render first
@@ -340,7 +339,10 @@ export default function MapContainerFactory(MapPopover, MapControl) {
id: '_keplergl_3d-building',
mapboxApiAccessToken,
mapboxApiUrl,
- threeDBuildingColor: mapStyle.threeDBuildingColor
+ threeDBuildingColor: mapStyle.threeDBuildingColor,
+ updateTriggers: {
+ getFillColor: mapStyle.threeDBuildingColor
+ }
}));
}
diff --git a/src/components/side-panel.js b/src/components/side-panel.js
index fde9bd1ca..076d509a7 100644
--- a/src/components/side-panel.js
+++ b/src/components/side-panel.js
@@ -182,6 +182,7 @@ export default function SidePanelFactory(
onConfigChange: mapStyleActions.mapConfigChange,
onStyleChange: mapStyleActions.mapStyleChange,
onBuildingChange: mapStyleActions.mapBuildingChange,
+ set3dBuildingColor: mapStyleActions.set3dBuildingColor,
showAddMapStyleModal: this._showAddMapStyleModal
};
diff --git a/src/components/side-panel/map-manager.js b/src/components/side-panel/map-manager.js
index 99f499a9b..2c1a83114 100644
--- a/src/components/side-panel/map-manager.js
+++ b/src/components/side-panel/map-manager.js
@@ -21,17 +21,16 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
-import {Button} from 'components/common/styled-components';
+import {Button, SidePanelSection} from 'components/common/styled-components';
import MapStyleSelectorFactory from 'components/side-panel/map-style-panel/map-style-selector';
import LayerGroupSelectorFactory from 'components/side-panel/map-style-panel/map-layer-selector';
import {Add} from 'components/common/icons';
-import { DEFAULT_LAYER_GROUPS } from 'constants/default-settings';
+import {DEFAULT_LAYER_GROUPS} from 'constants/default-settings';
+import ColorSelector from './layer-panel/color-selector';
+import {createSelector} from 'reselect';
-MapManagerFactory.deps = [
- MapStyleSelectorFactory,
- LayerGroupSelectorFactory
-];
+MapManagerFactory.deps = [MapStyleSelectorFactory, LayerGroupSelectorFactory];
function MapManagerFactory(MapStyleSelector, LayerGroupSelector) {
return class MapManager extends Component {
@@ -46,6 +45,19 @@ function MapManagerFactory(MapStyleSelector, LayerGroupSelector) {
isSelecting: false
};
+ buildingColorSelector = props => props.mapStyle.threeDBuildingColor;
+ setColorSelector = props => props.set3dBuildingColor;
+ colorSetSelector = createSelector(
+ this.buildingColorSelector,
+ this.setColorSelector,
+ (selectedColor, setColor) => ([{
+ selectedColor,
+ setColor,
+ isRange: false,
+ label: '3D Building Color'
+ }])
+ );
+
_updateConfig = newProp => {
const newConfig = {...this.props.mapStyle, ...newProp};
this.props.onConfigChange(newConfig);
@@ -62,9 +74,9 @@ function MapManagerFactory(MapStyleSelector, LayerGroupSelector) {
render() {
const {mapStyle} = this.props;
- const editableLayers = DEFAULT_LAYER_GROUPS.map(lg =>
- lg.slug
- );
+ const editableLayers = DEFAULT_LAYER_GROUPS.map(lg => lg.slug);
+ const hasBuildingLayer = mapStyle.visibleLayerGroups['3d building'];
+ const colorSets = this.colorSetSelector(this.props);
return (
<div className="map-style-panel">
@@ -83,16 +95,21 @@ function MapManagerFactory(MapStyleSelector, LayerGroupSelector) {
onChange={this._updateConfig}
/>
) : null}
- <Button
- onClick={this.props.showAddMapStyleModal}
- secondary>
- <Add height="12px" />Add Map Style
+ <SidePanelSection>
+ <ColorSelector
+ colorSets={colorSets}
+ disabled={!hasBuildingLayer}
+ />
+ </SidePanelSection>
+ <Button onClick={this.props.showAddMapStyleModal} secondary>
+ <Add height="12px" />
+ Add Map Style
</Button>
</div>
</div>
);
}
- }
+ };
}
export default MapManagerFactory;
diff --git a/src/constants/action-types.js b/src/constants/action-types.js
index c3e4ce2be..960704574 100644
--- a/src/constants/action-types.js
+++ b/src/constants/action-types.js
@@ -106,6 +106,7 @@ const ActionTypes = keyMirror({
LOAD_CUSTOM_MAP_STYLE: null,
ADD_CUSTOM_MAP_STYLE: null,
REQUEST_MAP_STYLES: null,
+ SET_3D_BUILDING_COLOR: null,
// uiState
TOGGLE_SIDE_PANEL: null,
diff --git a/src/deckgl-layers/3d-building-layer/3d-building-layer.js b/src/deckgl-layers/3d-building-layer/3d-building-layer.js
index 2561cd1e5..1d9f35544 100644
--- a/src/deckgl-layers/3d-building-layer/3d-building-layer.js
+++ b/src/deckgl-layers/3d-building-layer/3d-building-layer.js
@@ -51,12 +51,12 @@ export default class ThreeDBuildingLayer extends CompositeLayer {
}
renderLayers() {
-
return [
new DeckGLTileLayer({
getTileData: (args) => getTileData(this.props.mapboxApiUrl, this.props.mapboxApiAccessToken, args),
minZoom: 13,
- renderSubLayers: this.renderSubLayers.bind(this)
+ renderSubLayers: this.renderSubLayers.bind(this),
+ updateTriggers: this.props.updateTriggers
})
];
}
diff --git a/src/reducers/map-style-updaters.js b/src/reducers/map-style-updaters.js
index bd62054ef..ffa623e68 100644
--- a/src/reducers/map-style-updaters.js
+++ b/src/reducers/map-style-updaters.js
@@ -63,7 +63,8 @@ const getDefaultState = () => {
mapboxApiUrl: null,
mapStylesReplaceDefault: false,
inputStyle: getInitialInputStyle(),
- threeDBuildingColor: hexToRgb(DEFAULT_BLDG_COLOR)
+ threeDBuildingColor: hexToRgb(DEFAULT_BLDG_COLOR),
+ custom3DBuildingColor: false
};
};
@@ -175,32 +176,45 @@ export function getMapStyles({
visibleLayerGroups: topLayers
})
: null;
- const threeDBuildingColor = get3DBuildingColor(mapStyle);
- return {bottomMapStyle, topMapStyle, editable, threeDBuildingColor};
+
+ return {bottomMapStyle, topMapStyle, editable};
+}
+
+function findLayerFillColor(layer) {
+ return layer && layer.paint && layer.paint['background-color'];
}
function get3DBuildingColor(style) {
// set building color to be the same as the background color.
+ if (!style.style) {
+ return DEFAULT_BLDG_COLOR;
+ }
+
const backgroundLayer = (style.style.layers || []).find(
({id}) => id === 'background'
);
+
+ const buildingLayer = (style.style.layers || []).find(({id}) =>
+ id.match(/building/)
+ );
+
const buildingColor =
- backgroundLayer &&
- backgroundLayer.paint &&
- backgroundLayer.paint['background-color']
- ? backgroundLayer.paint['background-color']
- : DEFAULT_BLDG_COLOR;
+ findLayerFillColor(buildingLayer) ||
+ findLayerFillColor(backgroundLayer) ||
+ DEFAULT_BLDG_COLOR;
+
// brighten or darken building based on style
const operation = style.id.match(/(?=(dark|night))/) ? 'brighter' : 'darker';
+
const alpha = 0.2;
const rgbObj = rgb(buildingColor)[operation]([alpha]);
return [rgbObj.r, rgbObj.g, rgbObj.b];
}
function getLayerGroupsFromStyle(style) {
- return Array.isArray(style.layers) ? DEFAULT_LAYER_GROUPS.filter(
- lg => style.layers.filter(lg.filter).length
- ) : [];
+ return Array.isArray(style.layers)
+ ? DEFAULT_LAYER_GROUPS.filter(lg => style.layers.filter(lg.filter).length)
+ : [];
}
// Updaters
@@ -222,7 +236,10 @@ export const initMapStyleUpdater = (state, action) => ({
// save mapbox access token to map style state
mapboxApiAccessToken: (action.payload || {}).mapboxApiAccessToken,
mapboxApiUrl: (action.payload || {}).mapboxApiUrl,
- mapStyles: action.payload && !action.payload.mapStylesReplaceDefault ? state.mapStyles : {},
+ mapStyles:
+ action.payload && !action.payload.mapStylesReplaceDefault
+ ? state.mapStyles
+ : {},
mapStylesReplaceDefault: action.payload.mapStylesReplaceDefault || false
});
// });
@@ -268,10 +285,13 @@ export const mapStyleChangeUpdater = (state, {payload: styleType}) => {
state.visibleLayerGroups
);
+ const threeDBuildingColor = state.custom3DBuildingColor ? state.threeDBuildingColor :
+ get3DBuildingColor(state.mapStyles[styleType]);
return {
...state,
styleType,
visibleLayerGroups,
+ threeDBuildingColor,
...getMapStyles({
...state,
visibleLayerGroups,
@@ -291,14 +311,18 @@ export const mapStyleChangeUpdater = (state, {payload: styleType}) => {
*/
export const loadMapStylesUpdater = (state, action) => {
const newStyles = action.payload || {};
- const addLayerGroups = Object.keys(newStyles).reduce((accu, id) => ({
- ...accu,
- [id]: {
- ...newStyles[id],
- layerGroups:
- newStyles[id].layerGroups || getLayerGroupsFromStyle(newStyles[id].style)
- }
- }), {});
+ const addLayerGroups = Object.keys(newStyles).reduce(
+ (accu, id) => ({
+ ...accu,
+ [id]: {
+ ...newStyles[id],
+ layerGroups:
+ newStyles[id].layerGroups ||
+ getLayerGroupsFromStyle(newStyles[id].style)
+ }
+ }),
+ {}
+ );
// add new styles to state
const newState = {
@@ -324,10 +348,14 @@ export const loadMapStylesUpdater = (state, action) => {
* @public
*/
// do nothing for now, if didn't load, skip it
-export const loadMapStyleErrUpdater = (state) => state;
+export const loadMapStyleErrUpdater = state => state;
export const requestMapStylesUpdater = (state, {payload: mapStyles}) => {
- const loadMapStyleTasks = getLoadMapStyleTasks(mapStyles, state.mapboxApiAccessToken, state.mapboxApiUrl);
+ const loadMapStyleTasks = getLoadMapStyleTasks(
+ mapStyles,
+ state.mapboxApiAccessToken,
+ state.mapboxApiUrl
+ );
return withTask(state, loadMapStyleTasks);
};
@@ -346,18 +374,26 @@ export const receiveMapConfigUpdater = (state, {payload: {mapStyle}}) => {
// if saved custom mapStyles load the style object
const loadMapStyleTasks = mapStyle.mapStyles
- ? getLoadMapStyleTasks(mapStyle.mapStyles, state.mapboxApiAccessToken, state.mapboxApiUrl)
+ ? getLoadMapStyleTasks(
+ mapStyle.mapStyles,
+ state.mapboxApiAccessToken,
+ state.mapboxApiUrl
+ )
: null;
// merge default mapStyles
- const merged = mapStyle.mapStyles ? {
- ...mapStyle,
- mapStyles: {
- ...mapStyle.mapStyles,
- ...state.mapStyles
- }
- } : mapStyle;
+ const merged = mapStyle.mapStyles
+ ? {
+ ...mapStyle,
+ mapStyles: {
+ ...mapStyle.mapStyles,
+ ...state.mapStyles
+ }
+ }
+ : mapStyle;
+ // set custom3DBuildingColor: true if mapStyle contains threeDBuildingColor
+ merged.custom3DBuildingColor = Boolean(mapStyle.threeDBuildingColor) || merged.custom3DBuildingColor;
const newState = mapConfigChangeUpdater(state, {payload: merged});
return loadMapStyleTasks ? withTask(newState, loadMapStyleTasks) : newState;
@@ -369,7 +405,13 @@ function getLoadMapStyleTasks(mapStyles, mapboxApiAccessToken, mapboxApiUrl) {
Object.values(mapStyles)
.map(({id, url, accessToken}) => ({
id,
- url: isValidStyleUrl(url) ? getStyleDownloadUrl(url, accessToken || mapboxApiAccessToken, mapboxApiUrl) : url
+ url: isValidStyleUrl(url)
+ ? getStyleDownloadUrl(
+ url,
+ accessToken || mapboxApiAccessToken,
+ mapboxApiUrl
+ )
+ : url
}))
.map(LOAD_MAP_STYLE_TASK)
).bimap(
@@ -493,6 +535,12 @@ export const addCustomMapStyleUpdater = state => {
return mapStyleChangeUpdater(newState, {payload: styleId});
};
+export const set3dBuildingColorUpdater = (state, {payload: color}) => ({
+ ...state,
+ threeDBuildingColor: color,
+ custom3DBuildingColor: true
+});
+
export function getInitialInputStyle() {
return {
accessToken: null,
diff --git a/src/reducers/map-style.js b/src/reducers/map-style.js
index 5baf0ef20..edeb44182 100644
--- a/src/reducers/map-style.js
+++ b/src/reducers/map-style.js
@@ -37,7 +37,8 @@ const actionHandler = {
[ActionTypes.RECEIVE_MAP_CONFIG]: mapStyleUpdaters.receiveMapConfigUpdater,
[ActionTypes.LOAD_CUSTOM_MAP_STYLE]: mapStyleUpdaters.loadCustomMapStyleUpdater,
[ActionTypes.ADD_CUSTOM_MAP_STYLE]: mapStyleUpdaters.addCustomMapStyleUpdater,
- [ActionTypes.RESET_MAP_CONFIG]: mapStyleUpdaters.resetMapConfigMapStyleUpdater
+ [ActionTypes.RESET_MAP_CONFIG]: mapStyleUpdaters.resetMapConfigMapStyleUpdater,
+ [ActionTypes.SET_3D_BUILDING_COLOR]: mapStyleUpdaters.set3dBuildingColorUpdater
};
export const mapStyleReducerFactory = (initialState = {}) =>
diff --git a/src/schemas/map-style-schema.js b/src/schemas/map-style-schema.js
index f0c2da9f2..28c144bce 100644
--- a/src/schemas/map-style-schema.js
+++ b/src/schemas/map-style-schema.js
@@ -65,6 +65,14 @@ export const propertiesV0 = {
mapStyles: new MapStyleSchemaV1()
};
+export const propertiesV1 = {
+ styleType: null,
+ topLayerGroups: null,
+ visibleLayerGroups: null,
+ threeDBuildingColor: null,
+ mapStyles: new MapStyleSchemaV1()
+};
+
const mapStyleSchema = {
[VERSIONS.v0]: new Schema({
version: VERSIONS.v0,
@@ -73,7 +81,7 @@ const mapStyleSchema = {
}),
[VERSIONS.v1]: new Schema({
version: VERSIONS.v1,
- properties: propertiesV0,
+ properties: propertiesV1,
key: 'mapStyle'
})
};
diff --git a/test/helpers/mock-state.js b/test/helpers/mock-state.js
index 664fb6b1c..8bb47c705 100644
--- a/test/helpers/mock-state.js
+++ b/test/helpers/mock-state.js
@@ -258,6 +258,10 @@ function mockStateWithCustomMapStyle() {
{
action: MapStyleActions.addCustomMapStyle,
payload: [{}]
+ },
+ {
+ action: MapStyleActions.set3dBuildingColor,
+ payload: [[1, 2, 3]]
}
]);
diff --git a/test/node/reducers/map-style-test.js b/test/node/reducers/map-style-test.js
index 5d10b2b67..766d97ea9 100644
--- a/test/node/reducers/map-style-test.js
+++ b/test/node/reducers/map-style-test.js
@@ -103,7 +103,7 @@ test('#mapStyleReducer -> INIT & LOAD_MAP_STYLES', t => {
t.deepEqual(
finalState,
- { ...newState, mapStyles: mapStyles },
+ {...newState, mapStyles},
'user provided mapStyles are populated, defaults ignored'
);
@@ -117,6 +117,7 @@ test('#mapStyleReducer -> RECEIVE_MAP_CONFIG', t => {
);
const stateToSave = StateWCustomMapStyle;
+
// save state
const savedState = SchemaManager.getConfigToSave(stateToSave);
@@ -163,7 +164,8 @@ test('#mapStyleReducer -> RECEIVE_MAP_CONFIG', t => {
url: null,
custom: true
},
- threeDBuildingColor: [209, 206, 199],
+ threeDBuildingColor: [1, 2, 3],
+ custom3DBuildingColor: true,
initialState: {}
};
@@ -231,11 +233,8 @@ test('#mapStyleReducer -> RECEIVE_MAP_CONFIG', t => {
url: null,
custom: true
},
- threeDBuildingColor: [
- 194.6103322548211,
- 191.81688250953655,
- 185.2988331038727
- ],
+ threeDBuildingColor: [1, 2, 3],
+ custom3DBuildingColor: true,
initialState: {},
bottomMapStyle: {layers: [], name: 'smoothie_the_cat'},
topMapStyle: null,
diff --git a/test/node/reducers/root-test.js b/test/node/reducers/root-test.js
index 53fcade68..0229d5bba 100644
--- a/test/node/reducers/root-test.js
+++ b/test/node/reducers/root-test.js
@@ -36,8 +36,8 @@ test('keplerGlReducer.initialState', t => {
});
const test1ReducerInitialState = test1Reducer(undefined, registerEntry({id: 'test'}));
- t.deepEqual(test1ReducerInitialState.test.visState.layerClasses, [], 'should overide initialState');
- t.equal(test1ReducerInitialState.test.mapStyle.styleType, 'light', 'should overide initialState');
+ t.deepEqual(test1ReducerInitialState.test.visState.layerClasses, [], 'should override initialState');
+ t.equal(test1ReducerInitialState.test.mapStyle.styleType, 'light', 'should override initialState');
t.deepEqual(test1ReducerInitialState.test.visState.initialState, {layerClasses: []}, 'should save initialState');
t.deepEqual(test1ReducerInitialState.test.mapStyle.initialState, {styleType: 'light'}, 'should save initialState');
@@ -54,8 +54,8 @@ test('keplerGlReducer.initialState', t => {
// reset reducer
const restTestReducer = test1Reducer(modifiedReducer, resetMapConfig());
- t.deepEqual(restTestReducer.test.visState.layerClasses, [], 'should overide initialState');
- t.equal(restTestReducer.test.mapStyle.styleType, 'light', 'should overide initialState');
+ t.deepEqual(restTestReducer.test.visState.layerClasses, [], 'should override initialState');
+ t.equal(restTestReducer.test.mapStyle.styleType, 'light', 'should override initialState');
t.deepEqual(restTestReducer.test.visState.initialState, {layerClasses: []}, 'should save initialState');
t.deepEqual(restTestReducer.test.mapStyle.initialState, {styleType: 'light'}, 'should save initialState');
diff --git a/test/node/schemas/map-style-schema-test.js b/test/node/schemas/map-style-schema-test.js
index d87035936..13408d178 100644
--- a/test/node/schemas/map-style-schema-test.js
+++ b/test/node/schemas/map-style-schema-test.js
@@ -32,20 +32,22 @@ test('#mapStyleSchema -> v1 -> save load mapStyle', t => {
const msLoaded = SchemaManager.parseSavedConfig(savedState).mapStyle;
t.deepEqual(Object.keys(msToSave),
- ['styleType', 'topLayerGroups', 'visibleLayerGroups', 'mapStyles'],
+ ['styleType', 'topLayerGroups', 'visibleLayerGroups', 'threeDBuildingColor', 'mapStyles'],
'mapStyle should have all 4 entries');
const expectedSaved = {
styleType: 'dark',
topLayerGroups: {},
visibleLayerGroups: {},
- mapStyles: {}
+ mapStyles: {},
+ threeDBuildingColor: [209, 206, 199]
};
const expectedLoaded = {
styleType: 'dark',
topLayerGroups: {},
- visibleLayerGroups: {}
+ visibleLayerGroups: {},
+ threeDBuildingColor: [209, 206, 199]
};
t.deepEqual(msToSave, expectedSaved, 'saved mapStyle should be current');
@@ -68,6 +70,7 @@ test('#mapStyleSchema -> v1 -> save load mapStyle with custom style', t => {
label: true,
road: true
},
+ threeDBuildingColor: [1, 2, 3],
mapStyles: {
'smoothie_the_cat': {
id: 'smoothie_the_cat',