Skip to content

Commit 306ddcd

Browse files
nicolaswurtzflomonster
authored andcommitted
front: bugfix on snapped cursor #2365 (#2375)
Co-authored-by: Nicolas Wurtz <[email protected]>
1 parent ea681dc commit 306ddcd

File tree

3 files changed

+18
-38
lines changed

3 files changed

+18
-38
lines changed

front/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@turf/bezier-spline": "^6.5.0",
2323
"@turf/boolean-intersects": "^6.3.0",
2424
"@turf/boolean-point-in-polygon": "^6.3.0",
25+
"@turf/combine": "^6.5.0",
2526
"@turf/distance": "^6.3.0",
2627
"@turf/helpers": "^6.3.0",
2728
"@turf/length": "^6.5.0",

front/src/applications/osrd/views/OSRDConfig/Map.tsx

+9-38
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import ReactMapGL, {
77
MapRef,
88
MapEvent,
99
} from 'react-map-gl';
10-
import { lineString as turfLineString, point as turfPoint } from '@turf/helpers';
10+
import { point as turfPoint, featureCollection } from '@turf/helpers';
1111
import { useDispatch, useSelector } from 'react-redux';
1212
import turfNearestPointOnLine, { NearestPointOnLine } from '@turf/nearest-point-on-line';
13-
import { Feature, Position, LineString } from 'geojson';
13+
import combine from '@turf/combine';
14+
import { Position, LineString } from 'geojson';
1415
import { useParams } from 'react-router-dom';
1516

1617
import { RootState } from 'reducers';
@@ -55,7 +56,6 @@ function Map() {
5556
const { viewport, mapSearchMarker, mapStyle, mapTrackSources, showOSM, layersSettings } =
5657
useSelector((state: RootState) => state.map);
5758
const [idHover, setIdHover] = useState<string | undefined>(undefined);
58-
const [trackSectionHover, setTrackSectionHover] = useState<Feature<any>>();
5959
const [lngLatHover, setLngLatHover] = useState<Position>();
6060
const [trackSectionGeoJSON, setTrackSectionGeoJSON] = useState<LineString>();
6161
const [snappedPoint, setSnappedPoint] = useState<NearestPointOnLine>();
@@ -83,12 +83,7 @@ function Map() {
8383
};
8484

8585
const onFeatureClick = (e: MapEvent) => {
86-
if (
87-
e.features &&
88-
e.features.length > 0 &&
89-
e.features[0].properties.id !== undefined
90-
// && e.features[0].properties.type_voie === 'VP') {
91-
) {
86+
if (e.features && e.features.length > 0 && e.features[0].properties.id !== undefined) {
9287
dispatch(
9388
updateFeatureInfoClickOSRD({
9489
displayPopup: true,
@@ -107,32 +102,9 @@ function Map() {
107102
};
108103

109104
const getGeoJSONFeature = (e: MapEvent) => {
110-
if (
111-
trackSectionHover === undefined ||
112-
e?.features?.[0].properties.id !== trackSectionHover?.properties?.id
113-
) {
114-
setTrackSectionHover(e?.features?.[0]);
115-
}
116-
117-
// Get GEOJSON of features hovered for snapping
118-
const width = 5;
119-
const height = 5;
120-
if (mapRef.current) {
121-
const features = mapRef.current.queryRenderedFeatures(
122-
[
123-
[e.point[0] - width / 2, e.point[1] - height / 2],
124-
[e.point[0] + width / 2, e.point[1] + height / 2],
125-
],
126-
{
127-
layers:
128-
mapTrackSources === 'geographic'
129-
? ['chartis/tracks-geo/main']
130-
: ['chartis/tracks-sch/main'],
131-
}
132-
);
133-
if (features[0] !== undefined) {
134-
setTrackSectionGeoJSON(features[0].geometry);
135-
}
105+
if (e.features && e.features[0] !== undefined) {
106+
const mergedFeatures = combine(featureCollection(e.features));
107+
setTrackSectionGeoJSON(mergedFeatures.features[0].geometry);
136108
}
137109
};
138110

@@ -169,15 +141,14 @@ function Map() {
169141

170142
useEffect(() => {
171143
if (trackSectionGeoJSON !== undefined && lngLatHover !== undefined) {
172-
const line = turfLineString(trackSectionGeoJSON.coordinates);
173144
const point = turfPoint(lngLatHover);
174145
try {
175-
setSnappedPoint(turfNearestPointOnLine(line, point));
146+
setSnappedPoint(turfNearestPointOnLine(trackSectionGeoJSON, point));
176147
} catch (error) {
177148
console.warn(`Ìmpossible to snapPoint - error ${error}`);
178149
}
179150
}
180-
}, [trackSectionGeoJSON, trackSectionHover, lngLatHover]);
151+
}, [trackSectionGeoJSON, lngLatHover]);
181152

182153
useEffect(() => {
183154
if (urlLat) {

front/yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -3385,6 +3385,14 @@
33853385
dependencies:
33863386
"@turf/helpers" "^6.5.0"
33873387

3388+
"@turf/combine@^6.5.0":
3389+
version "6.5.0"
3390+
resolved "https://registry.yarnpkg.com/@turf/combine/-/combine-6.5.0.tgz#e0f3468ac9c09c24fa7184ebbd8a79d2e595ef81"
3391+
integrity sha512-Q8EIC4OtAcHiJB3C4R+FpB4LANiT90t17uOd851qkM2/o6m39bfN5Mv0PWqMZIHWrrosZqRqoY9dJnzz/rJxYQ==
3392+
dependencies:
3393+
"@turf/helpers" "^6.5.0"
3394+
"@turf/meta" "^6.5.0"
3395+
33883396
"@turf/destination@^6.5.0":
33893397
version "6.5.0"
33903398
resolved "https://registry.yarnpkg.com/@turf/destination/-/destination-6.5.0.tgz#30a84702f9677d076130e0440d3223ae503fdae1"

0 commit comments

Comments
 (0)