Example #1
0
        public void TestBearingCalculating()
        {
            Point a = new Point
            {
                Lat  = 39.099912,
                Long = -94.581213
            };
            Point b = new Point
            {
                Lat  = 38.627089,
                Long = -90.200203
            };

            Assert.AreEqual(96.51, Math.Round(GeoHelper.GetBearing(a, b), 2));

            Point a1 = new Point
            {
                Lat  = 52.457182,
                Long = 31.026222
            };
            Point b1 = new Point
            {
                Lat  = 52.457323,
                Long = 31.026065
            };

            double bearing = GeoHelper.GetBearing(a1, b1);

            Assert.AreEqual(325.84, Math.Round(bearing, 2));
        }
        /// <summary>
        /// If the a ship's bearing to a given position is less than 90 degrees away from exact heading needed to get to the port,
        /// Then we says that a ship is moving toward that port
        /// </summary>
        /// <param name="shipTest">shipTest object</param>
        /// <param name="position">Location of port that ship may be moving toward</param>
        /// <returns>true or false</returns>
        public static bool IsMovingToward(this ShipTest shipTest, Position position)
        {
            var bearing    = GeoHelper.GetBearing(shipTest.Position, position);
            var difference = Math.Abs(shipTest.Heading - bearing);

            return(difference <= 90);
        }
Example #3
0
 public void SuccessfulBearingCalculation()
 {
     Assert.AreEqual(
         GeoHelper.GetBearing(TestShipPosition, TestDestination),
         ExpectedBearing,
         ExpectedBearing * DeltaPercentage
         );
 }
        /// <summary>
        /// Does the ship's heading put it on course to enter Duluth Canal?
        /// Ships report numerous false destinations. Therefore, we use this method
        /// to check a ship's actual heading against the bearing to the two nearest ports
        /// and make a determination whether or not the ship will actually use Duluth Canal
        /// </summary>
        /// <param name="shipTest">shipTest object</param>
        /// <returns>true or false</returns>
        public static bool IsHeadingTowardDuluth(this ShipTest shipTest)
        {
            var twoNearestPortsAndBearing = Ports.All.Select(p => {
                var bearing    = GeoHelper.GetBearing(shipTest.Position, p);
                var adjustment = Math.Abs(bearing - shipTest.Heading);
                if (adjustment > 180)
                {
                    adjustment = 360 - adjustment;
                }
                return(new {
                    Position = p,
                    Bearing = bearing,
                    Adjustment = adjustment
                });
            }).OrderBy(
                p => p.Adjustment
                ).Take(2).ToArray();

            var firstPort  = twoNearestPortsAndBearing[0];
            var secondPort = twoNearestPortsAndBearing[1];

            var portBearingDiff = Math.Abs(firstPort.Bearing - secondPort.Bearing);

            var weight = 1 / 2D;

            if (shipTest.Destination == firstPort.Position)
            {
                weight = 2 / 3D;
            }
            else if (shipTest.Destination == secondPort.Position)
            {
                weight = 1 / 3D;
            }

            var threshold = weight * portBearingDiff;

            Position nearestPort = null;

            if (firstPort.Adjustment <= threshold)
            {
                nearestPort = firstPort.Position;
            }
            else if (secondPort.Adjustment <= threshold)
            {
                nearestPort = secondPort.Position;
            }
            else if (shipTest.IsMovingToward(firstPort.Position))
            {
                nearestPort = firstPort.Position;
            }

            return(nearestPort == Ports.Duluth);
        }
Example #5
0
 public double CalculateAngle(GpsInfo current)
 {
     //return current.AngleDegrees;
     foreach (var gps in CarSignalSet.Where(x => x.IsGpsValid).Select(x => x.Gps))
     {
         var distance = GeoHelper.GetDistanceFast(current.LongitudeDegrees, current.LatitudeDegrees,
                                                  gps.LongitudeDegrees, gps.LatitudeDegrees);
         if (distance > 0.5)
         {
             var angle = GeoHelper.GetBearing(gps.LongitudeDegrees, gps.LatitudeDegrees, current.LongitudeDegrees,
                                              current.LatitudeDegrees);
             return(angle);
         }
     }
     return(double.NaN);
 }
Example #6
0
        public static IEnumerable <Point> GetPathPoints(PolyLine line, double speed, int period)
        {
            double stepDistance = speed / 1000d * period;
            var    points       = new List <Point>();
            double currentRemainderOfDistance = 0d;
            Point  current  = null;
            Point  previous = null;
            double bearing;

            for (int i = 1; i < line.Count; i++)
            {
                current  = line[i];
                previous = line[i - 1];
                bearing  = GeoHelper.GetBearing(previous, current);
                double lineDistance = previous.GetDistance(current);

                if (currentRemainderOfDistance > 0)
                {
                    var firstPoint = GeoHelper.GetNextPoint(previous, bearing, stepDistance - currentRemainderOfDistance);
                    points.Add(firstPoint);
                    previous     = firstPoint;
                    lineDistance = lineDistance - (stepDistance - currentRemainderOfDistance);
                }

                while (lineDistance >= stepDistance)
                {
                    var point = GeoHelper.GetNextPoint(previous, bearing, stepDistance);
                    points.Add(point);
                    lineDistance -= stepDistance;
                    previous      = point;
                }
                currentRemainderOfDistance = lineDistance;
            }

            if (currentRemainderOfDistance > 0)
            {
                points.Add(line.LatLongs.Last());
            }
            return(points);
        }
Example #7
0
        public void TestNextPointAndDistanceCalculating()
        {
            Point a = new Point
            {
                Lat  = 52.458393,
                Long = 31.025021
            };
            Point b = new Point
            {
                Lat  = 52.459146,
                Long = 31.024851
            };

            double expectedDistance = 2;
            double bearing          = GeoHelper.GetBearing(a, b);

            Point nextPoint = GeoHelper.GetNextPoint(a, bearing, expectedDistance);

            double distance = GeoHelper.GetDistance(a, nextPoint);

            Assert.AreEqual(expectedDistance, Math.Round(distance));
        }