/// <summary> /// Returns the direction to the next shape segment. /// </summary> public static DirectionEnum DirectionToNext(this Route route, int i) { if (i < 0 || i >= route.Shape.Length - 1) { throw new ArgumentOutOfRangeException("i"); } return(DirectionCalculator.Calculate( new Coordinate(route.Shape[i].Latitude, route.Shape[i].Longitude), new Coordinate(route.Shape[i + 1].Latitude, route.Shape[i + 1].Longitude))); }
public void RelativeDirectionTest() { var direction = DirectionCalculator.Calculate(new Coordinate(0, 1), new Coordinate(0, 0), new Coordinate(1, 0)); Assert.AreEqual(90, direction.Angle, 0.0001); Assert.AreEqual(RelativeDirectionEnum.Right, direction.Direction); direction = DirectionCalculator.Calculate(new Coordinate(1, 0), new Coordinate(0, 0), new Coordinate(0, 1)); Assert.AreEqual(270, direction.Angle, 0.0001); Assert.AreEqual(RelativeDirectionEnum.Left, direction.Direction); }
/// <summary> /// Returns the turn direction for the shape point at the given index. /// </summary> public static RelativeDirection RelativeDirectionAt(this Route route, int i) { if (i < 0 || i >= route.Shape.Length) { throw new ArgumentOutOfRangeException("i"); } if (i == 0 || i == route.Shape.Length - 1) { // not possible to calculate a relative direction for the first or last segment. throw new ArgumentOutOfRangeException("i", "It's not possible to calculate a relative direction for the first or last segment."); } return(DirectionCalculator.Calculate( new Coordinate(route.Shape[i - 1].Latitude, route.Shape[i - 1].Longitude), new Coordinate(route.Shape[i].Latitude, route.Shape[i].Longitude), new Coordinate(route.Shape[i + 1].Latitude, route.Shape[i + 1].Longitude))); }
public static int GetStartInstruction(Route r, int i, ILanguageReference languageReference, out Instruction instruction) { instruction = (Instruction)null; if (i != 0 || r.Segments.Count <= 0) { return(0); } DirectionEnum directionEnum = DirectionCalculator.Calculate(new GeoCoordinate((double)r.Segments[0].Latitude, (double)r.Segments[0].Longitude), new GeoCoordinate((double)r.Segments[1].Latitude, (double)r.Segments[1].Longitude)); string str = languageReference[directionEnum.ToInvariantString()]; instruction = new Instruction() { Text = string.Format(languageReference["Start {0}."], (object)str), Type = "start", Segment = 0 }; return(1); }
public void DirectionTest() { Assert.AreEqual(DirectionEnum.North, DirectionCalculator.Calculate(new Coordinate(0, 0), new Coordinate(1, 0))); Assert.AreEqual(DirectionEnum.NorthEast, DirectionCalculator.Calculate(new Coordinate(0, 0), new Coordinate(1, 1))); Assert.AreEqual(DirectionEnum.East, DirectionCalculator.Calculate(new Coordinate(0, 0), new Coordinate(0, 1))); Assert.AreEqual(DirectionEnum.SouthEast, DirectionCalculator.Calculate(new Coordinate(0, 0), new Coordinate(-1, 1))); Assert.AreEqual(DirectionEnum.South, DirectionCalculator.Calculate(new Coordinate(0, 0), new Coordinate(-1, 0))); Assert.AreEqual(DirectionEnum.SouthWest, DirectionCalculator.Calculate(new Coordinate(0, 0), new Coordinate(-1, -1))); Assert.AreEqual(DirectionEnum.West, DirectionCalculator.Calculate(new Coordinate(0, 0), new Coordinate(0, -1))); Assert.AreEqual(DirectionEnum.NorthWest, DirectionCalculator.Calculate(new Coordinate(0, 0), new Coordinate(1, -1))); }
/// <summary> /// Returns the turn direction for the shape point at the given index. /// </summary> public static RelativeDirection RelativeDirectionAt(this Route route, int i, float toleranceInMeters = 1) { if (i < 0 || i >= route.Shape.Count) { throw new ArgumentOutOfRangeException(nameof(i)); } if (i == 0 || i == route.Shape.Count - 1) { // not possible to calculate a relative direction for the first or last segment. throw new ArgumentOutOfRangeException(nameof(i), "It's not possible to calculate a relative direction for the first or last segment."); } var h = i - 1; while (h > 0 && Coordinate.DistanceEstimateInMeter(route.Shape[h].Latitude, route.Shape[h].Longitude, route.Shape[i].Latitude, route.Shape[i].Longitude) < toleranceInMeters) { // work backward from i to make sure we don't use an identical coordinate or one that's too close to be useful. h--; } var j = i + 1; while (j < route.Shape.Count - 1 && Coordinate.DistanceEstimateInMeter(route.Shape[j].Latitude, route.Shape[j].Longitude, route.Shape[i].Latitude, route.Shape[i].Longitude) < toleranceInMeters) { // work forward from i to make sure we don't use an identical coordinate or one that's too close to be useful. j++; } var dir = DirectionCalculator.Calculate( new Coordinate(route.Shape[h].Latitude, route.Shape[h].Longitude), new Coordinate(route.Shape[i].Latitude, route.Shape[i].Longitude), new Coordinate(route.Shape[j].Latitude, route.Shape[j].Longitude)); if (double.IsNaN(dir.Angle)) { // it's possible the angle doesn't make sense, best to not return anything in that case. return(null); } return(dir); }
/// <summary> /// Gets the direction at this position. /// </summary> public static DirectionEnum Direction(this RoutePosition position) { return(DirectionCalculator.Calculate(position.Location(), position.NextLocation())); }