コード例 #1
0
ファイル: MGRS.cs プロジェクト: OOP-03376400/MissionPlanner-1
        /// <summary><para>Konstruktor mit zusammengesetzter MGRS Koordinate.</para></summary>
        ///
        /// <example>Das folgende Beispiel erzeugt eine Instanz der <see cref="MGRS"/>-Klasse und übergibt als Parameter
        /// eine zusammengesetzte MGRS Koordinate:
        /// <code>
        /// using GeoUtility.GeoSystem;
        /// MGRS mgrs = new MGRS("32UMA1234567890");
        /// </code>
        /// </example>
        ///
        /// <param name="mgrs">Zusammengesetze MGRS Koordinate (z. B. 32UMN1234567890)</param>
        public MGRS(string mgrs)
        {
            string error;

            try
            {
                MGRS Mgrs = new MGRS();
                if (MGRS.TryParse(mgrs, out Mgrs, out error) == false)
                {
                    throw new Exception(error);
                }
                else
                {
                    this.Zone  = Mgrs.Zone;
                    this.Band  = Mgrs.Band;
                    this.Grid  = Mgrs.Grid;
                    this.East  = Mgrs.East;
                    this.North = Mgrs.North;
                }
            }
            catch (Exception ex)
            {
                throw new ErrorProvider.GeoException(new ErrorProvider.ErrorMessage("ERROR_MGRS"), ex);
            }
        }
コード例 #2
0
ファイル: MGRS.cs プロジェクト: OOP-03376400/MissionPlanner-1
        /// <summary>Die Funktion wird aus performancegründen implementiert.</summary>
        /// <param name="obj">Ein beliebiges Objekt.</param>
        /// <returns>Das übergebene Objekt ist gleich oder nicht.</returns>
        public override bool Equals(Object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }
            MGRS mgrs = (MGRS)obj;

            return(this.ToString() == mgrs.ToString());
        }
コード例 #3
0
ファイル: MGRS.cs プロジェクト: OOP-03376400/MissionPlanner-1
        /// <summary><para>Die generische Methode konvertiert den generischen Typ T in das aktuelle <see cref="MGRS"/> Objekt.</para></summary>
        ///
        /// <example>Das Beispiel konvertiert ein bestehendes <see cref="Geographic"/> Objekt in das aktuelle
        /// <see cref="MGRS"/> Objekt mit Hilfe der generischen Methode <see cref="ConvertFrom{T}(T)">ConvertFrom&lt;T&gt;(T)</see>:
        /// <code>
        /// using GeoUtility.GeoSystem;
        /// Geographic geo = new Geographic(8.12345, 50.56789);
        /// MGRS mgrs;
        /// mgrs.ConvertFrom&lt;Geographic&gt;(geo);
        /// </code>
        /// </example>
        ///
        /// <typeparam name="T">Ein aus der Basisklasse <see cref="GeoUtility.GeoSystem.Base.BaseSystem"/> abgeleiteter Typ.</typeparam>
        /// <param name="t">Das zu konvertierende Objekt als generischer Parameter.</param>
        public void ConvertFrom <T>(T t) where T : BaseSystem
        {
            MGRS o = null;

            try
            {
                if (typeof(T) == typeof(UTM))
                {
                    o = (MGRS)(UTM)(BaseSystem)t;
                }
                else if (typeof(T) == typeof(Geographic))
                {
                    o = (MGRS)(Geographic)(BaseSystem)t;
                }
                else if (typeof(T) == typeof(GaussKrueger))
                {
                    o = (MGRS)(GaussKrueger)(BaseSystem)t;
                }
                else if (typeof(T) == typeof(MapService))
                {
                    o = (MGRS)(MapService)(BaseSystem)t;
                }
                if (o != null)
                {
                    this.Zone  = o.Zone;
                    this.Band  = o.Band;
                    this.Grid  = o.Grid;
                    this.East  = o.East;
                    this.North = o.North;
                    o          = null;
                }
            }
            catch (Exception ex)
            {
                throw new ErrorProvider.GeoException(new ErrorProvider.ErrorMessage("ERROR_CONVERTFROM"), ex);
            }
        }
コード例 #4
0
ファイル: MGRS.cs プロジェクト: OOP-03376400/MissionPlanner-1
        /// <summary><para>Die statische Methode kann dazu verwendet werden, als String-Werte übergebene MGRS Parameter
        /// auf ihre Gültigkeit zu überprüfen. Die Methode gibt eine Liste gültiger Parameter, eine Fehlermeldung und
        /// ein <see cref="MGRS"/>-Objekt zurück. Ist einer der Parameter ungültig, wird ein <see cref="MGRS"/>-Objekt
        /// mir dem Wert null zurückgegeben.</para></summary>
        /// <param name="Zone">Zone als Typ <see cref="System.String"/>.</param>
        /// <param name="Band">Band als Typ <see cref="System.String"/>.</param>
        /// <param name="Grid">Band als Typ <see cref="System.String"/>.</param>
        /// <param name="East">East-Wert als Typ <see cref="System.String"/>.</param>
        /// <param name="North">North-Wert als Typ <see cref="System.String"/>.</param>
        /// <param name="Mgrs">Ein gültiges <see cref="MGRS"/>-Objekt oder null.</param>
        /// <param name="ErrorMessage">Eine ausführliche Fehlermeldung, falls ein Fehler aufgetreten ist.</param>
        /// <param name="ValidItems">Ein <see cref="System.Collections.Generic.Dictionary{T, T}"/>-Objekt, in dem die gültigen und ungültigen Parameter aufgeführt werden.</param>
        /// <returns>True, wenn alle Parameter gültig sind, sonst False.</returns>
        static public bool TryParse(string Zone, string Band, string Grid, string East, string North, out MGRS Mgrs, out string ErrorMessage, out Dictionary <string, bool> ValidItems)
        {
            bool                      result = true;
            bool                      converted = true;
            StringBuilder             sb = new StringBuilder();
            MGRS                      mgrs = null;
            Dictionary <string, bool> list = new Dictionary <string, bool>();
            int    zone = 0, east = 0, north = 0;
            char   band = 'A';
            string grid = Grid;

            Band = Band.ToUpper();
            Grid = Grid.ToUpper();


            try { zone = int.Parse(Zone); } catch { converted = false; }
            if ((zone < 1) || (zone > 60))
            {
                converted = false;
            }
            list.Add("Zone", converted);
            if (list["Zone"] == false)
            {
                sb.Append(new ErrorProvider.ErrorMessage("ERROR_UTM_ZONE2").Text + "\r\n");
                result = false;
            }
            converted = true;

            converted = char.IsLetter(Band, 0);
            try { band = Convert.ToChar(Band); }
            catch { converted = false; }
            if ((band < 'C') || (band > 'X') || (band == 'I') || (band == 'O'))
            {
                converted = false;
            }
            list.Add("Band", converted);
            if (list["Band"] == false)
            {
                sb.Append(new ErrorProvider.ErrorMessage("ERROR_UTM_Band").Text + "\r\n");
                result = false;
            }
            converted = true;

            list.Add("Grid", (Grid.Length == 2));
            if (list["Grid"] == true)
            {
                char c0 = Grid.ToCharArray()[0];
                char c1 = Grid.ToCharArray()[1];

                if ((c0 < 'A') || (c0 >= 'Z') || (c1 < 'A') || (c1 >= 'Z') || (c0 == 'I') || (c0 == 'O') || (c1 == 'I') || (c1 == 'O'))
                {
                    list["Grid"] = false;
                }
            }
            if (list["Grid"] == false)
            {
                sb.Append(new ErrorProvider.ErrorMessage("ERROR_MGRS_GRID").Text + "\r\n");
                result = false;
            }

            if (East.Length > 5)
            {
                East = East.Substring(0, 5);
            }
            else if (East.Length < 5)
            {
                East = East.PadRight(5, '0');
            }
            try { east = int.Parse(East); } catch { converted = false; }
            if ((east < 0) || (east >= 100000))
            {
                converted = false;
            }
            list.Add("East", converted);
            if (list["East"] == false)
            {
                sb.Append(new ErrorProvider.ErrorMessage("ERROR_MGRS_EAST").Text + "\r\n");
                result = false;
            }
            converted = true;

            if (North.Length > 5)
            {
                North = North.Substring(0, 5);
            }
            else if (North.Length < 5)
            {
                North = North.PadRight(5, '0');
            }
            try { north = int.Parse(North); } catch { converted = false; }
            if ((north < 0) || (north >= 100000))
            {
                converted = false;
            }
            list.Add("North", converted);
            if (list["North"] == false)
            {
                sb.Append(new ErrorProvider.ErrorMessage("ERROR_MGRS_NORTH").Text + "\r\n");
                result = false;
            }

            if (result == true)
            {
                mgrs = new MGRS(zone, band.ToString(), grid, east, north);
            }
            Mgrs         = mgrs;
            ErrorMessage = sb.ToString();
            ValidItems   = list;
            return(result);
        }
コード例 #5
0
ファイル: MGRS.cs プロジェクト: OOP-03376400/MissionPlanner-1
        /// <summary><para>Die statische Methode kann dazu verwendet werden, einen zusammengesetzten MGRS-String
        /// auf seine Gültigkeit zu überprüfen. eine Fehlermeldung und
        /// ein <see cref="MGRS"/>-Objekt zurück. Ist einer der Parameter ungültig, wird ein <see cref="MGRS"/>-Objekt
        /// mir dem Wert null zurückgegeben.</para></summary>
        /// <param name="mstring">MGRS Koordinatenstring als Typ <see cref="System.String"/>.</param>
        /// <param name="Mgrs">Ein gültiges <see cref="MGRS"/>-Objekt oder null.</param>
        /// <param name="ErrorMessage">Eine ausführliche Fehlermeldung, falls ein Fehler aufgetreten ist.</param>
        /// <returns>True, wenn alle Parameter gültig sind, sonst False.</returns>
        static public bool TryParse(string mstring, out MGRS Mgrs, out string ErrorMessage)
        {
            string message = "";
            MGRS   mgrs    = new MGRS();
            bool   result  = true;

            try
            {
                mstring = mstring.Replace(" ", "");
                mstring = mstring.ToUpper();

                // Mindestens Zone (1), Band (1), Grid (2), East (1), North (1) = 6
                if (mstring.Length < 6)
                {
                    throw new ErrorProvider.GeoException(new ErrorProvider.ErrorMessage("ERROR_MGRS"));
                }

                int zone = 0;
                // Test auf 1 oder 2-stellige Zonenangabe

                if (char.IsDigit(Convert.ToChar(mstring.Substring(0, 1))))
                {
                    if (char.IsDigit(Convert.ToChar(mstring.Substring(1, 1))))
                    {
                        zone    = int.Parse(mstring.Substring(0, 2));
                        mstring = mstring.Remove(0, 2);
                    }
                    else
                    {
                        zone    = int.Parse(mstring.Substring(0, 1));
                        mstring = mstring.Remove(0, 1);
                    }
                }
                else
                {
                    throw new ErrorProvider.GeoException(new ErrorProvider.ErrorMessage("ERROR_MGRS"));
                }
                mgrs.Zone = zone;

                if (mstring.Length < 5)
                {
                    throw new ErrorProvider.GeoException(new ErrorProvider.ErrorMessage("ERROR_MGRS"));
                }

                char band = mstring.Substring(0, 1).ToCharArray()[0];
                if ((band < 'C') || (band > 'X') || (band == 'I') || (band == 'O'))
                {
                    throw new ErrorProvider.GeoException(new ErrorProvider.ErrorMessage("ERROR_MGRS_GRID"));
                }
                mgrs.Band = band.ToString();
                mstring   = mstring.Remove(0, 1);

                string grid = mstring.Substring(0, 2);
                char   c0   = grid.ToCharArray()[0];
                char   c1   = grid.ToCharArray()[1];
                if ((c0 < 'A') || (c0 >= 'Z') || (c1 < 'A') || (c1 >= 'Z') || (c0 == 'I') || (c0 == 'O') || (c1 == 'I') || (c1 == 'O'))
                {
                    throw new ErrorProvider.GeoException(new ErrorProvider.ErrorMessage("ERROR_MGRS_GRID"));
                }
                mgrs.Grid = grid;
                mstring   = mstring.Remove(0, 2);

                if (mstring.Length % 2 > 0)
                {
                    throw new ErrorProvider.GeoException(new ErrorProvider.ErrorMessage("ERROR_MGRS"));
                }
                int    len        = mstring.Length / 2;
                string eaststring = mstring.Substring(0, len);
                mstring = mstring.Remove(0, len);
                string northstring = mstring;

                if (eaststring.Length > 5)
                {
                    eaststring = eaststring.Substring(0, 5);
                }
                else if (eaststring.Length < 5)
                {
                    eaststring = eaststring.PadRight(5, '0');
                }

                if (northstring.Length > 5)
                {
                    northstring = northstring.Substring(0, 5);
                }
                else if (northstring.Length < 5)
                {
                    northstring = northstring.PadRight(5, '0');
                }

                mgrs.East  = double.Parse(eaststring);
                mgrs.North = double.Parse(northstring);
            }
            catch
            {
                mgrs    = null;
                result  = false;
                message = new ErrorProvider.ErrorMessage("ERROR_MGRS").Text;
            }
            Mgrs         = mgrs;
            ErrorMessage = message;
            return(result);
        }