-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathMap.js
254 lines (236 loc) · 8.92 KB
/
Map.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
import 'common/Map/Map.scss';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import ReactMapGL, { AttributionControl, FlyToInterpolator, ScaleControl } from 'react-map-gl';
import { lineString as turfLineString, point as turfPoint } from '@turf/helpers';
import { useDispatch, useSelector } from 'react-redux';
/* Main data & layers */
import Background from 'common/Map/Layers/Background';
/* Settings & Buttons */
import MapButtons from 'common/Map/Buttons/MapButtons';
import Catenaries from 'common/Map/Layers/Catenaries';
import Hillshade from 'common/Map/Layers/Hillshade';
import OSM from 'common/Map/Layers/OSM';
import OperationalPoints from 'common/Map/Layers/OperationalPoints';
import Platform from 'common/Map/Layers/Platform';
import RenderItinerary from 'applications/osrd/components/OSRDConfMap/RenderItinerary';
import RenderItineraryMarkers from 'applications/osrd/components/OSRDConfMap/RenderItineraryMarkers';
/* Interactions */
import RenderPopup from 'applications/osrd/components/OSRDConfMap/RenderPopup';
import Routes from 'common/Map/Layers/Routes';
import SearchMarker from 'common/Map/Layers/SearchMarker';
import SignalingType from 'common/Map/Layers/SignalingType';
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 TVDs from 'common/Map/Layers/TVDs';
import TracksGeographic from 'common/Map/Layers/TracksGeographic';
import TracksSchematic from 'common/Map/Layers/TracksSchematic';
import colors from 'common/Map/Consts/colors';
import osmBlankStyle from 'common/Map/Layers/osmBlankStyle';
import turfNearestPointOnLine from '@turf/nearest-point-on-line';
import { updateFeatureInfoClickOSRD } from 'reducers/osrdconf';
import { updateViewport } from 'reducers/map';
import { useParams } from 'react-router-dom';
function Map() {
const { viewport, mapSearchMarker, mapStyle, mapTrackSources, showOSM, layersSettings } =
useSelector((state) => state.map);
const [idHover, setIdHover] = useState(undefined);
const [trackSectionHover, setTrackSectionHover] = useState(undefined);
const [lngLatHover, setLngLatHover] = useState(undefined);
const [trackSectionGeoJSON, setTrackSectionGeoJSON] = useState(undefined);
const [snappedPoint, setSnappedPoint] = useState(undefined);
const { urlLat, urlLon, urlZoom, urlBearing, urlPitch } = useParams();
const dispatch = useDispatch();
const updateViewportChange = useCallback(
(value) => dispatch(updateViewport(value, undefined)),
[dispatch]
);
const mapRef = useRef(null);
const scaleControlStyle = {
left: 20,
bottom: 20,
};
const resetPitchBearing = () => {
updateViewportChange({
...viewport,
bearing: parseFloat(0),
pitch: parseFloat(0),
transitionDuration: 1000,
transitionInterpolator: new FlyToInterpolator(),
});
};
const onFeatureClick = (e) => {
if (
e.features &&
e.features.length > 0 &&
e.features[0].properties.id !== undefined
// && e.features[0].properties.type_voie === 'VP') {
) {
dispatch(
updateFeatureInfoClickOSRD({
displayPopup: true,
feature: e.features[0],
lngLat: e.lngLat,
})
);
} else {
dispatch(
updateFeatureInfoClickOSRD({
displayPopup: false,
feature: undefined,
})
);
}
};
const getGeoJSONFeature = (e) => {
if (
trackSectionHover === undefined ||
e.features[0].properties.id !== trackSectionHover.properties.id
) {
setTrackSectionHover(e.features[0]);
}
// Get GEOJSON of features hovered for snapping
const width = 5;
const height = 5;
const features = mapRef.current.queryRenderedFeatures(
[
[e.point[0] - width / 2, e.point[1] - height / 2],
[e.point[0] + width / 2, e.point[1] + height / 2],
],
{
layers:
mapTrackSources === 'geographic'
? ['chartis/tracks-geo/main']
: ['chartis/tracks-sch/main'],
}
);
if (features[0] !== undefined) {
setTrackSectionGeoJSON(features[0].geometry);
}
};
const onFeatureHover = (e) => {
if (e.features !== null && e.features[0] !== undefined) {
getGeoJSONFeature(e);
setIdHover(e.features[0].properties.id);
setLngLatHover(e.lngLat);
} else {
setIdHover(undefined);
setSnappedPoint(undefined);
}
};
const defineInteractiveLayers = () => {
const interactiveLayersLocal = [];
if (mapTrackSources === 'geographic') {
interactiveLayersLocal.push('chartis/tracks-geo/main');
if (layersSettings.operationalpoints) {
interactiveLayersLocal.push('chartis/osrd_operational_point/geo');
}
}
if (mapTrackSources === 'schematic') {
interactiveLayersLocal.push('chartis/tracks-sch/main');
if (layersSettings.operationalpoints) {
interactiveLayersLocal.push('chartis/osrd_operational_point/sch');
}
}
if (layersSettings.tvds) {
interactiveLayersLocal.push('chartis/osrd_tvd_section/geo');
}
return interactiveLayersLocal;
};
useEffect(() => {
if (trackSectionGeoJSON !== undefined && lngLatHover !== undefined) {
const line = turfLineString(trackSectionGeoJSON.coordinates);
const point = turfPoint(lngLatHover);
setSnappedPoint(turfNearestPointOnLine(line, point));
}
}, [trackSectionGeoJSON, trackSectionHover, lngLatHover]);
useEffect(() => {
if (urlLat) {
updateViewportChange({
...viewport,
latitude: parseFloat(urlLat),
longitude: parseFloat(urlLon),
zoom: parseFloat(urlZoom),
bearing: parseFloat(urlBearing),
pitch: parseFloat(urlPitch),
});
}
}, []);
return (
<>
<MapButtons resetPitchBearing={resetPitchBearing} />
<ReactMapGL
ref={mapRef}
{...viewport}
style={{ cursor: 'pointer' }}
width="100%"
height="100%"
mapStyle={osmBlankStyle}
onViewportChange={updateViewportChange}
clickRadius={10}
attributionControl={false} // Defined below
onClick={onFeatureClick}
onHover={onFeatureHover}
interactiveLayerIds={defineInteractiveLayers()}
touchRotate
asyncRender
>
<AttributionControl
className="attribution-control"
customAttribution="©SNCF/DGEX Solutions"
/>
<ScaleControl maxWidth={100} unit="metric" style={scaleControlStyle} />
<Background colors={colors[mapStyle]} />
{!showOSM ? null : (
<>
<OSM mapStyle={mapStyle} />
<Hillshade mapStyle={mapStyle} />
</>
)}
{/* Have to duplicate objects with sourceLayer to avoid cache problems in mapbox */}
{mapTrackSources === 'geographic' ? (
<>
<TVDs geomType="geo" colors={colors[mapStyle]} idHover={idHover} />
<Catenaries geomType="geo" colors={colors[mapStyle]} />
<Platform colors={colors[mapStyle]} />
<TracksGeographic colors={colors[mapStyle]} />
<TracksOSM colors={colors[mapStyle]} />
<OperationalPoints geomType="geo" colors={colors[mapStyle]} />
<SignalingType geomType="geo" />
<Routes geomType="geo" colors={colors[mapStyle]} />
<SpeedLimits geomType="geo" colors={colors[mapStyle]} />
<Signals sourceTable="signals" colors={colors[mapStyle]} sourceLayer="geo" />
<BufferStops geomType="geo" colors={colors[mapStyle]} />
<Detectors geomType="geo" colors={colors[mapStyle]} />
<Switches geomType="geo" colors={colors[mapStyle]} />
<RenderPopup />
</>
) : (
<>
<TracksSchematic colors={colors[mapStyle]} idHover={idHover} />
<OperationalPoints geomType="sch" colors={colors[mapStyle]} />
<Routes geomType="sch" colors={colors[mapStyle]} />
<Signals sourceTable="signals" colors={colors[mapStyle]} sourceLayer="sch" />
<SpeedLimits geomType="sch" colors={colors[mapStyle]} />
<BufferStops geomType="sch" colors={colors[mapStyle]} />
<Detectors geomType="sch" colors={colors[mapStyle]} />
<Switches geomType="sch" colors={colors[mapStyle]} />
</>
)}
<RenderPopup />
<RenderItinerary />
<RenderItineraryMarkers />
{mapSearchMarker !== undefined ? (
<SearchMarker data={mapSearchMarker} colors={colors[mapStyle]} />
) : null}
{snappedPoint !== undefined ? <SnappedMarker geojson={snappedPoint} /> : null}
</ReactMapGL>
</>
);
}
export default Map;