1、using System;using System.Collections.Generic;using System.Linq;using System.Text;using ESRI.ArcGIS.AnalysisTools;using ESRI.ArcGIS.Analyst3D;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.Display;using ESRI.ArcGIS.DisplayUI;using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.Geodat
2、abase;using ESRI.ArcGIS.Geometry;using ESRI.ArcGIS.GlobeCore;using ESRI.ArcGIS.Output;using ESRI.ArcGIS.SystemUI;using ESRI.ArcGIS.DataSourcesRaster;using ESRI.ArcGIS.DataSourcesRasterUI;using System.Windows.Forms;using System.Drawing;using ESRI.ArcGIS.DataSourcesFile;namespace AEExercisepublic clas
3、s GeoMapLoad public static IMapDocument pMapDocument;/定义地图文档接口变量public static int count=0;public static IRgbColor pColor;/设置颜色public static string text=“空“;/标注内容public static void CreateMeasuredGrid(AxPageLayoutControl axPageLayoutControl1)/设置格网点ISnapGrid pSnapGrid;IPageLayout pPageLayout = axPageLa
4、youtControl1.PageLayout;pSnapGrid = pPageLayout.SnapGrid;/获取 PageLayout 的 SnapGrid 对象pSnapGrid.VerticalSpacing = 2;/设置垂直间距pSnapGrid.HorizontalSpacing = 1;pSnapGrid.IsVisible = true;/设置为可见axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);public static void
5、 operatePageLayout(AxPageLayoutControl axPageLayoutControl1, string opt, IEnvelope pEnv,int strBarType=0)switch (opt)case “AddScale“:if (count 0)return;IScaleBar pScaleBar;IMapFrame pMapFrame;IMapSurroundFrame pMapSurroundFrame;IMapSurround pMapSurround;IElementProperties pElementPro;/产生一个 UID 对象,使用
6、它产生不同的 MapSurround 对象UID pUID = new UIDClass();pUID.Value = “ esriCarto.scalebar“;IPageLayout pPageLayout;pPageLayout = axPageLayoutControl1.PageLayout;IGraphicsContainer pGraphicsContainer;pGraphicsContainer = pPageLayout as IGraphicsContainer;IActiveView pActiveView;pActiveView = pGraphicsContaine
7、r as IActiveView;IMap pMap;pMap = pActiveView.FocusMap;/获得于地图相关的 MapFramepMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame;/产生 MapsurroundFramepMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUID, null);/依据传入参数不同使用不同类型的比例尺MessageBox.Show(strBarType.ToString();switch (strBarType)case 0:/西
8、安交互比例尺 pScaleBar = new AlternatingScaleBarClass();break;case 1:/双线交互比例尺pScaleBar = new DoubleAlternatingScaleBarClass();break;case 2:/中空式比例尺pScaleBar = new HollowScaleBarClass();break;case 3:/线式比例尺pScaleBar = new ScaleLineClass();break;case 4:/分割式比例尺pScaleBar = new SingleDivisionScaleBarClass();brea
9、k;case 5:/阶梯式比例尺pScaleBar = new SteppedScaleLineClass();break;default:pScaleBar = new ScaleLineClass();break;pScaleBar.Division = 4;pScaleBar.Divisions = 4;pScaleBar.LabelGap = 4;pScaleBar.LabelPosition = esriVertPosEnum.esriAbove;pScaleBar.Map = pMap;pScaleBar.Name = “比例尺“;pScaleBar.Subdivisions =
10、2;pScaleBar.UnitLabel = “千米“;pScaleBar.UnitLabelGap = 4;pScaleBar.UnitLabelPosition = esriScaleBarPos.esriScaleBarAbove;pScaleBar.Units = esriUnits.esriKilometers;pMapSurround = pScaleBar;pMapSurroundFrame.MapSurround = pMapSurround;pElementPro = pMapSurroundFrame as IElementProperties;pElementPro.N
11、ame = “my scalebar“;/将 MapSurrooundFrame 对象添加到控件中 axPageLayoutControl1.AddElement(pMapSurroundFrame as IElement,pEnv, Type.Missing, Type.Missing, 0);pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);break;public void OperateMapDoc(AxMapControl axMapControl1, AxMapControl axM
12、apControl2, string strOperateType)/定义打开文件对话框OpenFileDialog OpenFileDlg = new OpenFileDialog();/定义保存文件对话框SaveFileDialog SaveFileDlg = new SaveFileDialog();OpenFileDlg.Filter = “地图文档文件(*.mxd)|*.mxd“;SaveFileDlg.Filter = “地图文档文件(*.mxd)|*.mxd“;string strDocFileN = string.Empty;pMapDocument = new MapDocu
13、mentClass();/判断操作文档地图的类型switch (strOperateType)case “NewDoc“:SaveFileDlg.Title = “输入需要新建地图文档的名称“;SaveFileDlg.ShowDialog();strDocFileN = SaveFileDlg.FileName;if (strDocFileN = string.Empty)return;pMapDocument.New(strDocFileN);pMapDocument.Open(strDocFileN, “);axMapControl1.Map = pMapDocument.get_Map(
14、0);break;case “OpenDoc“:OpenFileDlg.Title = “选择需要加载的地图文档文件“;OpenFileDlg.ShowDialog();strDocFileN = OpenFileDlg.FileName;if (strDocFileN = string.Empty)return;/将数据加载入 pMapDocument 并与 map 控件联动起来pMapDocument.Open(strDocFileN, “);for (int i = 0; i pMapDocument.MapCount; i+)/遍历可能的 Map 对象axMapControl1.Map
15、 = pMapDocument.get_Map(i);/axMapControl2.Map = pMapDocument.get_Map(i);/刷新地图axMapControl1.Refresh();break;case “SaveDoc“:/判断文档是否为只读文档if (pMapDocument.get_IsReadOnly(pMapDocument.DocumentFilename) = true)MessageBox.Show(“此地图文档为只读文档!“, “ 信息提示“);return;/用相对路径保存地图文档pMapDocument.Save(pMapDocument.UsesRe
16、lativePaths, true);MessageBox.Show(“保存成功!“, “信息提示“);break;case “SaveDocAs“:SaveFileDlg.Title = “地图文档另存“;SaveFileDlg.ShowDialog();strDocFileN = SaveFileDlg.FileName;if (strDocFileN = string.Empty)return;if (strDocFileN = pMapDocument.DocumentFilename)/将修改后的地图文档保存在原文件中/用相对路径保存地图文档pMapDocument.Save(pMa
17、pDocument.UsesRelativePaths, true);MessageBox.Show(“保存成功!“, “ 信息提示“);break;else/将修改后的地图文档保存为新文件pMapDocument.SaveAs(strDocFileN, true, true);MessageBox.Show(“保存成功!“, “ 信息提示“);break;default:break;public void SetColor()pColor = getRGBColor();public void MapOperate(AxMapControl axMapControl1, IMapContro
18、lEvents2_OnMouseDownEvent e, string strOperat)if (pColor = null)pColor = new RgbColorClass();pColor.Blue = 5;pColor.Red = 213;pColor.Green = 9;switch (strOperat)case “strLKoper“:axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair;axMapControl1.Extent = axMapControl1.TrackRecta
19、ngle();axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);break;case “strMapPan“:axMapControl1.Pan();break;/绘制线case “strDrawLine“:/绘制线IMap pMap;IActiveView pActiveView;pMap = axMapControl1.Map;pActiveView = pMap as IActiveView;IPolyline pPolyline;pPolyline = axM
20、apControl1.TrackLine() as IPolyline;/产生一个 SimpleLineSymbol 符号ISimpleLineSymbol pSimpleLineSym;pSimpleLineSym = new SimpleLineSymbolClass();pSimpleLineSym.Style = esriSimpleLineStyle.esriSLSSolid;/需要用户动态选择/设置符号颜色pSimpleLineSym.Color = pColor;/需要用户动态选择pSimpleLineSym.Width = 1;/产生一个 PolylineElement 对象I
21、LineElement pLineEle;pLineEle = new LineElementClass();IElement pEle;pEle = pLineEle as IElement;pEle.Geometry = pPolyline;try /将元素添加到 Map 对象之中IGraphicsContainer pGraphicsContainer;pGraphicsContainer = pMap as IGraphicsContainer;pGraphicsContainer.AddElement(pEle, 0);pActiveView.PartialRefresh(esriV
22、iewDrawPhase.esriViewGraphics, null, null);catch(ExecutionEngineException e3) MessageBox.Show(“两点距离太近“);return;break;/绘制面case “strDrawPolygon“:/绘制面IMap pMap;IActiveView pActiveView;pMap = axMapControl1.Map;pActiveView = pMap as IActiveView;IPolygon pPolygion;pPolygion = axMapControl1.TrackPolygon()
23、as IPolygon;/产生一个 SimpleFillSymbol 符号ISimpleFillSymbol pSimpleFillSym;pSimpleFillSym = new SimpleFillSymbolClass();pSimpleFillSym.Style = esriSimpleFillStyle.esriSFSDiagonalCross;/需要用户动态选择pSimpleFillSym.Color = pColor;/需要用户动态选择/产生一个 PolygonElement 对象IFillShapeElement pPolygonEle;pPolygonEle = new Po
24、lygonElementClass();pPolygonEle.Symbol = pSimpleFillSym;IElement pEle;pEle = pPolygonEle as IElement;pEle.Geometry = pPolygion;/将元素添加到 Map 对象之中IGraphicsContainer pGraphicsContainer;pGraphicsContainer = pMap as IGraphicsContainer;pGraphicsContainer.AddElement(pEle, 0);pActiveView.PartialRefresh(esriV
25、iewDrawPhase.esriViewGraphics, null, null);break;/地图标注case “strMapLable“:/地图标注IMap pMap;IActiveView pActiveView;pMap = axMapControl1.Map;pActiveView = pMap as IActiveView;ITextElement pTextEle;IElement pEles;/建立文字符号对象,并设置相应的属性pTextEle = new TextElementClass();pTextEle.Text = text;pEles = pTextEle as
26、 IElement;/设置文字字符的几何形体属性IPoint pPoint;pPoint = new PointClass();pPoint.PutCoords(e.mapX, e.mapY);pEles.Geometry = pPoint;/添加到 Map 对象中,并刷新显示IGraphicsContainer pGraphicsContainer;pGraphicsContainer = pMap as IGraphicsContainer;pGraphicsContainer.AddElement(pEles, 0);pActiveView.PartialRefresh(esriView
27、DrawPhase.esriViewGraphics, null, null);break;case “strSelect“:/要素选择/得到一个 包络线 Envelope 对象IEnvelope pEnv;pEnv = axMapControl1.TrackRectangle();/新建选择集环境对象ISelectionEnvironment pSelectionEnv;pSelectionEnv = new SelectionEnvironmentClass();/改变选择集的默认颜色pSelectionEnv.DefaultColor = pColor;/选择要素,并将其放入选择集axM
28、apControl1.Map.SelectByShape(pEnv, pSelectionEnv, false);axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);/需要遍历所选要素break;case “LKselect“:IMap pMap;IActiveView pActiveView;pMap = axMapControl1.Map;pActiveView = pMap as IActiveView;IEnvelope pEnv;pEnv = axMap
29、Control1.TrackRectangle();ISelectionEnvironment pSelct;pSelct = new SelectionEnvironmentClass();pSelct.DefaultColor = pColor;pMap.SelectByShape(pEnv, pSelct, false);pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);break;case “PointSelect“:IMap pMap;IActiveView pActiveVi
30、ew;pMap = axMapControl1.Map;pActiveView = pMap as IActiveView;IPoint pPt;pPt = new PointClass();pPt.PutCoords(e.mapX, e.mapY);IEnumElement pEnumEle;/选择元素,其中 1 为容差IGraphicsContainer pGraphicsContainer;pGraphicsContainer = pMap as IGraphicsContainer;pEnumEle = pGraphicsContainer.LocateElements(pPt, 10
31、);IElement pElement;/获得单个元素trypElement = pEnumEle.Next();pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, pActiveView.Extent);catchreturn;break;case “strDrawPoint“:ISimpleMarkerSymbol pMarkerSymbol;pMarkerSymbol = new SimpleMarkerSymbolClass();pMarkerSymbol.Style = esriSimpleMark
32、erStyle.esriSMSCircle;pMarkerSymbol.Color = pColor;pMarkerSymbol.Angle = 60;pMarkerSymbol.Size = 6;pMarkerSymbol.Outline = true;pMarkerSymbol.OutlineSize = 2;pMarkerSymbol.OutlineColor = pColor;IPoint pPoint;pPoint = new PointClass();pPoint.PutCoords(e.mapX, e.mapY);object oMarkerSymbol = pMarkerSym
33、bol;axMapControl1.DrawShape(pPoint, ref oMarkerSymbol);break;default:break;public void OpenMap(AxPageLayoutControl axPageLayoutControl1)/定义 IMapDocument 接口变量,并实例化IMapDocument pMapDocument;pMapDocument = new MapDocumentClass();/打开文件对话框选取 Mxd 文件OpenFileDialog openFileDlg;openFileDlg = new OpenFileDial
34、og();openFileDlg.Title = “选择需要加载的文档文件“;openFileDlg.Filter = “文档文件(*.mxd)|*.mxd“;openFileDlg.ShowDialog();string strFileName = openFileDlg.FileName;/将数据载入 pMapDocument 并与 Map 控件联系起来if (strFileName=string.Empty)MessageBox.Show(“请选择“);return;pMapDocument.Open(strFileName, “);axPageLayoutControl1.PageLa
35、yout = pMapDocument.PageLayout;axPageLayoutControl1.Refresh();public void AddMap(AxMapControl axMapControl1, AxMapControl axMapControl2, string strFileName)/获取需要加载的地理文件的扩展名string strExt = System.IO.Path.GetExtension(strFileName);string strPath = System.IO.Path.GetDirectoryName(strFileName);string st
36、rName = System.IO.Path.GetFileNameWithoutExtension(strFileName);switch (strExt)case “.shp“:/strFileName 是用户选择的地图文件axMapControl1.AddShapeFile(strPath, strName);axMapControl2.AddShapeFile(strPath, strName);axMapControl2.Extent = axMapControl2.FullExtent;break;case “.mxd“:if (axMapControl1.CheckMxFile(
37、strFileName)/是 Mxd 文档文件IArray pArray;pArray = axMapControl1.ReadMxMaps(strFileName, Type.Missing);IMap pMap;for (int i = 0; i pArray.Count; i+)/QI 接口跳转pMap = pArray.get_Element(i) as IMap;if (pMap.Name = “Layers“)/加载文档对象中特定的 Map 对象axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHour
38、glass;axMapControl1.LoadMxFile(strFileName, 0, Type.Missing);axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;elseMessageBox.Show(“请选择地图文档文件!“, “ 信息提示“);return;break;case “.bmp“:case “.img“:case “.tif“:case “.jpg“:string pathName = System.IO.Path.GetDirectoryName(strFileName)
39、;/strFileName 是栅格数据文件string fileName = System.IO.Path.GetFileName(strFileName);IWorkspaceFactory pWSF;pWSF = new RasterWorkspaceFactoryClass();IWorkspace pWS;pWS = pWSF.OpenFromFile(pathName, 0);IRasterWorkspace pRWS;pRWS = pWS as IRasterWorkspace;IRasterDataset pRasterDataset;pRasterDataset = pRWS.
40、OpenRasterDataset(fileName);/影像金字塔判断与创建IRasterPyramid pRasPyrmid;pRasPyrmid = pRasterDataset as IRasterPyramid;if (pRasPyrmid != null)if (!(pRasPyrmid.Present)pRasPyrmid.Create();/在进度条中说明正在创建金字塔IRaster pRaster;pRaster = pRasterDataset.CreateDefaultRaster();IRasterLayer pRasterLayer;pRasterLayer = ne
41、w RasterLayerClass();pRasterLayer.CreateFromRaster(pRaster);ILayer pLayer = pRasterLayer as ILayer;/向 axMapControl1 控件中添加栅格图层axMapControl1.AddLayer(pLayer, 0);break;case “.dwg“:IWorkspaceFactory pCadWorkspacefactory;pCadWorkspacefactory = new CadWorkspaceFactoryClass();IFeatureWorkspace pWorkspace;p
42、Workspace =pCadWorkspacefactory.OpenFromFile(strPath,0) as IFeatureWorkspace;/打开一个要素数据集IFeatureDataset pFeatDataset;pFeatDataset = pWorkspace.OpenFeatureDataset(“PARCELS.DWG“);/pFeatClassContainer 可以管理 pFeatDataset 的每个要素 IFeatureClassContainer pFeatClassContainer;pFeatClassContainer = pFeatDataset a
43、s IFeatureClassContainer;IFeatureClass pFeatClass;IFeatureLayer pFeatLayer;int i;/对 CAD 文件中的要素进行遍历处理 for (i = 0; i = pFeatClassContainer.ClassCount - 1; i+)pFeatClass = pFeatClassContainer.get_Class(i);if (pFeatClass.FeatureType =esriFeatureType.esriFTCoverageAnnotation)/标注类型,必须设置为单位的标注图层 pFeatLayer
44、 = new CadAnnotationLayerClass();else/点线面类型pFeatLayer = new FeatureLayerClass();pFeatLayer.Name = pFeatClass.AliasName;pFeatLayer.FeatureClass = pFeatClass;axMapControl1.AddLayer(pFeatLayer, 0);axMapControl2.AddLayer(pFeatLayer, 0);axMapControl2.Extent = axMapControl2.FullExtent;break;default:break;
45、public IRgbColor getRGBColor()ColorDialog col = new ColorDialog();DialogResult d=col.ShowDialog();if (d = DialogResult.OK)return ConvertColorToIColor(col.Color);return null;public IRgbColor ConvertColorToIColor(Color color) IRgbColor pColor = new RgbColorClass();pColor.Red = color.R;pColor.Blue = color.B;pColor.Green = color.G;return pColor;