Click here to Skip to main content
11,641,392 members (61,898 online)
Click here to Skip to main content
Add your own
alternative version

Parsing Latitude and Longitude Information

, 21 Feb 2012 CPOL 50.4K 1.4K 72
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)

Share

About the Author

Samuel Cragg
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 21 Feb 2012
Article Copyright 2011 by Samuel Cragg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid