public GPSCoordinatesHelper(double degrees, double minutes, double seconds, CoordinatesPosition position)
 {
     Degrees  = degrees;
     Minutes  = minutes;
     Seconds  = seconds;
     Position = position;
 }
Example #2
0
        public void SetFrameCoordinates(int frame, CoordinatesPosition position)
        {
            Vector2 coord = Vector2.One;
            Vector2 frameSize;

            if (this.Orientation == Orientations.BOTH)
            {
                frameSize = new Vector2(1f / this.XFramesQty, 1f / (float)Math.Ceiling(this.QtyFrames / (float)this.XFramesQty));
            }
            else
            {
                frameSize = new Vector2(1f / this.QtyFrames, 1f / this.QtyFrames);
            }

            if (this.Orientation == Orientations.HORIZONTAL)
            {
                coord.X = frameSize.X * frame;
            }
            else if (this.Orientation == Orientations.VERTICAL)
            {
                coord.Y = frameSize.Y * frame;
            }
            else
            {
                coord.X = frameSize.X * (frame % this.XFramesQty);
                coord.Y = frameSize.Y * (frame / this.XFramesQty);
            }

            switch (position)
            {
            case CoordinatesPosition.TOP_RIGHT:
                coord.X += this.Orientation == Orientations.VERTICAL ? 1 : frameSize.X;
                break;

            case CoordinatesPosition.BOTTOM_LEFT:
                coord.Y += this.Orientation == Orientations.HORIZONTAL ? 1 : frameSize.Y;
                break;

            case CoordinatesPosition.BOTTOM_RIGHT:
                coord.X += this.Orientation == Orientations.VERTICAL ? 1 : frameSize.X;
                coord.Y += this.Orientation == Orientations.HORIZONTAL ? 1 : frameSize.Y;
                break;
            }

            gl.TexCoord(coord.X, coord.Y);
        }
        public GPSCoordinatesHelper(double?value, CoordinatesPosition position)
        {
            //sanity
            if (value < 0 && position == CoordinatesPosition.N)
            {
                position = CoordinatesPosition.S;
            }
            //sanity
            if (value < 0 && position == CoordinatesPosition.E)
            {
                position = CoordinatesPosition.W;
            }
            //sanity
            if (value > 0 && position == CoordinatesPosition.S)
            {
                position = CoordinatesPosition.N;
            }
            //sanity
            if (value > 0 && position == CoordinatesPosition.W)
            {
                position = CoordinatesPosition.E;
            }

            var decimalValue = Convert.ToDecimal(value);

            decimalValue = Math.Abs(decimalValue);

            var degrees = Decimal.Truncate(decimalValue);

            decimalValue = (decimalValue - degrees) * 60;

            var minutes = Decimal.Truncate(decimalValue);
            var seconds = (decimalValue - minutes) * 60;

            Degrees  = Convert.ToDouble(degrees);
            Minutes  = Convert.ToDouble(minutes);
            Seconds  = Math.Round(Convert.ToDouble(seconds), 1);
            Position = position;
        }
Example #4
0
        /// <summary>
        /// Creates a populated CoordinatePart from a Degrees Minutes Seconds part.
        /// </summary>
        /// <param name="deg">Degrees</param>
        /// <param name="minSec">Decimal Minutes</param>
        /// <param name="pos">Coordinate part position</param>
        /// <example>
        /// The following example creates a populated latitudinal coordinate part from Decimal Degree Minute values and assigns it to a Coordinate object.
        /// <code>
        /// //Create a new default coordinate
        /// Coordinate c = new Coordinate();
        ///
        /// //Create a coordinate part using the Decimal Degree latitude N25 36.24854°.
        /// CoordinatePart cp = new CoordinatePart(25, 36.24854, CoordinatesPosition.N);
        ///
        /// //Assign the latitudinal value to the coordinate.
        /// c.Latitude = cp;
        /// </code>
        /// </example>
        public CoordinatePart(int deg, double minSec, CoordinatesPosition pos)
        {
            if (pos == CoordinatesPosition.N || pos == CoordinatesPosition.S)
            {
                type = CoordinateType.Lat;
            }
            else
            {
                type = CoordinateType.Long;
            }

            if (deg < 0)
            {
                throw new ArgumentOutOfRangeException("Degree out of range", "Degree cannot be less than 0.");
            }
            if (minSec < 0)
            {
                throw new ArgumentOutOfRangeException("Minutes out of range", "Minutes cannot be less than 0.");
            }

            if (minSec >= 60)
            {
                throw new ArgumentOutOfRangeException("Minutes out of range", "Minutes cannot be greater than or equal to 60.");
            }

            if (type == CoordinateType.Lat)
            {
                if (deg + (minSec / 60) > 90)
                {
                    throw new ArgumentOutOfRangeException("Degree out of range", "Latitudinal degrees cannot be greater than 90.");
                }
            }
            else
            {
                if (deg + (minSec / 60) > 180)
                {
                    throw new ArgumentOutOfRangeException("Degree out of range", "Longitudinal degrees cannot be greater than 180.");
                }
            }
            degrees       = deg;
            decimalMinute = minSec;
            position      = pos;

            decimal minD     = Convert.ToDecimal(minSec);
            decimal minFloor = Math.Floor(minD);

            minutes = Convert.ToInt32(minFloor);
            decimal sec = minD - minFloor;

            sec *= 60;
            decimal secD = Convert.ToDecimal(sec);

            seconds = Convert.ToDouble(secD);
            decimal dd = deg + (minD / 60);

            if (pos == CoordinatesPosition.S || pos == CoordinatesPosition.W)
            {
                dd *= -1;
            }
            decimalDegree = Convert.ToDouble(dd);
        }
Example #5
0
        /// <summary>
        /// Creates a populated CoordinatePart from a Degrees Minutes Seconds value.
        /// </summary>
        /// <param name="deg">Degrees</param>
        /// <param name="min">Minutes</param>
        /// <param name="sec">Seconds</param>
        /// <param name="pos">Coordinate part position</param>
        /// <example>
        /// The following example creates a populated latitudinal coordinate part from Degrees Minutes Seconds values and assigns it to a Coordinate object.
        /// <code>
        /// //Create a new default coordinate
        /// Coordinate c = new Coordinate();
        ///
        /// //Create a coordinate part using the Degrees Minutes Seconds latitude N25 36 24.657°.
        /// CoordinatePart cp = new CoordinatePart(25, 36, 24.657, CoordinatesPosition.N);
        ///
        /// //Assign the latitudinal value to the coordinate.
        /// c.Latitude = cp;
        /// </code>
        /// </example>
        public CoordinatePart(int deg, int min, double sec, CoordinatesPosition pos)
        {
            if (pos == CoordinatesPosition.N || pos == CoordinatesPosition.S)
            {
                type = CoordinateType.Lat;
            }
            else
            {
                type = CoordinateType.Long;
            }

            if (deg < 0)
            {
                throw new ArgumentOutOfRangeException("Degrees out of range", "Degrees cannot be less than 0.");
            }
            if (min < 0)
            {
                throw new ArgumentOutOfRangeException("Minutes out of range", "Minutes cannot be less than 0.");
            }
            if (sec < 0)
            {
                throw new ArgumentOutOfRangeException("Seconds out of range", "Seconds cannot be less than 0.");
            }
            if (min >= 60)
            {
                throw new ArgumentOutOfRangeException("Minutes out of range", "Minutes cannot be greater than or equal to 60.");
            }
            if (sec >= 60)
            {
                throw new ArgumentOutOfRangeException("Seconds out of range", "Seconds cannot be greater than or equal to 60.");
            }
            degrees  = deg;
            minutes  = min;
            seconds  = sec;
            position = pos;

            decimal secD = Convert.ToDecimal(sec);

            secD /= 60; //Decimal Seconds
            decimal minD = Convert.ToDecimal(min);

            minD += secD; //Decimal Minutes

            if (type == CoordinateType.Long)
            {
                if (deg + (minD / 60) > 180)
                {
                    throw new ArgumentOutOfRangeException("Degrees out of range", "Longitudinal Degrees cannot be greater than 180.");
                }
            }
            else
            {
                if (deg + (minD / 60) > 90)
                {
                    throw new ArgumentOutOfRangeException("Degrees out of range", "Latitudinal Degrees cannot be greater than 90.");
                }
            }
            decimalMinute = Convert.ToDouble(minD);
            decimal dd = Convert.ToDecimal(deg) + (minD / 60);


            if (pos == CoordinatesPosition.S || pos == CoordinatesPosition.W)
            {
                dd *= -1;
            }
            decimalDegree = Convert.ToDouble(dd);
        }
        //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);
        }
Example #7
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, 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);
                    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);
                    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, c);
                    CoordinatePart lng = new CoordinatePart((int)d[3], d[4], lngP, c);
                    c           = new Coordinate(eg);
                    c.Latitude  = lat;
                    c.Longitude = lng;
                    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, c);
                    CoordinatePart lng = new CoordinatePart((int)d[4], (int)d[5], d[6], lngP, c);
                    c           = new Coordinate(eg);
                    c.Latitude  = lat;
                    c.Longitude = lng;
                    return(true);
                }
                catch
                {//Parser failed try next method
                }
            }

            string[] um;
            //Try MGRS
            if (TryMGRS(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);
                    return(true);
                }
                catch
                {//Parser failed try next method
                }
            }
            //Try UTM
            if (TryUTM(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);
                    return(true);
                }
                catch
                {//Parser failed try next method
                }
            }
            if (TryCartesian(s, out d))
            {
                try
                {
                    Cartesian cart = new Cartesian(d[0], d[1], d[2]);
                    c = Cartesian.CartesianToLatLong(cart);
                    return(true);
                }
                catch
                {//Parser failed try next method
                }
            }
            //Try Cartesian
            return(false);
        }