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 virtual bool CanGetDDM(int srFactoryCode, out string coord)
        {
            CoordinateDDM ddm;

            if (CoordinateDDM.TryParse(InputCoordinate, out ddm))
            {
                coord = ddm.ToString("", new CoordinateDDMFormatter());
                return(true);
            }
            else
            {
                coord = string.Empty;
                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 CoordinateDD(CoordinateDDM ddm)
 {
     Lat = (double)ddm.LatDegrees + (ddm.LatMinutes / 60.0);
     Lon = (double)ddm.LonDegrees + (ddm.LonMinutes / 60.0);
 }
        public void FormatterDDM()
        {
            var coord = new CoordinateDDM(40, 16.38288, -78, 50.84562);
            var temp = coord.ToString("", new CoordinateDDMFormatter());
            Assert.AreEqual(temp, "40°16.38288' -78°50.84562'");

            temp = coord.ToString("A0°B0.0#####'N X0°Y0.0#####'E", new CoordinateDDMFormatter());
            Assert.AreEqual(temp, "40°16.38288'N 78°50.84562'W");

            temp = coord.ToString("A+-0°B0.0#####' X+-0°Y0.0#####'", new CoordinateDDMFormatter());
            Assert.AreEqual(temp, "+40°16.38288' -78°50.84562'");

            temp = coord.ToString("NA0°B0.0#####' EX0°Y0.0#####'", new CoordinateDDMFormatter());
            Assert.AreEqual(temp, "N40°16.38288' W78°50.84562'");
            
            temp = coord.ToString("A0°B0.0#####'N, X0°Y0.0#####'E", new CoordinateDDMFormatter());
            Assert.AreEqual(temp, "40°16.38288'N, 78°50.84562'W");
            
            temp = coord.ToString("A0° B0.0#####'N X0° Y0.0#####'E", new CoordinateDDMFormatter());
            Assert.AreEqual(temp, "40° 16.38288'N 78° 50.84562'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);
        }
Пример #8
0
 public CoordinateDD(CoordinateDDM ddm)
 {
     Lat = (double)ddm.LatDegrees + (ddm.LatMinutes / 60.0);
     Lon = (double)ddm.LonDegrees + (ddm.LonMinutes / 60.0);
 }