Пример #1
0
        /// <summary>
        /// Creates a Coordinate object from an MGRS/NATO UTM Coordinate
        /// </summary>
        /// <param name="mgrs">MilitaryGridReferenceSystem</param>
        /// <returns>Coordinate object</returns>
        public static Coordinate MGRStoLatLong(MilitaryGridReferenceSystem mgrs)
        {
            string latz    = mgrs.LatZone;
            string digraph = mgrs.Digraph;

            char eltr = digraph[0];
            char nltr = digraph[1];

            string digraphLettersE   = "ABCDEFGHJKLMNPQRSTUVWXYZ";
            string digraphLettersN   = "ABCDEFGHJKLMNPQRSTUV";
            string digraphLettersAll = "";

            for (int lt = 1; lt < 25; lt++)
            {
                digraphLettersAll += "ABCDEFGHJKLMNPQRSTUV";
            }

            var eidx = digraphLettersE.IndexOf(eltr);
            var nidx = digraphLettersN.IndexOf(nltr);

            if (mgrs.LongZone / 2.0 == Math.Floor(mgrs.LongZone / 2.0))
            {
                nidx -= 5;  // correction for even numbered zones
            }

            var ebase = 100000 * (1 + eidx - 8 * Math.Floor(Convert.ToDouble(eidx) / 8));

            var latBand     = digraphLettersE.IndexOf(latz);
            var latBandLow  = 8 * latBand - 96;
            var latBandHigh = 8 * latBand - 88;

            if (latBand < 2)
            {
                latBandLow  = -90;
                latBandHigh = -80;
            }
            else if (latBand == 21)
            {
                latBandLow  = 72;
                latBandHigh = 84;
            }
            else if (latBand > 21)
            {
                latBandLow  = 84;
                latBandHigh = 90;
            }

            var    lowLetter      = Math.Floor(100 + 1.11 * latBandLow);
            var    highLetter     = Math.Round(100 + 1.11 * latBandHigh);
            string latBandLetters = null;
            int    l = Convert.ToInt32(lowLetter);
            int    h = Convert.ToInt32(highLetter);

            if (mgrs.LongZone / 2.0 == Math.Floor(mgrs.LongZone / 2.0))
            {
                latBandLetters = digraphLettersAll.Substring(l + 5, h + 5).ToString();
            }
            else
            {
                latBandLetters = digraphLettersAll.Substring(l, h).ToString();
            }
            var nbase = 100000 * (lowLetter + latBandLetters.IndexOf(nltr));

            var x = ebase + mgrs.Easting;
            var y = nbase + mgrs.Northing;

            if (y > 10000000)
            {
                y = y - 10000000;
            }
            if (nbase >= 10000000)
            {
                y = nbase + mgrs.northing - 10000000;
            }

            var southern = nbase < 10000000;

            UniversalTransverseMercator utm = new UniversalTransverseMercator(mgrs.LatZone, mgrs.LongZone, x, y);

            utm.equatorial_radius  = mgrs.equatorialRadius;
            utm.inverse_flattening = mgrs.inverseFlattening;
            Coordinate c = UniversalTransverseMercator.ConvertUTMtoLatLong(utm);

            c.Set_Datum(mgrs.equatorialRadius, mgrs.inverseFlattening);
            // Coordinate nc = new Coordinate(c.Latitude.ToDouble(), c.Longitude.ToDouble());

            return(c);
        }
Пример #2
0
        //Add main to Coordinate and tunnel to Format class. Add private methods to format.
        //WHEN PARSING NO EXCPETIONS FOR OUT OF RANGE ARGS WILL BE THROWN
        public static bool TryParse(string coordString, CartesianType ct, out Coordinate c)
        {
            //Turn of eagerload for efficiency
            EagerLoad eg = new EagerLoad();

            eg.Cartesian = false;
            eg.Celestial = false;
            eg.UTM_MGRS  = false;

            c = new Coordinate(eg);
            string s = coordString;

            s = s.Trim(); //Trim all spaces before and after string
            double[] d;
            //Try Signed Degree
            if (TrySignedDegree(s, out d))
            {
                try
                {
                    c = new Coordinate(d[0], d[1], eg);
                    c.Parse_Format = Parse_Format_Type.Signed_Degree;
                    return(true);
                }
                catch
                {//Parser failed try next method
                }
            }

            //Try Decimal Degree
            if (TryDecimalDegree(s, out d))
            {
                try
                {
                    c = new Coordinate(d[0], d[1], eg);
                    c.Parse_Format = Parse_Format_Type.Decimal_Degree;
                    return(true);
                }
                catch
                {//Parser failed try next method
                }
            }
            //Try DDM
            if (TryDegreeDecimalMinute(s, out d))
            {
                try
                {
                    //0 Lat Degree
                    //1 Lat Minute
                    //2 Lat Direction (0 = N, 1 = S)
                    //3 Long Degree
                    //4 Long Minute
                    //5 Long Direction (0 = E, 1 = W)
                    CoordinatesPosition latP = CoordinatesPosition.N;
                    CoordinatesPosition lngP = CoordinatesPosition.E;
                    if (d[2] != 0)
                    {
                        latP = CoordinatesPosition.S;
                    }
                    if (d[5] != 0)
                    {
                        lngP = CoordinatesPosition.W;
                    }
                    CoordinatePart lat = new CoordinatePart((int)d[0], d[1], latP);
                    CoordinatePart lng = new CoordinatePart((int)d[3], d[4], lngP);
                    c              = new Coordinate(eg);
                    c.Latitude     = lat;
                    c.Longitude    = lng;
                    c.Parse_Format = Parse_Format_Type.Degree_Decimal_Minute;
                    return(true);
                }
                catch
                {//Parser failed try next method
                }
            }
            //Try DMS
            if (TryDegreeMinuteSecond(s, out d))
            {
                try
                {
                    //0 Lat Degree
                    //1 Lat Minute
                    //2 Lat Second
                    //3 Lat Direction (0 = N, 1 = S)
                    //4 Long Degree
                    //5 Long Minute
                    //6 Long Second
                    //7 Long Direction (0 = E, 1 = W)
                    CoordinatesPosition latP = CoordinatesPosition.N;
                    CoordinatesPosition lngP = CoordinatesPosition.E;
                    if (d[3] != 0)
                    {
                        latP = CoordinatesPosition.S;
                    }
                    if (d[7] != 0)
                    {
                        lngP = CoordinatesPosition.W;
                    }

                    CoordinatePart lat = new CoordinatePart((int)d[0], (int)d[1], d[2], latP);
                    CoordinatePart lng = new CoordinatePart((int)d[4], (int)d[5], d[6], lngP);
                    c              = new Coordinate(eg);
                    c.Latitude     = lat;
                    c.Longitude    = lng;
                    c.Parse_Format = Parse_Format_Type.Degree_Minute_Second;
                    return(true);
                }
                catch
                {//Parser failed try next method
                }
            }

            string[] um;
            //Try MGRS
            if (TryMGRS(s, out um) || TryMGRS_Polar(s, out um))
            {
                try
                {
                    double zone     = Convert.ToDouble(um[0]);
                    double easting  = Convert.ToDouble(um[3]);
                    double northing = Convert.ToDouble(um[4]);
                    MilitaryGridReferenceSystem mgrs = new MilitaryGridReferenceSystem(um[1], (int)zone, um[2], easting, northing);
                    c = MilitaryGridReferenceSystem.MGRStoLatLong(mgrs);
                    c.Parse_Format = Parse_Format_Type.MGRS;
                    return(true);
                }
                catch
                {//Parser failed try next method
                }
            }
            //Try UTM
            if (TryUTM(s, out um) || TryUPS(s, out um))
            {
                try
                {
                    double zone     = Convert.ToDouble(um[0]);
                    double easting  = Convert.ToDouble(um[2]);
                    double northing = Convert.ToDouble(um[3]);
                    UniversalTransverseMercator utm = new UniversalTransverseMercator(um[1], (int)zone, easting, northing);
                    c = UniversalTransverseMercator.ConvertUTMtoLatLong(utm);
                    c.Parse_Format = Parse_Format_Type.UTM;
                    return(true);
                }
                catch
                {//Parser failed try next method
                }
            }

            //Try Cartesian
            if (TryCartesian(s.ToUpper().Replace("KM", "").Replace("X", "").Replace("Y", "").Replace("Z", ""), out d))
            {
                if (ct == CartesianType.Cartesian)
                {
                    try
                    {
                        Cartesian cart = new Cartesian(d[0], d[1], d[2]);
                        c = Cartesian.CartesianToLatLong(cart);
                        c.Parse_Format = Parse_Format_Type.Cartesian_Spherical;
                        return(true);
                    }
                    catch
                    {//Parser failed try next method
                    }
                }
                if (ct == CartesianType.ECEF)
                {
                    try
                    {
                        ECEF ecef = new ECEF(d[0], d[1], d[2]);
                        c = ECEF.ECEFToLatLong(ecef);
                        c.Parse_Format = Parse_Format_Type.Cartesian_ECEF;
                        return(true);
                    }
                    catch
                    {//Parser failed try next method
                    }
                }
            }

            c = null;
            return(false);
        }
Пример #3
0
        private static Coordinate MGRS_Polar_ToLatLong(MilitaryGridReferenceSystem mgrs)
        {
            //WORKING
            bool isNorth = true;

            if (mgrs.latZone.ToUpper() == "A" || mgrs.latZone.ToUpper() == "B")
            {
                isNorth = false;
            }

            string latz    = mgrs.LatZone;
            string digraph = mgrs.Digraph;

            char eltr = digraph[0];
            char nltr = digraph[1];


            string digraphLettersE;

            if (!isNorth)
            {
                digraphLettersE = "KLPQRSTUXYZABCFGH";
            }
            else
            {
                digraphLettersE = "RSTUXYZABCFGHJ";
            }

            string digraphLettersN;

            if (!isNorth)
            {
                digraphLettersN = "VWXYBCDEFGHJKLMNPQRSTUVWXYZ";
            }
            else
            {
                digraphLettersN = "ABCDEFGHJKLMNP";
            }


            string digraphLettersAll = "";

            for (int lt = 1; lt < 31; lt++)
            {
                digraphLettersAll += digraphLettersN;
            }

            var eidx = digraphLettersE.IndexOf(eltr);

            //Offsets are set due to less Easting Identifiers.
            //North has 4 less than S
            double offset = 9;

            if (isNorth)
            {
                offset = 13;
            }

            if (mgrs.latZone == "B" && eidx < offset && mgrs.easting != 0)
            {
                eidx += 18;
            }


            double subbase = eidx + offset;

            var ebase       = 100000 * subbase;
            var latBand     = digraphLettersE.IndexOf(latz);
            var latBandLow  = 8 * latBand - 96;
            var latBandHigh = 8 * latBand - 88;

            if (!isNorth)
            {
                latBandLow  = -90;
                latBandHigh = -80;
            }
            else
            {
                latBandLow  = 84;
                latBandHigh = 90;
            }

            var lowLetter  = Math.Floor(100 + 1.11 * latBandLow);
            var highLetter = Math.Round(100 + 1.11 * latBandHigh);

            string latBandLetters = null;
            int    l = Convert.ToInt32(lowLetter);
            int    h = Convert.ToInt32(highLetter + 7);

            if (mgrs.LongZone / 2.0 == Math.Floor(mgrs.LongZone / 2.0))
            {
                latBandLetters = digraphLettersAll.Substring(l + 5, h + 5).ToString();
            }
            else
            {
                latBandLetters = digraphLettersAll.Substring(l, h).ToString();
            }

            //North offset + 4 due to lower band count.
            double nOffset = 13;

            if (!isNorth)
            {
                nOffset = 10;
            }
            else
            {
                latBandLetters = digraphLettersN;
            }
            int index = latBandLetters.IndexOf(nltr);

            if (index == -1 && nltr == 'A')
            {
                index -= 1;
            }                                            //ALPHA PATCH

            //int subset = 0;
            //if ((latz == "Y" || latz == "Z") && (nOffset+index)>25 && (ebase> 2100000 || ebase<2000000) && ebase!= 2000000) { subset = -14; }
            var nbase = 100000 * (index + nOffset);

            var x = ebase + mgrs.Easting;
            var y = nbase + mgrs.Northing;

            if (mgrs.systemType != MGRS_Type.MGRS_Polar)
            {
                if (y > 10000000)
                {
                    y = y - 10000000;
                }
                if (nbase >= 10000000)
                {
                    y = nbase + mgrs.northing - 10000000;
                }
            }

            // Debug.WriteLine("MGRS {0} {1}", x, y);
            UniversalTransverseMercator utm = new UniversalTransverseMercator(mgrs.LatZone, mgrs.LongZone, x, y);

            utm.equatorial_radius  = mgrs.equatorialRadius;
            utm.inverse_flattening = mgrs.inverseFlattening;
            Coordinate c = UniversalTransverseMercator.ConvertUTMtoLatLong(utm);

            c.Set_Datum(mgrs.equatorialRadius, mgrs.inverseFlattening);

            return(c);
        }
        //Add main to Coordinate and tunnel to Format class. Add private methods to format.
        //WHEN PARSING NO EXCPETIONS FOR OUT OF RANGE ARGS WILL BE THROWN
        public static bool TryParse(string coordString, CartesianType ct, out Coordinate c)
        {
            try
            {
                //Turn of eagerload for efficiency
                EagerLoad eg = new EagerLoad();
                eg.Cartesian = false;
                eg.Celestial = false;
                eg.UTM_MGRS  = false;

                c = new Coordinate(eg);
                if (string.IsNullOrEmpty(coordString))
                {
                    return(false);
                }

                string s = coordString;
                s = s.Trim(); //Trim all spaces before and after string
                double[] d;
                //Try Signed Degree
                if (TrySignedDegree(s, out d))
                {
                    try
                    {
                        c = new Coordinate(d[0], d[1], eg);
                        c.Parse_Format = Parse_Format_Type.Signed_Degree;
                        return(true);
                    }
                    catch
                    {//Parser failed try next method
                    }
                }

                //Try Decimal Degree
                if (TryDecimalDegree(s, out d))
                {
                    try
                    {
                        c = new Coordinate(d[0], d[1], eg);
                        c.Parse_Format = Parse_Format_Type.Decimal_Degree;
                        return(true);
                    }
                    catch
                    {//Parser failed try next method
                    }
                }
                //Try DDM
                if (TryDegreeDecimalMinute(s, out d))
                {
                    try
                    {
                        //0 Lat Degree
                        //1 Lat Minute
                        //2 Lat Direction (0 = N, 1 = S)
                        //3 Long Degree
                        //4 Long Minute
                        //5 Long Direction (0 = E, 1 = W)
                        CoordinatesPosition latP = CoordinatesPosition.N;
                        CoordinatesPosition lngP = CoordinatesPosition.E;
                        if (d[2] != 0)
                        {
                            latP = CoordinatesPosition.S;
                        }
                        if (d[5] != 0)
                        {
                            lngP = CoordinatesPosition.W;
                        }
                        CoordinatePart lat = new CoordinatePart((int)d[0], d[1], latP);
                        CoordinatePart lng = new CoordinatePart((int)d[3], d[4], lngP);
                        c              = new Coordinate(eg);
                        c.Latitude     = lat;
                        c.Longitude    = lng;
                        c.Parse_Format = Parse_Format_Type.Degree_Decimal_Minute;
                        return(true);
                    }
                    catch
                    {//Parser failed try next method
                    }
                }
                //Try DMS
                if (TryDegreeMinuteSecond(s, out d))
                {
                    try
                    {
                        //0 Lat Degree
                        //1 Lat Minute
                        //2 Lat Second
                        //3 Lat Direction (0 = N, 1 = S)
                        //4 Long Degree
                        //5 Long Minute
                        //6 Long Second
                        //7 Long Direction (0 = E, 1 = W)
                        CoordinatesPosition latP = CoordinatesPosition.N;
                        CoordinatesPosition lngP = CoordinatesPosition.E;
                        if (d[3] != 0)
                        {
                            latP = CoordinatesPosition.S;
                        }
                        if (d[7] != 0)
                        {
                            lngP = CoordinatesPosition.W;
                        }

                        CoordinatePart lat = new CoordinatePart((int)d[0], (int)d[1], d[2], latP);
                        CoordinatePart lng = new CoordinatePart((int)d[4], (int)d[5], d[6], lngP);
                        c              = new Coordinate(eg);
                        c.Latitude     = lat;
                        c.Longitude    = lng;
                        c.Parse_Format = Parse_Format_Type.Degree_Minute_Second;
                        return(true);
                    }
                    catch
                    {//Parser failed try next method
                    }
                }

                Cartesian cart;
                if (ct == CartesianType.Cartesian && Cartesian.TryParse(s, out cart))
                {
                    try
                    {
                        c = Cartesian.CartesianToLatLong(cart);
                        c.Parse_Format = Parse_Format_Type.Cartesian_Spherical;
                        return(true);
                    }
                    catch
                    {//Parser failed try next method
                    }
                }

                ECEF ecef;
                if (ct == CartesianType.ECEF && ECEF.TryParse(s, out ecef))
                {
                    try
                    {
                        c = ECEF.ECEFToLatLong(ecef);
                        c.Parse_Format = Parse_Format_Type.Cartesian_ECEF;
                        return(true);
                    }
                    catch
                    {//Parser failed try next method
                    }
                }


                //Try MGRS
                MilitaryGridReferenceSystem mgrs;
                if (MilitaryGridReferenceSystem.TryParse(s, out mgrs))
                {
                    try
                    {
                        c = MilitaryGridReferenceSystem.MGRStoLatLong(mgrs);
                        c.Parse_Format = Parse_Format_Type.MGRS;
                        return(true);
                    }
                    catch
                    {//Parser failed try next method
                    }
                }

                //Try UTM
                UniversalTransverseMercator utm;
                if (UniversalTransverseMercator.TryParse(s, out utm))
                {
                    try
                    {
                        c = UniversalTransverseMercator.ConvertUTMtoLatLong(utm);
                        c.Parse_Format = Parse_Format_Type.UTM;
                        return(true);
                    }
                    catch
                    {//Parser failed try next method
                    }
                }
            }
            catch (Exception ex)
            {
                //Parser exception has occurred
                Debug.WriteLine("PARSER EXCEPTION HANDLED: " + ex.ToString());
            }
            c = null;
            return(false);
        }
Пример #5
0
        /// <summary>
        /// Creates a Coordinate object from an MGRS/NATO UTM Coordinate
        /// </summary>
        /// <param name="mgrs">MilitaryGridReferenceSystem</param>
        /// <param name="eagerLoad">EagerLoad</param>
        /// <returns>Coordinate object</returns>
        /// <example>
        /// The following example creates (converts to) a geodetic Coordinate object based on a MGRS object.
        /// <code>
        /// MilitaryGridReferenceSystem mgrs = new MilitaryGridReferenceSystem("N", 21, "SA", 66037, 61982);
        /// Coordinate c = MilitaryGridReferenceSystem.MGRStoLatLong(mgrs, new EagerLoad(false));
        /// Console.WriteLine(c); //N 0º 33' 35.988" W 60º 0' 0.01"
        /// </code>
        /// </example>
        public static Coordinate MGRStoLatLong(MilitaryGridReferenceSystem mgrs, EagerLoad eagerLoad)
        {
            if (mgrs.systemType == MGRS_Type.MGRS_Polar)
            {
                return(MGRS_Polar_ToLatLong(mgrs));
            }

            string latz    = mgrs.LatZone;
            string digraph = mgrs.Digraph;

            char eltr = digraph[0];
            char nltr = digraph[1];

            string digraphLettersE = "ABCDEFGHJKLMNPQRSTUVWXYZ";
            string digraphLettersN = "ABCDEFGHJKLMNPQRSTUV";

            string digraphLettersAll = "";

            for (int lt = 1; lt < 25; lt++)
            {
                digraphLettersAll += digraphLettersN;
            }

            var eidx = digraphLettersE.IndexOf(eltr);

            var pbase = 100000;

            double fl = Math.Floor(Convert.ToDouble(eidx) / 8);

            double subbase = 1 + eidx - 8 * fl;

            var ebase       = pbase * subbase;
            var latBand     = digraphLettersE.IndexOf(latz);
            var latBandLow  = 8 * latBand - 96;
            var latBandHigh = 8 * latBand - 88;

            if (latBand < 2)
            {
                latBandLow  = -90;
                latBandHigh = -80;
            }
            else if (latBand == 21)
            {
                latBandLow  = 72;
                latBandHigh = 84;
            }
            else if (latBand > 21)
            {
                latBandLow  = 84;
                latBandHigh = 90;
            }


            var lowLetter  = Math.Floor(100 + 1.11 * latBandLow);
            var highLetter = Math.Round(100 + 1.11 * latBandHigh);

            string latBandLetters = null;
            int    l = Convert.ToInt32(lowLetter);
            int    h = Convert.ToInt32(highLetter);

            if (mgrs.LongZone / 2.0 == Math.Floor(mgrs.LongZone / 2.0))
            {
                latBandLetters = digraphLettersAll.Substring(l + 5, h + 5).ToString();
            }
            else
            {
                latBandLetters = digraphLettersAll.Substring(l, h).ToString();
            }



            var nbase = 100000 * (lowLetter + latBandLetters.IndexOf(nltr));

            //latBandLetters.IndexOf(nltr) value causing incorrect Northing below -80
            var x = ebase + mgrs.Easting;
            var y = nbase + mgrs.Northing;

            if (mgrs.systemType != MGRS_Type.MGRS_Polar)
            {
                if (y > 10000000)
                {
                    y = y - 10000000;
                }
                if (nbase >= 10000000)
                {
                    y = nbase + mgrs.northing - 10000000;
                }
            }

            UniversalTransverseMercator utm = new UniversalTransverseMercator(mgrs.LatZone, mgrs.LongZone, x, y);

            utm.equatorial_radius  = mgrs.equatorialRadius;
            utm.inverse_flattening = mgrs.inverseFlattening;
            Coordinate c = UniversalTransverseMercator.ConvertUTMtoLatLong(utm, eagerLoad);

            c.Set_Datum(mgrs.equatorialRadius, mgrs.inverseFlattening);

            return(c);
        }