Click here to Skip to main content
15,892,480 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.6K   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 LongitudeTest
    {
        private const double Delta = 0.000001;

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

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

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

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

            Assert.AreEqual('E', Longitude.FromDegrees(0).Direction);
            Assert.AreEqual('E', Longitude.FromDegrees(1).Direction);
            Assert.AreEqual('W', Longitude.FromDegrees(-1).Direction);
            Assert.AreEqual('W', Longitude.FromDegrees(180).Direction); // This will be turned to negative
        }

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

            // Special case for the 180th meridian, which should always be -180
            // degrees according to ISO 6709
            Assert.AreEqual(-180, Longitude.FromDegrees(180).Degrees);

            Assert.Throws<ArgumentOutOfRangeException>(() => Longitude.FromDegrees(180.1));
            Assert.Throws<ArgumentOutOfRangeException>(() => Longitude.FromRadians(-Math.PI - 0.01));

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

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

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

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

            longitude = new Longitude(Angle.FromDegrees(15.45));
            Assert.AreEqual("15\u00B0 27\u2032 E", longitude.ToString("DM", CultureInfo.InvariantCulture));

            longitude = new Longitude(Angle.FromDegrees(0, 45, 6));
            Assert.AreEqual("0\u00B0 45.1\u2032 E", longitude.ToString("DM", CultureInfo.InvariantCulture));
            Assert.AreEqual("+0004506", longitude.ToString("ISO", CultureInfo.InvariantCulture));

            longitude = new Longitude(Angle.FromDegrees(0, -45, -6));
            Assert.AreEqual("0\u00B0 45.1\u2032 W", longitude.ToString("DM", CultureInfo.InvariantCulture));
            Assert.AreEqual("-0004506", longitude.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