public static bool Validate(CoordinateGARS gars) { if (gars.LonBand < 1 || gars.LonBand > 720) { return(false); } Regex regexGARS = new Regex(@"^\s*(?<latband1>[A-HJ-NP-Q]{1}?)(?<latband2>[A-HJ-NP-Z]{1}?)\s*"); var matchGARS = regexGARS.Match(gars.LatBand); if (!matchGARS.Success) { return(false); } if (gars.Quadrant < 1 || gars.Quadrant > 4) { return(false); } if (gars.Key < 1 || gars.Key > 9) { return(false); } return(true); }
public static bool TryParse(string input, out CoordinateGARS gars) { gars = new CoordinateGARS(); if (string.IsNullOrWhiteSpace(input)) { return(false); } input = input.Trim(); Regex regexGARS = new Regex(@"^\s*(?<lonband>\d{3})[-,;:\s]*(?<latband1>[A-HJ-NP-Q]{1}?)(?<latband2>[A-HJ-NP-Z]{1}?)[-,;:\s]*(?<quadrant>\d?)[-,;:\s]*(?<key>\d?)\s*"); var matchGARS = regexGARS.Match(input); if (matchGARS.Success && matchGARS.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchGARS, new string[] { "lonband", "quadrant", "key" })) { // need to validate the latband try { gars.LonBand = Int32.Parse(matchGARS.Groups["lonband"].Value); gars.LatBand = string.Format("{0}{1}", matchGARS.Groups["latband1"].Value, matchGARS.Groups["latband2"].Value); gars.Quadrant = Int32.Parse(matchGARS.Groups["quadrant"].Value); gars.Key = Int32.Parse(matchGARS.Groups["key"].Value); } catch { /* Conversion Failed */ return(false); } return(Validate(gars)); } } return(false); }
public static bool TryParse(string input, out CoordinateGARS gars) { gars = new CoordinateGARS(); if (string.IsNullOrWhiteSpace(input)) return false; input = input.Trim(); Regex regexGARS = new Regex(@"^\s*(?<lonband>\d{3})[-,;:\s]*(?<latband1>[A-HJ-NP-Q]{1}?)(?<latband2>[A-HJ-NP-Z]{1}?)[-,;:\s]*(?<quadrant>\d?)[-,;:\s]*(?<key>\d?)\s*"); var matchGARS = regexGARS.Match(input); if (matchGARS.Success && matchGARS.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchGARS, new string[] { "lonband", "quadrant", "key" })) { // need to validate the latband try { gars.LonBand = Int32.Parse(matchGARS.Groups["lonband"].Value); gars.LatBand = string.Format("{0}{1}",matchGARS.Groups["latband1"].Value,matchGARS.Groups["latband2"].Value); gars.Quadrant = Int32.Parse(matchGARS.Groups["quadrant"].Value); gars.Key = Int32.Parse(matchGARS.Groups["key"].Value); } catch { return false; } return Validate(gars); } } return false; }
public static bool Validate(CoordinateGARS gars) { if (gars.LonBand < 1 || gars.LonBand > 720) return false; Regex regexGARS = new Regex(@"^\s*(?<latband1>[A-HJ-NP-Q]{1}?)(?<latband2>[A-HJ-NP-Z]{1}?)\s*"); var matchGARS = regexGARS.Match(gars.LatBand); if (!matchGARS.Success) return false; if (gars.Quadrant < 1 || gars.Quadrant > 4) return false; if (gars.Key < 1 || gars.Key > 9) return false; return true; }
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); }
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); }