Beispiel #1
0
 public void LatZone()
 {
     var autm = new UtmRef(10, 'T', 574595, 5316784);
     var expected = 'T';
     var actual = autm.LatZone;
     Assert.AreEqual(expected, actual);
 }
Beispiel #2
0
 public void UTMRefTest()
 {
     var autm = new UtmRef(10, 'T', 574595, 5316784);
     Assert.AreEqual(10, autm.LngZone);
     Assert.AreEqual('T', autm.LatZone);
     Assert.AreEqual(574595, autm.Easting);
     Assert.AreEqual(5316784, autm.Northing);
 }
Beispiel #3
0
 public void UTMRefDatumTest()
 {
     var utmd = new UtmRef(10, 'T', 574595, 5316784, NAD27ContiguousUSDatum.Instance);
     Assert.AreEqual(10, utmd.LngZone);
     Assert.AreEqual('T', utmd.LatZone);
     Assert.AreEqual(574595, utmd.Easting);
     Assert.AreEqual(5316784, utmd.Northing);
     Assert.AreEqual(NAD27ContiguousUSDatum.Instance, utmd.Datum);
 }
Beispiel #4
0
        public void ToLatLngTest()
        {
            var autm = new UtmRef(10, 'T', 574595, 5316784);

            var alat = 47.99999993;
            var alon = -122.000001509;
            var alatlon = autm.ToLatLng();

            Assert.AreEqual(alat, alatlon.Latitude, 0.00000001);

            Assert.AreEqual(alon, alatlon.Longitude, 0.00000001);
        }
Beispiel #5
0
        public void ToLatLngTestWithNad27()
        {
            var utmd = new UtmRef(10, 'T', 574595, 5316784, NAD27ContiguousUSDatum.Instance);

            var lat = 48.00196908;
            var lon = -121.99999492;

            var latlon = utmd.ToLatLng();

            Assert.AreEqual(lat, latlon.Latitude, 0.00000001);
            Assert.AreEqual(lon, latlon.Longitude, 0.00000001);
        }
Beispiel #6
0
        public void ToLatLngSouthern()
        {
            var autm = new UtmRef(10, 'D', 574595, 5316784);

            var alat = -42.2976024212208;
            var alon = -122.095060688641;

            var alatlon = autm.ToLatLng();

            Assert.AreEqual(alat, alatlon.Latitude, 0.00000001);

            Assert.AreEqual(alon, alatlon.Longitude, 0.00000001);
        }
Beispiel #7
0
        /// <summary>
        ///     Create a new MGRS reference object from the given UTM reference. It is assumed that this
        ///     MGRS reference represents a point using the GRS 1980, International or WGS84 ellipsoids.
        ///     It is assumed that the UTMRef object is valid.
        /// </summary>
        /// <param name="utm"> A UTM reference. </param>
        /// <param name="isBessel">
        ///     true if the parameters represent an MGRS reference using the Bessel 1841 ellipsoid;
        ///     false is the parameters represent an MGRS reference using the GRS 1980, International or
        ///     WGS84 ellipsoids.
        /// </param>
        public MgrsRef(UtmRef utm, bool isBessel)
            : base(utm.Datum)
        {
            var lngZone = utm.LngZone;
            var set     = (lngZone - 1) % 6 + 1;
            var eID     =
                (int)Math.Floor(utm.Easting / 100000.0) + 8 * ((set - 1) % 3);
            var nID = (int)Math.Floor(utm.Northing % 2000000 / 100000.0);

            if (eID > 8)
            {
                eID++; // Offset for no I character
            }
            if (eID > 14)
            {
                eID++; // Offset for no O character
            }
            var eIDc = (char)(eID + 64);

            // Northing ID offset for sets 2, 4 and 6
            if (set % 2 == 0)
            {
                nID += 5;
            }

            if (isBessel)
            {
                nID += 10;
            }

            if (nID > 19)
            {
                nID -= 20;
            }

            var nIDc = latBand[nID];

            LngZone       = lngZone;
            LatZone       = utm.LatZone;
            CollumLetter  = eIDc;
            RowLetter     = nIDc;
            Easting       = (int)Math.Round(utm.Easting) % 100000;
            Northing      = (int)Math.Round(utm.Northing) % 100000;
            Precision     = Precision.Precision1M;
            this.isBessel = isBessel;
        }
Beispiel #8
0
        public void UtmAreEqualNullObject()
        {
            UtmRef utmActual = null;
            var utmExpected = new UtmRef(10, 'T', 574595, 5316784, WGS84Datum.Instance);

            Assert.AreNotEqual(utmExpected, utmActual);
        }
Beispiel #9
0
        public void LatLontoUtmRef()
        {
            const double Lat = 47.99999993;
            const double Lon = -122.000001509;
            var ll = new LatLng(Lat, Lon);
            var utmActual = ll.ToUtmRef();
            var utmExpected = new UtmRef(10, 'T', 574595, 5316784, WGS84Datum.Instance);

            Assert.AreEqual(utmExpected, utmActual);
        }
Beispiel #10
0
 public void UtmNotDefinedNorthing()
 {
     var autm = new UtmRef(10, 'T', 10, -10);
 }
Beispiel #11
0
 public void UtmNotDefinedEasting()
 {
     var autm = new UtmRef(10, 'T', -1, 5316784);
 }
Beispiel #12
0
 private bool Equals(UtmRef other)
 {
     return Easting.Equals(other.Easting) && Northing.Equals(other.Northing) && LatZone == other.LatZone &&
            LngZone == other.LngZone;
 }
Beispiel #13
0
        /// <summary>
        ///     Convert this latitude and longitude to a UTM reference.
        /// </summary>
        /// <returns> The converted UTM reference. </returns>
        /// <exception cref="NotDefinedOnUtmGridException">
        ///     If an attempt is made to convert a LatLng that falls outside the area covered by the UTM
        ///     grid. The UTM grid is only defined for latitudes south of 84°N and north of 80°S.
        /// </exception>
        public UtmRef ToUtmRef()
        {
            if (Latitude < -80 || Latitude > 84)
            {
                throw new NotDefinedOnUtmGridException("Latitude (" + Latitude
                                                       + ") falls outside the UTM grid.");
            }

            if (Math.Abs(Longitude - 180.0) < Tolerance)
            {
                Longitude = -180.0;
            }

            var UTM_F0    = 0.9996;
            var a         = Datum.ReferenceEllipsoid.SemiMajorAxis;
            var eSquared  = Datum.ReferenceEllipsoid.EccentricitySquared;
            var longitude = Longitude;
            var latitude  = Latitude;

            var latitudeRad   = latitude * (Math.PI / 180.0);
            var longitudeRad  = longitude * (Math.PI / 180.0);
            var longitudeZone = (int)Math.Floor((longitude + 180.0) / 6.0) + 1;

            // Special zone for Norway
            if (latitude >= 56.0 && latitude < 64.0 && longitude >= 3.0 &&
                longitude < 12.0)
            {
                longitudeZone = 32;
            }

            // Special zones for Svalbard
            if (latitude >= 72.0 && latitude < 84.0)
            {
                if (longitude >= 0.0 && longitude < 9.0)
                {
                    longitudeZone = 31;
                }
                else if (longitude >= 9.0 && longitude < 21.0)
                {
                    longitudeZone = 33;
                }
                else if (longitude >= 21.0 && longitude < 33.0)
                {
                    longitudeZone = 35;
                }
                else if (longitude >= 33.0 && longitude < 42.0)
                {
                    longitudeZone = 37;
                }
            }

            double longitudeOrigin    = (longitudeZone - 1) * 6 - 180 + 3;
            var    longitudeOriginRad = longitudeOrigin * (Math.PI / 180.0);

            var utmZone = UtmRef.GetUtmLatitudeZoneLetter(latitude);

            var ePrimeSquared = eSquared / (1 - eSquared);

            var n = a
                    / Math.Sqrt(1 - eSquared * Math.Sin(latitudeRad)
                                * Math.Sin(latitudeRad));
            var t = Math.Tan(latitudeRad) * Math.Tan(latitudeRad);
            var c = ePrimeSquared * Math.Cos(latitudeRad) * Math.Cos(latitudeRad);
            var A = Math.Cos(latitudeRad) * (longitudeRad - longitudeOriginRad);

            var M = a
                    * ((1 - eSquared / 4 - 3 * eSquared * eSquared / 64 - 5 * eSquared
                        * eSquared * eSquared / 256)
                       * latitudeRad
                       - (3 * eSquared / 8 + 3 * eSquared * eSquared / 32 + 45 * eSquared
                          * eSquared * eSquared / 1024)
                       * Math.Sin(2 * latitudeRad)
                       + (15 * eSquared * eSquared / 256 + 45 * eSquared * eSquared
                          * eSquared / 1024) * Math.Sin(4 * latitudeRad) - 35 * eSquared
                       * eSquared * eSquared / 3072
                       * Math.Sin(6 * latitudeRad));

            var utmEasting = UTM_F0
                             * n
                             * (A + (1 - t + c) * Math.Pow(A, 3.0) / 6 + (5 - 18 * t + t * t + 72
                                                                          * c - 58 * ePrimeSquared)
                                * Math.Pow(A, 5.0) / 120) + 500000.0;

            var utmNorthing = UTM_F0 * (M + n
                                        * Math.Tan(latitudeRad)
                                        * (A * A / 2 + (5 - t + 9 * c + 4 * c * c) * Math.Pow(A, 4.0) / 24 + (61
                                                                                                              - 58 * t + t * t +
                                                                                                              600 * c -
                                                                                                              330 * ePrimeSquared)
                                           * Math.Pow(A, 6.0) / 720));

            // Adjust for the southern hemisphere
            if (latitude < 0)
            {
                utmNorthing += 10000000.0;
            }

            utmNorthing = Math.Round(utmNorthing);
            utmEasting  = Math.Round(utmEasting);
            return(new UtmRef(longitudeZone, utmZone, utmEasting, utmNorthing, Datum));
        }
Beispiel #14
0
        /// <summary>
        ///     Create a new MGRS reference object from the given UTM reference. It is assumed that this
        ///     MGRS reference represents a point using the GRS 1980, International or WGS84 ellipsoids.
        ///     It is assumed that the UTMRef object is valid.
        /// </summary>
        /// <param name="utm"> A UTM reference. </param>
        /// <param name="isBessel">
        ///     true if the parameters represent an MGRS reference using the Bessel 1841 ellipsoid;
        ///     false is the parameters represent an MGRS reference using the GRS 1980, International or
        ///     WGS84 ellipsoids.
        /// </param>
        public MgrsRef(UtmRef utm, bool isBessel)
            : base(utm.Datum)
        {
            var lngZone = utm.LngZone;
            var set = (lngZone - 1)%6 + 1;
            var eID =
                (int) Math.Floor(utm.Easting/100000.0) + 8*((set - 1)%3);
            var nID = (int) Math.Floor(utm.Northing%2000000/100000.0);

            if (eID > 8)
                eID++; // Offset for no I character
            if (eID > 14)
                eID++; // Offset for no O character

            var eIDc = (char) (eID + 64);

            // Northing ID offset for sets 2, 4 and 6
            if (set%2 == 0)
            {
                nID += 5;
            }

            if (isBessel)
            {
                nID += 10;
            }

            if (nID > 19)
            {
                nID -= 20;
            }

            var nIDc = latBand[nID];

            LngZone = lngZone;
            LatZone = utm.LatZone;
            CollumLetter = eIDc;
            RowLetter = nIDc;
            Easting = (int) Math.Round(utm.Easting)%100000;
            Northing = (int) Math.Round(utm.Northing)%100000;
            Precision = Precision.Precision1M;
            this.isBessel = isBessel;
        }
Beispiel #15
0
 /// <summary>
 ///     Create a new MGRS reference object from the given UTM reference. It is assumed that the
 ///     UTMRef object is valid.
 /// </summary>
 /// <param name="utm"> A UTM reference. </param>
 public MgrsRef(UtmRef utm)
     : this(utm, false)
 {
 }
Beispiel #16
0
 public void UtmNotDefinedLongZone()
 {
     var autm = new UtmRef(-1, 'T', 1, 5316784);
 }
Beispiel #17
0
 public void UtmNotDefinedLatZone()
 {
     var autm = new UtmRef(10, 'A', 1, 5316784);
 }
Beispiel #18
0
 public void ToStringTest()
 {
     var expected = "10T 574595 5316784";
     var autm = new UtmRef(10, 'T', 574595, 5316784);
     Assert.AreEqual(expected, autm.ToString());
 }
Beispiel #19
0
        public void UtmAreSameObject()
        {
            var utmExpected = new UtmRef(10, 'T', 574595, 5316784, WGS84Datum.Instance);
            var utmActual = utmExpected;

            Assert.AreSame(utmExpected, utmActual);
        }
Beispiel #20
0
 private bool Equals(UtmRef other)
 {
     return(Easting.Equals(other.Easting) && Northing.Equals(other.Northing) && LatZone == other.LatZone &&
            LngZone == other.LngZone);
 }
Beispiel #21
0
 /// <summary>
 ///     Create a new MGRS reference object from the given UTM reference. It is assumed that the
 ///     UTMRef object is valid.
 /// </summary>
 /// <param name="utm"> A UTM reference. </param>
 public MgrsRef(UtmRef utm)
     : this(utm, false)
 {
 }