CoordinateConversionLibrary.Models.CoordinateUTM.TryParse C# (CSharp) Method

TryParse() public static method

public static TryParse ( string input, CoordinateUTM &utm ) : bool
input string
utm CoordinateUTM
return bool
        public static bool TryParse(string input, out CoordinateUTM utm)
        {
            utm = new CoordinateUTM();

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

            input = input.Trim();

            Regex regexUTM = new Regex(@"^\s*(?<zone>\d{1,2})(?<hemi>[NS]?)[-,;:\sm]*(?<easting>\d{1,9})[-,;:\sm]*(?<northing>\d{1,9})[-,;:\sm]*");

            var matchUTM = regexUTM.Match(input);

            if (matchUTM.Success && matchUTM.Length == input.Length)
            {
                if (ValidateNumericCoordinateMatch(matchUTM, new string[] { "zone","easting","northing" }))
                {
                    // need to validate the gzd and gs
                    try
                    {
                        utm.Zone = Int32.Parse(matchUTM.Groups["zone"].Value);
                        utm.Easting = Int32.Parse(matchUTM.Groups["easting"].Value);
                        utm.Northing = Int32.Parse(matchUTM.Groups["northing"].Value);
                        utm.Hemi = matchUTM.Groups["hemi"].Value;
                    }
                    catch
                    {
                        return false;
                    }

                    return Validate(utm);
                }
            }

            return false;
        }

Usage Example

        public static string GetFormattedCoord(CoordinateType cType, string coord, string format)
        {
            if (cType == CoordinateType.DD)
            {
                CoordinateDD dd;
                if (CoordinateDD.TryParse(coord, out dd, true))
                {
                    return(dd.ToString(format, new CoordinateDDFormatter()));
                }
            }
            if (cType == CoordinateType.DDM)
            {
                CoordinateDDM ddm;
                if (CoordinateDDM.TryParse(coord, out ddm, true))
                {
                    return(ddm.ToString(format, new CoordinateDDMFormatter()));
                }
            }
            if (cType == CoordinateType.DMS)
            {
                CoordinateDMS dms;
                if (CoordinateDMS.TryParse(coord, out dms, true))
                {
                    return(dms.ToString(format, new CoordinateDMSFormatter()));
                }
            }
            if (cType == CoordinateType.GARS)
            {
                CoordinateGARS gars;
                if (CoordinateGARS.TryParse(coord, out gars))
                {
                    return(gars.ToString(format, new CoordinateGARSFormatter()));
                }
            }
            if (cType == CoordinateType.MGRS)
            {
                CoordinateMGRS mgrs;
                if (CoordinateMGRS.TryParse(coord, out mgrs))
                {
                    return(mgrs.ToString(format, new CoordinateMGRSFormatter()));
                }
            }
            if (cType == CoordinateType.USNG)
            {
                CoordinateUSNG usng;
                if (CoordinateUSNG.TryParse(coord, out usng))
                {
                    return(usng.ToString(format, new CoordinateMGRSFormatter()));
                }
            }
            if (cType == CoordinateType.UTM)
            {
                CoordinateUTM utm;
                if (CoordinateUTM.TryParse(coord, out utm))
                {
                    return(utm.ToString(format, new CoordinateUTMFormatter()));
                }
            }

            return(null);
        }