private double ParseCoordinate(string coordinate, CoordinateReferenceType type) { if (coordinate.Contains("'") && !coordinate.Contains("\"")) { throw new ArgumentException("Coordinate does not contain requisite formatting characters.", coordinate); } // Regex Patterns Regex noSeparator = new Regex(@"^\d{6,7}([\.,]\d+)?\W?[NSEW]$"); Regex degrees = new Regex(@"\d{1,3}"); Regex minutes = new Regex(@"\d{1,2}"); Regex seconds = new Regex(@"\d{1,2}([\.,]\d+)?"); Regex direction = new Regex(@"[NSEW]"); string DegreesString; string MinuteString; string SecondString; string DirectionString; Match DMS = noSeparator.Match(coordinate); // Try matching with no separators first if (DMS.Success) { DegreesString = coordinate.Substring(0, 2 + ((type == CoordinateReferenceType.Longitude) ? 1 : 0)); MinuteString = coordinate.Substring(DegreesString.Length, 2); SecondString = seconds.Match(coordinate, DegreesString.Length + MinuteString.Length).Value; DirectionString = direction.Match(coordinate).Value; } else { Match DegreesMatch = degrees.Match(coordinate); Match MinutesMatch = minutes.Match(coordinate, DegreesMatch.Index + DegreesMatch.Length); Match SecondsMatch = seconds.Match(coordinate, MinutesMatch.Index + MinutesMatch.Length); Match DirectionMatch = direction.Match(coordinate); DegreesString = DegreesMatch.Value; MinuteString = MinutesMatch.Value; SecondString = SecondsMatch.Value; DirectionString = DirectionMatch.Value; } int Degrees; int Minutes; double Seconds; int Direction = (DirectionString == "S" || DirectionString == "W") ? -1 : 1; double decimalDegrees; if (int.TryParse(DegreesString, out Degrees) && int.TryParse(MinuteString, out Minutes) && double.TryParse(SecondString, out Seconds)) { decimalDegrees = Direction * (Degrees + Minutes / 60d + Seconds / 3600d); } else { throw new ArgumentException("Unable to parse coorindate.", type.ToString()); } return(decimalDegrees); }
/// <summary> /// /// </summary> /// <param name="coordinate"></param> /// <param name="type"></param> /// <returns></returns> private double ParseCoordinate(string coordinate, CoordinateReferenceType type) { if (coordinate.Contains("\"")) { throw new ArgumentException("Coordinate contains invalid character.", coordinate); } // Regex Patterns Regex noSeparator = new Regex(@"^\d{4,5}(\.\d+)?\W?[NSEW]$"); Regex degrees = new Regex(@"\d{1,3}"); Regex minutes = new Regex(@"\d{1,2}(\.\d+)?"); Regex direction = new Regex(@"[NSEW]"); string DegreesString; string MinuteString; string DirectionString; Match DM = noSeparator.Match(coordinate); // Try matching with no separators first if (DM.Success) { DegreesString = coordinate.Substring(0, 2 + ((type == CoordinateReferenceType.Longitude) ? 1 : 0)); MinuteString = minutes.Match(coordinate, DegreesString.Length).Value; DirectionString = direction.Match(coordinate).Value; } else { Match DegreesMatch = degrees.Match(coordinate); Match MinutesMatch = minutes.Match(coordinate, DegreesMatch.Index + DegreesMatch.Length); Match DirectionMatch = direction.Match(coordinate); DegreesString = DegreesMatch.Value; MinuteString = MinutesMatch.Value; DirectionString = DirectionMatch.Value; } int Degrees; double Minutes; int Direction = (DirectionString == "S" || DirectionString == "W") ? -1 : 1; double DecimalDegrees; if (int.TryParse(DegreesString, out Degrees) && double.TryParse(MinuteString, out Minutes)) { DecimalDegrees = Direction * (Degrees + Minutes / 60d); } else { throw new ArgumentException("Unable to parse coorindate.", type.ToString()); } return(DecimalDegrees); }
/// <summary> /// /// </summary> /// <param name="coordinate"></param> /// <param name="type"></param> /// <returns></returns> private double ParseCoordinate(string coordinate, CoordinateReferenceType type) { if (coordinate.Contains("\"")) { throw new ArgumentException("Coordinate contains invalid character.", coordinate); } // Regex Patterns Regex noSeparator = new Regex(@"^\d{4,5}(\.\d+)?\W?[NSEW]$"); Regex degrees = new Regex(@"\d{1,3}"); Regex minutes = new Regex(@"\d{1,2}(\.\d+)?"); Regex direction = new Regex(@"[NSEW]"); string DegreesString; string MinuteString; string DirectionString; Match DM = noSeparator.Match(coordinate); // Try matching with no separators first if (DM.Success) { DegreesString = coordinate.Substring(0, 2 + ((type == CoordinateReferenceType.Longitude) ? 1 : 0)); MinuteString = minutes.Match(coordinate, DegreesString.Length).Value; DirectionString = direction.Match(coordinate).Value; } else { Match DegreesMatch = degrees.Match(coordinate); Match MinutesMatch = minutes.Match(coordinate, DegreesMatch.Index + DegreesMatch.Length); Match DirectionMatch = direction.Match(coordinate); DegreesString = DegreesMatch.Value; MinuteString = MinutesMatch.Value; DirectionString = DirectionMatch.Value; } int Degrees; double Minutes; int Direction = (DirectionString == "S" || DirectionString == "W") ? -1 : 1; double DecimalDegrees; if (int.TryParse(DegreesString, out Degrees) && double.TryParse(MinuteString, out Minutes)) { DecimalDegrees = Direction * (Degrees + Minutes / 60d); } else { throw new ArgumentException("Unable to parse coorindate.", type.ToString()); } return DecimalDegrees; }
private double ParseCoordinate(string coordinate, CoordinateReferenceType type) { if (coordinate.Contains("'") && !coordinate.Contains("\"")) { throw new ArgumentException("Coordinate does not contain requisite formatting characters.", coordinate); } // Regex Patterns Regex noSeparator = new Regex(@"^\d{6,7}(\.\d+)?\W?[NSEW]$"); Regex degrees = new Regex(@"\d{1,3}"); Regex minutes = new Regex(@"\d{1,2}"); Regex seconds = new Regex(@"\d{1,2}(\.\d+)?"); Regex direction = new Regex(@"[NSEW]"); string DegreesString; string MinuteString; string SecondString; string DirectionString; Match DMS = noSeparator.Match(coordinate); // Try matching with no separators first if (DMS.Success) { DegreesString = coordinate.Substring(0, 2 + ((type == CoordinateReferenceType.Longitude) ? 1 : 0)); MinuteString = coordinate.Substring(DegreesString.Length, 2); SecondString = seconds.Match(coordinate, DegreesString.Length + MinuteString.Length).Value; DirectionString = direction.Match(coordinate).Value; } else { Match DegreesMatch = degrees.Match(coordinate); Match MinutesMatch = minutes.Match(coordinate, DegreesMatch.Index + DegreesMatch.Length); Match SecondsMatch = seconds.Match(coordinate, MinutesMatch.Index + MinutesMatch.Length); Match DirectionMatch = direction.Match(coordinate); DegreesString = DegreesMatch.Value; MinuteString = MinutesMatch.Value; SecondString = SecondsMatch.Value; DirectionString = DirectionMatch.Value; } int Degrees; int Minutes; double Seconds; int Direction = (DirectionString == "S" || DirectionString == "W") ? -1 : 1; double decimalDegrees; if (int.TryParse(DegreesString, out Degrees) && int.TryParse(MinuteString, out Minutes) && double.TryParse(SecondString, out Seconds)) { decimalDegrees = Direction * (Degrees + Minutes / 60d + Seconds / 3600d); } else { throw new ArgumentException("Unable to parse coorindate.", type.ToString()); } return decimalDegrees; }