public static bool TryParse(string input, out CoordinateDDM ddm) { ddm = new CoordinateDDM(); if (string.IsNullOrWhiteSpace(input)) { return(false); } input = input.Trim(); Regex regexDDM = new Regex(@"^\s*[+]*(?<latitudeSuffix>[NS])?(?<latitudeD>[^NSDd*°,:\s]*)?[Dd*°,:\s]*(?<latitudeM>[^NS',:\s]*)?[',:\s]*(?<latitudeSuffix>[NS])? *[+,]*(?<longitudeSuffix>[EW])?(?<longitudeD>[^EWDd*°,:\s]*)?[Dd*°,:\s]*(?<longitudeM>[^EW',:\s]*)?[',:\s]*(?<longitudeSuffix>[EW])?"); var matchDDM = regexDDM.Match(input); if (matchDDM.Success && matchDDM.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchDDM, new string[] { "latitudeD", "latitudeM", "longitudeD", "longitudeM" })) { try { var LatDegrees = int.Parse(matchDDM.Groups["latitudeD"].Value); var LatMinutes = double.Parse(matchDDM.Groups["latitudeM"].Value); var LonDegrees = int.Parse(matchDDM.Groups["longitudeD"].Value); var LonMinutes = double.Parse(matchDDM.Groups["longitudeM"].Value); var temp = matchDDM.Groups["latitudeSuffix"]; if (temp.Success && temp.Value.ToUpper().Equals("S")) { LatDegrees = Math.Abs(LatDegrees) * -1; } temp = matchDDM.Groups["longitudeSuffix"]; if (temp.Success && temp.Value.ToUpper().Equals("W")) { LonDegrees = Math.Abs(LonDegrees) * -1; } ddm = new CoordinateDDM(LatDegrees, LatMinutes, LonDegrees, LonMinutes); } catch { return(false); } return(true); } } return(false); }
public static bool TryParse(string input, out CoordinateDDM ddm) { ddm = new CoordinateDDM(); if (string.IsNullOrWhiteSpace(input)) return false; input = input.Trim(); Regex regexDDM = new Regex(@"^\s*[+]*(?<latitudeSuffix>[NS])?(?<latitudeD>[^NSDd*°,:\s]*)?[Dd*°,:\s]*(?<latitudeM>[^NS',:\s]*)?[',:\s]*(?<latitudeSuffix>[NS])? *[+,]*(?<longitudeSuffix>[EW])?(?<longitudeD>[^EWDd*°,:\s]*)?[Dd*°,:\s]*(?<longitudeM>[^EW',:\s]*)?[',:\s]*(?<longitudeSuffix>[EW])?"); var matchDDM = regexDDM.Match(input); if (matchDDM.Success && matchDDM.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchDDM, new string[] { "latitudeD", "latitudeM", "longitudeD", "longitudeM" })) { try { var LatDegrees = int.Parse(matchDDM.Groups["latitudeD"].Value); var LatMinutes = double.Parse(matchDDM.Groups["latitudeM"].Value); var LonDegrees = int.Parse(matchDDM.Groups["longitudeD"].Value); var LonMinutes = double.Parse(matchDDM.Groups["longitudeM"].Value); var temp = matchDDM.Groups["latitudeSuffix"]; if (temp.Success && temp.Value.ToUpper().Equals("S")) { LatDegrees = Math.Abs(LatDegrees) * -1; } temp = matchDDM.Groups["longitudeSuffix"]; if (temp.Success && temp.Value.ToUpper().Equals("W")) { LonDegrees = Math.Abs(LonDegrees) * -1; } ddm = new CoordinateDDM(LatDegrees, LatMinutes, LonDegrees, LonMinutes); } catch { return false; } return true; } } return false; }
// use base CanGetDD public override bool CanGetDDM(int srFactoryCode, out string coord) { coord = string.Empty; if(base.CanGetDDM(srFactoryCode, out coord)) { return true; } else { if(base.CanGetDD(srFactoryCode, out coord)) { // convert dd to ddm CoordinateDD dd; if(CoordinateDD.TryParse(coord, out dd)) { var ddm = new CoordinateDDM(dd); coord = ddm.ToString("", new CoordinateDDMFormatter()); return true; } } } return false; }
public static bool TryParse(string input, out CoordinateDDM ddm) { ddm = new CoordinateDDM(); if (string.IsNullOrWhiteSpace(input)) { return(false); } input = input.Trim(); string numSep = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator; input = numSep != "." ? input.Replace(".", numSep) : input; Regex regexDDM = new Regex(@"^(?i) *[+]*(?<firstPrefix>[NSEW])?(?<latitudeD>\-*\d{1,3})?[°˚º^~*\s\-_]+(?<latitudeM>(?=\d+[.,:]\d+)\d+[.,:]\d*|\d+)['′]*(?<firstSuffix>[NSEW])?[,\s|\/\\]*[+]*(?<lastPrefix>[NSEW])?(?<longitudeD>\-*\d{1,3})?[°˚º^~*\s\-_]+(?<longitudeM>\d+[.,:]?\d*)['′]*(?<lastSuffix>[NSEW])?"); var matchDDM = regexDDM.Match(input); if (matchDDM.Success && matchDDM.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchDDM, new string[] { "latitudeD", "latitudeM", "longitudeD", "longitudeM" })) { try { var firstPrefix = matchDDM.Groups["firstPrefix"]; var firstSuffix = matchDDM.Groups["firstSuffix"]; var lastPrefix = matchDDM.Groups["lastPrefix"]; var lastSuffix = matchDDM.Groups["lastSuffix"]; // Don't allow both prefix and suffix for lat or lon if (firstPrefix.Success && firstSuffix.Success) { return(false); } if (lastPrefix.Success && lastSuffix.Success) { return(false); } // Don't allow same prefix/suffix for both lat and lon if ((firstSuffix.Success || firstPrefix.Success) && (firstSuffix.Value.ToUpper().Equals("E") || firstPrefix.Value.ToUpper().Equals("E") || firstSuffix.Value.ToUpper().Equals("W") || firstPrefix.Value.ToUpper().Equals("W")) && (lastSuffix.Success || lastPrefix.Success) && (lastSuffix.Value.ToUpper().Equals("E") || lastPrefix.Value.ToUpper().Equals("E") || lastSuffix.Value.ToUpper().Equals("W") || lastPrefix.Value.ToUpper().Equals("W"))) { return(false); } if ((firstSuffix.Success || firstPrefix.Success) && (firstSuffix.Value.ToUpper().Equals("N") || firstPrefix.Value.ToUpper().Equals("N") || firstSuffix.Value.ToUpper().Equals("S") || firstPrefix.Value.ToUpper().Equals("S")) && (lastSuffix.Success || lastPrefix.Success) && (lastSuffix.Value.ToUpper().Equals("N") || lastPrefix.Value.ToUpper().Equals("N") || lastSuffix.Value.ToUpper().Equals("S") || lastPrefix.Value.ToUpper().Equals("S"))) { return(false); } var LatDegrees = int.Parse(matchDDM.Groups["latitudeD"].Value); var LatMinutes = double.Parse(matchDDM.Groups["latitudeM"].Value); var LonDegrees = int.Parse(matchDDM.Groups["longitudeD"].Value); var LonMinutes = double.Parse(matchDDM.Groups["longitudeM"].Value); // if E/W is in first coordinate or N/S is in second coordinate then flip the lat/lon values if ((firstSuffix.Success || firstPrefix.Success) && (firstSuffix.Value.ToUpper().Equals("E") || firstPrefix.Value.ToUpper().Equals("E") || firstSuffix.Value.ToUpper().Equals("W") || firstPrefix.Value.ToUpper().Equals("W")) || (lastSuffix.Success || lastPrefix.Success) && (lastSuffix.Value.ToUpper().Equals("N") || lastPrefix.Value.ToUpper().Equals("N") || lastSuffix.Value.ToUpper().Equals("S") || lastPrefix.Value.ToUpper().Equals("S"))) { LatDegrees = int.Parse(matchDDM.Groups["longitudeD"].Value); LatMinutes = double.Parse(matchDDM.Groups["longitudeM"].Value); LonDegrees = int.Parse(matchDDM.Groups["latitudeD"].Value); LonMinutes = double.Parse(matchDDM.Groups["latitudeM"].Value); } // no suffix or prefix was added so allow user to specify longitude first by checking for absolute value greater than 90 // fix for bug Bob Booth found in issue #42 if (!firstPrefix.Success && !firstSuffix.Success && !lastPrefix.Success && !lastSuffix.Success) { // switch the values if longitude was added first if ((Math.Abs(LatDegrees) > 90.0) && (Math.Abs(LonDegrees) <= 90.0)) { LatDegrees = int.Parse(matchDDM.Groups["longitudeD"].Value); LatMinutes = double.Parse(matchDDM.Groups["longitudeM"].Value); LonDegrees = int.Parse(matchDDM.Groups["latitudeD"].Value); LonMinutes = double.Parse(matchDDM.Groups["latitudeM"].Value); } if ((Math.Abs(LatDegrees) > 90.0) && (Math.Abs(LonDegrees) > 90.0)) { return(false); } } if ((firstSuffix.Success || firstPrefix.Success) && (firstSuffix.Value.ToUpper().Equals("S") || firstPrefix.Value.ToUpper().Equals("S")) || (lastSuffix.Success || lastPrefix.Success) && (lastSuffix.Value.ToUpper().Equals("S") || lastPrefix.Value.ToUpper().Equals("S"))) { LatDegrees = Math.Abs(LatDegrees) * -1; } if ((firstSuffix.Success || firstPrefix.Success) && (firstSuffix.Value.ToUpper().Equals("W") || firstPrefix.Value.ToUpper().Equals("W")) || (lastSuffix.Success || lastPrefix.Success) && (lastSuffix.Value.ToUpper().Equals("W") || lastPrefix.Value.ToUpper().Equals("W"))) { LonDegrees = Math.Abs(LonDegrees) * -1; } ddm = new CoordinateDDM(LatDegrees, LatMinutes, LonDegrees, LonMinutes); } catch { return(false); } return(true); } } return(false); }
public static bool TryParse(string input, out CoordinateDDM ddm, bool displayAmbiguousCoordsDlg = false) { ddm = new CoordinateDDM(); if (string.IsNullOrWhiteSpace(input)) { return(false); } input = input.Trim(); string numSep = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator; input = numSep != "." ? input.Replace(".", numSep) : input; Regex regexDDMLat = new Regex(@"^((?<firstPrefix>[\+\-NnSs])?(?<latitudeD>[0-8]?\d|90)[°˚º^~*\s\-_]+(?<latitudeM>([0-5]?\d|\d)([.,:]\d*)?)['′\s_]*(?<firstSuffix>[\+\-NnSs])?)([,:;\s|\/\\]+)((?<lastPrefix>[\+\-EeWw])?(?<longitudeD>[0]?\d?\d|1[0-7]\d|180)[°˚º^~*\s\-_]+(?<longitudeM>([0-5]\d|\d)([.,:]\d*)?)['′\s_]*(?<lastSuffix>[\+\-EeWw])?)[\s]*$"); Regex regexDDMLon = new Regex(@"^((?<firstPrefix>[\+\-EeWw])?(?<longitudeD>[0]?\d?\d|1[0-7]\d|180)[°˚º^~*\s\-_]+(?<longitudeM>([0-5]\d|\d)([.,:]\d*)?)['′\s_]*(?<firstSuffix>[\+\-EeWw])?)([,:;\s|\/\\]+)((?<lastPrefix>[\+\-NnSs])?(?<latitudeD>[0-8]?\d|90)[°˚º^~*\s\-_]+(?<latitudeM>([0-5]?\d|\d)([.,:]\d*)?)['′\s_]*(?<lastSuffix>[\+\-NnSs])?)[\s]*$"); var matchDDMLat = regexDDMLat.Match(input); var matchDDMLon = regexDDMLon.Match(input); bool blnMatchDDMLat = matchDDMLat.Success; int LatDegrees = -1, LonDegrees = -1; double LatMinutes = -1, LonMinutes = -1; Group firstPrefix = null, firstSuffix = null, lastPrefix = null, lastSuffix = null; // Ambiguous coordinate, could be both lat/lon && lon/lat if (matchDDMLat.Success && matchDDMLat.Length == input.Length && matchDDMLon.Success && matchDDMLon.Length == input.Length) { if (CoordinateConversionLibraryConfig.AddInConfig.DisplayAmbiguousCoordsDlg && displayAmbiguousCoordsDlg) { double latValue = -1, longValue = -1; if (matchDDMLat.Success && matchDDMLat.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchDDMLat, new string[] { "latitudeD", "latitudeM", "longitudeD", "longitudeM" })) { latValue = Double.Parse(matchDDMLat.Groups["latitude"].Value); longValue = Double.Parse(matchDDMLat.Groups["longitude"].Value); } } else if (matchDDMLon.Success && matchDDMLon.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchDDMLon, new string[] { "latitudeD", "latitudeM", "longitudeD", "longitudeM" })) { latValue = Double.Parse(matchDDMLon.Groups["latitudeD"].Value); longValue = Double.Parse(matchDDMLon.Groups["longitudeD"].Value); } } else { return(false); } if (latValue < 90 && longValue < 90) { ShowAmbiguousDialog(); } } blnMatchDDMLat = CoordinateConversionLibraryConfig.AddInConfig.isLatLong; } // Lat/Lon if (matchDDMLat.Success && matchDDMLat.Length == input.Length && blnMatchDDMLat) { if (ValidateNumericCoordinateMatch(matchDDMLat, new string[] { "latitudeD", "latitudeM", "longitudeD", "longitudeM" })) { LatDegrees = int.Parse(matchDDMLat.Groups["latitudeD"].Value); LatMinutes = double.Parse(matchDDMLat.Groups["latitudeM"].Value); LonDegrees = int.Parse(matchDDMLat.Groups["longitudeD"].Value); LonMinutes = double.Parse(matchDDMLat.Groups["longitudeM"].Value); firstPrefix = matchDDMLat.Groups["firstPrefix"]; firstSuffix = matchDDMLat.Groups["firstSuffix"]; lastPrefix = matchDDMLat.Groups["lastPrefix"]; lastSuffix = matchDDMLat.Groups["lastSuffix"]; blnMatchDDMLat = true; } else { return(false); } } // Lon/Lat else if (matchDDMLon.Success && matchDDMLon.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchDDMLon, new string[] { "latitudeD", "latitudeM", "longitudeD", "longitudeM" })) { LatDegrees = int.Parse(matchDDMLon.Groups["latitudeD"].Value); LatMinutes = double.Parse(matchDDMLon.Groups["latitudeM"].Value); LonDegrees = int.Parse(matchDDMLon.Groups["longitudeD"].Value); LonMinutes = double.Parse(matchDDMLon.Groups["longitudeM"].Value); firstPrefix = matchDDMLon.Groups["firstPrefix"]; firstSuffix = matchDDMLon.Groups["firstSuffix"]; lastPrefix = matchDDMLon.Groups["lastPrefix"]; lastSuffix = matchDDMLon.Groups["lastSuffix"]; } else { return(false); } } else { return(false); } // Don't allow both prefix and suffix for lat or lon if (firstPrefix.ValidatePrefix(ShowHyphen, ShowPlus) && firstSuffix.Success) { return(false); } if (lastPrefix.ValidatePrefix(ShowHyphen, ShowPlus) && lastSuffix.Success) { return(false); } if ((firstSuffix.Success || firstPrefix.ValidatePrefix(ShowHyphen, ShowPlus)) && (firstSuffix.Value.ToUpper().Equals("S") || firstPrefix.Value.ToUpper().Equals("S")) || (lastSuffix.Success || lastPrefix.ValidatePrefix(ShowHyphen, ShowPlus)) && (lastSuffix.Value.ToUpper().Equals("S") || lastPrefix.Value.ToUpper().Equals("S"))) { LatDegrees = Math.Abs(LatDegrees) * -1; } if ((firstSuffix.Success || firstPrefix.ValidatePrefix(ShowHyphen, ShowPlus)) && (firstSuffix.Value.ToUpper().Equals("W") || firstPrefix.Value.ToUpper().Equals("W")) || (lastSuffix.Success || lastPrefix.ValidatePrefix(ShowHyphen, ShowPlus)) && (lastSuffix.Value.ToUpper().Equals("W") || lastPrefix.Value.ToUpper().Equals("W"))) { LonDegrees = Math.Abs(LonDegrees) * -1; } if ((firstSuffix.Success || firstPrefix.ValidatePrefix(ShowHyphen, ShowPlus)) && (firstSuffix.Value.ToUpper().Equals("-") || firstPrefix.Value.ToUpper().Equals("-"))) { if (blnMatchDDMLat) { LatDegrees = Math.Abs(LatDegrees) * -1; } else { LonDegrees = Math.Abs(LonDegrees) * -1; } } if ((lastSuffix.Success || lastPrefix.ValidatePrefix(ShowHyphen, ShowPlus)) && (lastSuffix.Value.ToUpper().Equals("-") || lastPrefix.Value.ToUpper().Equals("-"))) { if (blnMatchDDMLat) { LonDegrees = Math.Abs(LonDegrees) * -1; } else { LatDegrees = Math.Abs(LatDegrees) * -1; } } ddm = new CoordinateDDM(LatDegrees, LatMinutes, LonDegrees, LonMinutes); return(true); }
public CoordinateDD(CoordinateDDM ddm) { Lat = (double)ddm.LatDegrees + (ddm.LatMinutes / 60.0); Lon = (double)ddm.LonDegrees + (ddm.LonMinutes / 60.0); }
public CoordinateDD(CoordinateDDM ddm) { Lat = (Math.Abs((double)ddm.LatDegrees) + (ddm.LatMinutes / 60.0)) * ((ddm.LatDegrees < 0) ? -1.0 : 1.0); Lon = (Math.Abs((double)ddm.LonDegrees) + (ddm.LonMinutes / 60.0)) * ((ddm.LonDegrees < 0) ? -1.0 : 1.0); }
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); }
public CoordinateDD(CoordinateDDM ddm) { Lat = (double)ddm.LatDegrees + (ddm.LatMinutes / 60.0); Lon = (double)ddm.LonDegrees + (ddm.LonMinutes / 60.0); }
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); }