-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathMap.tsx
269 lines (251 loc) · 9.58 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
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
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { MapLayerMouseEvent } from 'maplibre-gl';
import ReactMapGL, { AttributionControl, ScaleControl, MapRef } from 'react-map-gl/maplibre';
import { useDispatch, useSelector } from 'react-redux';
import { NearestPointOnLine } from '@turf/nearest-point-on-line';
import { useParams } from 'react-router-dom';
import { RootState } from 'reducers';
import { updateFeatureInfoClickOSRD } from 'reducers/osrdconf';
import { updateViewport, Viewport } from 'reducers/map';
/* Main data & layers */
import Background from 'common/Map/Layers/Background';
import VirtualLayers from 'modules/simulationResult/components/SimulationResultsMap/VirtualLayers';
/* Settings & Buttons */
import MapButtons from 'common/Map/Buttons/MapButtons';
import Catenaries from 'common/Map/Layers/Catenaries';
import NeutralSections from 'common/Map/Layers/NeutralSections';
import Hillshade from 'common/Map/Layers/Hillshade';
import OSM from 'common/Map/Layers/OSM';
import OperationalPoints from 'common/Map/Layers/OperationalPoints';
import Platforms from 'common/Map/Layers/Platforms';
import RenderItinerary from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/RenderItinerary';
import RenderItineraryMarkers from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/RenderItineraryMarkers';
/* Interactions */
import RenderPopup from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/RenderPopup';
import Routes from 'common/Map/Layers/Routes';
import SearchMarker from 'common/Map/Layers/SearchMarker';
import Signals from 'common/Map/Layers/Signals';
import SnappedMarker from 'common/Map/Layers/SnappedMarker';
import SpeedLimits from 'common/Map/Layers/SpeedLimits';
import BufferStops from 'common/Map/Layers/BufferStops';
import Detectors from 'common/Map/Layers/Detectors';
import Switches from 'common/Map/Layers/Switches';
import TracksOSM from 'common/Map/Layers/TracksOSM';
/* Objects & various */
import TracksGeographic from 'common/Map/Layers/TracksGeographic';
import colors from 'common/Map/Consts/colors';
import { useMapBlankStyle } from 'common/Map/Layers/blankStyle';
import { LAYER_GROUPS_ORDER, LAYERS } from 'config/layerOrder';
import 'common/Map/Map.scss';
import SNCF_PSL from 'common/Map/Layers/extensions/SNCF/SNCF_PSL';
import IGN_BD_ORTHO from 'common/Map/Layers/IGN_BD_ORTHO';
import IGN_SCAN25 from 'common/Map/Layers/IGN_SCAN25';
import IGN_CADASTRE from 'common/Map/Layers/IGN_CADASTRE';
import { CUSTOM_ATTRIBUTION } from 'common/Map/const';
import LineSearchLayer from 'common/Map/Layers/LineSearchLayer';
import Terrain from 'common/Map/Layers/Terrain';
import { getTerrain3DExaggeration } from 'reducers/map/selectors';
import { getMapMouseEventNearestFeature } from '../../../../utils/mapHelper';
function Map() {
const { viewport, mapSearchMarker, mapStyle, showOSM, layersSettings } = useSelector(
(state: RootState) => state.map
);
const terrain3DExaggeration = useSelector(getTerrain3DExaggeration);
const mapBlankStyle = useMapBlankStyle();
const [snappedPoint, setSnappedPoint] = useState<NearestPointOnLine>();
const { urlLat = '', urlLon = '', urlZoom = '', urlBearing = '', urlPitch = '' } = useParams();
const dispatch = useDispatch();
const updateViewportChange = useCallback(
(value: Partial<Viewport>) => dispatch(updateViewport(value, undefined)),
[dispatch]
);
const mapRef = useRef<MapRef | null>(null);
const scaleControlStyle = {
left: 20,
bottom: 20,
};
const resetPitchBearing = () => {
updateViewportChange({
...viewport,
bearing: 0,
pitch: 0,
});
};
const onFeatureClick = (e: MapLayerMouseEvent) => {
const result = getMapMouseEventNearestFeature(e, { layersId: ['chartis/tracks-geo/main'] });
if (
result &&
result.feature.properties &&
result.feature.properties.id &&
result.feature.geometry.type === 'LineString'
) {
dispatch(
updateFeatureInfoClickOSRD({
displayPopup: true,
feature: result.feature,
coordinates: result.nearest,
})
);
} else {
dispatch(
updateFeatureInfoClickOSRD({
displayPopup: false,
feature: undefined,
})
);
}
};
const onMoveGetFeature = (e: MapLayerMouseEvent) => {
const result = getMapMouseEventNearestFeature(e, { layersId: ['chartis/tracks-geo/main'] });
if (
result &&
result.feature.properties &&
result.feature.properties.id &&
result.feature.geometry.type === 'LineString'
) {
setSnappedPoint({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: result.nearest,
},
properties: {
distance: result.distance,
},
});
} else {
setSnappedPoint(undefined);
}
};
const defineInteractiveLayers = () => {
const interactiveLayersLocal: Array<string> = [];
interactiveLayersLocal.push('chartis/tracks-geo/main');
if (layersSettings.operationalpoints) {
interactiveLayersLocal.push('chartis/osrd_operational_point/geo');
}
if (layersSettings.tvds) {
interactiveLayersLocal.push('chartis/osrd_tvd_section/geo');
}
return interactiveLayersLocal;
};
useEffect(() => {
if (urlLat) {
updateViewportChange({
...viewport,
latitude: parseFloat(urlLat),
longitude: parseFloat(urlLon),
zoom: parseFloat(urlZoom),
bearing: parseFloat(urlBearing),
pitch: parseFloat(urlPitch),
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<>
<MapButtons map={mapRef.current ?? undefined} resetPitchBearing={resetPitchBearing} />
<ReactMapGL
ref={mapRef}
{...viewport}
style={{ width: '100%', height: '100%' }}
cursor="pointer"
mapStyle={mapBlankStyle}
onMove={(e) => updateViewportChange(e.viewState)}
onMouseMove={(e) => onMoveGetFeature(e)}
attributionControl={false} // Defined below
onClick={onFeatureClick}
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
}
>
<VirtualLayers />
<AttributionControl position="bottom-right" 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]} />
{!showOSM ? null : (
<>
<OSM mapStyle={mapStyle} layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
<Hillshade
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
/>
</>
)}
<Platforms
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.PLATFORMS.GROUP]}
/>
<TracksGeographic
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.TRACKS_GEOGRAPHIC.GROUP]}
/>
<TracksOSM
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.TRACKS_OSM.GROUP]}
/>
<Routes colors={colors[mapStyle]} layerOrder={LAYER_GROUPS_ORDER[LAYERS.ROUTES.GROUP]} />
<OperationalPoints
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.OPERATIONAL_POINTS.GROUP]}
/>
<Catenaries
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.CATENARIES.GROUP]}
/>
<NeutralSections layerOrder={LAYER_GROUPS_ORDER[LAYERS.DEAD_SECTIONS.GROUP]} />
<BufferStops
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BUFFER_STOPS.GROUP]}
/>
<Detectors
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.DETECTORS.GROUP]}
/>
<Switches
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.SWITCHES.GROUP]}
/>
<SpeedLimits
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.SPEED_LIMITS.GROUP]}
/>
<SNCF_PSL
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.SPEED_LIMITS.GROUP]}
/>
<Signals
sourceTable="signals"
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.SIGNALS.GROUP]}
/>
<LineSearchLayer layerOrder={LAYER_GROUPS_ORDER[LAYERS.LINE_SEARCH.GROUP]} />
<RenderPopup />
<RenderItinerary layerOrder={LAYER_GROUPS_ORDER[LAYERS.ITINERARY.GROUP]} />
<RenderItineraryMarkers />
{mapSearchMarker !== undefined && (
<SearchMarker data={mapSearchMarker} colors={colors[mapStyle]} />
)}
{snappedPoint !== undefined && <SnappedMarker geojson={snappedPoint} />}
</ReactMapGL>
</>
);
}
export default Map;