Example #1
0
        ///<summary>Flash geometry on the display. The geometry type could be polygon, polyline, point, or multipoint.</summary>
        ///
        ///<param name="geometry"> An IGeometry interface</param>
        ///<param name="color">An IRgbColor interface</param>
        ///<param name="display">An IDisplay interface</param>
        ///<param name="delay">A System.Int32 that is the time im milliseconds to wait.</param>
        ///
        ///<remarks></remarks>
        public static void FlashGeometry(ESRI.ArcGIS.Geometry.IGeometry geometry, ESRI.ArcGIS.Display.IRgbColor color, ESRI.ArcGIS.Display.IDisplay display, System.Int32 delay, IEnvelope envelope)
        {
            if (geometry == null || color == null || display == null)
            {
                return;
            }

            display.StartDrawing(display.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit Cast

            switch (geometry.GeometryType)
            {
            case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
            {
                //Set the flash geometry's symbol.
                ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();
                simpleFillSymbol.Color = color;
                ESRI.ArcGIS.Display.ISymbol symbol = simpleFillSymbol as ESRI.ArcGIS.Display.ISymbol;         // Dynamic Cast
                symbol.ROP2 = ESRI.ArcGIS.Display.esriRasterOpCode.esriROPNotXOrPen;

                //Flash the input polygon geometry.
                display.SetSymbol(symbol);
                display.DrawPolygon(geometry);
                System.Threading.Thread.Sleep(delay);
                display.DrawPolygon(geometry);
                break;
            }

            case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
            {
                //Set the flash geometry's symbol.
                ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
                simpleLineSymbol.Width = 4;
                simpleLineSymbol.Color = color;
                ESRI.ArcGIS.Display.ISymbol symbol = simpleLineSymbol as ESRI.ArcGIS.Display.ISymbol;         // Dynamic Cast
                symbol.ROP2 = ESRI.ArcGIS.Display.esriRasterOpCode.esriROPNotXOrPen;

                //Flash the input polyline geometry.
                display.SetSymbol(symbol);
                display.DrawPolyline(geometry);
                System.Threading.Thread.Sleep(delay);
                display.DrawPolyline(geometry);
                break;
            }

            case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
            {
                //Set the flash geometry's symbol.
                ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
                simpleMarkerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCircle;
                simpleMarkerSymbol.Size  = 12;
                simpleMarkerSymbol.Color = color;
                ESRI.ArcGIS.Display.ISymbol markerSymbol = simpleMarkerSymbol as ESRI.ArcGIS.Display.ISymbol;         // Dynamic Cast
                markerSymbol.ROP2 = ESRI.ArcGIS.Display.esriRasterOpCode.esriROPNotXOrPen;

                ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
                simpleLineSymbol.Width = 1;
                simpleLineSymbol.Color = color;
                ESRI.ArcGIS.Display.ISymbol lineSymbol = simpleLineSymbol as ESRI.ArcGIS.Display.ISymbol;         // Dynamic Cast
                lineSymbol.ROP2 = ESRI.ArcGIS.Display.esriRasterOpCode.esriROPNotXOrPen;

                //Flash the input polygon geometry.
                display.SetSymbol(markerSymbol);
                display.SetSymbol(lineSymbol);

                ArcMapHelpers.DrawCrossHair(geometry, display, envelope, markerSymbol, lineSymbol);

                //Flash the input point geometry.
                display.SetSymbol(markerSymbol);
                display.DrawPoint(geometry);
                System.Threading.Thread.Sleep(delay);
                display.DrawPoint(geometry);
                break;
            }

            case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultipoint:
            {
                //Set the flash geometry's symbol.
                ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
                simpleMarkerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCircle;
                simpleMarkerSymbol.Size  = 12;
                simpleMarkerSymbol.Color = color;
                ESRI.ArcGIS.Display.ISymbol symbol = simpleMarkerSymbol as ESRI.ArcGIS.Display.ISymbol;         // Dynamic Cast
                symbol.ROP2 = ESRI.ArcGIS.Display.esriRasterOpCode.esriROPNotXOrPen;

                //Flash the input multipoint geometry.
                display.SetSymbol(symbol);
                display.DrawMultipoint(geometry);
                System.Threading.Thread.Sleep(delay);
                display.DrawMultipoint(geometry);
                break;
            }
            }

            display.FinishDrawing();
        }
        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);
        }