示例#1
0
        public void IsValid_CannotValidateBaseInstance_Test()
        {
            bool expectedResult = false;
            var  cb             = new CoordinateBase();

            bool actualResult = cb.IsValid;

            Assert.AreEqual(expectedResult, actualResult);
        }
示例#2
0
        public void ValidateIsLatDegreesTestNeg100000()
        {
            string  testInput         = "-100000";
            decimal expectedOutResult = 0.0m;
            bool    expectedResult    = false;

            bool actualResult = CoordinateBase.ValidateIsLatDegrees(testInput, out decimal actualOutResult);

            Assert.AreEqual(expectedResult, actualResult);
            Assert.AreEqual(expectedOutResult, actualOutResult);
        }
示例#3
0
        public void ValidateIsLonDegreesTest90()
        {
            string  testInput         = "90";
            decimal expectedOutResult = 90.0m;
            bool    expectedResult    = true;

            bool actualResult = CoordinateBase.ValidateIsLonDegrees(testInput, out decimal actualOutResult);

            Assert.AreEqual(expectedResult, actualResult);
            Assert.AreEqual(expectedOutResult, actualOutResult);
        }
示例#4
0
        public void ValidateLattitude()
        {
            bool    expectedResult = true;
            decimal expectedOutput = -35m;

            string strLattitude = "-35";

            bool actualResult = CoordinateBase.ValidateIsLatDegrees(strLattitude, out decimal actualOutput);

            Assert.AreEqual(expectedResult, actualResult);
            Assert.AreEqual(expectedOutput, actualOutput);
        }
示例#5
0
        public void ValidateLongitude()
        {
            bool    expectedresult = true;
            decimal expectedOutput = -123m;

            string strLongitude = "-123";

            bool actualResult = CoordinateBase.ValidateIsLonDegrees(strLongitude, out decimal actualOutput);

            Assert.AreEqual(expectedresult, actualResult);
            Assert.AreEqual(expectedOutput, actualOutput);
        }
示例#6
0
 public CoordinateTextFormatter(CoordinateBase coordinateToUse)
     : base(coordinateToUse)
 {
 }
        private CoordinateType GetCoordinateType(string input, out ESRI.ArcGIS.Geometry.IPoint point, out CoordinateBase pointInformation)
        {
            point = new PointClass();
            var  cn = point as IConversionNotation;
            Type t  = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");

            System.Object            obj    = Activator.CreateInstance(t);
            ISpatialReferenceFactory srFact = obj as ISpatialReferenceFactory;

            pointInformation = null;

            // Use the enumeration to create an instance of the predefined object.

            IGeographicCoordinateSystem geographicCS =
                srFact.CreateGeographicCoordinateSystem((int)
                                                        esriSRGeoCSType.esriSRGeoCS_WGS1984);

            point.SpatialReference = geographicCS;
            string numSep = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;

            input = numSep != "." ? input.Replace(numSep, ".") : input;

            try
            {
                CoordinateDD dd;
                if (CoordinateDD.TryParse(input, out dd, true))
                {
                    // Reformat the string for cases where lat/lon have been switched
                    // PutCoords calls fail if the double uses decimal separator other than a decimal point
                    // Added InvariantCulture option to ensure the current culture is ignored
                    string newInput = string.Format(CultureInfo.InvariantCulture, "{0} {1}", dd.Lat, dd.Lon);
                    cn.PutCoordsFromDD(newInput);
                    pointInformation = dd;
                    return(CoordinateType.DD);
                }
            }
            catch { }

            try
            {
                CoordinateDDM ddm;
                if (CoordinateDDM.TryParse(input, out ddm, true))
                {
                    // Reformat the string for cases where lat/lon have been switched
                    // PutCoords calls fail if the double uses decimal separator other than a decimal point
                    // Added InvariantCulture option to ensure the current culture is ignored
                    string newInput = string.Format(CultureInfo.InvariantCulture, "{0} {1} {2} {3}", ddm.LatDegrees, ddm.LatMinutes, ddm.LonDegrees, ddm.LonMinutes);
                    cn.PutCoordsFromDD(newInput);
                    pointInformation = ddm;
                    return(CoordinateType.DDM);
                }
            }
            catch { }

            try
            {
                CoordinateDMS dms;
                if (CoordinateDMS.TryParse(input, out dms, true))
                {
                    // Reformat the string for cases where lat/lon have been switched
                    // PutCoords calls fail if the double uses decimal separator other than a decimal point
                    // Added InvariantCulture option to ensure the current culture is ignored
                    string newInput = string.Format(CultureInfo.InvariantCulture, "{0} {1} {2} {3} {4} {5}", dms.LatDegrees, dms.LatMinutes, dms.LatSeconds, dms.LonDegrees, dms.LonMinutes, dms.LonSeconds);
                    cn.PutCoordsFromDD(newInput);
                    pointInformation = dms;
                    return(CoordinateType.DMS);
                }
            }
            catch { }

            try
            {
                cn.PutCoordsFromGARS(esriGARSModeEnum.esriGARSModeCENTER, input);
                return(CoordinateType.GARS);
            }
            catch { }

            try
            {
                cn.PutCoordsFromGARS(esriGARSModeEnum.esriGARSModeLL, input);
                return(CoordinateType.GARS);
            }
            catch { }

            CoordinateGARS gars;

            if (CoordinateGARS.TryParse(input, out gars))
            {
                try
                {
                    cn.PutCoordsFromGARS(esriGARSModeEnum.esriGARSModeCENTER, gars.ToString("", new CoordinateGARSFormatter()));
                    pointInformation = gars;
                    return(CoordinateType.GARS);
                }
                catch { }
            }

            // mgrs try parse
            CoordinateMGRS mgrs;

            try
            {
                cn.PutCoordsFromMGRS(input, esriMGRSModeEnum.esriMGRSMode_Automatic);
                CoordinateMGRS.TryParse(input, out mgrs);
                pointInformation = mgrs;
                return(CoordinateType.MGRS);
            }
            catch { }
            try
            {
                cn.PutCoordsFromMGRS(input, esriMGRSModeEnum.esriMGRSMode_NewStyle);
                CoordinateMGRS.TryParse(input, out mgrs);
                pointInformation = mgrs;
                return(CoordinateType.MGRS);
            }
            catch { }
            try
            {
                cn.PutCoordsFromMGRS(input, esriMGRSModeEnum.esriMGRSMode_NewWith180InZone01);
                CoordinateMGRS.TryParse(input, out mgrs);
                pointInformation = mgrs;
                return(CoordinateType.MGRS);
            }
            catch { }
            try
            {
                cn.PutCoordsFromMGRS(input, esriMGRSModeEnum.esriMGRSMode_OldStyle);
                CoordinateMGRS.TryParse(input, out mgrs);
                pointInformation = mgrs;
                return(CoordinateType.MGRS);
            }
            catch { }
            try
            {
                cn.PutCoordsFromMGRS(input, esriMGRSModeEnum.esriMGRSMode_OldWith180InZone01);
                CoordinateMGRS.TryParse(input, out mgrs);
                pointInformation = mgrs;
                return(CoordinateType.MGRS);
            }
            catch { }


            if (CoordinateMGRS.TryParse(input, out mgrs))
            {
                try
                {
                    cn.PutCoordsFromMGRS(mgrs.ToString("", new CoordinateMGRSFormatter()), esriMGRSModeEnum.esriMGRSMode_NewStyle);
                    pointInformation = mgrs;
                    return(CoordinateType.MGRS);
                }
                catch { }
            }

            CoordinateUSNG usng;

            try
            {
                cn.PutCoordsFromUSNG(input);
                CoordinateUSNG.TryParse(input, out usng);
                pointInformation = usng;
                return(CoordinateType.USNG);
            }
            catch { }

            if (CoordinateUSNG.TryParse(input, out usng))
            {
                try
                {
                    cn.PutCoordsFromUSNG(usng.ToString("", new CoordinateMGRSFormatter()));
                    pointInformation = usng;
                    return(CoordinateType.USNG);
                }
                catch { }
            }

            CoordinateUTM utm;

            try
            {
                cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMAddSpaces, input);
                CoordinateUTM.TryParse(input, out utm);
                pointInformation = utm;
                return(CoordinateType.UTM);
            }
            catch { }

            try
            {
                cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMNoOptions, input);
                CoordinateUTM.TryParse(input, out utm);
                pointInformation = utm;
                return(CoordinateType.UTM);
            }
            catch { }

            try
            {
                cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMUseNS, input);
                CoordinateUTM.TryParse(input, out utm);
                pointInformation = utm;
                return(CoordinateType.UTM);
            }
            catch { }


            if (CoordinateUTM.TryParse(input, out utm))
            {
                try
                {
                    cn.PutCoordsFromUTM(esriUTMConversionOptionsEnum.esriUTMNoOptions, utm.ToString("", new CoordinateUTMFormatter()));
                    pointInformation = utm;
                    return(CoordinateType.UTM);
                }
                catch { }
            }

            /*
             * Updated RegEx to capture invalid coordinates like 00, 45, or 456987.
             */
            Regex regexMercator = new Regex(@"^(?<latitude>\-?\d+[.,]?\d*)[+,;:\s]{1,}(?<longitude>\-?\d+[.,]?\d*)");

            var matchMercator = regexMercator.Match(input);

            if (matchMercator.Success && matchMercator.Length == input.Length)
            {
                try
                {
                    var  Lat = Double.Parse(matchMercator.Groups["latitude"].Value);
                    var  Lon = Double.Parse(matchMercator.Groups["longitude"].Value);
                    IMap map = ((IMxDocument)ArcMap.Application.Document).FocusMap;
                    var  sr  = map.SpatialReference != null ? map.SpatialReference : ArcMapHelpers.GetSR((int)esriSRProjCS3Type.esriSRProjCS_WGS1984WebMercatorMajorAuxSphere);
                    point.X = Lon;
                    point.Y = Lat;
                    point.SpatialReference = sr;
                    return(CoordinateType.DD);
                }
                catch (Exception)
                {
                    // do nothing
                }
            }

            return(CoordinateType.Unknown);
        }
示例#8
0
 public static bool LonDecimalIsValid(decimal longitudeDecimal)
 {
     return(CoordinateBase.ValidateLonDegrees(longitudeDecimal));
 }
示例#9
0
 public static bool LatDecimalIsValid(decimal lattitudeDecimal)
 {
     return(CoordinateBase.ValidateLatDegrees(lattitudeDecimal));
 }
示例#10
0
 public static bool IsValidLatDegreesAndLonDegrees(decimal lattitude, decimal longitude)
 {
     return(CoordinateBase.ValidateLatDegrees(lattitude) && CoordinateBase.ValidateLonDegrees(longitude));
 }