From 04a8eb43eead686c0e32255965f6e573c5ffcbfa Mon Sep 17 00:00:00 2001 From: Ariel Costas Guerrero Date: Fri, 21 Nov 2025 21:22:33 +0100 Subject: feat: Enhance shape retrieval with bus and stop point indexing; update related components --- .../Services/ShapeTraversalService.cs | 30 +++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'src/Costasdev.Busurbano.Backend/Services/ShapeTraversalService.cs') diff --git a/src/Costasdev.Busurbano.Backend/Services/ShapeTraversalService.cs b/src/Costasdev.Busurbano.Backend/Services/ShapeTraversalService.cs index 37b76ee..7263ad0 100644 --- a/src/Costasdev.Busurbano.Backend/Services/ShapeTraversalService.cs +++ b/src/Costasdev.Busurbano.Backend/Services/ShapeTraversalService.cs @@ -68,27 +68,45 @@ public class ShapeTraversalService var result = new List(); // Ensure startIndex is within bounds if (startIndex < 0) startIndex = 0; + // If startIndex is beyond the end, return empty list if (startIndex >= shape.Points.Count) return result; for (int i = startIndex; i < shape.Points.Count; i++) { - result.Add(TransformToLatLng(shape.Points[i])); + var pos = TransformToLatLng(shape.Points[i]); + pos.ShapeIndex = i; + result.Add(pos); } return result; } + public async Task FindClosestPointIndexAsync(string shapeId, double lat, double lon) + { + var shape = await LoadShapeAsync(shapeId); + if (shape == null) return null; + + // Transform input WGS84 to EPSG:25829 + // Input is [Longitude, Latitude] + var inverseTransform = _transformation.MathTransform.Inverse(); + var transformed = inverseTransform.Transform(new[] { lon, lat }); + + var location = new Epsg25829 { X = transformed[0], Y = transformed[1] }; + + return FindClosestPointIndex(shape.Points, location); + } + /// /// Calculates the bus position by reverse-traversing the shape from the stop location /// /// The shape points (in EPSG:25829 meters) /// The stop location (in EPSG:25829 meters) /// Distance in meters from the stop to traverse backwards - /// The lat/lng position of the bus, or null if not calculable - public Position? GetBusPosition(Shape shape, Epsg25829 stopLocation, int distanceMeters) + /// The lat/lng position of the bus and the stop index on the shape + public (Position? BusPosition, int StopIndex) GetBusPosition(Shape shape, Epsg25829 stopLocation, int distanceMeters) { if (shape.Points.Count == 0 || distanceMeters <= 0) { - return null; + return (null, -1); } // Find the closest point on the shape to the stop @@ -99,7 +117,7 @@ public class ShapeTraversalService if (busPoint == null) { - return null; + return (null, closestPointIndex); } var forwardPoint = shape.Points[forwardIndex]; @@ -114,7 +132,7 @@ public class ShapeTraversalService var pos = TransformToLatLng(busPoint); pos.OrientationDegrees = (int)Math.Round(bearing); pos.ShapeIndex = forwardIndex; - return pos; + return (pos, closestPointIndex); } /// -- cgit v1.3