整理AE开发之地图加载.docx
《整理AE开发之地图加载.docx》由会员分享,可在线阅读,更多相关《整理AE开发之地图加载.docx(25页珍藏版)》请在冰点文库上搜索。
整理AE开发之地图加载
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingESRI.ArcGIS.AnalysisTools;
usingESRI.ArcGIS.Analyst3D;
usingESRI.ArcGIS.Carto;
usingESRI.ArcGIS.Controls;
usingESRI.ArcGIS.Display;
usingESRI.ArcGIS.DisplayUI;
usingESRI.ArcGIS.esriSystem;
usingESRI.ArcGIS.Geodatabase;
usingESRI.ArcGIS.Geometry;
usingESRI.ArcGIS.GlobeCore;
usingESRI.ArcGIS.Output;
usingESRI.ArcGIS.SystemUI;
usingESRI.ArcGIS.DataSourcesRaster;
usingESRI.ArcGIS.DataSourcesRasterUI;
usingSystem.Windows.Forms;
usingSystem.Drawing;
usingESRI.ArcGIS.DataSourcesFile;
namespaceAEExercise
{
publicclassGeoMapLoad
{
publicstaticIMapDocumentpMapDocument;//定义地图文档接口变量
publicstaticintcount=0;
publicstaticIRgbColorpColor;//设置颜色
publicstaticstringtext="空";//标注内容
publicstaticvoidCreateMeasuredGrid(AxPageLayoutControlaxPageLayoutControl1)
{
//设置格网点
ISnapGridpSnapGrid;
IPageLayoutpPageLayout=axPageLayoutControl1.PageLayout;
pSnapGrid=pPageLayout.SnapGrid;//获取PageLayout的SnapGrid对象
pSnapGrid.VerticalSpacing=2;//设置垂直间距
pSnapGrid.HorizontalSpacing=1;
pSnapGrid.IsVisible=true;//设置为可见
axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);
}
publicstaticvoidoperatePageLayout(AxPageLayoutControlaxPageLayoutControl1,stringopt,IEnvelopepEnv,intstrBarType=0)
{
switch(opt)
{
case"AddScale":
{
if(count>0)
return;
IScaleBarpScaleBar;
IMapFramepMapFrame;
IMapSurroundFramepMapSurroundFrame;
IMapSurroundpMapSurround;
IElementPropertiespElementPro;
//产生一个UID对象,使用它产生不同的MapSurround对象
UIDpUID=newUIDClass();
pUID.Value="esriCarto.scalebar";
IPageLayoutpPageLayout;
pPageLayout=axPageLayoutControl1.PageLayout;
IGraphicsContainerpGraphicsContainer;
pGraphicsContainer=pPageLayoutasIGraphicsContainer;
IActiveViewpActiveView;
pActiveView=pGraphicsContainerasIActiveView;
IMappMap;
pMap=pActiveView.FocusMap;
//获得于地图相关的MapFrame
pMapFrame=pGraphicsContainer.FindFrame(pMap)asIMapFrame;
//产生MapsurroundFrame
pMapSurroundFrame=pMapFrame.CreateSurroundFrame(pUID,null);
//依据传入参数不同使用不同类型的比例尺
MessageBox.Show(strBarType.ToString());
switch(strBarType)
{
case0:
//西安交互比例尺pScaleBar=newAlternatingScaleBarClass();
break;
case1:
//双线交互比例尺
pScaleBar=newDoubleAlternatingScaleBarClass();
break;
case2:
//中空式比例尺
pScaleBar=newHollowScaleBarClass();
break;
case3:
//线式比例尺
pScaleBar=newScaleLineClass();
break;
case4:
//分割式比例尺
pScaleBar=newSingleDivisionScaleBarClass();
break;
case5:
//阶梯式比例尺
pScaleBar=newSteppedScaleLineClass();
break;
default:
pScaleBar=newScaleLineClass();
break;
}
pScaleBar.Division=4;
pScaleBar.Divisions=4;
pScaleBar.LabelGap=4;
pScaleBar.LabelPosition=esriVertPosEnum.esriAbove;
pScaleBar.Map=pMap;
pScaleBar.Name="比例尺";
pScaleBar.Subdivisions=2;
pScaleBar.UnitLabel="千米";
pScaleBar.UnitLabelGap=4;
pScaleBar.UnitLabelPosition=esriScaleBarPos.esriScaleBarAbove;
pScaleBar.Units=esriUnits.esriKilometers;
pMapSurround=pScaleBar;
pMapSurroundFrame.MapSurround=pMapSurround;
pElementPro=pMapSurroundFrameasIElementProperties;
pElementPro.Name="myscalebar";
//将MapSurrooundFrame对象添加到控件中axPageLayoutControl1.AddElement(pMapSurroundFrameasIElement,
pEnv,Type.Missing,Type.Missing,0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);
break;
}
}
}
publicvoidOperateMapDoc(AxMapControlaxMapControl1,AxMapControlaxMapControl2,stringstrOperateType)
{
//定义打开文件对话框
OpenFileDialogOpenFileDlg=newOpenFileDialog();
//定义保存文件对话框
SaveFileDialogSaveFileDlg=newSaveFileDialog();
OpenFileDlg.Filter="地图文档文件(*.mxd)|*.mxd";
SaveFileDlg.Filter="地图文档文件(*.mxd)|*.mxd";
stringstrDocFileN=string.Empty;
pMapDocument=newMapDocumentClass();
//判断操作文档地图的类型
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(0);
break;
}
case"OpenDoc":
{
OpenFileDlg.Title="选择需要加载的地图文档文件";
OpenFileDlg.ShowDialog();
strDocFileN=OpenFileDlg.FileName;
if(strDocFileN==string.Empty)
return;
//将数据加载入pMapDocument并与map控件联动起来
pMapDocument.Open(strDocFileN,"");
for(inti=0;i{
//遍历可能的Map对象
axMapControl1.Map=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.UsesRelativePaths,true);
MessageBox.Show("保存成功!
","信息提示");
break;
}
case"SaveDocAs":
{
SaveFileDlg.Title="地图文档另存";
SaveFileDlg.ShowDialog();
strDocFileN=SaveFileDlg.FileName;
if(strDocFileN==string.Empty)
return;
if(strDocFileN==pMapDocument.DocumentFilename)
{
//将修改后的地图文档保存在原文件中
//用相对路径保存地图文档
pMapDocument.Save(pMapDocument.UsesRelativePaths,true);
MessageBox.Show("保存成功!
","信息提示");
break;
}
else
{
//将修改后的地图文档保存为新文件
pMapDocument.SaveAs(strDocFileN,true,true);
MessageBox.Show("保存成功!
","信息提示");
}
break;
}
default:
break;
}
}
publicvoidSetColor()
{
pColor=getRGBColor();
}
publicvoidMapOperate(AxMapControlaxMapControl1,IMapControlEvents2_OnMouseDownEvente,stringstrOperat)
{
if(pColor==null)
{
pColor=newRgbColorClass();
pColor.Blue=5;
pColor.Red=213;
pColor.Green=9;
}
switch(strOperat)
{
case"strLKoper":
{
axMapControl1.MousePointer=esriControlsMousePointer.esriPointerCrosshair;
axMapControl1.Extent=axMapControl1.TrackRectangle();
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,null,null);
break;
}
case"strMapPan":
{
axMapControl1.Pan();
break;
}
//绘制线
case"strDrawLine":
{
//绘制线
IMappMap;
IActiveViewpActiveView;
pMap=axMapControl1.Map;
pActiveView=pMapasIActiveView;
IPolylinepPolyline;
pPolyline=axMapControl1.TrackLine()asIPolyline;
//产生一个SimpleLineSymbol符号
ISimpleLineSymbolpSimpleLineSym;
pSimpleLineSym=newSimpleLineSymbolClass();
pSimpleLineSym.Style=esriSimpleLineStyle.esriSLSSolid;//需要用户动态选择
//设置符号颜色
pSimpleLineSym.Color=pColor;//需要用户动态选择
pSimpleLineSym.Width=1;
//产生一个PolylineElement对象
ILineElementpLineEle;
pLineEle=newLineElementClass();
IElementpEle;
pEle=pLineEleasIElement;
pEle.Geometry=pPolyline;
try
{//将元素添加到Map对象之中
IGraphicsContainerpGraphicsContainer;
pGraphicsContainer=pMapasIGraphicsContainer;
pGraphicsContainer.AddElement(pEle,0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);
}
catch(ExecutionEngineExceptione3)
{
MessageBox.Show("两点距离太近");
return;
}
break;
}
//绘制面
case"strDrawPolygon":
{
//绘制面
IMappMap;
IActiveViewpActiveView;
pMap=axMapControl1.Map;
pActiveView=pMapasIActiveView;
IPolygonpPolygion;
pPolygion=axMapControl1.TrackPolygon()asIPolygon;
//产生一个SimpleFillSymbol符号
ISimpleFillSymbolpSimpleFillSym;
pSimpleFillSym=newSimpleFillSymbolClass();
pSimpleFillSym.Style=esriSimpleFillStyle.esriSFSDiagonalCross;//需要用户动态选择
pSimpleFillSym.Color=pColor;//需要用户动态选择
//产生一个PolygonElement对象
IFillShapeElementpPolygonEle;
pPolygonEle=newPolygonElementClass();
pPolygonEle.Symbol=pSimpleFillSym;
IElementpEle;
pEle=pPolygonEleasIElement;
pEle.Geometry=pPolygion;
//将元素添加到Map对象之中
IGraphicsContainerpGraphicsContainer;
pGraphicsContainer=pMapasIGraphicsContainer;
pGraphicsContainer.AddElement(pEle,0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);
break;
}
//地图标注
case"strMapLable":
{
//地图标注
IMappMap;
IActiveViewpActiveView;
pMap=axMapControl1.Map;
pActiveView=pMapasIActiveView;
ITextElementpTextEle;
IElementpEles;
//建立文字符号对象,并设置相应的属性
pTextEle=newTextElementClass();
pTextEle.Text=text;
pEles=pTextEleasIElement;
//设置文字字符的几何形体属性
IPointpPoint;
pPoint=newPointClass();
pPoint.PutCoords(e.mapX,e.mapY);
pEles.Geometry=pPoint;
//添加到Map对象中,并刷新显示
IGraphicsContainerpGraphicsContainer;
pGraphicsContainer=pMapasIGraphicsContainer;
pGraphicsContainer.AddElement(pEles,0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);
break;
}
case"strSelect":
{
//要素选择
//得到一个包络线Envelope对象
IEnvelopepEnv;
pEnv=axMapControl1.TrackRectangle();
//新建选择集环境对象
ISelectionEnvironmentpSelectionEnv;
pSelectionEnv=newSelectionEnvironmentClass();
//改变选择集的默认颜色
pSelectionEnv.DefaultColor=pColor;
//选择要素,并将其放入选择集
axMapControl1.Map.SelectByShape(pEnv,pSelectionEnv,false);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection,null,null);
//需要遍历所选要素
break;
}
case"LKselect":
{
IMappMap;
IActiveViewpActiveView;
pMap=axMapControl1.Map;
pActiveView=pMapasIActiveView;
IEnvelopepEnv;
pEnv=axMapControl1.TrackRectangle();
ISelectionEnvironmentpSelct;
pSelct=newSelectionEnvironmentClass();
pSelct.DefaultColor=pColor;
pMap.SelectByShape(pEnv,pSelct,false);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection,null,null);
break;
}
case"PointSelect":
{
IMappMap;
IActiveViewpActiveView;
pMap=axMapControl1.Map;
pActiveView=pMapasIActiveView;
IP