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> /// Parses a Degrees, Decimal Minutes coordinate to a string. /// </summary> /// <param name="coordinate">Coordinate</param> /// <param name="type">Coordinate Type</param> /// <returns>A string of Degrees, Decimal Minutes</returns> private string ParseToDegreesDecimalMinutes(double coordinate, CoordinateReferenceType type) { char Direction; if (type == CoordinateReferenceType.Longitude) { Direction = (coordinate < 0) ? 'W' : 'E'; } else { Direction = (coordinate < 0) ? 'S' : 'N'; } coordinate = Math.Abs(coordinate); int Degrees = (int)(coordinate); double Minutes = (coordinate - Degrees) * 60; return(Degrees.ToString("00" + ((type == CoordinateReferenceType.Longitude) ? "0" : "")) + "°" + DecimalFormat.Format(Minutes, 5) + "'" + Direction); }
/// <summary> /// Parses a Degrees, Minutes, Seconds coordinate to a string. /// </summary> /// <param name="coordinate">Coordinate</param> /// <param name="type">Coordinate Type</param> /// <returns>A string of Degrees, Minutes, Seconds</returns> private string ParseToDegreesMinutesSeconds(double coordinate, CoordinateReferenceType type) { char Direction; if (type == CoordinateReferenceType.Longitude) { Direction = (coordinate < 0) ? 'W' : 'E'; } else { Direction = (coordinate < 0) ? 'S' : 'N'; } coordinate = Math.Abs(coordinate); int Degrees = (int)(coordinate); int Minutes = (int)((coordinate - Degrees) * 60); double Seconds = ((coordinate - Degrees) * 60 - Minutes) * 60; // return Degrees.ToString("00" + ((type == CoordinateReferenceType.Longitude) ? "0" : "")) + "°" + Minutes.ToString("00") + "'" + Seconds.ToString("00.##") + "\"" + Direction; // Above make longitude degrees 3 places and latitude 2; do neither and // only show what is needed (match Google Maps output) return(Degrees.ToString() + "°" + Minutes.ToString("00") + "'" + DecimalFormat.Format(Seconds, 2) + "\"" + Direction); }
/// <summary> /// Parses a Degrees, Minutes, Seconds coordinate to a string. /// </summary> /// <param name="coordinate">Coordinate</param> /// <param name="type">Coordinate Type</param> /// <returns>A string of Degrees, Minutes, Seconds</returns> private string ParseToDegreesMinutesSeconds(double coordinate, CoordinateReferenceType type) { char Direction; if (type == CoordinateReferenceType.Longitude) { Direction = (coordinate < 0) ? 'W' : 'E'; } else { Direction = (coordinate < 0) ? 'S' : 'N'; } coordinate = Math.Abs(coordinate); int Degrees = (int)(coordinate); int Minutes = (int)((coordinate - Degrees) * 60); double Seconds = ((coordinate - Degrees) * 60 - Minutes) * 60; return Degrees.ToString("00" + ((type == CoordinateReferenceType.Longitude) ? "0" : "")) + "°" + Minutes.ToString("00") + "'" + Seconds.ToString("00.##") + "\"" + Direction; }
/// <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; }