[C#] [ArcGIS] [Engine] 0x005 地圖操作(更新中)

 

1.Intro

最近在忙一個政府的項目,所以一直沒更新,地圖操作庫吸收了很多網上、書上的優秀代碼,同時又自己進行了優化,這裏先給出部分的代碼。

2.Environment

Environment:Windows 7及以上

Language:C#

IDE:Visual Studio 2012

SDK:ArcGIS Engine 10.2

3.Source

    [Guid("afb9174c-2fe5-4a86-acca-256c3e946162")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Libs_GIS.Map_Operate")]
    public sealed class Map_Operate
    {
        /// <summary>
        /// 選中圖中想要的要素
        /// </summary>
        /// <param name="axMapControl">地圖控件對象</param>
        /// <param name="e">鼠標點擊事件對象</param>
        /// <returns>返回要素枚舉集合</returns>
        public static void SelectAreaFeature(AxMapControl axMapControl, IMapControlEvents2_OnMouseDownEvent e)
        {
            IMap pMap = axMapControl.Map;
            // 獲取控件激活的視圖
            IActiveView pActiveView = pMap as IActiveView;
            IGeometry pGeometry = null;
            IEnvelope pEnvlope;
            // 獲取空間當前方形區域
            pEnvlope = axMapControl.TrackRectangle();
            if (pEnvlope.IsEmpty) // 點選操作
            {
                tagRECT r;
                r.bottom = e.y;
                r.top = e.y;
                r.left = e.x;
                r.right = e.x;
                //
                pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnvlope, ref r, 4);
                // 獲取當前激活視圖的空間參考並賦值給方形區域的空間參考
                pEnvlope.SpatialReference = pActiveView.FocusMap.SpatialReference;
            }
            pGeometry = pEnvlope as IGeometry;

            // 選中區域的要素
            axMapControl.Map.SelectByShape(pGeometry, null, false);
            // 激活視圖刷新
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
        }

        /// <summary>
        /// 根據圖形集合選擇要素
        /// </summary>
        /// <param name="axMapControl">地圖對象</param>
        /// <param name="pGraphicsContainer">圖形集合</param>
        /// <param name="clearAll">是否清除已選擇的要素</param>
        public static void SelectFeatureByGraphicsContainer(AxMapControl axMapControl, IGraphicsContainer pGraphicsContainer, bool clearAll)
        {
            if (clearAll)
            {
                //清空地圖選擇集後進行後續的操作選擇
                axMapControl.Map.FeatureSelection.Clear();
            }    
            //重置遊標,訪問第一個圖形
            pGraphicsContainer.Reset();
            //獲取第一個圖形
            IElement pElement = pGraphicsContainer.Next();
            while (pElement != null)
            {
                //獲取圖形的幾何信息
                IGeometry pGeometry = pElement.Geometry;
                //選擇地圖要素,根據第一個圖形的幾何形狀所包含的要素
                axMapControl.Map.SelectByShape(pGeometry, null, false);
                pElement = pGraphicsContainer.Next();
            }
            axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, axMapControl.ActiveView.Extent);
        }

        /// <summary>
        /// 根據元組清除要素
        /// </summary>
        /// <param name="axMapControl">地圖對象</param>
        /// <param name="pElement">元素對象</param>
        /// <param name="clearAll">是否清除已選擇的要素</param>
        public static void SelectFeatureByElement(AxMapControl axMapControl, IElement pElement, bool clearAll)
        {
            if (clearAll)
            {
                //清空地圖選擇集後進行後續的操作選擇
                axMapControl.Map.FeatureSelection.Clear();
            }    
            //獲取圖形的幾何信息
            IGeometry pGeometry = pElement.Geometry;
            //選擇地圖要素,根據第一個圖形的幾何形狀所包含的要素
            axMapControl.Map.SelectByShape(pGeometry, null, false);
            axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, axMapControl.ActiveView.Extent);
        }

        /// <summary>
        /// 通過圖層名稱獲取圖層對象
        /// </summary>
        /// <param name="axMapControl">地圖控件對象</param>
        /// <param name="layerName">圖層名稱</param>
        /// <returns>返回圖層對象</returns>
        public static ILayer GetLayerByName(AxMapControl axMapControl, string layerName)
        {
            IEnumLayer pEnumLayer = axMapControl.Map.Layers;

            ILayer pLayer = pEnumLayer.Next();
            while (pLayer != null)
            {
                string layerName_LayerTmp = pLayer.Name;
                string layerName_LayerTmp2 = "";
                try
                {     
                    layerName_LayerTmp2 = (pLayer as IFeatureLayer).FeatureClass.AliasName.ToString();
                }
                catch
                {
                    layerName_LayerTmp2 = pLayer.Name;
                } 

                if (layerName == layerName_LayerTmp || layerName == layerName_LayerTmp2)
                {
                    break;
                }
                pLayer = pEnumLayer.Next();
            }
            return pLayer;
        }

        /// <summary>
        /// 檢查地圖中是否包含某圖層
        /// </summary>
        /// <param name="axMapControl">地圖空間</param>
        /// <param name="layerName">圖層名稱</param>
        /// <returns>返回檢查結果</returns>
        public static bool CheckLayerExsits(AxMapControl axMapControl, string layerName)
        {
            //遍歷MapControl中所有圖層,找到與layerName名稱相同的圖層
            //遍歷MapControl中所有圖層,找到與layerName名稱相同的圖層
            for (int i = 0; i < axMapControl.LayerCount; i++)
            {
                //如果該圖層爲圖層組類型,則分別對所包含的每個圖層進行操作
                if (axMapControl.get_Layer(i) is GroupLayer)
                {
                    //用ICompositeLayer接口進行遍歷操作
                    ICompositeLayer pCompositeLayer = axMapControl.get_Layer(i) as ICompositeLayer;
                    for (int j = 0; j < pCompositeLayer.Count; j++)
                    {
                        //獲取到每個圖層的名稱賦值給臨時變量
                        string layerName_tmp = pCompositeLayer.get_Layer(j).Name;
                        if (layerName_tmp == layerName)
                        {
                            return true;
                        }
                    }
                }
                else
                {
                    string layerName_tmp = axMapControl.get_Layer(i).Name.ToString();
                    if (layerName_tmp == layerName)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        #region 閃爍並縮放

        /// <summary>
        /// 縮放至要素並閃爍
        /// </summary>
        /// <param name="axMapControl">地圖控件對象</param>
        /// <param name="pFeature">要素對象</param>
        public static void Zoom_Flash_Feature(AxMapControl axMapControl, IFeature pFeature)
        {

            IMap pMap = axMapControl.Map;
            IActiveView pActiveView = pMap as IActiveView;
            IEnvelope pEnvelope = new EnvelopeClass();
            string layerName = pFeature.Class.AliasName.ToString();
            IFeatureLayer currentLayer = GetLayerByName(axMapControl, layerName) as IFeatureLayer;

            if (currentLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
            {
                pEnvelope = pActiveView.Extent;
                pEnvelope.Height = pEnvelope.Height / 2;
                pEnvelope.Width = pEnvelope.Width / 2;
                pEnvelope.CenterAt(pFeature.ShapeCopy as IPoint);
            }
            else
            {
                pEnvelope = SetZoomEnvelopeParam(pFeature.Extent.Envelope, 1.2);
            }

            pActiveView.Extent = pEnvelope;

            // 閃爍要素
            IGeometry geometry = pFeature.Shape;
            IPoint pCenterPoint = new PointClass();
            double x = (geometry.Envelope.LowerLeft.X + geometry.Envelope.UpperRight.X) / 2;
            double y = (geometry.Envelope.LowerLeft.Y + geometry.Envelope.UpperRight.Y) / 2;
            pCenterPoint.PutCoords(x, y);

            IDisplayTransformation pDisplayTransform = pActiveView.ScreenDisplay.DisplayTransformation;
            IEnvelope pEnvelope2 = pDisplayTransform.VisibleBounds;
            pEnvelope2.CenterAt(pCenterPoint);
            pDisplayTransform.VisibleBounds = pEnvelope2;

            pActiveView.PartialRefresh((esriViewDrawPhase)6, null, pActiveView.Extent);
            pActiveView.ScreenDisplay.UpdateWindow();

            ISymbol symbol = CreateSimpleSsymbol(geometry.GeometryType);
            if (symbol == null)
            { return; }
            DrawSymbol(symbol, geometry, pActiveView);
        }

        /// <summary>
        /// 縮放至要素並閃爍
        /// </summary>
        /// <param name="axMapControl">地圖控件對象</param>
        /// <param name="pFeatureClass">要素類對象</param>
        /// <param name="pFeature">要素對象</param>
        public static void Zoom_Flash_Feature(AxMapControl axMapControl, IFeatureClass pFeatureClass, IFeature pFeature)
        {

            IMap pMap = axMapControl.Map;
            IActiveView pActiveView = pMap as IActiveView;
            IEnvelope pEnvelope = new EnvelopeClass();

            if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
            {
                pEnvelope = pActiveView.Extent;
                pEnvelope.Height = pEnvelope.Height / 2;
                pEnvelope.Width = pEnvelope.Width / 2;
                pEnvelope.CenterAt(pFeature.ShapeCopy as IPoint);
            }
            else
            {
                pEnvelope = SetZoomEnvelopeParam(pFeature.Extent.Envelope, 0.6);
            }

            pActiveView.Extent = pEnvelope;

            // 閃爍要素
            IGeometry geometry = pFeature.Shape;
            IPoint pCenterPoint = new PointClass();
            double x = (geometry.Envelope.LowerLeft.X + geometry.Envelope.UpperRight.X) / 2;
            double y = (geometry.Envelope.LowerLeft.Y + geometry.Envelope.UpperRight.Y) / 2;
            pCenterPoint.PutCoords(x, y);

            IDisplayTransformation pDisplayTransform = pActiveView.ScreenDisplay.DisplayTransformation;
            IEnvelope pEnvelope2 = pDisplayTransform.VisibleBounds;
            pEnvelope2.CenterAt(pCenterPoint);
            pDisplayTransform.VisibleBounds = pEnvelope2;

            pActiveView.PartialRefresh((esriViewDrawPhase)6, null, pActiveView.Extent);
            pActiveView.ScreenDisplay.UpdateWindow();

            ISymbol symbol = CreateSimpleSsymbol(geometry.GeometryType);
            if (symbol == null)
            { return; }
            DrawSymbol(symbol, geometry, pActiveView);
        }

        /// <summary>
        /// 設置縮放比例參數
        /// </summary>
        /// <param name="pEnvelope">外接矩形對象</param>
        /// <param name="size">縮放比例</param>
        /// <returns>返回外接矩形對象</returns>
        private static IEnvelope SetZoomEnvelopeParam(IEnvelope pEnvelope, double size = 0.6)
        {
            double xMin = pEnvelope.XMin;
            double xMax = pEnvelope.XMax;
            double yMin = pEnvelope.YMin;
            double yMax = pEnvelope.YMax;

            double xDec = xMax - xMin;
            double yDec = yMax - yMin;

            double xMin_Le = xMin - size * xDec;
            double yMin_Le = yMin - size * yDec;
            double xMax_Le = xMax + size * xDec;
            double yMax_Le = yMax + size * yDec;

            pEnvelope.XMin = xMin_Le;
            pEnvelope.XMax = xMax_Le;
            pEnvelope.YMin = yMin_Le;
            pEnvelope.YMax = yMax_Le;
            return pEnvelope;

        }

        /// <summary>
        /// 創建簡單樣式
        /// </summary>
        /// <param name="geometryType">幾何類型</param>
        /// <returns>返回樣式對象</returns>
        private static ISymbol CreateSimpleSsymbol(esriGeometryType geometryType)
        {
            ISymbol symbol = null;
            switch (geometryType)
            {
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
                    ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass();
                    markerSymbol.Color = getRGB(255, 0, 0);
                    markerSymbol.Size = 8;
                    symbol = markerSymbol as ISymbol;
                    break;
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
                    ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();
                    lineSymbol.Color = getRGB(255, 0, 0);
                    lineSymbol.Width = 4;
                    symbol = lineSymbol as ISymbol;
                    break;
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
                    ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
                    fillSymbol.Color = getRGB(255, 0, 0);
                    symbol = fillSymbol as ISymbol;
                    break;
                default:
                    break;
            }
            symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;

            return symbol;
        }

        /// <summary>
        /// 獲取RGB對象
        /// </summary>
        /// <param name="yourRed">紅色值</param>
        /// <param name="yourGreen">綠色值</param>
        /// <param name="yourBlue">藍色值</param>
        /// <returns>返回顏色對象</returns>
        private static IColor getRGB(int yourRed, int yourGreen, int yourBlue)
        {

            IRgbColor pRGB;
            pRGB = new RgbColorClass();
            pRGB.Red = yourRed;
            pRGB.Green = yourGreen;
            pRGB.Blue = yourBlue;
            pRGB.UseWindowsDithering = true;
            return pRGB;
        }

        /// <summary>
        /// 樣式繪製
        /// </summary>
        /// <param name="symbol">樣式對象</param>
        /// <param name="geometry">幾何類型對象</param>
        /// <param name="pActiveView">激活視圖對象</param>
        private static void DrawSymbol(ISymbol symbol, IGeometry geometry, IActiveView pActiveView)
        {
            IScreenDisplay pDisplay = pActiveView.ScreenDisplay;

            pDisplay.StartDrawing(0, (short)esriScreenCache.esriNoScreenCache);
            pDisplay.SetSymbol(symbol);
            for (int i = 0; i < 10; i++)
            {
                switch (geometry.GeometryType)
                {
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
                        pDisplay.DrawPoint(geometry);
                        break;
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultipoint:
                        pDisplay.DrawMultipoint(geometry);
                        break;
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
                        pDisplay.DrawPolyline(geometry);
                        break;
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
                        pDisplay.DrawPolygon(geometry);
                        break;
                    default:
                        break;
                }
                System.Threading.Thread.Sleep(100);
            }
            pDisplay.FinishDrawing();
        }

        #endregion

        #region 地圖導出
        
        /// 導出視圖
        /// </summary>
        /// <param name="view">激活視圖對象</param>
        /// <param name="pGeo">幾何對象</param>
        /// <param name="OutputResolution">輸出分辨率</param>
        /// <param name="Width">寬度</param>
        /// <param name="Height">高度</param>
        /// <param name="ExpPath">導出路徑</param>
        /// <param name="bRegion">是否刪除當前視圖的所有元素(判斷全域或局域導出)</param>
        public static void ExportView(IActiveView view, IGeometry pGeo, int OutputResolution, int Width, int Height, string ExpPath, bool bRegion)
        {
            //實例化導出類
            IExport pExport = null;
            //實例化目標結構
            tagRECT exportRect = new tagRECT();
            //通過傳入的幾何對象實例化包絡線對象
            IEnvelope pEnvelope = pGeo.Envelope;
            //通過文件路徑名獲取文件的類型並轉換成字符串類型的
            string sType = System.IO.Path.GetExtension(ExpPath);
            //依據類型返回對應的類型對象
            switch (sType)
            {
                case ".jpg":
                    pExport = new ExportJPEGClass();
                    break;
                case ".bmp":
                    pExport = new ExportBMPClass();
                    break;
                case ".tif":
                    pExport = new ExportTIFFClass();
                    break;
                case ".png":
                    pExport = new ExportPNGClass();
                    break;
                case ".pdf":
                    pExport = new ExportPDFClass();
                    break;
                default:
                    MessageBox.Show("沒有輸出格式,默認到JPEG格式");
                    pExport = new ExportJPEGClass();
                    break;
            }
            //將保存的文件名複製給導出文件名
            pExport.ExportFileName = ExpPath;

            //導出的結構屬性值分別爲,左0,上0,右=寬,下=高,因爲左上角座標爲0,0
            exportRect.left = 0;
            exportRect.top = 0;
            exportRect.right = Width;
            exportRect.bottom = Height;
            //如果bRegion爲true
            if (bRegion)
            {
                //當前視圖刪除所有元素
                view.GraphicsContainer.DeleteAllElements();
                view.Refresh();
            }
            //實例化包絡線對象
            IEnvelope envelope = new EnvelopeClass();
            //通過PutCoords方法傳入參數,上下左右的座標值,確定包絡線範圍
            envelope.PutCoords((double)exportRect.left, (double)exportRect.top, (double)exportRect.right, (double)exportRect.bottom);
            //將包絡線對象賦值給導出對象的像元值
            pExport.PixelBounds = envelope;
            //視圖輸出方法,傳入參數:導出對象的開始導出方法,導出分辨率,導出結構,包絡線對象,
            view.Output(pExport.StartExporting(), OutputResolution, ref exportRect, pEnvelope, null);
            //完成導出
            pExport.FinishExporting();
            //清除
            pExport.Cleanup();
        }

        /// <summary>
        /// 地圖全域導出
        /// </summary>
        /// <param name="axMapControl">地圖控件</param>
        /// <returns>返回幾何對象</returns>
        public static IGeometry DrawGeometry_ExportFullArea(AxMapControl axMapControl)
        {
            IGeometry pGeometry = axMapControl.ActiveView.Extent;
            return pGeometry;
        }

        /// <summary>
        /// 地圖局域導出
        /// </summary>
        /// <param name="axMapControl">地圖控件</param>
        /// <returns>返回幾何對象</returns>
        public static IGeometry DrawGeometry_ExportLocalArea(AxMapControl axMapControl)
        {
            //刪除視圖中數據
            axMapControl.ActiveView.GraphicsContainer.DeleteAllElements();
            //刷新
            axMapControl.ActiveView.Refresh();
            //調用繪製多邊形方法,傳入當前視圖參數,賦值給多邊形對象完成實例化
            IGeometry pGeometry = DrawPolygon(axMapControl);
            if (pGeometry == null) return null;
            //調用導出地圖的添加要素方法,傳入幾何對象,當前視圖的激活視圖
            AddElement(pGeometry, axMapControl.ActiveView);
            //將多邊形對象轉化爲幾何對象賦值給幾何對象Geometry
            return pGeometry;
        }

        #region 局域導出設置

        /// <summary>
        /// 添加元素
        /// </summary>
        /// <param name="pGeometry">幾何對象</param>
        /// <param name="activeView">激活視圖</param>
        public static void AddElement(IGeometry pGeometry, IActiveView activeView)
        {
            //通過GetRgbColor方法傳入三基色,實例化顏色對象
            IRgbColor fillColor = GetRgbColor(255, 0, 0);
            IRgbColor lineColor = GetRgbColor(255, 0, 0);
            //通過CreateElement方法,傳入幾何對象,線顏色,填充顏色,實例化元素對象
            IElement pEle = CreateElement(pGeometry, lineColor, fillColor);
            //通過將當前激活的繪圖包含者屬性,實例化繪圖包含者對象
            IGraphicsContainer pGC = activeView.GraphicsContainer;
            if (pGC != null)//如果繪圖包含者對象爲空
            {
                //添加元素
                pGC.AddElement(pEle, 0);
                //當前激活視圖局部刷新
                activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, pEle, null);
            }
        }

        /// <summary>
        /// 獲取RGB顏色
        /// </summary>
        /// <param name="intR">R色值</param>
        /// <param name="intG">G色值</param>
        /// <param name="intB">B色值</param>
        /// <returns>返回IRgbColor對象</returns>
        public static IRgbColor GetRgbColor(int intR, int intG, int intB)
        {
            //實例化顏色對象
            IRgbColor pRgbColor = null;
            if (intR < 0 || intR > 255 || intG < 0 || intG > 255 || intB < 0 || intB > 255)
            {
                return pRgbColor;
            }
            pRgbColor = new RgbColorClass();
            pRgbColor.Red = intR;
            pRgbColor.Green = intG;
            pRgbColor.Blue = intB;
            return pRgbColor;
        }

        /// <summary>
        /// 創建元素
        /// </summary>
        /// <param name="pGeometry">幾何對象</param>
        /// <param name="lineColor">線顏色對象</param>
        /// <param name="fillColor">填充顏色對象</param>
        /// <returns>元素</returns>
        public static IElement CreateElement(IGeometry pGeometry, IRgbColor lineColor, IRgbColor fillColor)
        {
            if (pGeometry == null || lineColor == null || fillColor == null)
            {
                return null;
            }
            //實例化元素對象
            IElement pElem = null;
            try
            {
                //如果幾何對象是包絡線類型
                if (pGeometry is IEnvelope)
                {
                    //實例化一個矩形元素對象
                    pElem = new RectangleElementClass();
                }
                //如果幾何對象是多邊形類型
                else if (pGeometry is IPolygon)
                {
                    //實例化一個多邊形元素對象
                    pElem = new PolygonElementClass();
                }
                //如果幾何對象是圓形類型
                else if (pGeometry is ICircularArc)
                {
                    //通過幾何對象實例化一個部分對象
                    ISegment pSegCircle = pGeometry as ISegment;//QI
                    //通過多邊形組件類,實例化一個部分收集對象
                    ISegmentCollection pSegColl = new PolygonClass();
                    object ojb = Type.Missing;
                    //調用部分對象的添加部分AddSegment方法,傳入參數:部分圓
                    pSegColl.AddSegment(pSegCircle, ref ojb, ref ojb);
                    //通過部分收集對象,實例化一個多邊形對象
                    IPolygon pPolygon = pSegColl as IPolygon;
                    //通過多邊形對象實例化爲幾何對象
                    pGeometry = pPolygon as IGeometry;
                    //實例化一個圓形元素對象
                    pElem = new CircleElementClass();
                }
                //如果幾何對象時是線類型
                else if (pGeometry is IPolyline)
                {
                    //實例化一個線元素對象
                    pElem = new LineElementClass();
                }

                //如果對象爲空,返回一個空
                if (pElem == null)
                { return null; }
                //將幾何對象複製給要素的幾何屬性
                pElem.Geometry = pGeometry;
                //將要素賦值給 填充Shape元素 對象完成實例化

                if (pGeometry is IPolyline)
                {
                    ILineElement pLineElem = pElem as ILineElement;
                    ILineFillSymbol pLineSymbol = new LineFillSymbolClass();
                    pLineSymbol.Color = fillColor;
                    pLineSymbol.Outline.Color = lineColor;
                    pLineSymbol.LineSymbol.Width = 2;
                    pLineSymbol.Separation = 0.5;
                    if (pLineSymbol == null)
                    { return null; }
                    pLineElem.Symbol = pLineSymbol.LineSymbol;
                }
                else if (pGeometry is IPolygon)
                {
                    IFillShapeElement pFElem = pElem as IFillShapeElement;
                    //直接實例化符號對象
                    ISimpleFillSymbol pSymbol = new SimpleFillSymbolClass();
                    //將傳入的填充顏色賦值給符號顏色屬性
                    pSymbol.Color = fillColor;

                    //將傳入的線段顏色賦值給符號的外邊框顏色
                    // pSymbol.Outline.Color = lineColor;
                    pSymbol.Outline.Color.CMYK = lineColor.CMYK;
                    pSymbol.Outline.Color.RGB = lineColor.RGB;
                    //pSymbol.Outline.Width = 10;
                    //符號樣式設置爲簡單的填充風格
                    pSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
                    //如果符號對象爲空,則返回空
                    if (pSymbol == null)
                    { return null; }
                    //將具有若干屬性的符號對象賦值給 填充Shape元素對象的符號屬性
                    pFElem.Symbol = pSymbol;
                }              
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return pElem;
        }

        /// <summary>
        /// 繪製多邊形
        /// </summary>
        /// <param name="axMapControl">地圖對象</param>
        /// <returns>幾何對象</returns>
        public static IGeometry DrawPolygon(AxMapControl axMapControl)
        {
            //設置局部變量幾何對象
            IGeometry pGeometry = null;
            //如果傳入地圖文檔參數爲空則返回空
            if (axMapControl == null)
            { return null; }
            //實例化一個RubberBand(橡皮圈)對象
            IRubberBand rb = new RubberPolygonClass();
            //通過調用橡皮圈對象的TrackNew方法,傳入參數爲當前地圖的激活視圖的屏幕顯示,null,完成對幾何對象的實例化
            pGeometry = rb.TrackNew(axMapControl.ActiveView.ScreenDisplay, null);
            //返回一個幾何對象,並轉爲多邊形對象
            return pGeometry;
        }

        #endregion

        #endregion

        /// <summary>
        /// 獲取地圖座標單位
        /// </summary>
        /// <param name="axMapControl">地圖控件</param>
        /// <returns>返回單位</returns>
        public static string GetMapUnit(AxMapControl axMapControl)
        {
            string sMapUnits = string.Empty;
            esriUnits pEsriMapUnit = axMapControl.Map.MapUnits;
            switch (pEsriMapUnit)
            {
                case esriUnits.esriCentimeters:
                    sMapUnits = "釐米";
                    break;
                case esriUnits.esriDecimalDegrees:
                    sMapUnits = "十進制";
                    break;
                case esriUnits.esriDecimeters:
                    sMapUnits = "分米";
                    break;
                case esriUnits.esriFeet:
                    sMapUnits = "英寸";
                    break;
                case esriUnits.esriInches:
                    sMapUnits = "英尺";
                    break;
                case esriUnits.esriKilometers:
                    sMapUnits = "公里";
                    break;
                case esriUnits.esriMeters:
                    sMapUnits = "米";
                    break;
                case esriUnits.esriMiles:
                    sMapUnits = "英里";
                    break;
                case esriUnits.esriMillimeters:
                    sMapUnits = "毫米";
                    break;
                case esriUnits.esriNauticalMiles:
                    sMapUnits = "海里";
                    break;
                case esriUnits.esriPoints:
                    sMapUnits = "點";
                    break;
                case esriUnits.esriUnitsLast:
                    sMapUnits = "最後單位";
                    break;
                case esriUnits.esriUnknownUnits:
                    sMapUnits = "未知單位";
                    break;
                case esriUnits.esriYards:
                    sMapUnits = "碼";
                    break;
            }
            return sMapUnits;
        }

        /// <summary>
        /// 通過點集合生成幾何對象
        /// </summary>
        /// <param name="pPointCollection">點集合</param>
        /// <returns>返回幾何對象</returns>
        public static IGeometry GetGeometryFromPointCollection(IPointCollection pPointCollection)
        {
            Ring ring = new RingClass();
            object missing = Type.Missing;
            ring.AddPointCollection(pPointCollection);
            IGeometryCollection pointPolygon = new PolygonClass();
            pointPolygon.AddGeometry(ring as IGeometry, ref missing, ref missing);
            IPolygon polyGonGeo = pointPolygon as IPolygon;
            polyGonGeo.SimplifyPreserveFromTo();
            return polyGonGeo as IGeometry; 
        }
    }

4.Conclusion

不多比比,直接上代碼,都在註釋裏了兄弟。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章