public bool Parse(string input, out CoordinateDD dd)
        {
            dd = new CoordinateDD();
            CoordinateDMS dms;

            // try to parse all types?

            if (CoordinateDD.TryParse(input, out dd))
            {
                return(true);
            }
            else if (CoordinateDMS.TryParse(input, out dms))
            {
                dd.Lat = Math.Abs(dms.LatDegrees) + (dms.LatMinutes / 60.0) + (dms.LatSeconds / 3600.0);
                dd.Lon = Math.Abs(dms.LonDegrees) + (dms.LonMinutes / 60.0) + (dms.LonSeconds / 3600.0);

                if (dms.LatDegrees < 0)
                {
                    dd.Lat *= -1;
                }

                if (dms.LonDegrees < 0)
                {
                    dd.Lon *= -1;
                }

                return(true);
            }

            return(false);
        }
Пример #2
0
        public static bool TryParse(string input, out CoordinateDMS dms)
        {
            dms = new CoordinateDMS();

            if (string.IsNullOrWhiteSpace(input))
            {
                return(false);
            }

            input = input.Trim();

            Regex regexDMS = new Regex("^ *[+]*(?<latitudeSuffix>[NS])?(?<latitudeD>[^NSDd*° ,:]*)?[Dd*° ,:]*(?<latitudeM>[^NS' ,:]*)?[' ,:]*(?<latitudeS>[^NS\\\" ,:]*)?[\\\" ,:]*(?<latitudeSuffix>[NS])? *[+,]*(?<longitudeSuffix>[EW])?(?<longitudeD>[^EWDd*° ,:]*)?[Dd*° ,:]*(?<longitudeM>[^EW' ,:]*)?[' ,:]*(?<longitudeS>[^EW\\\" ,:]*)?[\\\" ,:]*(?<longitudeSuffix>[EW])?[\\\"]*", RegexOptions.ExplicitCapture);

            var matchDMS = regexDMS.Match(input);

            if (matchDMS.Success && matchDMS.Length == input.Length)
            {
                if (ValidateNumericCoordinateMatch(matchDMS, new string[] { "latitudeD", "latitudeM", "latitudeS", "longitudeD", "longitudeM", "longitudeS" }))
                {
                    try
                    {
                        var LatDegrees = int.Parse(matchDMS.Groups["latitudeD"].Value);
                        var LatMinutes = int.Parse(matchDMS.Groups["latitudeM"].Value);
                        var LatSeconds = double.Parse(matchDMS.Groups["latitudeS"].Value);
                        var LonDegrees = int.Parse(matchDMS.Groups["longitudeD"].Value);
                        var LonMinutes = int.Parse(matchDMS.Groups["longitudeM"].Value);
                        var LonSeconds = double.Parse(matchDMS.Groups["longitudeS"].Value);

                        var temp = matchDMS.Groups["latitudeSuffix"];
                        if (temp.Success && temp.Value.ToUpper().Equals("S"))
                        {
                            LatDegrees = Math.Abs(LatDegrees) * -1;
                        }
                        temp = matchDMS.Groups["longitudeSuffix"];
                        if (temp.Success && temp.Value.ToUpper().Equals("W"))
                        {
                            LonDegrees = Math.Abs(LonDegrees) * -1;
                        }

                        dms = new CoordinateDMS(LatDegrees, LatMinutes, LatSeconds, LonDegrees, LonMinutes, LonSeconds);
                    }
                    catch
                    {
                        return(false);
                    }

                    return(true);
                }
            }
            return(false);
        }
        public virtual bool CanGetDMS(int srFactoryCode, out string coord)
        {
            CoordinateDMS dms;

            if (CoordinateDMS.TryParse(InputCoordinate, out dms))
            {
                coord = dms.ToString("", new CoordinateDMSFormatter());
                return(true);
            }
            else
            {
                coord = string.Empty;
                return(false);
            }
        }
        public CoordinateDMS GetDMS(CoordinateDD dd)
        {
            var dms = new CoordinateDMS();
            var tlat = Math.Truncate(dd.Lat);
            var tlon = Math.Truncate(dd.Lon);
            var latminDec = (Math.Abs(dd.Lat) - Math.Abs(tlat)) * 60.0;
            var lonminDec = (Math.Abs(dd.Lon) - Math.Abs(tlon)) * 60.0;
            dms.LatDegrees = (int)tlat;
            dms.LatMinutes = (int)latminDec;
            dms.LatSeconds = (latminDec - Math.Truncate(latminDec)) * 60.0;
            dms.LonDegrees = (int)tlon;
            dms.LonMinutes = (int)lonminDec;
            dms.LonSeconds = (lonminDec - Math.Truncate(lonminDec)) * 60.0;

            return dms;
        }
        public CoordinateDMS GetDMS(CoordinateDD dd)
        {
            var dms       = new CoordinateDMS();
            var tlat      = Math.Truncate(dd.Lat);
            var tlon      = Math.Truncate(dd.Lon);
            var latminDec = (Math.Abs(dd.Lat) - Math.Abs(tlat)) * 60.0;
            var lonminDec = (Math.Abs(dd.Lon) - Math.Abs(tlon)) * 60.0;

            dms.LatDegrees = (int)tlat;
            dms.LatMinutes = (int)latminDec;
            dms.LatSeconds = (latminDec - Math.Truncate(latminDec)) * 60.0;
            dms.LonDegrees = (int)tlon;
            dms.LonMinutes = (int)lonminDec;
            dms.LonSeconds = (lonminDec - Math.Truncate(lonminDec)) * 60.0;

            return(dms);
        }
 public override bool CanGetDMS(int srFactoryCode, out string coord)
 {
     coord = string.Empty;
     if (base.CanGetDMS(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 dms = new CoordinateDMS(dd);
                 coord = dms.ToString("", new CoordinateDMSFormatter());
                 return true;
             }
         }
     }
     return false;
 }
 public CoordinateDD(CoordinateDMS dms)
 {
     Lat = (double)dms.LatDegrees + ((double)dms.LatMinutes / 60.0) + (dms.LatSeconds / 3600.0);
     Lon = (double)dms.LonDegrees + ((double)dms.LonMinutes / 60.0) + (dms.LatSeconds / 3600.0);
 }
        public void FormatterDMS()
        {
            var coord = new CoordinateDMS(40, 16, 22.9728, -78, 50, 50.7372);
            var temp = coord.ToString("", new CoordinateDMSFormatter());
            Assert.AreEqual(temp, "40°16'23.0\"N 78°50'50.7\"W");

            temp = coord.ToString("A0°B0'C0.0##\"N X0°Y0'Z0.0##\"E", new CoordinateDMSFormatter());
            Assert.AreEqual(temp, "40°16'22.973\"N 78°50'50.737\"W");

            temp = coord.ToString("NA0°B0'C0.0##\" EX0°Y0'Z0.0##\"", new CoordinateDMSFormatter());
            Assert.AreEqual(temp, "N40°16'22.973\" W78°50'50.737\"");
            
            temp = coord.ToString("A0° B0' C0.0##\" N X0° Y0' Z0.0##\" E", new CoordinateDMSFormatter());
            Assert.AreEqual(temp, "40° 16' 22.973\" N 78° 50' 50.737\" W");
            
            temp = coord.ToString("A+-#°B0'C0.0##\" X+-#°Y0'Z0.0##\"", new CoordinateDMSFormatter());
            Assert.AreEqual(temp, "+40°16'22.973\" -78°50'50.737\"");
            
            temp = coord.ToString("A0 B0 C0.0## N X0 Y0 Z0.0## E", new CoordinateDMSFormatter());
            Assert.AreEqual(temp, "40 16 22.973 N 78 50 50.737 W");
            
            temp = coord.ToString("A0°B0'C0.0##\"N, X0°Y0'Z0.0##\"E", new CoordinateDMSFormatter());
            Assert.AreEqual(temp, "40°16'22.973\"N, 78°50'50.737\"W");
        }
        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);
        }
Пример #10
0
 public CoordinateDD(CoordinateDMS dms)
 {
     Lat = (double)dms.LatDegrees + ((double)dms.LatMinutes / 60.0) + (dms.LatSeconds / 3600.0);
     Lon = (double)dms.LonDegrees + ((double)dms.LonMinutes / 60.0) + (dms.LatSeconds / 3600.0);
 }
        public static bool TryParse(string input, out CoordinateDMS dms)
        {
            dms = new CoordinateDMS();

            if (string.IsNullOrWhiteSpace(input))
                return false;
            
            input = input.Trim();

            Regex regexDMS = new Regex("^ *[+]*(?<latitudeSuffix>[NS])?(?<latitudeD>[^NSDd*° ,:]*)?[Dd*° ,:]*(?<latitudeM>[^NS' ,:]*)?[' ,:]*(?<latitudeS>[^NS\\\" ,:]*)?[\\\" ,:]*(?<latitudeSuffix>[NS])? *[+,]*(?<longitudeSuffix>[EW])?(?<longitudeD>[^EWDd*° ,:]*)?[Dd*° ,:]*(?<longitudeM>[^EW' ,:]*)?[' ,:]*(?<longitudeS>[^EW\\\" ,:]*)?[\\\" ,:]*(?<longitudeSuffix>[EW])?[\\\"]*", RegexOptions.ExplicitCapture); 

            var matchDMS = regexDMS.Match(input);
            
            if (matchDMS.Success && matchDMS.Length == input.Length)
            {
                if (ValidateNumericCoordinateMatch(matchDMS, new string[] { "latitudeD", "latitudeM", "latitudeS", "longitudeD", "longitudeM", "longitudeS" }))
                {
                    try
                    {
                        var LatDegrees = int.Parse(matchDMS.Groups["latitudeD"].Value);
                        var LatMinutes = int.Parse(matchDMS.Groups["latitudeM"].Value);
                        var LatSeconds = double.Parse(matchDMS.Groups["latitudeS"].Value);
                        var LonDegrees = int.Parse(matchDMS.Groups["longitudeD"].Value);
                        var LonMinutes = int.Parse(matchDMS.Groups["longitudeM"].Value);
                        var LonSeconds = double.Parse(matchDMS.Groups["longitudeS"].Value);

                        var temp = matchDMS.Groups["latitudeSuffix"];
                        if (temp.Success && temp.Value.ToUpper().Equals("S"))
                        {
                            LatDegrees = Math.Abs(LatDegrees) * -1;
                        }
                        temp = matchDMS.Groups["longitudeSuffix"];
                        if (temp.Success && temp.Value.ToUpper().Equals("W"))
                        {
                            LonDegrees = Math.Abs(LonDegrees) * -1;
                        }

                        dms = new CoordinateDMS(LatDegrees, LatMinutes, LatSeconds, LonDegrees, LonMinutes, LonSeconds);
                    }
                    catch
                    {
                        return false;
                    }

                    return true;
                }
            }
            return false;
        }