-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathMap.tsx
415 lines (382 loc) · 13.9 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
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
import { type PropsWithChildren, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import bbox from '@turf/bbox';
import type { Feature, Point } from 'geojson';
import { compact } from 'lodash';
import type { MapLayerMouseEvent } from 'maplibre-gl';
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 captureMap from 'applications/operationalStudies/helpers/captureMap';
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import type { PathProperties } from 'common/api/osrdEditoastApi';
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 BufferStops from 'common/Map/Layers/BufferStops';
import Detectors from 'common/Map/Layers/Detectors';
import Electrifications from 'common/Map/Layers/Electrifications';
import NeutralSections from 'common/Map/Layers/extensions/SNCF/NeutralSections';
import SNCF_PSL from 'common/Map/Layers/extensions/SNCF/PSL';
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 LineSearchLayer from 'common/Map/Layers/LineSearchLayer';
import OperationalPoints from 'common/Map/Layers/OperationalPoints';
import OSM from 'common/Map/Layers/OSM';
import PlatformsLayer from 'common/Map/Layers/Platforms';
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 Switches from 'common/Map/Layers/Switches';
import Terrain from 'common/Map/Layers/Terrain';
import TracksGeographic from 'common/Map/Layers/TracksGeographic';
import TracksOSM from 'common/Map/Layers/TracksOSM';
import { removeSearchItemMarkersOnMap } from 'common/Map/utils';
import { computeBBoxViewport } from 'common/Map/WarpedMap/core/helpers';
import { useInfraID } from 'common/osrdContext';
import { LAYER_GROUPS_ORDER, LAYERS } from 'config/layerOrder';
import VirtualLayers from 'modules/simulationResult/components/SimulationResultsMap/VirtualLayers';
import AddPathStepPopup from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/AddPathStepPopup';
import { updateViewport } from 'reducers/map';
import type { Viewport } from 'reducers/map';
import { getMap, getTerrain3DExaggeration } from 'reducers/map/selectors';
import { useAppDispatch } from 'store';
import { getMapMouseEventNearestFeature } from 'utils/mapHelper';
import ItineraryLayer from './ManageTrainScheduleMap/ItineraryLayer';
import ItineraryMarkers, {
type MarkerInformation,
} from './ManageTrainScheduleMap/ItineraryMarkers';
import type { FeatureInfoClick } from './types';
type MapProps = {
pathProperties?: ManageTrainSchedulePathProperties;
pathGeometry?: NonNullable<PathProperties['geometry']>;
setMapCanvas?: (mapCanvas: string) => void;
isReadOnly?: boolean;
hideAttribution?: boolean;
hideItinerary?: boolean;
preventPointSelection?: boolean;
mapId?: string;
simulationPathSteps: MarkerInformation[];
showStdcmAssets?: boolean;
isFeasible?: boolean;
};
const Map = ({
pathProperties,
pathGeometry: geometry,
setMapCanvas,
isReadOnly = false,
hideAttribution = false,
hideItinerary = false,
preventPointSelection = false,
mapId = 'map-container',
simulationPathSteps,
showStdcmAssets = false,
isFeasible = true,
children,
}: PropsWithChildren<MapProps>) => {
const mapBlankStyle = useMapBlankStyle();
const infraID = useInfraID();
const terrain3DExaggeration = useSelector(getTerrain3DExaggeration);
const { viewport, mapSearchMarker, mapStyle, showOSM, layersSettings } = useSelector(getMap);
const pathGeometry = useMemo(
() => geometry || pathProperties?.geometry,
[pathProperties, geometry]
);
const mapViewport = useMemo(
() =>
isReadOnly && pathGeometry ? computeBBoxViewport(bbox(pathGeometry), viewport) : viewport,
[isReadOnly, pathGeometry, viewport]
);
const [mapIsLoaded, setMapIsLoaded] = useState(false);
const [snappedPoint, setSnappedPoint] = useState<Feature<Point> | undefined>();
const { urlLat = '', urlLon = '', urlZoom = '', urlBearing = '', urlPitch = '' } = useParams();
const dispatch = useAppDispatch();
const updateViewportChange = useCallback(
(value: Partial<Viewport>) => dispatch(updateViewport(value, undefined)),
[dispatch]
);
const mapRef = useRef<MapRef | null>(null);
const scaleControlStyle = {
left: 20,
bottom: 20,
};
const [featureInfoClick, setFeatureInfoClick] = useState<FeatureInfoClick>();
const resetFeatureInfoClick = useCallback(() => {
setFeatureInfoClick(undefined);
}, []);
const closeFeatureInfoClickPopup = useCallback(() => {
if (featureInfoClick) {
setFeatureInfoClick(undefined);
}
}, [featureInfoClick]);
const resetPitchBearing = () => {
updateViewportChange({
...mapViewport,
bearing: 0,
pitch: 0,
});
};
const onFeatureClick = (e: MapLayerMouseEvent) => {
if (preventPointSelection) return;
const result = getMapMouseEventNearestFeature(e, { layersId: ['chartis/tracks-geo/main'] });
if (
result &&
result.feature.properties &&
result.feature.properties.id &&
result.feature.geometry.type === 'LineString'
) {
setFeatureInfoClick({
feature: result.feature,
coordinates: result.nearest,
});
} else {
setFeatureInfoClick(undefined);
}
removeSearchItemMarkersOnMap(dispatch);
};
const onMoveGetFeature = (e: MapLayerMouseEvent) => {
if (preventPointSelection) return;
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 interactiveLayerIds = useMemo(() => {
const result: Array<string> = [];
result.push('chartis/tracks-geo/main');
if (layersSettings.operationalpoints) {
result.push('chartis/osrd_operational_point/geo');
}
if (layersSettings.tvds) {
result.push('chartis/osrd_tvd_section/geo');
}
return result;
}, [layersSettings]);
useEffect(() => {
if (urlLat) {
updateViewportChange({
...mapViewport,
latitude: parseFloat(urlLat),
longitude: parseFloat(urlLon),
zoom: parseFloat(urlZoom),
bearing: parseFloat(urlBearing),
pitch: parseFloat(urlPitch),
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
const points = pathGeometry ?? {
coordinates: compact(simulationPathSteps.map((step) => step.coordinates)),
type: 'LineString',
};
if (points.coordinates.length > 2) {
const newViewport = computeBBoxViewport(bbox(points), mapViewport);
dispatch(updateViewport(newViewport));
}
}, [pathGeometry, simulationPathSteps]);
return (
<>
{!isReadOnly && (
<MapButtons
map={mapRef.current ?? undefined}
resetPitchBearing={resetPitchBearing}
closeFeatureInfoClickPopup={closeFeatureInfoClickPopup}
bearing={mapViewport.bearing}
withMapKeyButton
viewPort={mapViewport}
isNewButtons
/>
)}
<ReactMapGL
dragPan={false}
scrollZoom={false}
ref={mapRef}
{...mapViewport}
style={{ width: '100%', height: '100%' }}
cursor={isReadOnly || preventPointSelection ? 'default' : 'pointer'}
mapStyle={mapBlankStyle}
attributionControl={false} // Defined below
{...(!isReadOnly && {
dragPan: true,
scrollZoom: true,
onMove: (e) => updateViewportChange(e.viewState),
onMouseMove: onMoveGetFeature,
onClick: onFeatureClick,
onResize: (e) => {
updateViewportChange({
width: e.target.getContainer().offsetWidth,
height: e.target.getContainer().offsetHeight,
});
},
})}
interactiveLayerIds={interactiveLayerIds}
touchZoomRotate
maxPitch={85}
terrain={
terrain3DExaggeration
? { source: 'terrain', exaggeration: terrain3DExaggeration }
: undefined
}
onLoad={() => {
setMapIsLoaded(true);
}}
onIdle={() => {
captureMap(mapViewport, mapId, setMapCanvas, pathGeometry);
}}
preserveDrawingBuffer
id={mapId}
>
<VirtualLayers />
{!hideAttribution && (
<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]} />
{mapIsLoaded && showOSM && (
<>
<OSM
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
mapIsLoaded={mapIsLoaded}
/>
<Hillshade
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
/>
</>
)}
<PlatformsLayer
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.PLATFORMS.GROUP]}
/>
<TracksGeographic
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.TRACKS_GEOGRAPHIC.GROUP]}
infraID={infraID}
/>
<TracksOSM
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.TRACKS_OSM.GROUP]}
/>
<Routes
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.ROUTES.GROUP]}
infraID={infraID}
/>
{!isReadOnly && (
<>
{layersSettings.operationalpoints && (
<OperationalPoints
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.OPERATIONAL_POINTS.GROUP]}
infraID={infraID}
/>
)}
<Electrifications
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.ELECTRIFICATIONS.GROUP]}
infraID={infraID}
/>
{layersSettings.neutral_sections && (
<NeutralSections
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.DEAD_SECTIONS.GROUP]}
infraID={infraID}
/>
)}
<BufferStops
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BUFFER_STOPS.GROUP]}
infraID={infraID}
/>
<Detectors
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.DETECTORS.GROUP]}
infraID={infraID}
/>
<Switches
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.SWITCHES.GROUP]}
infraID={infraID}
/>
<SpeedLimits
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.SPEED_LIMITS.GROUP]}
infraID={infraID}
/>
<SNCF_PSL
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.SPEED_LIMITS.GROUP]}
infraID={infraID}
/>
<Signals
sourceTable="signals"
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.SIGNALS.GROUP]}
infraID={infraID}
/>
<LineSearchLayer
layerOrder={LAYER_GROUPS_ORDER[LAYERS.LINE_SEARCH.GROUP]}
infraID={infraID}
/>
{!showStdcmAssets && featureInfoClick && (
<AddPathStepPopup
pathProperties={pathProperties}
featureInfoClick={featureInfoClick}
resetFeatureInfoClick={resetFeatureInfoClick}
/>
)}
</>
)}
<ItineraryLayer
layerOrder={LAYER_GROUPS_ORDER[LAYERS.ITINERARY.GROUP]}
geometry={pathGeometry}
hideItineraryLine={hideItinerary}
showStdcmAssets={showStdcmAssets}
isFeasible={isFeasible}
/>
{mapRef.current && (
<ItineraryMarkers
simulationPathSteps={simulationPathSteps}
map={mapRef.current.getMap()}
showStdcmAssets={showStdcmAssets}
/>
)}
{mapSearchMarker && <SearchMarker data={mapSearchMarker} colors={colors[mapStyle]} />}
{snappedPoint !== undefined && <SnappedMarker geojson={snappedPoint} />}
{children}
</ReactMapGL>
</>
);
};
export default Map;