Exemple #1
0
 static void Main(string[] args)
 {
     try {
         // See also example-GeoCoords.cpp
         {
             // Sample forward calculation
             double lat = 33.3, lon = 44.4; // Baghdad
             int    zone;
             bool   northp;
             double x, y;
             UTMUPS.Forward(lat, lon, out zone, out northp, out x, out y, -1, true);
             string mgrs;
             MGRS.Forward(zone, northp, x, y, lat, 5, out mgrs);
             Console.WriteLine(mgrs);
         }
         {
             // Sample reverse calculation
             string mgrs = "38SMB4488";
             int    zone, prec;
             bool   northp;
             double x, y;
             MGRS.Reverse(mgrs, out zone, out northp, out x, out y, out prec, true);
             double lat, lon;
             UTMUPS.Reverse(zone, northp, x, y, out lat, out lon, true);
             Console.WriteLine(String.Format("Latitude: {0} Longitude: {1}", lat, lon));
         }
     }
     catch (GeographicErr e) {
         Console.WriteLine(String.Format("Caught exception: {0}", e.Message));
     }
 }
        /// <summary>
        /// Reset the location from a string. See <see cref="GeoCoords(string, bool, bool)"/>.
        /// </summary>
        /// <param name="s">1-element, 2-element, or 3-element string representation of the position.</param>
        /// <param name="centerp">governs the interpretation of <see cref="MGRS"/> coordinates.</param>
        /// <param name="longfirst">governs the interpretation of geographic coordinates.</param>
        public void Reset(string s, bool centerp = true, bool longfirst = false)
        {
            var sa = s.Split(" \t\n\v\f\r,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            if (sa.Length == 1)
            {
                int prec;
                (_zone, _northp, _easting, _northing, prec) = MGRS.Reverse(sa[0].AsSpan(), centerp);
                (_lat, _long) = UTMUPS.Reverse(_zone, _northp, _easting, _northing, out _gamma, out _k);
            }
            else if (sa.Length == 2)
            {
                (_lat, _long) = DMS.Decode(sa[0], sa[1], longfirst);
                _long         = AngNormalize(_long);
                (_zone, _northp, _easting, _northing) = UTMUPS.Forward(_lat, _long, out _gamma, out _k);
            }
            else if (sa.Length == 3)
            {
                int zoneind, coordind;
                if (sa[0].Length > 0 && char.IsLetter(sa[0][sa[0].Length - 1]))
                {
                    zoneind  = 0;
                    coordind = 1;
                }
                else if (sa[2].Length > 0 && char.IsLetter(sa[2][sa[2].Length - 1]))
                {
                    zoneind  = 2;
                    coordind = 0;
                }
                else
                {
                    throw new GeographicException("Neither " + sa[0] + " nor " + sa[2]
                                                  + " of the form UTM/UPS Zone + Hemisphere"
                                                  + " (ex: 38n, 09s, n)");
                }

                (_zone, _northp) = UTMUPS.DecodeZone(sa[zoneind].AsSpan());
                for (int i = 0; i < 2; ++i)
                {
                    if (i != 0)
                    {
                        _northing = sa[coordind + i].ParseDouble();
                    }
                    else
                    {
                        _easting = sa[coordind + i].ParseDouble();
                    }
                }

                (_lat, _long) = UTMUPS.Reverse(_zone, _northp, _easting, _northing, out _gamma, out _k);
                FixHemisphere();
            }
            else
            {
                throw new GeographicException("Coordinate requires 1, 2, or 3 elements");
            }
            CopyToAlt();
        }
        private void OnValidate(object sender, EventArgs e)
        {
            try
            {
                string str;
                int    prec, zone, zout;
                bool   northp;
                double x, y, x1, y1, gamma, k, lat, lon;
                OSGB.Forward(52.0, -2.0, out x, out y, out gamma, out k);
                OSGB.Forward(52.0, -2.0, out x1, out y1);
                if (x != x1 || y != y1)
                {
                    throw new Exception("Error in OSGB.Forward");
                }
                OSGB.Reverse(x, y, out lat, out lon, out gamma, out k);
                OSGB.Reverse(x, y, out x1, out y1);
                if (lat != x1 || lon != y1)
                {
                    throw new Exception("Error in OSGB.Reverse");
                }
                OSGB.GridReference(x, y, 8, out str);
                OSGB.GridReference(str, out x1, out y1, out prec, true);
                UTMUPS.StandardZone(32.0, -80.0, (int)UTMUPS.ZoneSpec.STANDARD);
                UTMUPS.UTMShift();
                UTMUPS.StandardZone(32.0, -86.0, (int)UTMUPS.ZoneSpec.STANDARD);
                UTMUPS.Forward(32.0, -86.0, out zone, out northp, out x, out y, out gamma, out k, (int)UTMUPS.ZoneSpec.STANDARD, true);
                UTMUPS.Forward(32.0, -86.0, out zone, out northp, out x1, out y1, (int)UTMUPS.ZoneSpec.STANDARD, true);
                if (x != x1 || y != y1)
                {
                    throw new Exception("Error in UTMUPS.Forward");
                }
                UTMUPS.Reverse(zone, northp, x, y, out lat, out lon, out gamma, out k, true);
                UTMUPS.Reverse(zone, northp, x, y, out x1, out y1, true);
                if (lat != x1 || lon != y1)
                {
                    throw new Exception("Error in UTMUPS.Reverse");
                }
                UTMUPS.Transfer(zone, northp, x, y, zone + 1, true, out x1, out y1, out zout);
                str  = UTMUPS.EncodeZone(zone, northp, true);
                prec = UTMUPS.EncodeEPSG(zone, northp);
                UTMUPS.DecodeZone(str, out zone, out northp);
                UTMUPS.DecodeEPSG(prec, out zone, out northp);
                MGRS.Forward(zone, northp, x, y, 8, out str);
                MGRS.Forward(zone, northp, x, y, 32.0, 8, out str);
                MGRS.Reverse(str, out zone, out northp, out x, out y, out prec, true);

                MessageBox.Show("No errors detected", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception xcpt)
            {
                MessageBox.Show(xcpt.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #4
0
        public void TestReverse(double lat, double lon, string input)
        {
            var(zone, northp, x, y, _) = MGRS.Reverse(input);
            var(olat, olon)            = UTMUPS.Reverse(zone, northp, x, y);

            Assert.AreEqual(olat, lat, 1e-5);

            // No need to check longitude when near pole.
            if (Math.Abs(lat) != 90)
            {
                Assert.AreEqual(olon, lon, 1e-5);
            }
        }
        private void OnConvertMGRS(object sender, EventArgs e)
        {
            int    zone, prec;
            bool   northp;
            double x, y, lat, lon;

            MGRS.Reverse(m_mgrsTextBox.Text, out zone, out northp, out x, out y, out prec, true);
            m_utmPoleCheckBox.Checked = northp;
            m_utmXTextBox.Text        = x.ToString();
            m_utmYTextBox.Text        = y.ToString();
            m_utmZoneTextBox.Text     = zone.ToString();
            UTMUPS.Reverse(zone, northp, x, y, out lat, out lon, true);
            m_latitudeTextBox.Text  = lat.ToString();
            m_longitudeTextBox.Text = lon.ToString();
        }