-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
map-container.js
620 lines (555 loc) · 19.7 KB
/
map-container.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Copyright (c) 2021 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.
// libraries
import React, {Component, createRef} from 'react';
import PropTypes from 'prop-types';
import MapboxGLMap from 'react-map-gl';
import DeckGL from '@deck.gl/react';
import {createSelector} from 'reselect';
import WebMercatorViewport from 'viewport-mercator-project';
import {errorNotification} from 'utils/notifications-utils';
// components
import MapPopoverFactory from 'components/map/map-popover';
import MapControlFactory from 'components/map/map-control';
import {StyledMapContainer, StyledAttrbution} from 'components/common/styled-components';
import EditorFactory from './editor/editor';
// utils
import {generateMapboxLayers, updateMapboxLayers} from 'layers/mapbox-utils';
import {setLayerBlending} from 'utils/gl-utils';
import {transformRequest} from 'utils/map-style-utils/mapbox-utils';
import {
getLayerHoverProp,
renderDeckGlLayer,
prepareLayersToRender,
prepareLayersForDeck
} from 'utils/layer-utils';
// default-settings
import ThreeDBuildingLayer from 'deckgl-layers/3d-building-layer/3d-building-layer';
import {
FILTER_TYPES,
GEOCODER_LAYER_ID,
THROTTLE_NOTIFICATION_TIME
} from 'constants/default-settings';
import ErrorBoundary from 'components/common/error-boundary';
import {observeDimensions, unobserveDimensions} from '../utils/observe-dimensions';
import MapLegendPanelFactory from './map/map-legend-panel';
/** @type {{[key: string]: React.CSSProperties}} */
const MAP_STYLE = {
container: {
display: 'inline-block',
position: 'relative',
width: '100%',
height: '100%'
},
top: {
position: 'absolute',
top: '0px',
pointerEvents: 'none',
width: '100%',
height: '100%'
}
};
const MAPBOXGL_STYLE_UPDATE = 'style.load';
const MAPBOXGL_RENDER = 'render';
const TRANSITION_DURATION = 0;
export const Attribution = () => (
<StyledAttrbution>
<div className="attrition-logo">
Basemap by:
<a
className="mapboxgl-ctrl-logo"
target="_blank"
rel="noopener noreferrer"
href="https://www.mapbox.com/"
aria-label="Mapbox logo"
/>
</div>
<div className="attrition-link">
<a href="https://kepler.gl/policy/" target="_blank" rel="noopener noreferrer">
© kepler.gl |{' '}
</a>
<a href="https://www.mapbox.com/about/maps/" target="_blank" rel="noopener noreferrer">
© Mapbox |{' '}
</a>
<a href="http://www.openstreetmap.org/copyright" target="_blank" rel="noopener noreferrer">
© OpenStreetMap |{' '}
</a>
<a href="https://www.mapbox.com/map-feedback/" target="_blank" rel="noopener noreferrer">
<strong>Improve this map</strong>
</a>
</div>
</StyledAttrbution>
);
MapContainerFactory.deps = [
MapPopoverFactory,
MapControlFactory,
EditorFactory,
MapLegendPanelFactory
];
export default function MapContainerFactory(MapPopover, MapControl, Editor, MapLegendPanel) {
class MapContainer extends Component {
static propTypes = {
// required
datasets: PropTypes.object,
interactionConfig: PropTypes.object.isRequired,
layerBlending: PropTypes.string.isRequired,
layerOrder: PropTypes.arrayOf(PropTypes.any).isRequired,
layerData: PropTypes.arrayOf(PropTypes.any).isRequired,
layers: PropTypes.arrayOf(PropTypes.any).isRequired,
filters: PropTypes.arrayOf(PropTypes.any).isRequired,
mapState: PropTypes.object.isRequired,
mapControls: PropTypes.object.isRequired,
mapStyle: PropTypes.object.isRequired,
mousePos: PropTypes.object.isRequired,
mapboxApiAccessToken: PropTypes.string.isRequired,
mapboxApiUrl: PropTypes.string,
visStateActions: PropTypes.object.isRequired,
mapStateActions: PropTypes.object.isRequired,
uiStateActions: PropTypes.object.isRequired,
// optional
primary: PropTypes.bool, // primary one will be reporting its size to appState
readOnly: PropTypes.bool,
isExport: PropTypes.bool,
clicked: PropTypes.object,
hoverInfo: PropTypes.object,
mapLayers: PropTypes.object,
onMapToggleLayer: PropTypes.func,
onMapStyleLoaded: PropTypes.func,
onMapRender: PropTypes.func,
getMapboxRef: PropTypes.func,
index: PropTypes.number
};
static defaultProps = {
MapComponent: MapboxGLMap,
deckGlProps: {},
index: 0,
primary: true
};
constructor(props) {
super(props);
this.previousLayers = {
// [layers.id]: mapboxLayerConfig
};
this._deck = null;
this._ref = createRef();
}
componentDidMount() {
observeDimensions(this._ref.current, this._handleResize);
}
componentWillUnmount() {
// unbind mapboxgl event listener
if (this._map) {
this._map.off(MAPBOXGL_STYLE_UPDATE);
this._map.off(MAPBOXGL_RENDER);
}
unobserveDimensions(this._ref.current);
}
_handleResize = dimensions => {
const {primary} = this.props;
if (primary) {
const {mapStateActions} = this.props;
if (dimensions && dimensions.width > 0 && dimensions.height > 0) {
mapStateActions.updateMap(dimensions);
}
}
};
layersSelector = props => props.layers;
layerDataSelector = props => props.layerData;
mapLayersSelector = props => props.mapLayers;
layerOrderSelector = props => props.layerOrder;
layersToRenderSelector = createSelector(
this.layersSelector,
this.layerDataSelector,
this.mapLayersSelector,
prepareLayersToRender
);
layersForDeckSelector = createSelector(
this.layersSelector,
this.layerDataSelector,
prepareLayersForDeck
);
filtersSelector = props => props.filters;
polygonFilters = createSelector(this.filtersSelector, filters =>
filters.filter(f => f.type === FILTER_TYPES.polygon)
);
mapboxLayersSelector = createSelector(
this.layersSelector,
this.layerDataSelector,
this.layerOrderSelector,
this.layersToRenderSelector,
generateMapboxLayers
);
/* component private functions */
_onCloseMapPopover = () => {
this.props.visStateActions.onLayerClick(null);
};
_onLayerSetDomain = (idx, colorDomain) => {
this.props.visStateActions.layerConfigChange(this.props.layers[idx], {
colorDomain
});
};
_handleMapToggleLayer = layerId => {
const {index: mapIndex = 0, visStateActions} = this.props;
visStateActions.toggleLayerForMap(mapIndex, layerId);
};
_onMapboxStyleUpdate = () => {
// force refresh mapboxgl layers
this.previousLayers = {};
this._updateMapboxLayers();
if (typeof this.props.onMapStyleLoaded === 'function') {
this.props.onMapStyleLoaded(this._map);
}
};
_setMapboxMap = mapbox => {
if (!this._map && mapbox) {
this._map = mapbox.getMap();
// i noticed in certain context we don't access the actual map element
if (!this._map) {
return;
}
// bind mapboxgl event listener
this._map.on(MAPBOXGL_STYLE_UPDATE, this._onMapboxStyleUpdate);
this._map.on(MAPBOXGL_RENDER, () => {
if (typeof this.props.onMapRender === 'function') {
this.props.onMapRender(this._map);
}
});
}
if (this.props.getMapboxRef) {
// The parent component can gain access to our MapboxGlMap by
// providing this callback. Note that 'mapbox' will be null when the
// ref is unset (e.g. when a split map is closed).
this.props.getMapboxRef(mapbox, this.props.index);
}
};
_onDeckInitialized(gl) {
if (this.props.onDeckInitialized) {
this.props.onDeckInitialized(this._deck, gl);
}
}
_onBeforeRender = ({gl}) => {
setLayerBlending(gl, this.props.layerBlending);
};
_onDeckError = (error, layer) => {
const errorMessage = `An error in deck.gl: ${error.message} in ${layer.id}`;
const notificationId = `${layer.id}-${error.message}`;
// Throttle error notifications, as React doesn't like too many state changes from here.
this._deckGLErrorsElapsed = this._deckGLErrorsElapsed || {};
const lastShown = this._deckGLErrorsElapsed[notificationId];
if (!lastShown || lastShown < Date.now() - THROTTLE_NOTIFICATION_TIME) {
this._deckGLErrorsElapsed[notificationId] = Date.now();
// Create new error notification or update existing one with same id.
// Update is required to preserve the order of notifications as they probably are going to "jump" based on order of errors.
const {uiStateActions} = this.props;
uiStateActions.addNotification(
errorNotification({
message: errorMessage,
id: notificationId
})
);
}
};
/* component render functions */
/* eslint-disable complexity */
_renderMapPopover(layersToRender) {
// TODO: move this into reducer so it can be tested
const {
mapState,
hoverInfo,
clicked,
datasets,
interactionConfig,
layers,
mousePos: {mousePosition, coordinate, pinned}
} = this.props;
if (!mousePosition || !interactionConfig.tooltip) {
return null;
}
const layerHoverProp = getLayerHoverProp({
interactionConfig,
hoverInfo,
layers,
layersToRender,
datasets
});
const compareMode = interactionConfig.tooltip.config
? interactionConfig.tooltip.config.compareMode
: false;
let pinnedPosition = {};
let layerPinnedProp = null;
if (pinned || clicked) {
// project lnglat to screen so that tooltip follows the object on zoom
const viewport = new WebMercatorViewport(mapState);
const lngLat = clicked ? clicked.lngLat : pinned.coordinate;
pinnedPosition = this._getHoverXY(viewport, lngLat);
layerPinnedProp = getLayerHoverProp({
interactionConfig,
hoverInfo: clicked,
layers,
layersToRender,
datasets
});
if (layerHoverProp && layerPinnedProp) {
layerHoverProp.primaryData = layerPinnedProp.data;
layerHoverProp.compareType = interactionConfig.tooltip.config.compareType;
}
}
const commonProp = {
onClose: this._onCloseMapPopover,
zoom: mapState.zoom,
container: this._deck ? this._deck.canvas : undefined
};
return (
<ErrorBoundary>
{layerPinnedProp && (
<MapPopover
{...pinnedPosition}
{...commonProp}
layerHoverProp={layerPinnedProp}
coordinate={interactionConfig.coordinate.enabled && (pinned || {}).coordinate}
frozen={true}
isBase={compareMode}
/>
)}
{layerHoverProp && (!layerPinnedProp || compareMode) && (
<MapPopover
x={mousePosition[0]}
y={mousePosition[1]}
{...commonProp}
layerHoverProp={layerHoverProp}
frozen={false}
coordinate={interactionConfig.coordinate.enabled && coordinate}
/>
)}
</ErrorBoundary>
);
}
/* eslint-enable complexity */
_getHoverXY(viewport, lngLat) {
const screenCoord = !viewport || !lngLat ? null : viewport.project(lngLat);
return screenCoord && {x: screenCoord[0], y: screenCoord[1]};
}
_renderDeckOverlay(layersForDeck) {
const {
mapState,
mapStyle,
layerData,
layerOrder,
layers,
visStateActions,
mapboxApiAccessToken,
mapboxApiUrl
} = this.props;
// initialise layers from props if exists
let deckGlLayers = this.props.deckGlProps?.layers || [];
// wait until data is ready before render data layers
if (layerData && layerData.length) {
// last layer render first
const dataLayers = layerOrder
.slice()
.reverse()
.filter(idx => layersForDeck[layers[idx].id])
.reduce((overlays, idx) => {
const layerCallbacks = {
onSetLayerDomain: val => this._onLayerSetDomain(idx, val)
};
const layerOverlay = renderDeckGlLayer(this.props, layerCallbacks, idx);
return overlays.concat(layerOverlay || []);
}, []);
deckGlLayers = deckGlLayers.concat(dataLayers);
}
if (mapStyle.visibleLayerGroups['3d building']) {
deckGlLayers.push(
new ThreeDBuildingLayer({
id: '_keplergl_3d-building',
mapboxApiAccessToken,
mapboxApiUrl,
threeDBuildingColor: mapStyle.threeDBuildingColor,
updateTriggers: {
getFillColor: mapStyle.threeDBuildingColor
}
})
);
}
return (
<DeckGL
{...this.props.deckGlProps}
viewState={mapState}
id="default-deckgl-overlay"
layers={deckGlLayers}
onBeforeRender={this._onBeforeRender}
onHover={visStateActions.onLayerHover}
onClick={visStateActions.onLayerClick}
onError={this._onDeckError}
ref={comp => {
if (comp && comp.deck && !this._deck) {
this._deck = comp.deck;
}
}}
onWebGLInitialized={gl => this._onDeckInitialized(gl)}
/>
);
}
_updateMapboxLayers() {
const mapboxLayers = this.mapboxLayersSelector(this.props);
if (!Object.keys(mapboxLayers).length && !Object.keys(this.previousLayers).length) {
return;
}
updateMapboxLayers(this._map, mapboxLayers, this.previousLayers);
this.previousLayers = mapboxLayers;
}
_renderMapboxOverlays() {
if (this._map && this._map.isStyleLoaded()) {
this._updateMapboxLayers();
}
}
_onViewportChange = viewState => {
const {width, height, ...restViewState} = viewState;
const {primary} = this.props;
// react-map-gl sends 0,0 dimensions during initialization
// after we have received proper dimensions from observeDimensions
const next = {
...(width > 0 && height > 0 ? viewState : restViewState),
// enabling transition in two maps may lead to endless update loops
transitionDuration: primary ? TRANSITION_DURATION : 0
};
if (typeof this.props.onViewStateChange === 'function') {
this.props.onViewStateChange(next);
}
this.props.mapStateActions.updateMap(next);
};
_toggleMapControl = panelId => {
const {index, uiStateActions} = this.props;
uiStateActions.toggleMapControl(panelId, index);
};
/* eslint-disable complexity */
_renderMap() {
const {
mapState,
mapStyle,
mapStateActions,
layers,
MapComponent,
datasets,
mapboxApiAccessToken,
mapboxApiUrl,
mapControls,
isExport,
locale,
uiStateActions,
visStateActions,
interactionConfig,
editor,
index,
primary
} = this.props;
const layersToRender = this.layersToRenderSelector(this.props);
const layersForDeck = this.layersForDeckSelector(this.props);
const mapProps = {
...mapState,
width: '100%',
height: '100%',
preserveDrawingBuffer: true,
mapboxApiAccessToken,
mapboxApiUrl,
onViewportChange: this._onViewportChange,
transformRequest
};
const isEdit = (mapControls.mapDraw || {}).active;
const hasGeocoderLayer = layers.find(l => l.id === GEOCODER_LAYER_ID);
const isSplit = Boolean(mapState.isSplit);
return (
<>
<MapControl
datasets={datasets}
dragRotate={mapState.dragRotate}
isSplit={isSplit}
primary={primary}
isExport={isExport}
layers={layers}
layersToRender={layersToRender}
mapIndex={index}
mapControls={mapControls}
readOnly={this.props.readOnly}
scale={mapState.scale || 1}
top={interactionConfig.geocoder && interactionConfig.geocoder.enabled ? 52 : 0}
editor={editor}
locale={locale}
onTogglePerspective={mapStateActions.togglePerspective}
onToggleSplitMap={mapStateActions.toggleSplitMap}
onMapToggleLayer={this._handleMapToggleLayer}
onToggleMapControl={this._toggleMapControl}
onSetEditorMode={visStateActions.setEditorMode}
onSetLocale={uiStateActions.setLocale}
onToggleEditorVisibility={visStateActions.toggleEditorVisibility}
/>
<MapComponent
{...mapProps}
key="bottom"
ref={this._setMapboxMap}
mapStyle={mapStyle.bottomMapStyle}
getCursor={this.props.hoverInfo ? () => 'pointer' : undefined}
onMouseMove={this.props.visStateActions.onMouseMove}
>
{this._renderDeckOverlay(layersForDeck)}
{this._renderMapboxOverlays()}
<Editor
index={index}
datasets={datasets}
editor={editor}
filters={this.polygonFilters(this.props)}
isEnabled={isEdit}
layers={layers}
layersToRender={layersToRender}
onDeleteFeature={visStateActions.deleteFeature}
onSelect={visStateActions.setSelectedFeature}
onUpdate={visStateActions.setFeatures}
onTogglePolygonFilter={visStateActions.setPolygonFilterLayer}
style={{
pointerEvents: isEdit ? 'all' : 'none',
position: 'absolute',
display: editor.visible ? 'block' : 'none'
}}
/>
</MapComponent>
{mapStyle.topMapStyle || hasGeocoderLayer ? (
<div style={MAP_STYLE.top}>
<MapComponent {...mapProps} key="top" mapStyle={mapStyle.topMapStyle}>
{this._renderDeckOverlay({[GEOCODER_LAYER_ID]: true})}
</MapComponent>
</div>
) : null}
{this._renderMapPopover(layersToRender)}
{!isSplit || index === 1 ? <Attribution /> : null}
</>
);
}
render() {
const {mapState, mapStyle} = this.props;
return (
<StyledMapContainer ref={this._ref} style={MAP_STYLE.container} globe={mapState.globe}>
{mapStyle.bottomMapStyle && this._renderMap()}
</StyledMapContainer>
);
}
}
MapContainer.displayName = 'MapContainer';
return MapContainer;
}