C# 基于AE的GIS二次开发 基本操作方法

使用ae前需许可认证:加在Program  main函数里面

 

 

//签入AE运行许可
            if (!RuntimeManager.Bind(ProductCode.Engine))
            {
                if (!RuntimeManager.Bind(ProductCode.Desktop))
                {
                    MessageBox.Show("Unable to bind to ArcGIS runtime.Application will be shut down.");
                      return;
                }
            }

地图文档加载 鹰眼实现 等

/// <summary>
    /// 地图基本操作
    /// </summary>
    public class WMapBaseOperate  
    {
        /// <summary>
        /// 添加SHP文档
        /// </summary>
        /// <param name="mapControl"></param>
        public static void AddShapeFile(IMapControlDefault mapControl)
        {
            OpenFileDialog openfileDlg = new OpenFileDialog();
            openfileDlg.Title = "添加shp图层文件";
            openfileDlg.Filter = "map document (*.shp)|*.shp";
            openfileDlg.ShowDialog();
            string filepath = openfileDlg.FileName;

            bool exist = File.Exists(filepath);
            if (!exist)
            {
                MessageBoxEX.Show("路径不存在!");
                return;
            }

            string path;
            string filename;

            FileInfo fileinfo = new FileInfo(filepath);
            path = filepath.Substring(0, filepath.Length - fileinfo.Name.Length);
            filename = fileinfo.Name;
            try
            {
                //加载图层文件
                mapControl.AddShapeFile(path, filename);

                //设置MapControl的显示范围到数据的全局范围
                mapControl.Extent = mapControl.FullExtent;
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("添加图层文件失败!" + ex.Message);
            }

        }

        /// <summary>
        /// 添加LYR文档
        /// </summary>
        /// <param name="mapControl"></param>
        public static void AddLayerFile(IMapControlDefault mapControl)
        {
            OpenFileDialog openfileDlg = new OpenFileDialog();
            openfileDlg.Title = "添加lyr图层文件";
            openfileDlg.Filter = "map documents (*.lyr)|*.lyr";
            openfileDlg.ShowDialog();
            string filepath = openfileDlg.FileName;
            bool exist = File.Exists(filepath);
            if (!exist)
            {
                MessageBox.Show("路径不存在!");
                return;
            }
            try
            {
                mapControl.AddLayerFromFile(filepath);
                //设置MapControl的显示范围到数据的全局范围
                mapControl.Extent = mapControl.FullExtent;
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("添加图层文件失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 删除地图所有图层
        /// </summary>
        public static void DeleteAllLayers(IMapControlDefault mapControl)
        {
            try
            {
                for (int i = mapControl.LayerCount - 1; i >= 0; i--)
                {
                    mapControl.DeleteLayer(i);
                }
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("删除图层失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 将最底图层,移动到最上层
        /// </summary>
        public static void MoveLayerToTop(IMapControlDefault mapControl)
        {
            try
            {
                if (mapControl.LayerCount > 0)
                {
                    mapControl.MoveLayerTo(mapControl.LayerCount - 1, 0);
                }
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("移动图层失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 加载地图文档
        /// </summary>
        /// <param name="mapControl"></param>
        public static void LoadMapDocument(IMapControlDefault mapControl)
        {
            OpenFileDialog openfileDlg = new OpenFileDialog();
            openfileDlg.Title = "加载地图文档";
            openfileDlg.Filter = "map document (*.mxd)|*.mxd";
            openfileDlg.ShowDialog();
            string filepath = openfileDlg.FileName;
            if (mapControl.CheckMxFile(filepath))
            {
                mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                mapControl.LoadMxFile(filepath, 0, Type.Missing);
                mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
            }
            else
            {
                MessageBoxEX.Show(filepath + "不是有效的地图文档!");
            }
        }

        /// <summary>
        /// 加载特定地图文档
        /// </summary>
        /// <param name="mapControl"></param>
        public static void LoadSpecificMapDocument(IMapControlDefault mapControl, string specificMapName)
        {
            OpenFileDialog openfileDlg = new OpenFileDialog();
            openfileDlg.Title = "加载特定地图文档";
            openfileDlg.Filter = "map document (*.mxd)|*.mxd";
            openfileDlg.ShowDialog();
            string filepath = openfileDlg.FileName;
            if (mapControl.CheckMxFile(filepath))
            {
                if (string.IsNullOrWhiteSpace(specificMapName))
                {
                    int istart = filepath.LastIndexOf("\\");
                    int iend = filepath.LastIndexOf(".");
                    specificMapName = filepath.Substring(istart + 1, iend - istart - 1);
                }
                IArray arrayMap = mapControl.ReadMxMaps(filepath, Type.Missing);
                for (int i = 0; i < arrayMap.Count; i++)
                {
                    IMap map = arrayMap.get_Element(i) as IMap;
                    if (specificMapName == map.Name)
                    {
                        mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                        mapControl.LoadMxFile(filepath, 0, Type.Missing);
                        mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
                        break;
                    }
                }
            }
            else
            {
                MessageBoxEX.Show(filepath + "不是有效的地图文档!");
            }
        }


        /// <summary>
        /// 加载指定地图文档
        /// </summary>
        /// <param name="mapControl"></param>
        public static void LoadMapToDocument(IMapControlDefault mapControl, string filepath)
        {
            if (mapControl.CheckMxFile(filepath))
            {
                mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                mapControl.LoadMxFile(filepath, 0, Type.Missing);
                mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
            }
            else
            {
                MessageBoxEX.Show(filepath + "不是有效的地图文档!");
            }
        }

        /// <summary>
        /// By MapDocument
        /// </summary>
        public static IMapDocument LoadMapDoc(IMapControlDefault mapControl)
        {
            MapDocument mapdoc = new MapDocument();

            try
            {
                OpenFileDialog openfileDlg = new OpenFileDialog();
                openfileDlg.Title = "加载地图文档";
                openfileDlg.Filter = "map document (*.mxd)|*.mxd";
                openfileDlg.ShowDialog();
                string filepath = openfileDlg.FileName;

                mapdoc.Open(filepath, "");

                for (int i = 0; i < mapdoc.MapCount; i++)
                {
                    mapControl.Map = mapdoc.get_Map(i);
                }
                mapControl.Refresh();
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("加载地图文档失败" + ex.Message);
                mapdoc = null;
            }
            return mapdoc;

        }

        /// <summary>
        /// 获取MapDocument
        /// </summary>
        /// <param name="mapControl"></param>
        /// <returns></returns>
        public static IMapDocument GetMapDoc(string filepath)
        {
            MapDocument mapdoc = new MapDocument();
            try
            {
                mapdoc.Open(filepath, "");
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("获取当前地图文档失败!" + ex.Message);
                mapdoc = null;
            }
            return mapdoc;

        }

        /// <summary>
        /// By MapDocument保存
        /// </summary>
        /// <param name="mapDoc"></param>
        public static void SaveMapDoc(IMapControlDefault mapControl)
        {
            MapDocument mapDoc = new MapDocument();
            mapDoc = GetMapDoc(mapControl.DocumentFilename) as MapDocument;
            if (null == mapDoc)
            {
                MessageBoxEX.Show("保存地图文档失败!");
                return;
            }

            if (mapDoc.get_IsReadOnly(mapDoc.DocumentFilename) == true)
            {
                MessageBoxEX.Show("文档只读无法保存!");
            }

            try
            {
                mapDoc.Save(mapDoc.UsesRelativePaths, true);
                MessageBoxEX.Show("保存地图文档成功!");

            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("保存地图文档失败!" + ex.Message);
            }

        }

        /// <summary>
        /// By MapDocument另存为
        /// </summary>
        /// <param name="mapDoc"></param>
        public static void SaveAsMapDoc(IMapControlDefault mapControl)
        {
            MapDocument mapDoc = new MapDocument();
            mapDoc = GetMapDoc(mapControl.DocumentFilename) as MapDocument;
            if (null == mapDoc)
            {
                MessageBoxEX.Show("保存地图文档失败!");
                return;
            }

            if (mapDoc.get_IsReadOnly(mapDoc.DocumentFilename) == true)
            {
                MessageBoxEX.Show("文档只读无法保存!");
            }

            SaveFileDialog savefiledlg = new SaveFileDialog();
            savefiledlg.Title = "保存地图文档";
            savefiledlg.Filter = "map document (*.mxd)|*.mxd";
            savefiledlg.ShowDialog();
            string filepath = savefiledlg.FileName;
            try
            {
                mapDoc.SaveAs(filepath, mapDoc.UsesRelativePaths, true);
                MessageBoxEX.Show("保存地图文档成功!");

            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("保存地图文档失败!" + ex.Message);
            }

        }

        /// <summary>
        /// 缩小
        /// </summary>
        /// <param name="mapControl"></param>
        public static void ZoomOut(IMapControlDefault mapControl)
        {
            try
            {
                mapControl.MousePointer = esriControlsMousePointer.esriPointerPageZoomOut;
                //IEnvelope ipEnv = mapControl.TrackRectangle();
                IEnvelope ipEnv = mapControl.Extent;
                ipEnv.Expand(2, 2, true);
                mapControl.Extent = ipEnv;
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("缩小失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 放大
        /// </summary>
        /// <param name="mapControl"></param>
        public static void ZoomIn(IMapControlDefault mapControl)
        {
            try
            {
                mapControl.MousePointer = esriControlsMousePointer.esriPointerPageZoomIn;
                IEnvelope ipEnv = mapControl.TrackRectangle();
                if (ipEnv.IsEmpty)
                {
                    ipEnv = mapControl.Extent;
                    ipEnv.Expand(0.5, 0.5, true);
                }
                mapControl.Extent = ipEnv;
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("放大失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 漫游
        /// </summary>
        /// <param name="mapControl"></param>
        public static void Pan(IMapControlDefault mapControl)
        {
            try
            {
                mapControl.MousePointer = esriControlsMousePointer.esriPointerPan;
                //IEnvelope ipEnv = mapControl.Extent;
                mapControl.Pan();
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("漫游失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 全图
        /// </summary>
        /// <param name="mapControl"></param>
        public static void FullExtent(IMapControlDefault mapControl)
        {
            try
            {
                mapControl.Extent = mapControl.FullExtent;
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("全图失败!" + ex.Message);
            }
        }

        /// <summary>
        /// 写文字(待优化)
        /// </summary>
        /// <param name="mapControl"></param>
        /// <param name="pGeom"></param>
        /// <param name="pColor"></param>
        /// <param name="text"></param>
        public static void DrawMapText(IMapControlDefault mapControl, IGeometry pGeom, IRgbColor pColor, string text)
        {
            try
            {
                if (null == pColor)
                {
                    pColor = new RgbColorClass();
                    pColor.Red = 255;
                    pColor.Green = 0;
                    pColor.Blue = 0;
                }
                ITextSymbol textsymbol = new TextSymbolClass();
                textsymbol.Color = pColor;
                if (null == text)
                {
                    text = "Draw Text";
                }
                textsymbol.Text = "Text";
                object symbol = textsymbol;
                mapControl.DrawText(pGeom, text, ref symbol);
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("写文字失败!" + ex);
            }

        }

        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="mapControl"></param>
        /// <param name="pGeom"></param>
        /// <param name="pColor"></param>
        /// <param name="width"></param>
        public static void DrawMapShape(IMapControlDefault mapControl, IGeometry pGeom, IRgbColor pColor, int width)
        {
            try
            {
                if (null == pColor)
                {
                    pColor = new RgbColorClass();
                    pColor.Red = 255;
                    pColor.Green = 255;
                    pColor.Blue = 0;
                }

                if (width < 1 || width > 20)
                {
                    width = 5;
                }
                object symbol = null;
                if (pGeom.GeometryType == esriGeometryType.esriGeometryPolyline)
                {
                    ISimpleLineSymbol simpleLine = new SimpleLineSymbolClass();
                    simpleLine.Color = pColor;
                    simpleLine.Width = width;
                    symbol = simpleLine;
                }
                else
                {
                    ISimpleFillSymbol simpleFill = new SimpleFillSymbolClass();
                    simpleFill.Color = pColor;
                    symbol = simpleFill;
                }

                mapControl.DrawShape(pGeom, ref symbol);
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("画图失败!" + ex);
            }
        }

        /// <summary>
        /// 颜色
        /// </summary>
        /// <param name="r"></param>
        /// <param name="g"></param>
        /// <param name="b"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static IRgbColor GetColor(int r, int g, int b, int t)
        {
            IRgbColor rgbcolor = new RgbColorClass();
            rgbcolor.Red = r;
            rgbcolor.Green = g;
            rgbcolor.Blue = b;
            rgbcolor.Transparency = (byte)t;
            return rgbcolor;
        }

        /// <summary>
        /// 框选指定区域(鹰眼功能)
        /// </summary>
        /// <param name="envelope">e.NewEnvelope</param>
        /// <param name="mapControl"></param>
        public static void ShowRectangleByEnvelope(IEnvelope envelope, IMapControlDefault mapControl)
        {
            try
            {
                IGraphicsContainer graphicsContainer = mapControl.Map as IGraphicsContainer;
                IActiveView activeView = graphicsContainer as IActiveView;

                //在绘制前,清除axMapControl2中的任何图像元素
                graphicsContainer.DeleteAllElements();
                IElement element = new RectangleElementClass();
                element.Geometry = envelope;
                //设置鹰眼中的红线
                //产生一个符号对象
                ILineSymbol outLineSymbol = new SimpleLineSymbolClass();
                outLineSymbol.Width = 2;
                outLineSymbol.Color = GetColor(255, 0, 0, 255);

                //设置颜色属性
                //设置填充符号属性
                IFillSymbol fillsymbol = new SimpleFillSymbolClass();
                fillsymbol.Color = GetColor(9, 0, 0, 0);
                fillsymbol.Outline = outLineSymbol;
                IFillShapeElement fillShapeElement = element as IFillShapeElement;
                fillShapeElement.Symbol = fillsymbol;
                graphicsContainer.AddElement((IElement)fillShapeElement, 0);
                activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("框选指定区域失败!" + ex);
            }
        }

        /// <summary>
        /// 清除选择
        /// </summary>
        /// <param name="mapControl"></param>
        public static void ClearSelection(IMapControlDefault mapControl)
        {
            try
            {
                IActiveView activeView = (IActiveView)mapControl.Map;
                //清除数据集前必须先刷新
                for (int i = 0; i < mapControl.LayerCount; i++)
                {
                    activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, mapControl.get_Layer(i), null);
                    mapControl.Map.ClearSelection();
                    activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, mapControl.get_Layer(i), null);
                }
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("清除选择失败!" + ex);
            }
        }
 /// <summary>
        /// 依据指定的Geometry(Shape)选中要素
        /// </summary>
        /// <param name="mapControl"></param>
        /// <param name="geometry"></param>
        public static void SelectByShape(IMapControlDefault mapControl, IGeometry geometry)
        {
            try
            {
                mapControl.Map.SelectByShape(geometry, null, false);
                mapControl.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("依据指定的Geometry(Shape)选中要素失败!" + ex);
            }
        }

        /// <summary>
        /// 同步到MapControl控件
        /// </summary>
        public static void CopyToMapControl(IMapControlDefault mapControl, IMapControlDefault toMapControl)
        {
            try
            {
                IObjectCopy objCopy = new ObjectCopyClass();
                object copyFromMap = mapControl.Map;
                object copyMap = objCopy.Copy(copyFromMap);
                object copyToMap = toMapControl.ActiveView.FocusMap;
                objCopy.Overwrite(copyMap, ref copyToMap);
                toMapControl.Extent = mapControl.FullExtent;
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("Map间数据同步失败!" + ex);
            }

        }

        /// <summary>
        /// 同步到PageLayout控件
        /// </summary>
        public static void CopyToPageLayout(IMapControlDefault mapControl, IPageLayoutControlDefault pageLayoutControl)
        {
            try
            {
                IObjectCopy objCopy = new ObjectCopyClass();
                object copyFromMap = mapControl.Map;
                object copyMap = objCopy.Copy(copyFromMap);
                object copyToMap = pageLayoutControl.ActiveView.FocusMap;
                objCopy.Overwrite(copyMap, ref copyToMap);
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("Map与PageLayout数据同步失败!" + ex);
            }

        }

        /// <summary>
        /// 屏幕变化后刷新屏幕
        /// </summary>
        /// <param name="mapControl"></param>
        public static void AfterScreenDraw(IMapControlDefault mapControl)
        {
            try
            {
                IActiveView activeView = (IActiveView)mapControl.ActiveView.FocusMap;
                IDisplayTransformation displayTransformation = activeView.ScreenDisplay.DisplayTransformation;
                displayTransformation.VisibleBounds = mapControl.Extent;
                mapControl.ActiveView.Refresh();
            }
            catch (System.Exception ex)
            {
                MessageBoxEX.Show("刷新屏幕失败!" + ex);
            }

        }

        #region   辅助私有方法
        /// <summary>
        /// 获取指定名称的矢量图层对象
        /// </summary>
        /// <param name="layerName">图层名称</param>
        /// <param name="mapControl"></param>
        /// <returns>ILayer</returns>
        private static ILayer getFeatureLayer(IMapControlDefault mapControl,string layerName)
        {
            ILayer layer;
            for (int i = 0; i < mapControl.LayerCount; i++)
            {
                layer = mapControl.get_Layer(i);
                if (layer != null && layer.Name.Trim() == layerName)
                    return layer;
            }
            return null;
        }


        /// <summary>
        /// 显示符合条件的要素
        /// </summary>
        /// <param name="sMapCtr"></param>
        /// <param name="sFlyr"></param>
        /// <param name="sFilter"></param>
        private static void ShowByFilter(IMapControlDefault sMapCtr, IFeatureLayer sFlyr, string sFilter)
        {
            IFeatureLayerDefinition pDef = sFlyr as IFeatureLayerDefinition;
            pDef.DefinitionExpression = sFilter;
            sMapCtr.ActiveView.Refresh();
        }
        #endregion
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DXnima

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值