Ejemplo n.º 1
0
        /// <summary>
        /// Parses a string into a Coordinate with a specified date, Cartesian system type and eager loading settings.
        /// </summary>
        /// <param name="value">Coordinate string</param>
        /// <param name="geoDate">GeoDate</param>
        /// <param name="cartesianType">Cartesian Type</param>
        /// <param name="eagerLoad">Eager loading options</param>
        /// <returns>Coordinate</returns>
        /// <example>
        /// The following example parses an ECEF formatted coordinate string, with an included GeoDate.
        /// Because this is an ECEF Cartesian type coordinate, we will specify the Cartesian system type.
        /// Eager loading options have been specified for efficiency.
        /// <code>
        /// EagerLoad el = new EagerLoad(EagerLoadType.Cartesian);
        /// Coordinate c = Coordinate.Parse("5242.097 km, 2444.43 km, 2679.074 km", new DateTime(2018,7,7), CartesianType.ECEF, el);
        /// </code>
        /// </example>
        public static Coordinate Parse(string value, DateTime geoDate, CartesianType cartesianType, EagerLoad eagerLoad)
        {
            Coordinate coordinate = null;

            if (TryParse(value, geoDate, cartesianType, eagerLoad, out coordinate))
            {
                return(coordinate);
            }

            throw new FormatException(string.Format("Input Coordinate \"{0}\" was not in a correct format.", value));
        }
Ejemplo n.º 2
0
        public void Coordinate_Parse_Wrap_Tests()
        {
            string        coord   = "45.6, 22.4";
            EagerLoad     el      = new EagerLoad(EagerLoadType.Celestial | EagerLoadType.Cartesian | EagerLoadType.ECEF);
            CartesianType cType   = CartesianType.ECEF;
            DateTime      geoDate = new DateTime(2020, 3, 10, 10, 10, 12);

            Coordinate parseCoord;
            Coordinate tryParseCoord;

            parseCoord = Coordinate.Parse(coord);
            Coordinate.TryParse(coord, out tryParseCoord);
            Assert.IsTrue(Parse_Wrap_Check(parseCoord, tryParseCoord, false));

            parseCoord = Coordinate.Parse(coord, geoDate);
            Coordinate.TryParse(coord, geoDate, out tryParseCoord);
            Assert.IsTrue(Parse_Wrap_Check(parseCoord, tryParseCoord, false));

            parseCoord = Coordinate.Parse(coord, cType);
            Coordinate.TryParse(coord, cType, out tryParseCoord);
            Assert.IsTrue(Parse_Wrap_Check(parseCoord, tryParseCoord, false));

            parseCoord = Coordinate.Parse(coord, geoDate, cType);
            Coordinate.TryParse(coord, geoDate, cType, out tryParseCoord);
            Assert.IsTrue(Parse_Wrap_Check(parseCoord, tryParseCoord, false));

            parseCoord = Coordinate.Parse(coord, el);
            Coordinate.TryParse(coord, el, out tryParseCoord);
            Assert.IsTrue(Parse_Wrap_Check(parseCoord, tryParseCoord, false));

            parseCoord = Coordinate.Parse(coord, geoDate, el);
            Coordinate.TryParse(coord, geoDate, el, out tryParseCoord);
            Assert.IsTrue(Parse_Wrap_Check(parseCoord, tryParseCoord, false));

            parseCoord = Coordinate.Parse(coord, cType, el);
            Coordinate.TryParse(coord, cType, el, out tryParseCoord);
            Assert.IsTrue(Parse_Wrap_Check(parseCoord, tryParseCoord, false));

            parseCoord = Coordinate.Parse(coord, geoDate, cType, el);
            Coordinate.TryParse(coord, geoDate, cType, el, out tryParseCoord);
            Assert.IsTrue(Parse_Wrap_Check(parseCoord, tryParseCoord, false));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Attempts to parse a string into a Coordinate with a specified date, Cartesian system type and eager loading settings.
        /// </summary>
        /// <param name="value">Coordinate string</param>
        /// <param name="geoDate">GeoDate</param>
        /// <param name="cartesianType">Cartesian Type</param>
        /// <param name="eagerLoad">Eager loading options</param>
        /// <param name="coordinate">Coordinate</param>
        /// <returns>boolean</returns>
        /// <example>
        /// The following example parses an ECEF formatted coordinate string, with an included GeoDate.
        /// Because this is an ECEF Cartesian type coordinate, we will specify the Cartesian system type.
        /// Eager loading options have been specified for efficiency.
        /// <code>
        /// Coordinate c;
        /// EagerLoad el = new EagerLoad(EagerLoadType.Cartesian);
        /// if(Coordinate.TryParse("5242.097 km, 2444.43 km, 2679.074 km", new DateTime(2018,7,7), CartesianType.ECEF, el, out c))
        /// {
        ///     Console.WriteLine(c); //N 24º 59' 59.987" E 25º 0' 0.001"
        /// }
        /// </code>
        /// </example>
        public static bool TryParse(string value, DateTime geoDate, CartesianType cartesianType, EagerLoad eagerLoad, out Coordinate coordinate)
        {
            coordinate = null;
            if (FormatFinder.TryParse(value, cartesianType, out coordinate))
            {
                Parse_Format_Type pft = coordinate.Parse_Format;
                if (cartesianType == CartesianType.ECEF)
                {
                    Distance h = coordinate.ecef.GeoDetic_Height;
                    coordinate = new Coordinate(coordinate.Latitude.ToDouble(), coordinate.Longitude.ToDouble(), geoDate, eagerLoad); //Reset with eager load options specified.
                    coordinate.ecef.Set_GeoDetic_Height(coordinate, h);
                }
                else
                {
                    coordinate = new Coordinate(coordinate.Latitude.ToDouble(), coordinate.Longitude.ToDouble(), geoDate, eagerLoad); //Reset with eager load options specified.
                }
                coordinate.parse_Format = pft;

                return(true);
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Attempts to parse a string into a Coordinate with a specified Cartesian system type.
        /// </summary>
        /// <param name="value">Coordinate string</param>
        /// <param name="cartesianType">Cartesian Type</param>
        /// <param name="coordinate">Coordinate</param>
        /// <returns>boolean</returns>
        /// <example>
        /// The following example parses an ECEF formatted coordinate string.
        /// Because this is an ECEF Cartesian type coordinate, we will specify the Cartesian system type.
        /// <code>
        /// Coordinate c;
        /// if(Coordinate.TryParse("5242.097 km, 2444.43 km, 2679.074 km", CartesianType.Cartesian, out c))
        /// {
        ///     Console.WriteLine(c); //N 24º 59' 59.987" E 25º 0' 0.001"
        /// }
        /// </code>
        /// </example>
        public static bool TryParse(string value, CartesianType cartesianType, out Coordinate coordinate)
        {
            coordinate = null;
            if (FormatFinder.TryParse(value, cartesianType, out coordinate))
            {
                Parse_Format_Type pft = coordinate.Parse_Format;
                if (cartesianType == CartesianType.ECEF)
                {
                    Distance h = coordinate.ecef.GeoDetic_Height;
                    coordinate = new Coordinate(coordinate.Latitude.ToDouble(), coordinate.Longitude.ToDouble()); //Reset with EagerLoad default settings
                    coordinate.ecef.Set_GeoDetic_Height(coordinate, h);
                }
                else
                {
                    coordinate = new Coordinate(coordinate.Latitude.ToDouble(), coordinate.Longitude.ToDouble()); //Reset with EagerLoad default settings
                }
                coordinate.parse_Format = pft;

                return(true);
            }
            return(false);
        }
Ejemplo n.º 5
0
        //Verifies that Parse is wrapping Try_Parse correctly
        private static void Parse_Wrap_Tests()
        {
            bool pass = true;

            string        coord   = "45.6, 22.4";
            EagerLoad     el      = new EagerLoad(EagerLoadType.Celestial | EagerLoadType.Cartesian | EagerLoadType.ECEF);
            CartesianType cType   = CartesianType.ECEF;
            DateTime      geoDate = new DateTime(2020, 3, 10, 10, 10, 12);

            Coordinate parseCoord;
            Coordinate tryParseCoord;

            parseCoord = Coordinate.Parse(coord);
            Coordinate.TryParse(coord, out tryParseCoord);
            if (!Parse_Wrap_Check(parseCoord, tryParseCoord, false))
            {
                pass = false;
            }

            parseCoord = Coordinate.Parse(coord, geoDate);
            Coordinate.TryParse(coord, geoDate, out tryParseCoord);
            if (!Parse_Wrap_Check(parseCoord, tryParseCoord, false))
            {
                pass = false;
            }

            parseCoord = Coordinate.Parse(coord, cType);
            Coordinate.TryParse(coord, cType, out tryParseCoord);
            if (!Parse_Wrap_Check(parseCoord, tryParseCoord, false))
            {
                pass = false;
            }

            parseCoord = Coordinate.Parse(coord, geoDate, cType);
            Coordinate.TryParse(coord, geoDate, cType, out tryParseCoord);
            if (!Parse_Wrap_Check(parseCoord, tryParseCoord, false))
            {
                pass = false;
            }

            parseCoord = Coordinate.Parse(coord, el);
            Coordinate.TryParse(coord, el, out tryParseCoord);
            if (!Parse_Wrap_Check(parseCoord, tryParseCoord, false))
            {
                pass = false;
            }

            parseCoord = Coordinate.Parse(coord, geoDate, el);
            Coordinate.TryParse(coord, geoDate, el, out tryParseCoord);
            if (!Parse_Wrap_Check(parseCoord, tryParseCoord, false))
            {
                pass = false;
            }

            parseCoord = Coordinate.Parse(coord, cType, el);
            Coordinate.TryParse(coord, cType, el, out tryParseCoord);
            if (!Parse_Wrap_Check(parseCoord, tryParseCoord, false))
            {
                pass = false;
            }

            parseCoord = Coordinate.Parse(coord, geoDate, cType, el);
            Coordinate.TryParse(coord, geoDate, cType, el, out tryParseCoord);
            if (!Parse_Wrap_Check(parseCoord, tryParseCoord, false))
            {
                pass = false;
            }

            //CoordinatePart Check

            CoordinatePart cp = CoordinatePart.Parse("45");

            if (cp.ToDouble() != 45 || cp.Position != CoordinatesPosition.N)
            {
                pass = false;
            }

            cp = CoordinatePart.Parse("45", CoordinateType.Long);
            if (cp.ToDouble() != 45 || cp.Position != CoordinatesPosition.E)
            {
                pass = false;
            }

            Pass.Write("Parse Wrapper Test", pass);
        }
        //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
                    }
                }

                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
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Parser exception has occurred
                Debug.WriteLine("PARSER EXCEPTION HANDLED: " + ex.ToString());
            }
            c = null;
            return(false);
        }
        /// <summary>
        /// Attempts to parse a string into a Coordinate with specified DateTime
        /// </summary>
        /// <param name="value">Coordinate string</param>
        /// <param name="geoDate">GeoDate</param>
        /// <param name="coordinate">Coordinate</param>
        /// <param name="cartesianType">Cartesian Type</param>
        /// <returns>boolean</returns>
        /// <example>
        /// The following example parses an ECEF formatted coordinate string, with an included GeoDate.
        /// Because this is an ECEF Cartesian type coordinate, we will specify the Cartesian system type.
        /// <code>
        /// Coordinate c;
        /// if(Coordinate.TryParse("5242.097 km, 2444.43 km, 2679.074 km", new DateTime(2018,7,7), CartesianType.ECEF, out c))
        /// {
        ///     Console.WriteLine(c); //N 24º 59' 59.987" E 25º 0' 0.001"
        /// }
        /// </code>
        /// </example>
        public static bool TryParse(string value, DateTime geoDate, CartesianType cartesianType, out Coordinate coordinate)
        {
            var eagerLoad = new EagerLoad();

            return(TryParse(value, geoDate, cartesianType, eagerLoad, out coordinate));
        }