GIS应用开发课件3.ppt

上传人:苏美尔 文档编号:6226548 上传时间:2020-10-04 格式:PPT 页数:154 大小:1.37MB
返回 下载 相关 举报
GIS应用开发课件3.ppt_第1页
第1页 / 共154页
GIS应用开发课件3.ppt_第2页
第2页 / 共154页
GIS应用开发课件3.ppt_第3页
第3页 / 共154页
GIS应用开发课件3.ppt_第4页
第4页 / 共154页
GIS应用开发课件3.ppt_第5页
第5页 / 共154页
点击查看更多>>
资源描述

《GIS应用开发课件3.ppt》由会员分享,可在线阅读,更多相关《GIS应用开发课件3.ppt(154页珍藏版)》请在三一文库上搜索。

1、GIS 应 用 开 发 Developing GIS Applications with ArcObjects using C#.NET,教学目标,GIS应用开发课程是GIS研究生重要的专业课程,通过课程的学习,了解ArcGIS 软件体系结构及ArcObjects (ArcGIS Engine)组件构成,并能熟练使用ArcObjects (ArcGIS Engine) 进行应用型GIS开发; 掌握应用型GIS开发的基本思路、技术方法.,Chap. 3 使用ArcObjects控件编程(6学时),为了能够快速建构一个具有GIS功能的独立应用程序,ArcGIS Engine为开发者提供了多个可视化

2、的组件即控件,如 LicenseControl 、MapControl、PageLayoutControl、TOCControl、ToolbarControl和SymbologyControl等。 在VS .NET中使用ArcObjects,需要使用ESRI interop程序集,这些程序集在托管的.NET代码和非托管的COM代码之间起了桥梁作用。 ESRI interop程序集为ArcGIS控件提供了能够位于.NET窗体上的控件(继承自AxHost类),这些控件名前缀为“Ax”,如AxMapControl、AxPageLayoutControl、AxTOCControl和AxToolbarC

3、ontrol等。,Using ArcGIS Engine controls in Winforms Using ArcGIS Engine Controls in WPF,本章内容:,MapControl控件 PageLayoutControl控件 TOCControl控件 ToolbarControl及相关对象 Control Commands (SymbologyControl in Chapter 6) 在WPF程序中使用ArcGIS Engine控件,3.1 MapControl控件,MapControl对应于ArcMap中的数据视图,其主要功能为空间数据浏览、空间数据可视化、空间查询

4、、空间分析、空间数据编辑等。 在可视化环境中,可以通过控件的“属性”页设置控件的相关属性,也可以通过编程来设置。,MapControl是对Map对象的封装,并提供了额外的属性、方法、事件用于: 管理控件的外观、显示属性和地图属性; 添加并管理控件中的数据层(data layers); 装载Map文档(mxd)到控件中; 从其它应用程序拖放数据到控件中; 使用鼠标拉绘几何形状并绘制到屏幕显示中。,3.1.1 MapControl的主要接口,MapControl实现的主要接口有: IMapControlDefault IMapControl2 IMapControl3 IMapControl4(

5、new in 9.2 ) IMapControlEvents2事件接口 ITOCBuddy IToolbarBuddy,3.1.1.1 IMapControlDefault接口,IMapControlDefault接口是地图控件缺省接口,多数开发环境自动使用这个接口定义的属性、方法。 由于MapControl是一个自动化控件,当它被放到一个容器-如窗体上后,它会自动产生一个被称为axMapControl1的对象,这个对象可以直接使用缺省接口定义的属性和方法。这个接口也代表了控件最新版本的接口,MapControl当前最新版本接口为IMapControl4. 当需要使用这个接口的时候,可以使用下

6、面的代码: IMapControlDefault pMapControl = axMapControl1.Object as IMapControlDefault;,IMapControl2.Extent:地图的可视范围; IMapControl2.FullExtent: 地图中所有图层的覆盖范围.,3.1.1.2 IMapControl4接口,IMapControl4是任何一个与MapControl相关的任务的出发点,如设置控件外观,设置Map对象或控件的显示属性,添加或者管理图层、地图文档,在控件上绘制图形和返回Geometry等。,IMapControl2,3.1.1.3 IMapCon

7、trol3与IMapControl4,IMapControl3接口继承IMapControl2,并增加了以下8个属性和一个方法: CustomProperty -object:使用该属性关联有用信息 DocumentFilename -string):返回MapControl装入的地图文档的文件名 DocumentMap -string :返回MapControl最后装入的地图(Map)名称 KeyIntercept -int :返回或设置MapControl截取键盘按键信息,Object -object:返回潜在的MapControl控件,当在VB.NET或C#.NET中使用IMapCont

8、rol2时,一定要使用Object属性或者使用指定容器的代码。因为对于.NET,真正的控件是被包装于宿主对象里面的。 ShowMapTips -bool:确定是否显示地图的Map Tips TipDelay -int :设置Map Tips的延迟时间 TipStyle -esriTipStyle :设置Map Tips的显示样式 SuppressResizeDrawing ():在控件尺寸发生变化过程中,阻止数据实时重绘,与IMapControl3相比,IMapControl4多了以下两个可读写属性: public bool AutoKeyboardScrolling get; set;:确定

9、是否使用键盘上的Home (keyCode 36)键、End (keyCode 35) 键、Page Down (keyCode 34) 键、PageUp (keyCode 33) 键和箭头键 (keyCode 37-40) 来平移控件中的地图,缺省情况下该属性为true. 绝大多数开发环境使用箭头键来切换窗体或容器中的焦点控件;在这些开发环境中,缺省情况下MapControl 不会接收箭头键来平移地图的显示。为确保箭头键能够被MapControl接收并平移地图的显示,KeyIntercept属性应设置为拦截箭头键(esriKeyInterceptArrowKeys),且AutoKeyboar

10、dScrolling设置为true。,public bool AutoMouseWheel get; set;:确定鼠标滚轮是否用于缩放地图的显示;缺省情况下该属性为true。,引用控件本身,当使用IMapControl2接口时: IMapControl2 pMapControl = axMapControl1.Object as IMapControl2; 或IMapControl2 pMapControl = axMapControl1. GetOcx() as IMapControl2; 当使用IMapControl3接口时: IMapControl3 pMapControl = axM

11、apControl1.Object as IMapControl3; 或IMapControl3 pMapControl = axMapControl1. GetOcx() as IMapControl3;,当使用IMapControl4接口时: IMapControl4 pMapControl; pMapControl = axMapControl1.Object as IMapControl4; 或 pMapControl = axMapControl1. GetOcx() as IMapControl4;,使用箭头键、鼠标滚轮实现地图导航,使用箭头键平移(Panning)地图 axMap

12、Control1.KeyIntercept = (int) esriKeyIntercept.esriKeyInterceptArrowKeys; axMapControl1.AutoKeyboardScrolling=true; 使用鼠标滚轮缩放地图 axMapControl1.AutoMouseWheel = true;,axMapControl1.KeyIntercept = (int) esriKeyIntercept.esriKeyInterceptArrowKeys; AutoKeyboardScrolling=true;,3.1.1.4 IMapControlEvents2接口,

13、IMapControlEvents2是一个事件接口,它定义了 MapControl能够处理的全部事件,如 OnExtentUpdated是地图的 Extent(可视范围)属性发生变化时触发的事件, OnAfterscreenDraw是绘屏结束后触发的事件等。 当地图的覆盖范围变化时,触发OnFullExtentUpdated 事件,例如,往地图中新增加一个图层,其覆盖范围大于原图的范围。?,在绝大多数开发环境中,没有必要手工编写代码进行事件的注册,而只需在控件的属性窗口中双击某事件自动进行事件与其处理方法的关联。,3.1.1.5 ITOCBuddy,该接口定义了以下2个方法: GetActiv

14、eView Returns the underlying object implementing basic map and ActiveView. This is used to populate the TOC. GetScale Returns the scale of the supplied BasicMap, this allows for objects that do not support IMap.,3.1.1.6 IToolbarBuddy,Objects that implement the IToolbarBuddy interface can be passed t

15、o the IToolbarControl.SetBuddyControl method. The CurrentTool is the tool used to interact with the display area of the IToolbarControl.Buddy.,Samples: MapAndPageLayoutSynchApp MapControlMapTips MapControlGeoEvents MapControlDrawText MapControlCADViewer MapControlSaveLayerFile,Guid.ToString (string

16、format ) 命名空间:System;程序集:mscorlib format 一个单格式说明符,它指示如何格式化此 Guid 的值。format 参数可以是“N”、“D”、“B”或“P”。,3.1.2 MapControl控件与MXD文件,MapControl控件可以“链接”或“包含”地图文档。 对于文档文件,MapControl控件可以直接使用LoadMxFile方法来载入,这是最简单的方法。除此之外,可以使用IMapDocument接口定义的属性和方法来加载一个MXD文件。,下面是一个载入文档的例子-axMapControl1.LoadMxFile,private void LoadM

17、apDocument() System.Windows.Forms.OpenFileDialog openFileDialog2; openFileDialog2 = new OpenFileDialog(); openFileDialog2.Title = Open Map Document; openFileDialog2.Filter = Map Documents (*.mxd)|*.mxd; openFileDialog2.ShowDialog(); string sFilePath = openFileDialog2.FileName; if (axMapControl1.Chec

18、kMxFile(sFilePath) axMapControl1.LoadMxFile(sFilePath, 0,Type.Missing); else MessageBox.Show(sFilePath + is not a valid ArcMap document); return; ,在ArcMap中使用的地图文档对象为MxDocument,其主要接口是IMxDocument;在使用控件开发的独立应用程序中,使用的地图文档对象为MapDocument ,其主要接口是IMapDocument。 IMapDocument接口定义了操作和管理文档对象的方法和属性。MapDocument类能够

19、封装地图文档文件,如mxd、mxt和pmf等,它也可以封装 一个图层文件(*.lyr)。使用这个对象可以获取和更新一个文档的内容,设置文档文件的读、写属性,保存一个文档文件(*.mxd)。,IMxDocument 与IMapDocument,IApplication的属性、方法,IMxDocument. ActivatedView与 ActiveView 属性 在ArcMap的布局视图(layout view)状态下,图形元素可以添加到页面布局(PageLayout)或焦点地图(focus Map)的图形元素容器(graphics container )中;这时, IMxDocument.Ac

20、tiveView 属性与IMxDocument.ActivatedView 属性不同, ActiveView 属性引用PageLayout 对象, ActivatedView属性引用焦点地图对象(如果该地图对象处于活动状态) 。 在ArcMap的数据视图(data view)状态下, ActiveView 与ActivatedView属性都引用焦点地图对象。,IMapDocument m_MapDocument = new MapDocumentClass(); private void LoadMapDoc() OpenFileDialog openFileDialog2 = new Ope

21、nFileDialog(); openFileDialog2.Title = Open Map Document; openFileDialog2.Filter = Map Documents (*.mxd)|*.mxd; openFileDialog2.ShowDialog(); string sFilePath = openFileDialog2.FileName; If(m_MapDocument.get_IsMapDocument(sFilePath)) m_MapDocument.Open(sFilePath, ); axMapControl1.Map = m_MapDocument

22、.get_Map(0); axMapControl1.Refresh(); ,public void Open ( string sDocument, string bsPassword);,public bool get_IsMapDocument (string sDocument);,private void SaveDocument() if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename) = true) MessageBox.Show(This map document is read only!); ret

23、urn; m_MapDocument.Save(m_MapDocument.UsesRelativePaths,true); MessageBox.Show(Changes saved successfully!); ,public void Save ( bool bUseRelativePaths, bool bCreateThumnbail);,3.1.3 鼠标与控件的交互,用鼠标和地图控件进行交互是最常用的操作,例如改变地图显示范围、移动地图,在控件上绘制几何图形等。,使用鼠标拖曳确定地图显示的范围(拉框放大),private void axMapControl1_OnMouseDow

24、n (object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvent e) /改变地图控件显示范围为当前拖曳的区域 axMapControl1.Extent = axMapControl1.TrackRectangle(); /刷新地图axMapControl1.Refresh(esriViewDrawPhase.esriViewGeography, null, null); ,public void Refresh ( esriViewDrawPhase phase, object layerOrElemen

25、t, object envelope );,移动、旋转地图,在 MapControl中有一种更简单便利的方法pan()来移动其中的地图。 下面的方法使用在MapControl控件的OnMoseDown事件中: axMapControl1.pan(); axMapControl1.Rotation = 45;,在 MapControl控件中绘制图形,MapControl控件提供了直接在控件上绘制图形和文字的方法DrawShape与DrawText,这两种绘制方法绘制的图形都是缓存(cache),而不能真正保存,一旦窗口重绘,这些图形就将消失。,private void axMapControl1

26、_OnMouseDown (object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvent e) /产生拖曳多边形,并绘制 IGeometry pGeom = axMapControl1.TrackPolygon(); DrawMapShape(pGeom); /刷新地图/axMapControl1.Refresh(esriViewDrawPhase.esriViewGeography, null, null); ,private void DrawMapShape (IGeometry pGeom) IRgb

27、Color pColor; pColor = new RgbColorClass(); pColor.Red = 220; pColor.Green = 112; pColor.Blue = 60; /新建一个绘制图形的填充符号 ISimpleFillSymbol pFillsyl; pFillsyl = new SimpleFillSymbolClass(); pFillsyl.Color = pColor; object oFillsyl = pFillsyl; axMapControl1.DrawShape(pGeom, ref oFillsyl); ,public void DrawS

28、hape ( IGeometry Shape, ref object symbol );,3.1.4 数据选择,在MapControl控件中用户可以很方便地使用SelectByShape方法来构造一个基于Map的选择集。 SelectByShape()方法 选择控件中所有图层上处于选择范围内的要素,并将其设置为一个选择集。,private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) /产生拖曳多边形 IGeometry pGeom = axMapControl1.Tra

29、ckPolygon(); axMapControl1.Map.SelectByShape(pGeom, null, false); axMapControl1. Refresh(esriViewDrawPhase.esriViewGeoSelection,null,null); ,清除选择集的方法 axMapControl1.Map.Clearselection(); axMapControl1.ActiveView.Refresh();,3.1.5 实现鹰眼功能,这个例子中有两个AxMapControl控件:主控件axMapControl1和鹰眼控件axMapControl2. 要实现鹰眼功

30、能,关键有两点:一是如何让两个控件显示的数据保持一致,另一点是如何绘制鹰眼控件中的显示方框。,两个控件的数据共享,private void axMapControl1_OnMapReplaced (object sender, IMapControlEvents2_OnMapReplacedEvent e) IMap pMap; pMap = axMapControl1.Map; int i ; for( i = 0; i= pMap.LayerCount - 1;i+) axMapControl2.Map.AddLayer(pMap.get_Layer(i); ,绘制鹰眼控件中的显示方框,主

31、窗体视图的范围发生变化后,会触发控件的OnExtentUpdated事件,绘制方框的方法就在这个事件中进行: private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e) . ,3.2 PageLayoutControl控件,PageLayoutControl对应于ArcMap中的布局视图(Layout View),它封装了PageLayout对象,提供了在布局视图中控制地图元素的属性和方法。 PageLayoutControl控件用于制图,它可以方便地操

32、作各种元素对象,以产生一幅制作精美的地图对象。 与MapControl控件相似,PageLayoutControl控件并不仅仅只是包含了一个PageLayout对象,它也拥有许多附加的事件、属性和方法。,页面布局(通常简称为布局)是在页面上编排和组织的地图元素的集合,旨在用于地图的排版设计、打印等。布局中排布的常见地图元素包括一个或多个数据框(每个数据框都含有一组有序的地图图层)、比例尺、指北针、地图标题、描述性文本和符号图例。 打印机、页面布局、大小、单位,数据驱动页面根据地图文档的某个要素图层或规则格网将地图分割为多个部分,然后为每个部分生成一个相应的页面。,创建数据驱动页面,PageLa

33、youtControl控件同样实现了多个接口 IPageLayoutControlDefault IPageLayoutControl IPageLayoutControl2 IPageLayoutControl3 (New in 9.2) IPageLayoutControlEvents,IPageLayoutControl3,Samples: PageLayoutControlLoadMapDocument PageLayoutControlCopyFocusMap PageLayoutControlOverview PageLayoutControlPageAppearance Page

34、LayoutControlPrintPreview PageLayoutControlPrinting,3.2.1 PageLayoutControl操作 MXD文件,在AxPageLayoutControl控件对MXD文件的操作,与MapControl类似。 public void LoadMxFile ( string fileName, object password );,下面是在PageLayout控件中打开一个MXD文件的方法:,private void OpenMapDocument() IMapDocument m_MapDocument = new MapDocumentCl

35、ass(); OpenFileDialog openFileDialog1; openFileDialog1 = new OpenFileDialog(); openFileDialog1.Title = Open Map Document; openFileDialog1.Filter = Map Documents (*.mxd)|*.mxd; openFileDialog1.ShowDialog(); string sFilePath = openFileDialog1.FileName; m_MapDocument.Open(sFilePath, ); axPageLayoutCont

36、rol1.PageLayout = m_MapDocument.PageLayout; axPageLayoutControl1.Refresh(); ,private void SaveDocument() /Check that the document is not read only. if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename) = true) MessageBox.Show(This map document is read only!); return; /Save with the curren

37、t relative path setting. m_MapDocument.Save(m_MapDocument.UsesRelativePaths, true); MessageBox.Show(Changes saved successfully!); ,private void cmdSaveAs_Click(object sender, System.EventArgs e) /Open a file dialog for saving map documents. saveFileDialog1.Title = Save Map Document As; saveFileDialo

38、g1.Filter = Map Documents (*.mxd)|*.mxd; saveFileDialog1.ShowDialog(); string sFilePath = saveFileDialog1.FileName; if (sFilePath = “”) return; if (sFilePath = m_MapDocument.DocumentFilename) /Save changes to the current document. SaveDocument(); else /SaveAs a new document with relative paths. m_Ma

39、pDocument.SaveAs (sFilePath, true, true); /Open the document. OpenDocument(sFilePath); MessageBox.Show(Document saved successfully!); ,3.2.2 PageLayout与MapControl联动,在ArcMap程序中,数据视图和布局视图中的数据改变是实时互动的。 在使用ArcObjects控件开发的独立程序中实现这个功能,由于MapControl、 PageLayoutControl两个控件本身并不能自动实时互动,用户只能够通过拷贝的方法来实时传递两个控件的内容

40、。 Sample: MapAndPageLayoutSynchApp,3.3 TOCControl控件,TOCControl控件概述 ITOCControl与ITOCControl2 ITOCControlEvents 应用开发实例,3.3.1 TOCControl控件概述,TOCControl要与一个“伙伴控件” 协同工作。 “伙伴控件”可以是MapControl、PageLayoutControl、SceneControl或GlobeControl。 TOCControl用“伙伴控件”来显示其地图、图层和符号体系内容的一个交互式树视图,并保持其内容与“伙伴控件”同步。例如,如果 “伙伴控件

41、”是一个MapControl,而且从该MapControl中删除了一个图层,则该图层也会从TOCControl中删除。同样地,如果终端用户与TOCControl交互并取消了某个图层的Visibility复选框,则该图层在MapControl中不再可见。,TOCControl的“伙伴控件”可以在设计时通过TOCControl属性页设置或用SetBuddyControl方法通过编程设置。 TOCControl的主要接口有:ITOCControl 、 ITOCControl 2、ITOCControlEvents,3.3.1.1 ITOCControl与ITOCControl2,ITOCContro

42、l接口是任何与TOCControl有关的任务的出发点,如设置控件的外观,设置伙伴控件,管理图层的可见性和标签的编辑。,ITOCControl,LabelEdit 、 LayerVisibilityEdit :Label 、Visibility editing state. esriTOCControlAutomatic esriTOCControlManual,在C#.NET环境中,当查询接口到ITOCControl 或ITOCControl2时,必须使用Object属性或者容器指定代码。 ITOCControl pTOCControl; pTOCControl = axTOCControl1

43、.Object as ITOCControl; 或 pTOCControl = axTOCControl1. GetOcx() as ITOCControl;,public void HitTest (int X, int Y, ref esriTOCControlItem ItemType, ref IBasicMap BasicMap, ref ILayer Layer, ref object Unk, ref object Data );用于获取用户在TOCControl中点击的信相关息。 ItemType:TOCControl中项的类型,如none、map、layer、heading

44、或legend class. BasicMap:指定地图 对象。 Layer:ILayer 对象。 Unk :ILegendGroup(图例组,一个图层可以有多种符号化方案)对象。 Data:用户点击的图例组中某个图例的索引号(长整型);联合使用图例组Unk和这个索引号,可以获得用户点击的图例(legend class);如果这个索引号为-1,表明用户点击的是图例中的标题头。,IBasicMap map = new MapClass(); ILayer layer = new FeatureLayerClass(); object other = new object(); object in

45、dex = new object(); esriTOCControlItem item = new esriTOCControlItem(); axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index); if (e.button = 1) /判断用户是否按了鼠标左键 if (layer = null) return; IFeatureLayer featureLayer = layer as IFeatureLayer; if (featureLayer = null) return;

46、 IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer) featureLayer;,ILegendClass legendClass = new LegendClassClass(); ISymbol symbol = null; if (other is ILegendGroup ,private ISymbol GetSymbolByControl( ISymbol symbolType) ISymbol symbol = null; IStyleGalleryItem styleGalleryItem = null; esriSymb

47、ologyStyleClass styleClass = esriSymbologyStyleClass.esriStyleClassMarkerSymbols; if (symbolType is IMarkerSymbol) styleClass = esriSymbologyStyleClass.esriStyleClassMarkerSymbols; if (symbolType is ILineSymbol) styleClass = esriSymbologyStyleClass.esriStyleClassLineSymbols; if (symbolType is IFillS

48、ymbol) styleClass = esriSymbologyStyleClass.esriStyleClassFillSymbols; GetSymbolByControlForm symbolForm = new GetSymbolByControlForm(styleClass); symbolForm.ShowDialog(); styleGalleryItem = symbolForm.m_styleGalleryItem; if (styleGalleryItem = null) return null; symbol = styleGalleryItem.Item as IS

49、ymbol; symbolForm.Dispose(); this.Activate(); return symbol; ,ITOCControl2 (New in 9.2),与ITOCControl相比,ITOCControl2多了以下1个属性和2个方法: public bool EnableLayerDragDrop get; set; :指示图层是否可以拖放,以改变图层的顺序。 public void GetSelectedItem (ref esriTOCControlItem ItemType, ref IBasicMap BasicMap, ref ILayer Layer, ref object Unk, ref object Data ):返回TOCControl中被选择项。 public void SelectItem (object Unk, object Data ):用于设置TOCControl中的选择项,如:,/Selecting the focus m

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1