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 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; }
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 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 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; }