public CoordinateDDM(CoordinateDD dd) { LatDegrees = (int)Math.Truncate(dd.Lat); LatMinutes = Math.Abs(dd.Lat - Math.Truncate(dd.Lat)) * 60.0; LonDegrees = (int)Math.Truncate(dd.Lon); LonMinutes = Math.Abs(dd.Lon - Math.Truncate(dd.Lon)) * 60.0; }
public CoordinateDDM(CoordinateDD dd) { LatDegrees = (int)Math.Truncate(dd.Lat); LatMinutes = (dd.Lat - Math.Truncate(dd.Lat)) * 60.0; LonDegrees = (int)Math.Truncate(dd.Lon); LonMinutes = (dd.Lon - Math.Truncate(dd.Lon)) * 60.0; }
public CoordinateDMS(CoordinateDD dd) { LatDegrees = (int)Math.Truncate(dd.Lat); double latm = (dd.Lat - Math.Truncate(dd.Lat)) * 60.0; LatMinutes = (int)Math.Truncate(latm); LatSeconds = (latm - LatMinutes) * 60.0; LonDegrees = (int)Math.Truncate(dd.Lon); double lonm = (dd.Lon - Math.Truncate(dd.Lon)) * 60.0; LonMinutes = (int)Math.Truncate(lonm); LonSeconds = (lonm - LonMinutes) * 60.0; }
public static bool TryParse(string input, out CoordinateDD coord) { coord = new CoordinateDD(); if (string.IsNullOrWhiteSpace(input)) { return(false); } input = input.Trim(); Regex regexDD = new Regex(@"^ *[+]*(?<latitudeSuffix>[NS])?(?<latitude>[^NSDd*° ,]*)?[Dd*° ,]*(?<latitudeSuffix>[NS])?[+,;:\s]*(?<longitudeSuffix>[EW])?(?<longitude>[^EWDd*° ]*)?[Dd*° ]*(?<longitudeSuffix>[EW])?"); var matchDD = regexDD.Match(input); if (matchDD.Success && matchDD.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchDD, new string[] { "latitude", "longitude" })) { try { coord.Lat = Double.Parse(matchDD.Groups["latitude"].Value); coord.Lon = Double.Parse(matchDD.Groups["longitude"].Value); var temp = matchDD.Groups["latitudeSuffix"]; if (temp.Success && temp.Value.ToUpper().Equals("S")) { coord.Lat = Math.Abs(coord.Lat) * -1; } temp = matchDD.Groups["longitudeSuffix"]; if (temp.Success && temp.Value.ToUpper().Equals("W")) { coord.Lon = Math.Abs(coord.Lon) * -1; } } catch { return(false); } return(true); } } return(false); }
public static bool TryParse(string input, out CoordinateDD coord) { coord = new CoordinateDD(); if (string.IsNullOrWhiteSpace(input)) return false; input = input.Trim(); Regex regexDD = new Regex(@"^ *[+]*(?<latitudeSuffix>[NS])?(?<latitude>[^NSDd*° ,]*)?[Dd*° ,]*(?<latitudeSuffix>[NS])?[+,;:\s]*(?<longitudeSuffix>[EW])?(?<longitude>[^EWDd*° ]*)?[Dd*° ]*(?<longitudeSuffix>[EW])?"); var matchDD = regexDD.Match(input); if (matchDD.Success && matchDD.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchDD, new string[] { "latitude", "longitude" })) { try { coord.Lat = Double.Parse(matchDD.Groups["latitude"].Value); coord.Lon = Double.Parse(matchDD.Groups["longitude"].Value); var temp = matchDD.Groups["latitudeSuffix"]; if (temp.Success && temp.Value.ToUpper().Equals("S")) { coord.Lat = Math.Abs(coord.Lat) * -1; } temp = matchDD.Groups["longitudeSuffix"]; if (temp.Success && temp.Value.ToUpper().Equals("W")) { coord.Lon = Math.Abs(coord.Lon) * -1; } } catch { return false; } return true; } } return false; }
public static bool TryParse(string input, out CoordinateDD coord) { coord = new CoordinateDD(); 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 regexDDLat = new Regex(@"^((?<firstPrefix>[NnSs\+-])?(?<latitude>[0-8]?\d([,.:]\d*)?|90([,.:]0*)?)([°˚º^~*]*)(?<firstSuffix>[NnSs\+-])?)([,:;\s|\/\\]+)((?<lastPrefix>[EeWw\+-])?(?<longitude>[0]?\d?\d([,.:]\d*)?|1[0-7]\d([,.:]\d*)?|180([,.:]0*)?)([°˚º^~*]*)(?<lastSuffix>[EeWw\+-])?)$"); Regex regexDDLon = new Regex(@"^((?<firstPrefix>[EeWw\+-])?(?<longitude>[0]?\d?\d([,.:]\d*)?|1[0-7]\d([,.:]\d*)?|180([,.:]0*)?)([°˚º^~*]*)(?<firstSuffix>[EeWw\+-])?)([,:;\s|\/\\]+)((?<lastPrefix>[NnSs\+-])?(?<latitude>[0-8]?\d?\d([,.:]\d*)?|90([,.:]0*)?)([°˚º^~*]*)(?<lastSuffix>[NnSs\+-])?)$"); var matchDDLat = regexDDLat.Match(input); var matchDDLon = regexDDLon.Match(input); bool blnMatchDDLat = matchDDLat.Success; double latitude = -1, longitude = -1; Group firstPrefix = null, firstSuffix = null, lastPrefix = null, lastSuffix = null; // Ambiguous coordinate, could be both lat/lon && lon/lat if (matchDDLat.Success && matchDDLat.Length == input.Length && matchDDLon.Success && matchDDLon.Length == input.Length) { if (CoordinateConversionLibraryConfig.AddInConfig.DisplayAmbiguousCoordsDlg) { ambiguousCoordsViewDlg.ShowDialog(); } blnMatchDDLat = ambiguousCoordsViewDlg.CheckedLatLon; } // Lat/Lon if (matchDDLat.Success && matchDDLat.Length == input.Length && blnMatchDDLat) { if (ValidateNumericCoordinateMatch(matchDDLat, new string[] { "latitude", "longitude" })) { latitude = Double.Parse(matchDDLat.Groups["latitude"].Value); longitude = Double.Parse(matchDDLat.Groups["longitude"].Value); firstPrefix = matchDDLat.Groups["firstPrefix"]; firstSuffix = matchDDLat.Groups["firstSuffix"]; lastPrefix = matchDDLat.Groups["lastPrefix"]; lastSuffix = matchDDLat.Groups["lastSuffix"]; } } // Lon/Lat else if (matchDDLon.Success && matchDDLon.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchDDLon, new string[] { "latitude", "longitude" })) { latitude = Double.Parse(matchDDLon.Groups["latitude"].Value); longitude = Double.Parse(matchDDLon.Groups["longitude"].Value); firstPrefix = matchDDLon.Groups["firstPrefix"]; firstSuffix = matchDDLon.Groups["firstSuffix"]; lastPrefix = matchDDLon.Groups["lastPrefix"]; lastSuffix = matchDDLon.Groups["lastSuffix"]; } } else { return(false); } coord.Lat = latitude; coord.Lon = longitude; try { //// 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); } 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"))) { coord.Lat = Math.Abs(coord.Lat) * -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"))) { coord.Lon = Math.Abs(coord.Lon) * -1; } if ((firstSuffix.Success || firstPrefix.Success) && (firstSuffix.Value.ToUpper().Equals("-") || firstPrefix.Value.ToUpper().Equals("-"))) { if (blnMatchDDLat) { coord.Lat = Math.Abs(coord.Lat) * -1; } else { coord.Lon = Math.Abs(coord.Lon) * -1; } } if ((lastSuffix.Success || lastPrefix.Success) && (lastSuffix.Value.ToUpper().Equals("-") || lastPrefix.Value.ToUpper().Equals("-"))) { if (blnMatchDDLat) { coord.Lon = Math.Abs(coord.Lon) * -1; } else { coord.Lat = Math.Abs(coord.Lat) * -1; } } } catch { return(false); } return(true); }
public static bool TryParse(string input, out CoordinateDD coord) { coord = new CoordinateDD(); 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 regexDD = new Regex(@"^(?i) *[+]*(?<firstPrefix>[NSEW])?(?<latitude>\-*\d+[,.:]?\d*)[°˚º^~*]*?(?<firstSuffix>[NSEW])*[,:; |\/\\]*[+]*(?<lastPrefix>[NSEW])*(?<longitude>-*\d+[,.:]?\d*)[°˚º^~*]*(?<lastSuffix>[NSEW])*"); var matchDD = regexDD.Match(input); if (matchDD.Success && matchDD.Length == input.Length) { if (ValidateNumericCoordinateMatch(matchDD, new string[] { "latitude", "longitude" })) { try { var firstPrefix = matchDD.Groups["firstPrefix"]; var firstSuffix = matchDD.Groups["firstSuffix"]; var lastPrefix = matchDD.Groups["lastPrefix"]; var lastSuffix = matchDD.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); } coord.Lat = Double.Parse(matchDD.Groups["latitude"].Value); coord.Lon = Double.Parse(matchDD.Groups["longitude"].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"))) { coord.Lat = Double.Parse(matchDD.Groups["longitude"].Value); coord.Lon = Double.Parse(matchDD.Groups["latitude"].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(coord.Lat) > 90.0) && (Math.Abs(coord.Lon) <= 90.0)) { coord.Lat = Double.Parse(matchDD.Groups["longitude"].Value); coord.Lon = Double.Parse(matchDD.Groups["latitude"].Value); } if ((Math.Abs(coord.Lat) > 90.0) && (Math.Abs(coord.Lon) > 90.0)) { return(false); } } if (coord.Lat > 90.0 || coord.Lat < -90.0) { return(false); } if (coord.Lon > 180.0 || coord.Lon < -180.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"))) { coord.Lat = Math.Abs(coord.Lat) * -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"))) { coord.Lon = Math.Abs(coord.Lon) * -1; } } catch { return(false); } return(true); } } return(false); }
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); }
private void BroadcastCoordinateValues(MapPoint mapPoint) { var dict = new Dictionary<CoordinateType, string>(); if (mapPoint == null) return; var dd = new CoordinateDD(mapPoint.Y, mapPoint.X); try { dict.Add(CoordinateType.DD, dd.ToString("", new CoordinateDDFormatter())); } catch { } try { dict.Add(CoordinateType.DDM, new CoordinateDDM(dd).ToString("", new CoordinateDDMFormatter())); } catch { } try { dict.Add(CoordinateType.DMS, new CoordinateDMS(dd).ToString("", new CoordinateDMSFormatter())); } catch { } Mediator.NotifyColleagues(CoordinateConversionLibrary.Constants.BroadcastCoordinateValues, dict); }
private CoordinateType GetCoordinateType(string input, out MapPoint point) { point = null; // DD CoordinateDD dd; if(CoordinateDD.TryParse(input, out dd)) { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef); }).Result; return CoordinateType.DD; } // DDM CoordinateDDM ddm; if(CoordinateDDM.TryParse(input, out ddm)) { dd = new CoordinateDD(ddm); point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef); }).Result; return CoordinateType.DDM; } // DMS CoordinateDMS dms; if (CoordinateDMS.TryParse(input, out dms)) { dd = new CoordinateDD(dms); point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); return MapPointBuilder.CreateMapPoint(dd.Lon, dd.Lat, sptlRef); }).Result; return CoordinateType.DMS; } CoordinateGARS gars; if (CoordinateGARS.TryParse(input, out gars)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(gars.ToString("", new CoordinateGARSFormatter()), sptlRef, GeoCoordinateType.GARS, FromGeoCoordinateMode.Default); return tmp; }).Result; return CoordinateType.GARS; } catch { } } CoordinateMGRS mgrs; if(CoordinateMGRS.TryParse(input, out mgrs)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(mgrs.ToString("", new CoordinateMGRSFormatter()), sptlRef, GeoCoordinateType.MGRS, FromGeoCoordinateMode.Default); return tmp; }).Result; return CoordinateType.MGRS; } catch { } } CoordinateUSNG usng; if (CoordinateUSNG.TryParse(input, out usng)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(usng.ToString("", new CoordinateMGRSFormatter()), sptlRef, GeoCoordinateType.USNG, FromGeoCoordinateMode.Default); return tmp; }).Result; return CoordinateType.USNG; } catch { } } CoordinateUTM utm; if (CoordinateUTM.TryParse(input, out utm)) { try { point = QueuedTask.Run(() => { ArcGIS.Core.Geometry.SpatialReference sptlRef = SpatialReferenceBuilder.CreateSpatialReference(4326); var tmp = MapPointBuilder.FromGeoCoordinateString(utm.ToString("", new CoordinateUTMFormatter()), sptlRef, GeoCoordinateType.UTM, FromGeoCoordinateMode.Default); return tmp; }).Result; return CoordinateType.UTM; } catch { } } return CoordinateType.Unknown; }
private string ProcessInput(string input) { string result = string.Empty; MapPoint point; HasInputError = false; if (string.IsNullOrWhiteSpace(input)) return result; var coordType = GetCoordinateType(input, out point); if (coordType == CoordinateType.Unknown) HasInputError = true; else { proCoordGetter.Point = point; result = new CoordinateDD(point.Y, point.X).ToString("", new CoordinateDDFormatter()); } return result; }