Stores the Latitude/Longitude of a point.
Example #1
0
        public void LatLng_DistanceTo_GivesSameResultAsDistance()
        {
            LatLng testLatLngA = new LatLng(RandomLat(), RandomLng());
            LatLng testLatLngB = new LatLng(RandomLat(), RandomLng());

            Assert.AreEqual(
                LatLng.Distance(testLatLngA, testLatLngB),
                testLatLngA.DistanceTo(testLatLngB),
                "DistanceTo method should give the same result as static Distance method");
        }
Example #2
0
        public void LatLng_ValidConstructor_PropertiesMatchThoseSupplied()
        {
            double lat = RandomLat();
            double lng = RandomLng();

            LatLng testLatLng = new LatLng(lat, lng);

            Assert.AreEqual(lat, testLatLng.Latitude, "Latitude property should return the value initially supplied");
            Assert.AreEqual(lng, testLatLng.Longitude, "Longtitude property should return the value initially supplied");
        }
Example #3
0
        public void LatLng_EquivalantLatLng_AreEqual()
        {
            double lat = RandomLat();
            double lng = RandomLng();

            LatLng testLatLngA = new LatLng(lat, lng);
            LatLng testLatLngB = new LatLng(lat, lng);

            Assert.AreEqual(
                testLatLngA,
                testLatLngB,
                "Two LatLng instances describing the same location should be equivalant");
        }
Example #4
0
 /// <summary>
 ///   Calculates the midpoint between the current point and the <see cref="LatLng">point</see> specified.
 /// </summary>
 /// <param name="otherPoint">The other point to use in the calculation.</param>
 /// <returns>
 ///   The midpoint between the current point and the <paramref name="otherPoint"/> specified.
 /// </returns>
 public LatLng MidpointTo(LatLng otherPoint)
 {
     return MidPoint(this, otherPoint);
 }
Example #5
0
 /// <summary>
 ///   Calculates the distance (in km) between the current point and the <see cref="LatLng">point</see> specified.
 ///   By default this uses the Law of Cosines formula (http://en.wikipedia.org/wiki/Law_of_cosines).
 /// </summary>
 /// <param name="otherPoint">The point we want to calculate the distance to.</param>
 /// <param name="calculation">
 ///   <para>The distance calculation to use.</para>
 ///   <para>By default this uses the Law of Cosines formula.</para>
 /// </param>
 /// <returns>
 ///   The distance (in km) between the current point and the <paramref name="otherPoint"/> specified.
 /// </returns>
 /// <exception cref="ArgumentOutOfRangeException">
 ///   The specified <paramref name="calculation"/> is outside the allowable <see cref="DistanceCalculation">values</see>.
 /// </exception>
 public double DistanceTo(LatLng otherPoint, DistanceCalculation calculation = DistanceCalculation.LawOfCosines)
 {
     return Distance(this, otherPoint, calculation);
 }
Example #6
0
        /// <summary>
        ///   Calculates the midpoint between two specified <see cref="LatLng">latlong points</see>.
        /// </summary>
        /// <param name="pointOne">The first point to use in the calculation.</param>
        /// <param name="pointTwo">The second point to use in the calculation.</param>
        /// <returns>
        ///   The midpoint between the two points specified.
        /// </returns>
        public static LatLng MidPoint(LatLng pointOne, LatLng pointTwo)
        {
            double dLon = (pointTwo.Longitude - pointTwo.Longitude).ToRadians();
            double bx = Math.Cos(pointTwo.Latitude.ToRadians()) * Math.Cos(dLon);
            double by = Math.Cos(pointTwo.Latitude.ToRadians()) * Math.Sin(dLon);

            double latitude = (Math.Atan2(
                Math.Sin(pointOne.Latitude.ToRadians()) + Math.Sin(pointTwo.Latitude.ToRadians()),
                Math.Sqrt(
                    (Math.Cos(pointOne.Latitude.ToRadians()) + bx) *
                    (Math.Cos(pointOne.Latitude.ToRadians()) + bx) + by * by))).ToDegrees();

            double longitude = pointOne.Longitude +
                               Math.Atan2(by, Math.Cos(pointOne.Latitude.ToRadians()) + bx).ToDegrees();

            return new LatLng(latitude, longitude);
        }
Example #7
0
        /// <summary>
        ///   Calculates the distance (in km) between two specified <see cref="LatLng">latlong points</see>.
        ///   By default this uses the Law of Cosines formula (http://en.wikipedia.org/wiki/Law_of_cosines).
        /// </summary>
        /// <param name="pointOne">The first point to use in the calculation.</param>
        /// <param name="pointTwo">The second point to use in the calculation.</param>
        /// <param name="calculation">
        ///   <para>The calculation to use.</para>
        ///   <para>By default this uses the Law of Cosines formula.</para>
        /// </param>
        /// <returns>
        ///   The distance (in km) between the two points specified.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///   The specified <paramref name="calculation"/> is outside the allowable <see cref="DistanceCalculation">values</see>.
        /// </exception>
        public static double Distance(
            LatLng pointOne,
            LatLng pointTwo,
            DistanceCalculation calculation = DistanceCalculation.LawOfCosines)
        {
            switch (calculation)
            {
                case DistanceCalculation.Haversine:
                    double pointOneLatitude = pointOne.Latitude.ToRadians();
                    double pointOneLongitude = pointOne.Longitude.ToRadians();
                    double pointTwoLatitude = pointTwo.Latitude.ToRadians();
                    double pointTwoLongitude = pointTwo.Longitude.ToRadians();

                    double longitude = pointTwoLongitude - pointOneLongitude;
                    double latitude = pointTwoLatitude - pointOneLatitude;

                    double intermediateResult = Math.Pow(Math.Sin(latitude / 2.0), 2.0) +
                                                Math.Cos(pointOneLatitude) * Math.Cos(pointTwoLatitude) *
                                                Math.Pow(Math.Sin(longitude / 2.0), 2.0);

                    // Intermediate result c (great circle distance in Radians).

                    double c = 2.0 * Math.Atan2(Math.Sqrt(intermediateResult), Math.Sqrt(1.0 - intermediateResult));

                    return EarthsRadiusInKilometers * c;
                case DistanceCalculation.LawOfCosines:
                    return (Math.Acos(
                        Math.Sin(pointOne.Latitude) * Math.Sin(pointTwo.Latitude) +
                        Math.Cos(pointOne.Latitude) * Math.Cos(pointTwo.Latitude) *
                        Math.Cos(pointTwo.Longitude - pointOne.Longitude)
                        ) * EarthsRadiusInKilometers).ToRadians();
                default:
                    throw new ArgumentOutOfRangeException("calculation");
            }
        }
Example #8
0
        public void LatLng_MidpointTo_GivesSameResultAsMidpoint()
        {
            LatLng testLatLngA = new LatLng(RandomLat(), RandomLng());
            LatLng testLatLngB = new LatLng(RandomLat(), RandomLng());

            Assert.AreEqual(
                LatLng.MidPoint(testLatLngA, testLatLngB),
                testLatLngA.MidpointTo(testLatLngB),
                "MidpointTo method should give the same result as static Midpoint method");
        }