-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathTrackEditionLayers.tsx
185 lines (171 loc) · 5.58 KB
/
TrackEditionLayers.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
import { useContext } from 'react';
import type { Position } from 'geojson';
import { last } from 'lodash';
import { Layer, Source } from 'react-map-gl/maplibre';
import { useSelector } from 'react-redux';
import EditorContext from 'applications/editor/context';
import { POINTS_LAYER_ID, TRACK_LAYER_ID } from 'applications/editor/tools/trackEdition/consts';
import type { TrackEditionState } from 'applications/editor/tools/trackEdition/types';
import type { ExtendedEditorContextType } from 'applications/editor/types';
import colors from 'common/Map/Consts/colors';
import GeoJSONs from 'common/Map/Layers/InfraObjectLayers/GeoJSONs';
import { useInfraID } from 'common/osrdContext';
import { getMap } from 'reducers/map/selectors';
export const TRACK_COLOR = '#666';
export const TRACK_STYLE = { 'line-color': TRACK_COLOR, 'line-dasharray': [2, 1], 'line-width': 2 };
const TrackEditionLayers = () => {
const {
state,
renderingFingerprint,
editorState: { editorLayers },
} = useContext(EditorContext) as ExtendedEditorContextType<TrackEditionState>;
const { mapStyle, layersSettings, issuesSettings } = useSelector(getMap);
const infraID = useInfraID();
const isAddingPointOnExistingSection =
typeof state.nearestPoint?.properties?.sectionIndex === 'number';
const points = state.track.geometry.coordinates;
let additionalSegment: typeof points = [];
if (
points.length &&
state.editionState.type === 'addPoint' &&
state.mousePosition &&
!isAddingPointOnExistingSection
) {
const lastPosition =
state.anchorLinePoints &&
state.nearestPoint &&
typeof state.nearestPoint.properties?.sectionIndex !== 'number'
? (state.nearestPoint.geometry.coordinates as [number, number])
: state.mousePosition;
if (state.addNewPointsAtStart) {
additionalSegment = [lastPosition, points[0]];
} else {
additionalSegment = [last(points) as [number, number], lastPosition];
}
}
let highlightedPoint: Position | undefined;
if (state.editionState.type === 'movePoint') {
if (typeof state.editionState.draggedPointIndex === 'number') {
highlightedPoint = state.track.geometry.coordinates[state.editionState.draggedPointIndex];
} else if (typeof state.editionState.hoveredPointIndex === 'number') {
highlightedPoint = state.track.geometry.coordinates[state.editionState.hoveredPointIndex];
}
} else if (state.editionState.type === 'deletePoint') {
if (typeof state.editionState.hoveredPointIndex === 'number') {
highlightedPoint = state.track.geometry.coordinates[state.editionState.hoveredPointIndex];
}
}
return (
<>
{/* Editor data layer */}
<GeoJSONs
colors={colors[mapStyle]}
hidden={state.track.properties.id ? [state.track.properties.id] : undefined}
layers={editorLayers}
fingerprint={renderingFingerprint}
layersSettings={layersSettings}
issuesSettings={issuesSettings}
infraID={infraID}
/>
{/* Track path */}
<Source
type="geojson"
data={{
type: 'Feature',
properties: {},
geometry: {
type: 'LineString',
coordinates: points,
},
}}
>
<Layer id={TRACK_LAYER_ID} type="line" paint={TRACK_STYLE} />
</Source>
{additionalSegment.length > 0 && (
<Source
type="geojson"
data={{
type: 'Feature',
properties: {},
geometry: {
type: 'LineString',
coordinates: additionalSegment,
},
}}
>
<Layer type="line" paint={TRACK_STYLE} />
</Source>
)}
{/* Highlighted nearest point from data */}
{state.editionState.type === 'addPoint' && state.nearestPoint && (
<Source type="geojson" data={state.nearestPoint}>
<Layer
type="circle"
paint={{
'circle-radius': 4,
'circle-color': '#fff',
'circle-stroke-color': '#009EED',
'circle-stroke-width': 2,
}}
/>
</Source>
)}
{/* Track points */}
<Source
type="geojson"
data={{
type: 'FeatureCollection',
features: points.map((point, index) => ({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: point,
},
properties: { index },
})),
}}
>
<Layer
id={POINTS_LAYER_ID}
type="circle"
paint={{
'circle-radius': state.editionState.type !== 'addPoint' ? 4 : 2,
'circle-color': '#fff',
'circle-stroke-color': TRACK_COLOR,
'circle-stroke-width': 1,
}}
/>
</Source>
{/* Highlighted nearest point or dragged point from track */}
<Source
type="geojson"
data={{
type: 'FeatureCollection',
features: highlightedPoint
? [
{
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: highlightedPoint,
},
},
]
: [],
}}
>
<Layer
type="circle"
paint={{
'circle-radius': 4,
'circle-color': '#fff',
'circle-stroke-color': TRACK_COLOR,
'circle-stroke-width': 3,
}}
/>
</Source>
</>
);
};
export default TrackEditionLayers;