/// <summary> /// Project on route and return the next entry index and coordinate. /// </summary> /// <param name="route"></param> /// <param name="coordinates"></param> /// <returns></returns> private KeyValuePair <int, GeoCoordinate> ProjectOn(Route route, GeoCoordinate coordinates) { double distance = double.MaxValue; GeoCoordinate closest = null; int closestIdx = -1; List <GeoCoordinate> points = route.GetPoints(); for (int idx = 0; idx < points.Count - 1; idx++) { GeoCoordinateLine line = new GeoCoordinateLine(points[idx], points[idx + 1], true, true); PointF2D projectedPoint = line.ProjectOn(coordinates); GeoCoordinate projected; double currentDistance; if (projectedPoint != null) { projected = new GeoCoordinate(projectedPoint[1], projectedPoint[0]); currentDistance = coordinates.Distance(projected); if (currentDistance < distance) { closest = projected; closestIdx = idx + 1; distance = currentDistance; } } projected = points[idx]; currentDistance = coordinates.Distance(projected); if (currentDistance < distance) { closest = projected; closestIdx = idx; distance = currentDistance; } } return(new KeyValuePair <int, GeoCoordinate>(closestIdx, closest)); }
public static bool ProjectOn(this Route route, GeoCoordinate coordinates, out GeoCoordinate projectedCoordinates, out int entryIndex, out Meter distanceFromStart, out Second timeFromStart) { double num1 = double.MaxValue; distanceFromStart = (Meter)0.0; timeFromStart = (Second)0.0; double num2 = 0.0; projectedCoordinates = (GeoCoordinate)null; entryIndex = -1; List <GeoCoordinate> points = route.GetPoints(); for (int index = 0; index < points.Count - 1; ++index) { PointF2D pointF2D = new GeoCoordinateLine(points[index], points[index + 1], true, true).ProjectOn((PointF2D)coordinates); if (pointF2D != (PointF2D)null) { GeoCoordinate point = new GeoCoordinate(pointF2D[1], pointF2D[0]); double num3 = coordinates.Distance(point); if (num3 < num1) { projectedCoordinates = point; entryIndex = index; num1 = num3; double num4 = point.DistanceReal(points[index]).Value; distanceFromStart = (Meter)(num2 + num4); } } GeoCoordinate point1 = points[index]; double num5 = coordinates.Distance(point1); if (num5 < num1) { projectedCoordinates = point1; entryIndex = index; num1 = num5; distanceFromStart = (Meter)num2; } num2 += points[index].DistanceReal(points[index + 1]).Value; } GeoCoordinate point2 = points[points.Count - 1]; if (coordinates.Distance(point2) < num1) { projectedCoordinates = point2; entryIndex = points.Count - 1; distanceFromStart = (Meter)num2; } return(true); }
public override Task ExecuteAsyc() { var pointA = new GeoCoordinate(48.818508, 2.319620); var pointB = new GeoCoordinate(48.862360, 2.351950); var distance = pointA.Distance(pointB); this.logger.LogInformation($"Distance in meter between {pointA} and {pointB} : {distance}"); return(Task.CompletedTask); }
/// <summary> /// Calculates the closest point on the route. /// </summary> /// <param name="coordinates"></param> /// <returns></returns> public GeoCoordinate ProjectOn(GeoCoordinate coordinates) { double distance = double.MaxValue; GeoCoordinate closests = null; List <GeoCoordinate> points = this.GetPoints(); for (int idx = 0; idx < points.Count - 1; idx++) { GeoCoordinateLine line = new GeoCoordinateLine(points[idx], points[idx + 1]); PointF2D projectedPoint = line.ProjectOn(coordinates); GeoCoordinate projected = new GeoCoordinate(projectedPoint[1], projectedPoint[0]); double currentDistance = coordinates.Distance(projected); if (currentDistance < distance) { closests = projected; distance = currentDistance; } } return(closests); }
public void GeoCoordinate_Distance_Tests() { var c1 = new GeoCoordinate { Latitude = 34.10m, Longitude = -180.01m, }; var c2 = new GeoCoordinate { Latitude = 35.12m, Longitude = -179.11m, }; var distKm = GeoCoordinate.Distance(c1, c2, GeoDistanceUnit.Kilometer); var distNm = GeoCoordinate.Distance(c1, c2, GeoDistanceUnit.NauticalMile); var distSm = GeoCoordinate.Distance(c1, c2, GeoDistanceUnit.StatuteMile); Assert.AreEqual(140.16, Math.Round(distKm, 2)); Assert.AreEqual(75.63, Math.Round(distNm, 2)); Assert.AreEqual(87.09, Math.Round(distSm, 2)); }
/// <summary> /// Searches the data for a point on an edge closest to the given coordinate. /// </summary> /// <param name="graph"></param> /// <param name="vehicle"></param> /// <param name="coordinate"></param> /// <param name="delta"></param> /// <param name="matcher"></param> /// <param name="pointTags"></param> /// <param name="interpreter"></param> /// <param name="verticesOnly"></param> public SearchClosestResult SearchClosest(IBasicRouterDataSource <TEdgeData> graph, IRoutingInterpreter interpreter, Vehicle vehicle, GeoCoordinate coordinate, float delta, IEdgeMatcher matcher, TagsCollection pointTags, bool verticesOnly) { var closestWithMatch = new SearchClosestResult(double.MaxValue, 0); var closestWithoutMatch = new SearchClosestResult(double.MaxValue, 0); double searchBoxSize = delta; // create the search box. var searchBox = new GeoCoordinateBox(new GeoCoordinate( coordinate.Latitude - searchBoxSize, coordinate.Longitude - searchBoxSize), new GeoCoordinate( coordinate.Latitude + searchBoxSize, coordinate.Longitude + searchBoxSize)); // get the arcs from the data source. KeyValuePair <uint, KeyValuePair <uint, TEdgeData> >[] arcs = graph.GetArcs(searchBox); if (!verticesOnly) { // find both closest arcs and vertices. // loop over all. foreach (KeyValuePair <uint, KeyValuePair <uint, TEdgeData> > arc in arcs) { TagsCollection arcTags = graph.TagsIndex.Get(arc.Value.Value.Tags); bool canBeTraversed = vehicle.CanTraverse(arcTags); if (canBeTraversed) { // the edge can be traversed. // test the two points. float fromLatitude, fromLongitude; float toLatitude, toLongitude; double distance; if (graph.GetVertex(arc.Key, out fromLatitude, out fromLongitude) && graph.GetVertex(arc.Value.Key, out toLatitude, out toLongitude)) { // return the vertex. var fromCoordinates = new GeoCoordinate(fromLatitude, fromLongitude); distance = coordinate.Distance(fromCoordinates); if (distance < 0.00001) { // the distance is smaller than the tolerance value. closestWithoutMatch = new SearchClosestResult( distance, arc.Key); if (matcher == null || (pointTags == null || pointTags.Count == 0) || matcher.MatchWithEdge(vehicle, pointTags, arcTags)) { closestWithMatch = new SearchClosestResult( distance, arc.Key); break; } } if (distance < closestWithoutMatch.Distance) { // the distance is smaller for the without match. closestWithoutMatch = new SearchClosestResult( distance, arc.Key); } if (distance < closestWithMatch.Distance) { // the distance is smaller for the with match. if (matcher == null || (pointTags == null || pointTags.Count == 0) || matcher.MatchWithEdge(vehicle, pointTags, graph.TagsIndex.Get(arc.Value.Value.Tags))) { closestWithMatch = new SearchClosestResult( distance, arc.Key); } } var toCoordinates = new GeoCoordinate(toLatitude, toLongitude); distance = coordinate.Distance(toCoordinates); if (distance < closestWithoutMatch.Distance) { // the distance is smaller for the without match. closestWithoutMatch = new SearchClosestResult( distance, arc.Value.Key); } if (distance < closestWithMatch.Distance) { // the distance is smaller for the with match. if (matcher == null || (pointTags == null || pointTags.Count == 0) || matcher.MatchWithEdge(vehicle, pointTags, arcTags)) { closestWithMatch = new SearchClosestResult( distance, arc.Value.Key); } } // create a line. double distanceTotal = fromCoordinates.Distance(toCoordinates); if (distanceTotal > 0) { // the from/to are not the same location. var line = new GeoCoordinateLine(fromCoordinates, toCoordinates, true, true); distance = line.Distance(coordinate); if (distance < closestWithoutMatch.Distance) { // the distance is smaller. PointF2D projectedPoint = line.ProjectOn(coordinate); // calculate the position. if (projectedPoint != null) { // calculate the distance double distancePoint = fromCoordinates.Distance(projectedPoint); double position = distancePoint / distanceTotal; closestWithoutMatch = new SearchClosestResult( distance, arc.Key, arc.Value.Key, position); } } if (distance < closestWithMatch.Distance) { PointF2D projectedPoint = line.ProjectOn(coordinate); // calculate the position. if (projectedPoint != null) { // calculate the distance double distancePoint = fromCoordinates.Distance(projectedPoint); double position = distancePoint / distanceTotal; if (matcher == null || (pointTags == null || pointTags.Count == 0) || matcher.MatchWithEdge(vehicle, pointTags, arcTags)) { closestWithMatch = new SearchClosestResult( distance, arc.Key, arc.Value.Key, position); } } } } } } } } else { // only find closest vertices. // loop over all. foreach (KeyValuePair <uint, KeyValuePair <uint, TEdgeData> > arc in arcs) { float fromLatitude, fromLongitude; float toLatitude, toLongitude; if (graph.GetVertex(arc.Key, out fromLatitude, out fromLongitude) && graph.GetVertex(arc.Value.Key, out toLatitude, out toLongitude)) { var vertexCoordinate = new GeoCoordinate(fromLatitude, fromLongitude); double distance = coordinate.Distance(vertexCoordinate); if (distance < closestWithoutMatch.Distance) { // the distance found is closer. closestWithoutMatch = new SearchClosestResult( distance, arc.Key); } vertexCoordinate = new GeoCoordinate(toLatitude, toLongitude); distance = coordinate.Distance(vertexCoordinate); if (distance < closestWithoutMatch.Distance) { // the distance found is closer. closestWithoutMatch = new SearchClosestResult( distance, arc.Value.Key); } } } } // return the best result. if (closestWithMatch.Distance < double.MaxValue) { return(closestWithMatch); } return(closestWithoutMatch); }
/// <summary> /// Calculates the closest point on the route relative to the given coordinate. /// </summary> /// <returns></returns> public bool ProjectOn(GeoCoordinate coordinates, out GeoCoordinate projectedCoordinates, out int entryIndex, out Meter distanceFromStart, out Second timeFromStart) { double distance = double.MaxValue; distanceFromStart = 0; timeFromStart = 0; double currentDistanceFromStart = 0; projectedCoordinates = null; entryIndex = -1; // loop over all points and try to project onto the line segments. GeoCoordinate projected; double currentDistance; var points = this.GetPoints(); for (int idx = 0; idx < points.Count - 1; idx++) { var line = new GeoCoordinateLine(points[idx], points[idx + 1], true, true); var projectedPoint = line.ProjectOn(coordinates); if (projectedPoint != null) { // there was a projected point. projected = new GeoCoordinate(projectedPoint[1], projectedPoint[0]); currentDistance = coordinates.Distance(projected); if (currentDistance < distance) { // this point is closer. projectedCoordinates = projected; entryIndex = idx; distance = currentDistance; // calculate distance/time. double localDistance = projected.DistanceReal(points[idx]).Value; distanceFromStart = currentDistanceFromStart + localDistance; if (this.HasTimes && idx > 0) { // there should be proper timing information. double timeToSegment = this.Segments[idx].Time; double timeToNextSegment = this.Segments[idx + 1].Time; timeFromStart = timeToSegment + ((timeToNextSegment - timeToSegment) * (localDistance / line.LengthReal.Value)); } } } // check first point. projected = points[idx]; currentDistance = coordinates.Distance(projected); if (currentDistance < distance) { // this point is closer. projectedCoordinates = projected; entryIndex = idx; distance = currentDistance; distanceFromStart = currentDistanceFromStart; if (this.HasTimes) { // there should be proper timing information. timeFromStart = this.Segments[idx].Time; } } // update distance from start. currentDistanceFromStart = currentDistanceFromStart + points[idx].DistanceReal(points[idx + 1]).Value; } // check last point. projected = points[points.Count - 1]; currentDistance = coordinates.Distance(projected); if (currentDistance < distance) { // this point is closer. projectedCoordinates = projected; entryIndex = points.Count - 1; distance = currentDistance; distanceFromStart = currentDistanceFromStart; if (this.HasTimes) { // there should be proper timing information. timeFromStart = this.Segments[points.Count - 1].Time; } } return(true); }
public override double ShortestDistanceTo(GeoCoordinate coordinate) { return coordinate.Distance(this.Center); }
public void DistanceTest() { double distance1 = GPAOffices.Distance(TVAOffices); Assert.IsTrue(Math.Abs(GPAtoTVA - distance1) < threshold); }
public override double ShortestDistanceTo(GeoCoordinate coordinate) { return coordinate.Distance(this.Dot.Point); }
public override double ShortestDistanceTo(GeoCoordinate coordinate) { if (_box != null) { return coordinate.Distance(this.Box.Center); } return coordinate.Distance(this.Center); }