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 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;
        }
        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(CoordinateToolLibrary.Constants.BroadcastCoordinateValues, dict);

        }
        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);
        }
 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;
        }
Beispiel #7
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 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 virtual bool CanGetDD(int srFactoryCode, out string coord)
        {
            CoordinateDD dd;

            if (CoordinateDD.TryParse(InputCoordinate, out dd))
            {
                Project(srFactoryCode);
                coord = dd.ToString("", new CoordinateDDFormatter());
                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);
        }
Beispiel #11
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 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, 0, 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, 0, 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, 0, 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);
                        tmp = MapPointBuilder.CreateMapPoint(tmp.X, tmp.Y, 0, sptlRef);
                        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);
                        tmp = MapPointBuilder.CreateMapPoint(tmp.X, tmp.Y, 0, sptlRef);
                        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);
                        tmp = MapPointBuilder.CreateMapPoint(tmp.X, tmp.Y, 0, sptlRef);
                        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);
                        tmp = MapPointBuilder.CreateMapPoint(tmp.X, tmp.Y, 0, sptlRef);
                        return tmp;
                    }).Result;

                    return CoordinateType.UTM;
                }
                catch { }
            }


            return CoordinateType.Unknown;
        }
        public void FormatterDD()
        {
            var coord = new CoordinateDD(40.273048, -78.847427);
            var temp = coord.ToString("Y0.0#N X0.0#E", new CoordinateDDFormatter());
            Assert.AreEqual(temp, "40.27N 78.85W");

            temp = coord.ToString("Y0.0#S X0.0#W", new CoordinateDDFormatter());
            Assert.AreEqual(temp, "40.27N 78.85W");

            temp = coord.ToString("Y+-0.##N X+-0.##E", new CoordinateDDFormatter());
            Assert.AreEqual(temp, "+40.27N -78.85W");

            temp = coord.ToString("Y+-0.0# X+-0.0#", new CoordinateDDFormatter());
            Assert.AreEqual(temp, "+40.27 -78.85");

            temp = coord.ToString("Y0.0# N, X0.0# E", new CoordinateDDFormatter());
            Assert.AreEqual(temp, "40.27 N, 78.85 W");

            temp = coord.ToString("Y0.0#° N, X0.0#° E", new CoordinateDDFormatter());
            Assert.AreEqual(temp, "40.27° N, 78.85° W");

            temp = coord.ToString("N Y0.0#°, E X0.0#°", new CoordinateDDFormatter());
            Assert.AreEqual(temp, "N 40.27°, W 78.85°");

            // test the default
            temp = coord.ToString("", new CoordinateDDFormatter());
            Assert.AreEqual(temp, "40.2730 -78.8474");
        }
        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());
                UIHelpers.UpdateHistory(input, InputCoordinateHistoryList);
            }

            return result;
        }
        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);
        }