Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Parsing Latitude and Longitude Information

, 21 Feb 2012
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)

Share

About the Author

Samuel Cragg

United Kingdom United Kingdom
No Biography provided

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