Exemplo n.º 1
0
 public static bool TryParse(string inputString, out Elevation elevation)
 {
     try
     {
         elevation = Parse(inputString);
         return(true);
     }
     catch (Exception)
     {
         elevation = null;
         return(false);
     }
 }
Exemplo n.º 2
0
        //=======================================================================
        #region -= static methods =-

        /// <summary>
        /// Must be in the format 2398,M
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static Elevation Parse(string inputString)
        {
            //---- declare vars
            Elevation elevation = new Elevation();

            //---- split it in half
            string[] halves = inputString.Trim().Split(',');
            //----
            if (halves.Length < 2)
            {
                throw new FormatException("Input string must be in the format 2398,M");
            }

            //---- parse the value
            elevation.Value = decimal.Parse(halves[0]);

            //---- parse the units (should always be meters)
            elevation.UnitType = UnitTypeUtil.Parse(halves[1]);

            //----
            return(elevation);
        }
Exemplo n.º 3
0
        //=======================================================================
        #region -= static methods =-

        //=======================================================================
        public static GgaData Parse(string inputString)
        {
            //---- declare vars
            GgaData data       = new GgaData();
            string  dataString = inputString.Substring(0, inputString.IndexOf('*'));            // strip off the checksum

            string[] values = dataString.Split(',');

            //---- if we don't have 15 (header + 14), it's no good
            if (values.Length < 15)
            {
                throw new FormatException();
            }


            if (values[1].Length == 6)
            {//---- if the time is six digits
                //---- make sure that they're actually numbers
                int  temp;
                bool isparse = true;
                try
                {
                    temp = int.Parse(values[1]);
                }
                catch (ArgumentException)
                {
                    isparse = false;
                }
                if (isparse)
                {
                    //---- should add more validation here
                    int hour   = int.Parse(values[1].Substring(0, 2));
                    int minute = int.Parse(values[1].Substring(2, 2));
                    int second = int.Parse(values[1].Substring(4, 2));

                    data.UtcTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, second);
                }
                else
                {
                    throw new FormatException("Date or time string is invalid");
                }
            }
            else if (values[1].Length == 10)
            {//---- if the time is ten digits
                //---- make sure that they're actually numbers
                double temp;
                bool   isparse = true;
                try
                {
                    temp = double.Parse(values[1]);
                }
                catch (ArgumentException)
                {
                    isparse = false;
                }
                if (isparse)
                {
                    //---- should add more validation here
                    int hour         = int.Parse(values[1].Substring(0, 2));
                    int minute       = int.Parse(values[1].Substring(2, 2));
                    int second       = int.Parse(values[1].Substring(4, 2));
                    int milliseconds = int.Parse(values[1].Substring(7, 3));
                    data.UtcTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, second, milliseconds);
                }
                else
                {
                    throw new FormatException("Date or time string is invalid");
                }
            }
            else
            {
                throw new FormatException("Date or time string is invalid");
            }


            //---- quality
            data.Quality = (Quality)Enum.Parse(typeof(Quality), values[6], true);

            if (data.Quality == Quality.Invalid)
            {
                return(data);
            }

            //---- lat/long position
            data.Position = Position.Parse(values[2] + "," + values[3] + ";" + values[4] + "," + values[5]);

            //---- number of satellites
            data.NumberOfSatelitesInUse = int.Parse(values[7]);

            //---- HDOP
            data.HorizontalDilutionOfPrecision = (string.IsNullOrEmpty(values[8]) ? 0 : decimal.Parse(values[8]));

            //---- elevation
            data.Elevation = Elevation.Parse(values[9] + "," + values[10]);

            //---- geoidal separation
            data.GeoidalSeparation = GeoidalSeparation.Parse(values[11] + "," + values[12]);

            //---- age
            data.DifferenceCorrection.Age = (string.IsNullOrEmpty(values[13]) ? 0M : decimal.Parse(values[13]));

            //---- station ID
            data.DifferenceCorrection.StationID = (string.IsNullOrEmpty(values[14]) ? 0 : int.Parse(values[14]));

            //---- return
            return(data);

            //eg3. $GPGGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh
            //1    = UTC of Position
            //2    = Latitude
            //3    = N or S
            //4    = Longitude
            //5    = E or W
            //6    = GPS quality indicator (0=invalid; 1=GPS fix; 2=Diff. GPS fix)
            //7    = Number of satellites in use [not those in view]
            //8    = Horizontal dilution of position
            //9    = Antenna altitude above/below mean sea level (geoid)
            //10   = Meters  (Antenna height unit)
            //11   = Geoidal separation (Diff. between WGS-84 earth ellipsoid and
            //       mean sea level.  -=geoid is below WGS-84 ellipsoid)
            //12   = Meters  (Units of geoidal separation)
            //13   = Age in seconds since last update from diff. reference station
            //14   = Diff. reference station ID#
            //15   = Checksum
        }