Click here to Skip to main content
15,885,244 members
Articles / General Programming / Regular Expressions

Parsing Latitude and Longitude Information

Rate me:
Please Sign up or sign in to vote.
4.73/5 (24 votes)
21 Feb 2012CPOL9 min read 93.1K   2K   74  
Parses user input and extracts latitude and longitude information, taking into account the user's language and regional settings
using System;
using System.Globalization;
using Geospatial;
using NUnit.Framework;

namespace UnitTests
{
    [TestFixture]
    class LatitudeTest
    {
        private const double Delta = 0.000001;

        [Test]
        public void TestCompareTo()
        {
            Assert.AreEqual(-1, Latitude.FromRadians(0).CompareTo(Latitude.FromRadians(1)));
            Assert.AreEqual(1, Latitude.FromRadians(0).CompareTo(Latitude.FromRadians(-1)));
            Assert.AreEqual(0, Latitude.FromRadians(0).CompareTo(Latitude.FromRadians(0)));
        }

        [Test]
        public void TestEquals()
        {
            Latitude zero = Latitude.FromRadians(0);
            Angle angle = Angle.FromRadians(0);
            object box = zero;

            Assert.IsTrue(zero == angle);
            Assert.IsTrue(zero == Latitude.FromRadians(0));
            Assert.IsTrue(zero.Equals(Latitude.FromRadians(0)));
            Assert.IsTrue(zero.Equals(angle));
            Assert.IsTrue(zero.Equals(box));
            Assert.IsTrue(zero.Equals((object)angle));

            Assert.IsFalse(zero == Longitude.FromRadians(0));

            Assert.AreEqual('N', Latitude.FromDegrees(0).Direction);
            Assert.AreEqual('N', Latitude.FromDegrees(1).Direction);
            Assert.AreEqual('S', Latitude.FromDegrees(-1).Direction);
        }

        [Test]
        public void TestConstructors()
        {
            Assert.Throws<ArgumentNullException>(() => new Latitude(null));
            Assert.Throws<ArgumentOutOfRangeException>(() => new Latitude(Angle.FromRadians(double.MaxValue)));
            Assert.Throws<ArgumentOutOfRangeException>(() => new Latitude(Angle.FromRadians(double.NaN)));
            Assert.Throws<ArgumentOutOfRangeException>(() => new Latitude(Angle.FromRadians(double.NegativeInfinity)));
            Assert.DoesNotThrow(() => new Latitude(Angle.FromDegrees(-90)));

            Assert.Throws<ArgumentOutOfRangeException>(() => Latitude.FromDegrees(90.1));
            Assert.Throws<ArgumentOutOfRangeException>(() => Latitude.FromRadians(-Math.PI / 1.9));

            Assert.Throws<ArgumentOutOfRangeException>(() => Latitude.FromDegrees(89, 61));
            Assert.Throws<ArgumentOutOfRangeException>(() => Latitude.FromDegrees(89, 59, 61));
            Assert.Throws<ArgumentOutOfRangeException>(() => Latitude.FromDegrees(-89, -61));
            Assert.Throws<ArgumentOutOfRangeException>(() => Latitude.FromDegrees(-89, -60, -61));

            // These should be ok
            Assert.DoesNotThrow(() => Latitude.FromDegrees(89, 60));
            Assert.DoesNotThrow(() => Latitude.FromDegrees(89, 59, 60));
            Assert.DoesNotThrow(() => Latitude.FromDegrees(-89, -60));
            Assert.DoesNotThrow(() => Latitude.FromDegrees(-89, -59, -60));

            Assert.AreEqual(Math.PI / 4.0, Latitude.FromDegrees(45).Radians, Delta);
            Assert.AreEqual(-45, Latitude.FromRadians(-Math.PI / 4.0).Degrees, Delta);
        }

        [Test]
        public void TestToString()
        {
            Latitude latitude = new Latitude(Angle.FromDegrees(-15.45));
            Assert.AreEqual("15\u00B0 27\u2032 S", latitude.ToString("DM", CultureInfo.InvariantCulture));

            latitude = new Latitude(Angle.FromDegrees(15.45));
            Assert.AreEqual("15\u00B0 27\u2032 N", latitude.ToString("DM", CultureInfo.InvariantCulture));

            latitude = new Latitude(Angle.FromDegrees(0, 45, 6));
            Assert.AreEqual("0\u00B0 45.1\u2032 N", latitude.ToString("DM", CultureInfo.InvariantCulture));
            Assert.AreEqual("+004506", latitude.ToString("ISO", CultureInfo.InvariantCulture));

            latitude = new Latitude(Angle.FromDegrees(0, -45, -6));
            Assert.AreEqual("0\u00B0 45.1\u2032 S", latitude.ToString("DM", CultureInfo.InvariantCulture));
            Assert.AreEqual("-004506", latitude.ToString("ISO", CultureInfo.InvariantCulture));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions