-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathMap.tsx
180 lines (162 loc) · 6.22 KB
/
Map.tsx
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
import { useCallback, useEffect, useRef, useState } from 'react';
import { isNil } from 'lodash';
import ReactMapGL, { AttributionControl, ScaleControl } from 'react-map-gl/maplibre';
import type { MapRef } from 'react-map-gl/maplibre';
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import MapButtons from 'common/Map/Buttons/MapButtons';
import { CUSTOM_ATTRIBUTION } from 'common/Map/const';
import colors from 'common/Map/Consts/colors';
import Background from 'common/Map/Layers/Background';
import { useMapBlankStyle } from 'common/Map/Layers/blankStyle';
import Hillshade from 'common/Map/Layers/Hillshade';
import IGN_BD_ORTHO from 'common/Map/Layers/IGN_BD_ORTHO';
import IGN_CADASTRE from 'common/Map/Layers/IGN_CADASTRE';
import IGN_SCAN25 from 'common/Map/Layers/IGN_SCAN25';
import InfraObjectLayers from 'common/Map/Layers/InfraObjectLayers';
import LineSearchLayer from 'common/Map/Layers/LineSearchLayer';
import OSM from 'common/Map/Layers/OSM';
import PlatformsLayer from 'common/Map/Layers/Platforms';
import SearchMarker from 'common/Map/Layers/SearchMarker';
import Terrain from 'common/Map/Layers/Terrain';
import TracksOSM from 'common/Map/Layers/TracksOSM';
import { removeSearchItemMarkersOnMap } from 'common/Map/utils';
import { useInfraID } from 'common/osrdContext';
import { LAYER_GROUPS_ORDER, LAYERS } from 'config/layerOrder';
import VirtualLayers from 'modules/simulationResult/components/SimulationResultsMap/VirtualLayers';
import type { Viewport } from 'reducers/map';
import { updateViewport } from 'reducers/map';
import { getMap, getTerrain3DExaggeration } from 'reducers/map/selectors';
import { useAppDispatch } from 'store';
function Map() {
const mapBlankStyle = useMapBlankStyle();
const [mapLoaded, setMapLoaded] = useState(false);
const { viewport, mapSearchMarker, mapStyle, showOSM, layersSettings } = useSelector(getMap);
const infraID = useInfraID();
const terrain3DExaggeration = useSelector(getTerrain3DExaggeration);
const mapRef = useRef<MapRef | null>(null);
const { urlLat, urlLon, urlZoom, urlBearing, urlPitch } = useParams();
const dispatch = useAppDispatch();
const updateViewportChange = useCallback(
(value: Partial<Viewport>, updateRouter = false) => {
dispatch(updateViewport(value, `/map`, updateRouter));
},
[dispatch]
);
const scaleControlStyle = {
left: 20,
bottom: 20,
};
const resetPitchBearing = () => {
updateViewportChange({
...viewport,
bearing: 0,
pitch: 0,
});
};
const defineInteractiveLayers = () => {
const interactiveLayersLocal = [];
if (layersSettings.tvds) {
interactiveLayersLocal.push('chartis/osrd_tvd_section/geo');
}
return interactiveLayersLocal;
};
/**
* When the component mount
* => we check if url has viewport and set it in store
*/
useEffect(() => {
// viewport
const newViewport: Partial<Viewport> = {};
if (!isNil(urlLat)) newViewport.latitude = parseFloat(urlLat);
if (!isNil(urlLon)) newViewport.longitude = parseFloat(urlLon);
if (!isNil(urlZoom)) newViewport.zoom = parseFloat(urlZoom);
if (!isNil(urlBearing)) newViewport.bearing = parseFloat(urlBearing);
if (!isNil(urlPitch)) newViewport.pitch = parseFloat(urlPitch);
if (Object.keys(newViewport).length > 0) updateViewportChange(newViewport);
// we only do it at mount time
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<main className="mastcontainer mastcontainer-map">
<MapButtons
map={mapRef.current ?? undefined}
resetPitchBearing={resetPitchBearing}
withInfraButton
withMapKeyButton
bearing={viewport.bearing}
viewPort={viewport}
/>
<ReactMapGL
{...viewport}
ref={mapRef}
cursor="normal"
style={{ width: '100%', height: '100%' }}
mapStyle={mapBlankStyle}
onMove={(e) => updateViewportChange(e.viewState)}
onMoveEnd={(e) => updateViewportChange(e.viewState, true)}
attributionControl={false} // Defined below
onResize={(e) => {
updateViewportChange({
width: e.target.getContainer().offsetWidth,
height: e.target.getContainer().offsetHeight,
});
}}
interactiveLayerIds={defineInteractiveLayers()}
touchZoomRotate
maxPitch={85}
terrain={
terrain3DExaggeration
? { source: 'terrain', exaggeration: terrain3DExaggeration }
: undefined
}
onLoad={() => {
setMapLoaded(true);
}}
onClick={() => {
removeSearchItemMarkersOnMap(dispatch);
}}
>
<VirtualLayers />
<AttributionControl customAttribution={CUSTOM_ATTRIBUTION} />
<ScaleControl maxWidth={100} unit="metric" style={scaleControlStyle} />
<Background
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
/>
<Terrain />
<IGN_BD_ORTHO layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
<IGN_SCAN25 layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
<IGN_CADASTRE layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
{infraID && <InfraObjectLayers infraId={infraID} mapStyle={mapStyle} />}
{!showOSM ? null : (
<>
<OSM
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
mapIsLoaded={mapLoaded}
/>
<Hillshade
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
/>
</>
)}
<PlatformsLayer
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.PLATFORMS.GROUP]}
/>
<TracksOSM
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.TRACKS_OSM.GROUP]}
/>
<LineSearchLayer
layerOrder={LAYER_GROUPS_ORDER[LAYERS.LINE_SEARCH.GROUP]}
infraID={infraID}
/>
{mapSearchMarker && <SearchMarker data={mapSearchMarker} colors={colors[mapStyle]} />}
</ReactMapGL>
</main>
);
}
export default Map;