public static bool TryParse(string input, out CoordinateUSNG usng)
        {
            CoordinateMGRS mgrs = new CoordinateMGRS();
            usng = new CoordinateUSNG(mgrs.GZD, mgrs.GS, mgrs.Easting, mgrs.Northing);

            if (string.IsNullOrWhiteSpace(input))
                return false;

            if (CoordinateMGRS.TryParse(input, out mgrs))
            {
                usng = new CoordinateUSNG(mgrs.GZD, mgrs.GS, mgrs.Easting, mgrs.Northing);
                return true;
            }
            usng = null;
            return false;
        }
        public static bool TryParse(string input, out CoordinateMGRS mgrs)
        {
            mgrs = new CoordinateMGRS();

            if (string.IsNullOrWhiteSpace(input))
                return false;
            
            input = input.Trim();

            Regex regexMGRS = new Regex(@"^\s*(?<gzd>\d{1,2}[C-HJ-NP-X])[-,;:\s]*(?<gs1>[A-HJ-NP-Z]{1})(?<gs2>[A-HJ-NP-V]{1})[-,;:\s]*(?<numlocation>\d{0,10})[-,;:\s]*(?<numlocation2>\d{0,10})\s*");

            var matchMGRS = regexMGRS.Match(input);

            if(matchMGRS.Success && matchMGRS.Length == input.Length)
            {
                //if (ValidateNumericCoordinateMatch(matchMGRS, new string[] { "numlocation", "numlocation2" }))
                {
                    // need to validate the gzd and gs
                    try
                    {
                        mgrs.GZD = matchMGRS.Groups["gzd"].Value;
                        mgrs.GS = string.Format("{0}{1}",matchMGRS.Groups["gs1"].Value,matchMGRS.Groups["gs2"].Value);
                        var tempEN = string.Format("{0}{1}",matchMGRS.Groups["numlocation"].Value,matchMGRS.Groups["numlocation2"].Value);

                        if (tempEN.Length % 2 == 0 && tempEN.Length > 0)
                        {
                            int numSize = tempEN.Length / 2;
                            mgrs.Easting = Int32.Parse(tempEN.Substring(0, numSize));
                            mgrs.Northing = Int32.Parse(tempEN.Substring(numSize, numSize));
                        }
                        else
                        {
                            mgrs.Easting = 0;
                            mgrs.Northing = 0;
                        }
                    }
                    catch
                    {
                        return false;
                    }

                    return Validate(mgrs);
                }
            }

            return false;
        }
        public static bool TryParse(string input, out CoordinateUSNG usng)
        {
            CoordinateMGRS mgrs = new CoordinateMGRS();

            usng = new CoordinateUSNG(mgrs.GZD, mgrs.GS, mgrs.Easting, mgrs.Northing);

            if (string.IsNullOrWhiteSpace(input))
            {
                return(false);
            }

            if (CoordinateMGRS.TryParse(input, out mgrs))
            {
                usng = new CoordinateUSNG(mgrs.GZD, mgrs.GS, mgrs.Easting, mgrs.Northing);
                return(true);
            }
            usng = null;
            return(false);
        }
        private IPolygon GetMGRSPolygon(IPoint point)
        {
            CoordinateMGRS mgrs;

            IPointCollection pc = new RingClass();

            // bottom left
            CoordinateMGRS.TryParse(InputCoordinate, out mgrs);

            // don't create a polygon for 1m resolution
            if (mgrs.Easting.ToString().Length > 4 && mgrs.Northing.ToString().Length > 4)
                return null;

            var tempPoint = new PointClass() as IConversionNotation;
            (tempPoint as IPoint).SpatialReference = GetSR();
            var anotherMGRSstring = mgrs.ToString("", new CoordinateMGRSFormatter());
            tempPoint.PutCoordsFromMGRS(anotherMGRSstring, esriMGRSModeEnum.esriMGRSMode_Automatic);
            pc.AddPoint(tempPoint as IPoint);
            
            // top left
            var tempMGRS = new CoordinateMGRS(mgrs.GZD, mgrs.GS, mgrs.Easting, mgrs.Northing);
            var tempEasting = mgrs.Easting.ToString().PadRight(5,'0');
            tempMGRS.Easting = Convert.ToInt32(tempEasting);
            var tempNorthing = mgrs.Northing.ToString().PadRight(5,'9');
            tempMGRS.Northing = Convert.ToInt32(tempNorthing.Replace('0','9'));

            tempPoint = new PointClass() as IConversionNotation;
            (tempPoint as IPoint).SpatialReference = GetSR();
            anotherMGRSstring = tempMGRS.ToString("ZSX00000Y00000", new CoordinateMGRSFormatter());
            tempPoint.PutCoordsFromMGRS(anotherMGRSstring, esriMGRSModeEnum.esriMGRSMode_Automatic);
            pc.AddPoint(tempPoint as IPoint);

            // top right
            tempEasting = mgrs.Easting.ToString().PadRight(5,'9');
            tempMGRS.Easting = Convert.ToInt32(tempEasting.Replace('0', '9'));
            tempNorthing = mgrs.Northing.ToString().PadRight(5,'9');
            tempMGRS.Northing = Convert.ToInt32(tempNorthing.Replace('0', '9'));

            tempPoint = new PointClass() as IConversionNotation;
            (tempPoint as IPoint).SpatialReference = GetSR();
            tempPoint.PutCoordsFromMGRS(tempMGRS.ToString("ZSX00000Y00000", new CoordinateMGRSFormatter()), esriMGRSModeEnum.esriMGRSMode_Automatic);
            pc.AddPoint(tempPoint as IPoint);

            // bottom right
            tempEasting = mgrs.Easting.ToString().PadRight(5,'9');
            tempMGRS.Easting = Convert.ToInt32(tempEasting.Replace('0', '9'));
            tempNorthing = mgrs.Northing.ToString().PadRight(5,'0');
            tempMGRS.Northing = Convert.ToInt32(tempNorthing);

            tempPoint = new PointClass() as IConversionNotation;
            (tempPoint as IPoint).SpatialReference = GetSR();
            tempPoint.PutCoordsFromMGRS(tempMGRS.ToString("ZSX00000Y00000", new CoordinateMGRSFormatter()), esriMGRSModeEnum.esriMGRSMode_Automatic);
            pc.AddPoint(tempPoint as IPoint);

            // create polygon
            var poly = new PolygonClass();
            poly.SpatialReference = GetSR();
            poly.AddPointCollection(pc);
            poly.Close();

            return poly;
        }
        public void FormatterMGRS()
        {
            var coord = new CoordinateMGRS("17T", "PE", 83016, 60286);
            var temp = coord.ToString("ZSX00000Y00000", new CoordinateMGRSFormatter());
            Assert.AreEqual(temp, "17TPE8301660286");

            temp = coord.ToString("Z S X00000 Y00000", new CoordinateMGRSFormatter());
            Assert.AreEqual(temp, "17T PE 83016 60286");

            temp = coord.ToString("Z,S,X00000,Y00000", new CoordinateMGRSFormatter());
            Assert.AreEqual(temp, "17T,PE,83016,60286");
            
            temp = coord.ToString("Z-S-X00000-Y00000", new CoordinateMGRSFormatter());
            Assert.AreEqual(temp, "17T-PE-83016-60286");
            
            temp = coord.ToString("ZS X00000Y00000", new CoordinateMGRSFormatter());
            Assert.AreEqual(temp, "17TPE 8301660286");
            
            temp = coord.ToString("ZS X00000 Y00000", new CoordinateMGRSFormatter());
            Assert.AreEqual(temp, "17TPE 83016 60286");

            // test the default
            temp = coord.ToString("", new CoordinateMGRSFormatter());
            Assert.AreEqual(temp, "17TPE8301660286");
        }
        private void UpdateSample()
        {
            var type = GetCoordinateType();

            switch(type)
            {
                case CoordinateType.DD:
                    var dd = new CoordinateDD();

                    if (ctdict.ContainsKey(CoordinateType.DD))
                    {
                        CoordinateDD.TryParse(ctdict[type], out dd);
                    }

                    Sample = dd.ToString(Format, new CoordinateDDFormatter());

                    break;
                case CoordinateType.DDM:
                    var ddm = new CoordinateDDM();
                    
                    if(ctdict.ContainsKey(type))
                    {
                        CoordinateDDM.TryParse(ctdict[type], out ddm);
                    }

                    Sample = ddm.ToString(Format, new CoordinateDDMFormatter());
                    break;
                case CoordinateType.DMS:
                    var dms = new CoordinateDMS();

                    if(ctdict.ContainsKey(type))
                    {
                        CoordinateDMS.TryParse(ctdict[type], out dms);
                    }
                    Sample = dms.ToString(Format, new CoordinateDMSFormatter());
                    break;
                case CoordinateType.GARS:
                    var gars = new CoordinateGARS();

                    if(ctdict.ContainsKey(type))
                    {
                        CoordinateGARS.TryParse(ctdict[type], out gars);
                    }

                    Sample = gars.ToString(Format, new CoordinateGARSFormatter());
                    break;
                case CoordinateType.MGRS:
                    var mgrs = new CoordinateMGRS();

                    if(ctdict.ContainsKey(type))
                    {
                        CoordinateMGRS.TryParse(ctdict[type], out mgrs);
                    }

                    Sample = mgrs.ToString(Format, new CoordinateMGRSFormatter());
                    break;
                case CoordinateType.USNG:
                    var usng = new CoordinateUSNG();

                    if(ctdict.ContainsKey(type))
                    {
                        CoordinateUSNG.TryParse(ctdict[type], out usng);
                    }

                    Sample = usng.ToString(Format, new CoordinateMGRSFormatter());
                    break;
                case CoordinateType.UTM:
                    var utm = new CoordinateUTM();

                    if(ctdict.ContainsKey(type))
                    {
                        CoordinateUTM.TryParse(ctdict[type], out utm);
                    }

                    Sample = utm.ToString(Format, new CoordinateUTMFormatter());
                    break;
                default:
                    break;
            }

            RaisePropertyChanged(() => Sample);
        }
        public static bool Validate(CoordinateMGRS mgrs)
        {
            try
            {
                var zone = Convert.ToInt32(mgrs.GZD.Substring(0, mgrs.GZD.Length - 1));
                if (zone < 1 || zone > 60)
                    return false;
            }
            catch { return false; }

            return true;
        }