ArcGIS Engine基础开发教程(5)

更新时间:2023-10-19 19:08:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

ArcGIS Engine基础开发教程(5)——学习地图查询

地图查询

地图查询、统计是一个GIS系统的基本的功能模块,实际上统计功能的前提也是通过查询获取结果以文字或者

图表等报表的形式展现查询结果数据。

地图查询有两种类型的查询:

1.空间查询:在地图上设置一定范围,查询这一范围内的要素。例如在地图上画一范围查询这一区域内的所有消防

栓,这一范围的所有消防栓高亮显示(或者其他显示方式)并展现出所有的消防栓的属性信息。

2.属性查询:通过一定的查询条件获取目标要素。例如我们需要查询中国行政区上人口大于5000万,同时GDP大于1万亿的省份,通过执行查询,符号条件的省份高亮显示(或者其他显示方式)并展现出这些省份的属性信息。 ArcGIS Engine9.3为开发者提供了无需写代码即可进行查询功能的Identify工具,但是这个工具是一个通用的信息查询新工具。而开发者通常需要自己开发一套符合自己业务需求更加灵活的查询功能模块,甚至一些功能不仅仅是查询信息,很多编辑编辑功能以及和业务相结合的功能开发都用到查询功能。所以很有必要了解ArcGIS Engine

的查询机制。

5.1目标

1.熟悉Cursor,FeatureCursor对象,使用IFeatureCursor接口

2.熟悉QueryFilter, SpatialFilter对象使用IQueryFilter,ISpatialFilter接口 3.熟悉要素选择集SelectionSet对象,会使用IFeatureSelection,ISelectionSet接口

4.开发一个属性查询小功能Demo, 获取符号查询条件的Feature,并IFeatureLayerDefinition接口创建一个新

的要素图层加载到Mapcontrol上

5.开发一个空间多边形查询功能的小Demo, 学会创建内存半透明图层用于显示选择范围

5.2准备工作

1.IDE:Visual Studio 2005/2008 2.ArcGIS Engine Developer kit 9.3

3.准备一份用于查询的的矢量数据(文章内有说明)

5.3Cursor对象

Cursor(游标)本质上是一个指向数据的指针,本身并不包含数据内容,它是连接到Row对象或要素对象的桥梁。 游标有三种类型,即查询游标、插入游标和更新游标。每一种游标都是通过与之相适应的方法来获得的,如Search、Insert和Update方法。更新和插入游标都需要使用一个过滤器(Filter)对象,因为它们首先必须获得需

要进行操作的要素。

Cursor对象支持的接口是ICursor,它定义了操作一个Row集合或一个Row对象的属性和方法,下面是获

得插入型、更新型和查询型游标的方法: pCursor = IFeatureClass.Insert()

使用IFeatureClass的Insert方法返回一个插入型游标,它通常用于往表中插入一条记录。

pCursor = IFeatureClass.Update()

使用IFeatureClass的Update方法会返回一个更新型游标,它用于更新或者删除一条记录。

pCursor= IFeatureClass.Search()

使用IFeatureClass的Sarch方法对表进行查询后,可以得到一个查询型Cursor对象,它指向一个或多个Row对

象。

以我们这章的内容主要使用查询游标。

FeatureCursor是Cursor的一个子类,指向一个或多个要素,它实现了IFeatureCursor接口,ArcGIS Engine

开发所对矢量图层实现查询功能都是实行IFeatureCursor。

5.4QueryFilter对象与SpatialFilter对象

在关系型数据库中,查询条件是通过SQL语句的Where子语句来完成的。在ArcGIS Engine中不能直接使用

SQL语句,但ArcGIS Engine提供了QueryFilter和SpatialFilter两个过滤器对象来配合完成查询条件的设置,

从而查询到想要的数据。

QueryFilter过滤器主要用于对属性数据查询条件的设置,它主要实现IQueryFilter接口从而实现属性

查询功能。

1.

2.

IQueryFilter pQueryFilter = new QueryFilterClass();

3. 4.

5.

6.

//设置过滤器对象的属性

pQueryFilter.WhereClause = \人口> 10000000\

复制代码

SpatialFilter过滤器主要用于空间范围查询条件的设置,它主要实现ISpatialFilter(继承 IQueryFilter接口)接口从而实现空间查询功能。

1.

2.

ISpatialFilter pSpatialFilter = new SpatialFilterClass();

3. 4.

5. 6.

//设置空间过滤器的范围(多边形)

pSpatialFilter.Geometry = pGeometry;

7. 8.

9.

//设置空间过滤器空间关系类型

10. pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;

复制代码

空间过滤器空间关系类型主要有以下几种类型如表一所示:

表1

空间过滤器空间关系类型 esriSpatialRelUndefined esriSpatialRelIntersects esriSpatialRelEnvelopeIntersects esriSpatialRelIndexIntersects esriSpatialRelTouches esriSpatialRelOverlaps 描述(A是待查询图形,B是过滤条件图形) 未定义 A与B图形相交 A的Envelope和B的Envelope相交 A与B索引相交 A与B边界相接 A与B相叠加 A与B相交(两条线相交于一点,一条线和一个面相交) A在B内 A包含B A与B空间关联 esriSpatialRelCrosses esriSpatialRelWithin esriSpatialRelContains esriSpatialRelRelation 5.5SelectionSet要素选择集对象

熟悉ArcGIS Desktop使用的开发者一定会知道当在ArcMap中显示查询结果的时,所有的符号查询

调教的要素都以蓝色高亮的形式显示在地图控件上,蓝色高亮显示表示高亮选中,这些高亮显示选中的要素对应一个要素选择集对象中。通过IFeatureSelection的SelectionSet属性可以获取选择集,

FeatureLayer对象实现了IFeatureSelection接口。

实现代码如下:

IFeatureLayer pFeatureLayer = this.axMapControl1.get_Layer(0) as IFeatureLayer;

//QI至IFeatureSelection

IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;

//创建过滤器

IQueryFilter pQueryFilter = new QueryFilterClass();

//设置过滤器对象的查询条件

pQueryFilter.WhereClause = \人口> 10000000\

//选择要素

pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew,false); //获取选择集对象

ISelectionSet pSelectinSet=pFeatureSelection.SelectionSet;

//设置选择集的符号

pFeatureSelection.SelectionSymbol = pSymbol;

5.6属性查询实例 5.6.1程序实现目标:

查询人口大于5000000的城市,并把查询结果创建为一个新的图层,如图1-3所示:

图1

图2

图3

代码片段如下:

1.

2.

private void button1_Click(object sender, EventArgs e)

3.

{

4. IFeatureLayer pFeatureLayer = this.axMapControl1.get_Layer(0) as IFeatureLayer;

5.

6.

//QI到FeatureSelection

IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;

7.

8.

9.

//创建过滤器

IQueryFilter pQueryFilter = new QueryFilterClass();

10.

11. //设置过滤器对象的查询条件

12. pQueryFilter.WhereClause = \人口> 5000000\

13. //根据查询条件选择要素

14. pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);

15.

16. //QI到ISelectionSet

17. ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet;

18.

19. if (pSelectionSet.Count > 0)

20. { 21.

22. IFeatureLayerDefinition pFDefinition = pFeatureLayer as IFeatureLayerDefinition;

23. 24. //创建新图层

25.

26. IFeatureLayer pNewFeatureLayer = pFDefinition.CreateSelectionLayer(\

null, null);

27. pNewFeatureLayer.Name = \查询结果城市\

28. axMapControl1.AddLayer(pNewFeatureLayer as ILayer);

29. } 30. } 31. code]

32.

33. 5.7空间查询实例 34. 5.7.1目标 35. 实现多边形查询功能

36. 5.7.2功能开发 37. 代码片段如下:

38.

39. 引用的命名空间:

40. 41. [code] 42. using System; 43. using System.Drawing; 44. using System.Collections; 45. using System.ComponentModel;

30. }

31. //获取透明要素层

32. IFeatureLayer pFlyr = null;

33. for (int i = 0; i < pMapCtrl.LayerCount; i++)

34. {

35. if (pMapCtrl.get_Layer(i).Name == \

36. {

37. pFlyr = pMapCtrl.get_Layer(i) as IFeatureLayer;

38. break;

39. } 40. }

41. //透明临时层不存在需要创建 42. if (pFlyr == null)

43. {

44. pFlyr = AddFeatureLayerByMemoryWS(pMapCtrl,pMapCtrl.SpatialReference);

45. if (pFlyr == null)

46. {

47. MessageBox.Show(\创建透明临时图层发生异常\\提示\MessageBoxButtons.OK,

MessageBoxIcon.Information); 48. return;

49. } 50. }

51. IFeatureClass pFC = pFlyr.FeatureClass;

52. if (bAutoClear)

53. {

54. if (pFC.FeatureCount(null) > 0)

55. {

56. IFeatureCursor pFCursor = pFC.Search(null, false);

57. if (pFCursor != null)

58. { 59.

60. IFeature pFeature = pFCursor.NextFeature();

61. if (pFeature != null)

62. {

63. while (pFeature != null)

64. {

65. pFeature.Delete();

66. pFeature = pFCursor.NextFeature();

67. } 68. }

69. System.Runtime.InteropServices.Marshal.ReleaseComObject(pFCurso

r);

70. } 71. } 72. } 73. //创建要素

74. IFeature pNFeature = pFC.CreateFeature();

75. pNFeature.Shape = pPolygon;

76. pNFeature.set_Value(pFC.FindField(\

77. pNFeature.Store();

78. pMapCtrl.Refresh(esriViewDrawPhase.esriViewGeography, pFlyr,

pFlyr.AreaOfInterest); 79. }

80. catch (Exception Err)

81. {

82. MessageBox.Show(Err.Message, \提示\

MessageBoxIcon.Information);

83. } 84. } 复制代码

1.

2. 3. 4.

5. 6.

///

/// 获取查询要素 ///

/// /// 7.

/// 符号条件要素集合

8. private List GetSeartchFeatures( IFeatureLayer pFeatureLayer,IGeometry

pGeometry)

9. {

10. try 11. {

12. List pList = new List();

13. //创建SpatialFilter空间过滤器对象

14. ISpatialFilter pSpatialFilter = new SpatialFilterClass(); 15. IQueryFilter pQueryFilter = pSpatialFilter as ISpatialFilter;

16. //设置过滤器的Geometry

17. pSpatialFilter.Geometry = pGeometry;

18. //设置空间关系类型

19. pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;

20. //获取FeatureCursor游标

21. IFeatureCursor pFeatureCursor = pFeatureLayer.Search(pQueryFilter, false);

22. //遍历FeatureCursor

23. IFeature pFeature = pFeatureCursor.NextFeature();

24. while (pFeature != null)

25. {

26. //获取要素对象 27. pList.Add(pFeature);

28. pFeature = pFeatureCursor.NextFeature();

29. }

30. System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);

31. return pList;

32. }

33. catch (Exception Err)

34. {

35. MessageBox.Show(Err.Message, \提示\

MessageBoxIcon.Information); 36. return null;

37. } 38. } 复制代码

1.

2.

bool bSearch = false; //定义bool变量用于启动多边形查询功能

3. private void button1_Click(object sender, EventArgs e)

4. 5. 6.

7.

8.

{

try {

//向地图控件添加内存图层

IFeatureLayer pFeatureLayer =

this.AddFeatureLayerByMemoryWS(this.axMapControl1,this.axMapControl1.SpatialReference);

9.

this.axMapControl1.AddLayer(pFeatureLayer);

10. //设置鼠标样式为十字丝

11. this.axMapControl1.MousePointer =

esriControlsMousePointer.esriPointerCrosshair;

12. //启动范围查询功能 13. bSearch = true;

14. } 复制代码

添加一个新的Form在Form上添加一个DataGridView控件用于显示查询结果信息。如图4所示:

1. private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)

2.

3.

{

if (bSearch) 4.

{

5.

6.

//设置鼠标样式为十字丝

this.axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; 7.

//获取画定范围的Geometry

8. IGeometry pGeometry = this.axMapControl1.TrackPolygon();

9.

//添加半透名临时图形

10. this.AddTransTempEle(this.axMapControl1, pGeometry, false); 11. IFeatureLayer pFeatureLayer=this.axMapControl1.get_Layer(1) as

IFeatureLayer;

12. //执行查询获取符号条件的要素

13. List pFList = this.GetSeartchFeatures(pFeatureLayer, pGeometry);

14.

15. attribute pAttribute = new attribute(); 16. //设置信息显示窗体中DataGridView的属性

17. //设置行数pFList.Count+1包括字段名哪一行即表头

18. pAttribute.dataGridView1.RowCount = pFList.Count+1;

19. //设置边界风格

20. pAttribute.dataGridView1.ColumnHeadersBorderStyle =

DataGridViewHeaderBorderStyle.Sunken;

21. //设置列数

22. pAttribute.dataGridView1.ColumnCount = pFList[0].Fields.FieldCount;

23. //遍历第一个要素的字段用于给列头赋值(字段的名称) 24. for(int m=0;m

25. {

26. pAttribute.dataGridView1.Columns[m].HeaderText =

pFList[0].Fields.get_Field(m).AliasName;

27. } 28. //遍历要素

29. for (int i = 0; i < pFList.Count; i++)

30. {

31. IFeature pFeature=pFList;

32. for(int j=0;j

33. {

34. //填充字段值

35. pAttribute.dataGridView1[j, i].Value =

pFeature.get_Value(j).ToString();

36. } 37. }

38. pAttribute.Show();

39. 40. } 41. } 复制代码

5.7.3功能演示 1.单击按钮向地图控件上添加一内存图层,并启动多边形查询功能如图5示:

图5

2.点击鼠标左键在地图上画以多边形范围,双击结束,将看到在所画的范围以半透明的形式添加在地图之

上,并显示出查询结果的属性信息如图所示:

图6

5.7.4 Demo总结:

1.使用IWorkspaceFactory,IWorkspaceName,IName,IWorkspace等接口以及InMemoryWorkspaceFactoryClass对象

创建在程序运行时的内存中的矢量要素层

2.复习IUniqueValueRenderer等接口以及UniqueValueRendererClass对象唯一值符号化要素图层

3.使用ILayerEffects接口创建半透明效果要素图层

4.使用ISpatialFilter,IQueryFilter 等接口以及SpatialFilterClass对象获取符号条件的FeatureCursor,并

学会通过遍历整个FeatureCursor中Feature对象,获取符号查询条件的Feature集合

5.使用IFeatureLayerDefinition根据要素选择集创建新要素图层 5.8附IFeatureClass.Search(IQuery Filter,bool Recycling)参数说明 FeatureClass的Search方法大家经常用到,很多人对Search方法的其中一个参数bool Recycling不是很理解。

先做以下解释:

1.pFeatureCursor存储的是所有符合条件的Feature对象的引用,

2.Recycling的意思是回收,参数Recycling为True的时当执行这个方法IFeature

pFeature=pFeatureCursor.NextFeature()上一条记录的图形值在内存中所占的地址就会被销毁回收,为False的时候当执行这个方法IFeature pFeature=pFeatureCursor.NextFeature()上一条记录的图形值依然存在在内存中。 1.参数Recycling设置为True

IFeatureClass pFeatureClass = (this.axMapControl1.get_Layer(0) as IFeatureLayer).FeatureClass; IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true); //IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false); List pFeatures = new List(); IFeature pFeature = pFeatureCursor.NextFeature(); while (pFeature != null) {

pFeatures.Add(pFeature);

pFeature = pFeatureCursor.NextFeature(); }

for (int i = 0; i < pFeatures.Count; i++) {

MessageBox.Show(((pFeatures.Shape as IPolygon) as IArea).Area.ToString()); }

分析:pFeatures集合存储了指向FeatureClass上所有Feature的引用,但是由于Recycling参数设置为TRUE也就是说每执行一个NextFeautre方法上一条记录的Feature值在内存中被回收,所以到最后遍历pFeatures集合的时候所有的IFeature引用指向的Feature对象都为Null。所以会引发一下错误(如下图7所示):

图7

.参数Recycling设置为False

IFeatureClass pFeatureClass = (this.axMapControl1.get_Layer(0) as IFeatureLayer).FeatureClass;

//IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true); IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);

List pFeatures = new List(); IFeature pFeature = pFeatureCursor.NextFeature();

while (pFeature != null)

{

pFeatures.Add(pFeature);

pFeature = pFeatureCursor.NextFeature();

}

for (int i = 0; i < pFeatures.Count; i++)

{

MessageBox.Show(((pFeatures.Shape as IPolygon) as IArea).Area.ToString());

}

分析:pFeatures集合存储了指向FeatureClass上所有Feature的引用,但是由于Recycling参数设置为False也就是说每执行一个NextFeautre方法上一条记录的Feature值在内存中依然存在,所以到最后遍历pFeatures集合的时候所有的IFeature引用指向的Feature对象都依然存在。所以会执行的很Happy(如下图8所示): 图8

[专家讲座] Geodatabase and ArcSDE系列讲座 第一部分:Geodatabase的基础知识(之一)

Geodatabase能将空间数据存储在文件、MDB文件或者大型DBMS中。以上三种存储方式的区别在于可存储数据量的不同以及可支持的并发用户数量不同,能够实现从小数据量、单用户的文件数据库到大数据量、多用户并发编辑的企业级DBMS的不同层次的应用。

Geodatabase事实上是很多Geographic Dataset的集合,最基本的Dataset的类型包括Feature Classes、Raster Dataset、Attribute Tables。在以上基本要素的基础上,还能定义Geodatabase的Schema、数据的完整性、规则和行为,包括Spatial Reference、Spatial Resolution、Topology、Network、Domain等等。在创建Geodatabase时,首先生成不同的Dataset类型,然后添加或者扩展Geodatabase基本要素的能力,例如添加拓扑、网络、子类以实现GIS行为建模、维护数据完整性以及建立空间关系。

不同的数据库中,存储矢量和栅格数据几何特征的字段在是不一样的,如果DBMS能够支持Spatial type Extensions,则能够很容易

采用这种方式存储空间几何特征。目前有三种DBMS能够支持Geodatabase的Spatial Type:

(1)Oracle using the ESRI spatial type or optionally the Oracle Spatial type;

(2)IBM DB2 using the Spatial Extender Geometry Object; (3)Informix using the Spatial DataBlade Geometry Object。

Geodatabase将地理要素以表格的形式存储,每行记录代表一个要素。在Geodatabase的数据表支持的数据类型包括:

Numbers:包括Short Integers、Long Integers、Single-Precision floating points、Double-Precision Floating Point Numbers;

Text; Date;

BLOB’S;Binary Large Objects用来存储和管理二进制信息,例如Symbols和CAD几何特征; Globe Identifiers:Globe Unique Identifiers。GlobeID和GUID用来唯一标识一行记录。

GIS大讲堂内网友共享资源集锦(更新至2008年6月26日)

GIS基本概念与ArcGIS使用

GIS基本概念集锦 Cleen (风过无痕)

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=14703&extra=page=3

地理信息系统(GIS)与国土资源管理

Cleen (风过无痕)

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=14704&extra=page=6

初探时态GIS zz

yyilyzbc

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=9202&extra=page=7

3S基础知识普及——基本术语

By 骆驼人

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=3772&extra=page=4

将栅格数据从8.3升级到9.0

By 豆豆

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=3948&extra=page=6

推荐ArcGIS在线学习地址

By lxl

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=3988&extra=page=6

ArcGIS 坐标系统文件

By flyingis

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=6249&extra=page=1

ArcGIS中对矢量和栅格数据进行才裁剪切割的方法

heqjxiaoyao

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=14489&extra=page=4

距离与缓冲区 bathymetry

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=4734&extra=page=2

ArcGIS92之后GISer的学习方向

wisdomy04

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=17685&extra=page=1

SOA,你还等什么?

Mr.GIS

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=17579&extra=page=1

公共厕所的服务区

zc2006

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=23226&highlight=

SOA概念与基础----转 Cleen (love ztgame)

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=18373&highlight=

GIS也要SOA

kk2

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=21954&highlight=

GIS融合SOA 拓展应用范畴和服务领域(消息类)

kk2

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=21737&highlight=

《 适宜性选址模型的建立》ESRI王珏工程师图片精讲

xhxie04

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=18414&highlight=

ARCMAP学习资料

换了心情

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=20890&highlight=

我搜集的GIS例题(GIS空间站)

换了心情

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=20923&highlight=

GIS实际例题(GIS空间站)

换了心情

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=20925&highlight=

GIS网络分析功能的实现

kk2

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=21966&highlight=

GIS工具软件介绍

kk2

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=21969&highlight=

ArcGIS 开发

如何更好的使用ArcGIS帮助

Cleen (风过无痕)

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=17727&extra=page=1

收集到的engine资料

cxfwws

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=8174&extra=page=3

关于ArcEngine的论文

junjiang83

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=15380&extra=page=4

使用 ArcGIS Engine Runtime 制作安装包

图11

如下代码演示如何为PageLayout添加Legend:

1.

2.

3.

///

/// 为PageLayout对象添加图例对象

4.

///

5. /// ///

6.

7.

private void AddLegendToPageLayout(IPageLayout pPageLayout,IEnvelope pEnvelope)

8. 9.

{

try

10. {

11. IActiveView pActiveView=pPageLayout as IActiveView; 12. IMap pMap = pActiveView.FocusMap;

13. IGraphicsContainer pGraphicsContainer=pActiveView as IGraphicsContainer; 14. IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame;

15. UID pUID = new UID();

16. pUID.Value = \

17. ISymbolBackground pSymbolBackground = new SymbolBackgroundClass();

18. IFillSymbol pFillSymbol = new SimpleFillSymbolClass(); 19. ILineSymbol pLineSymbol = new SimpleLineSymbolClass();

20. pFillSymbol.Color = GetRgbColor(255, 255, 255); 21. pLineSymbol.Color = GetRgbColor(255, 255, 255);

22. pFillSymbol.Outline = pLineSymbol; 23. pSymbolBackground.FillSymbol = pFillSymbol;

24. IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUID, null);

25. pMapSurroundFrame.Background = pSymbolBackground; 26. IElement pElement = pMapSurroundFrame as IElement;

27. pElement.Geometry = pEnvelope;

28. IMapSurround pMapSurround = pMapSurroundFrame.MapSurround;

29. ILegend pLegend = pMapSurround as ILegend;

30. pLegend.ClearItems(); 31. pLegend.Title = \图例\

32. ITextSymbol pTextSymbol = new TextSymbolClass();

33. pTextSymbol.Size = 10;

34. pTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft;

35. ILegendItem pLegendItem = null;

36.

37. for (int i = 0; i < pActiveView.FocusMap.LayerCount; i++)

38. {

39. ILayer pLayer = pActiveView.FocusMap.get_Layer(i);

40. if (pLayer is IFeatureLayer)

41. {

42. IFeatureLayer pFLayer = pLayer as IFeatureLayer; 43. IFeatureClass pFeatureClass = pFLayer.FeatureClass;

44. if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)

45. {

46. continue;

47. } 48. else 49. {

50. pLegendItem = new HorizontalLegendItemClass();

51. pLegendItem.Layer = pLayer; 52. pLegendItem.Columns = 1;

53. pLegendItem.ShowDescriptions = false; 54. pLegendItem.ShowHeading = false; 55. pLegendItem.ShowLabels = true;

56. pLegendItem.LayerNameSymbol = pTextSymbol;

57. pLegend.AddItem(pLegendItem);

58. } 59. }

60. }

61. }

62. catch(Exception Err)

63. { 64. 65. } 66. }

复制代码

alphagis (阿尔祭祀)

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=14998&extra=page=5

ArcEngine导出图片 gis_wangchao

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=10714&extra=page=5

VBA+AO入门50例(转载)

chenyuanyuan

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=8756&extra=page=5

什么是GIS.NET Cleen (风过无痕)

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=14727&extra=page=6

server9.2案例 leonidxy

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=12843&highlight=+leonidxy

GIS项目开发文档 By geyunfa_2006

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=7377&extra=page=2

Ajax中文手册,CSS2中文手册,javascript初级教程教程下载

gispeng (GIS云中飞鹏)

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=11547&extra=page=1

常见的和许可相关的问题(有些甚至是怪问题)

IsNotNull (IsNotNull)

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=12806&extra=page=2

如何编写高质量“gis软件需求说明书”

kk2

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=21972&highlight=

用GeoTools读取Shapefile的属性数据(转)

geyunfa_2006

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=20079&highlight=

空间数据库

WebGIS 空间数据库 Cleen (风过无痕)

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=15037&extra=page=2

时空数据库设计

yyilyzbc

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=9184&extra=page=2

空间数据库建设案例分析

xhxie04

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=17229&extra=page=1

适宜性选址模型的建立

xhxie04

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=17227&extra=page=1

关于数字房产数据库设计研究文章

xiaoxiao_nb

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=8530&extra=page=1

其它

地理信息系统设计与标准化

donballking

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=17719&extra=page=1

GIS英文词汇翻译 快乐的日光

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=18508&highlight=

关于 ESRI 公司 培训课程的讲义资料的共享

wangwei02135 (星风雪雨)

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=17339&highlight=

校园网上最佳资源--地理信息系统

换了心情

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=21075&highlight=

AO入门的PPT文档(不是ESRI的)

gisbegin

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=24374&highlight=

ArcOjects 3D开发方法简介

luyuancyj

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=24433&highlight=

Geodatabase组织结构

luyuancyj

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=24434&highlight=

扫描矢量化软件R2V用法小结

kerry

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=24571&highlight=

我国4种比例尺DEM及其空间分辨率的对应关系

xiaolin1983

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=25003&highlight=

地图学基本知识 xiaolin1983

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=25086&highlight=

GIS三维地景仿真设计之建模及场景渲染

donballking

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=25631&highlight=

开发GIS项目的步骤

lutao2000

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=25912&highlight=

Geographic and Projected Coordinate Systems

陈奂生

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=26291&highlight=

地理信息系统词汇表(补充)

jiqimao-2005

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=26457&highlight=

基于GIS技术的滑坡监测分析及预测模型

jiqimao-2005

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=26476&highlight=

地图学专业词汇 jiqimao-2005

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=26654&highlight=

陈建春VC开发GIS的源码

heavencolt

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=27448&highlight=

地理信息系统比较牛的网站(原创)---之可以了解专业前沿的地方

jiandan0315

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=28329&highlight=

地理信息系统比较牛的网站(原创)---之可以丰富你的GIS知识

jiandan0315

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=28330&highlight=

最好的地理网站 jiandan0315

http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=28360&highlight= http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=28361&highlight= http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=28362&highlight=

地理信息系统数据库设计与管理

ArcGIS Engine基础开发教程(3)——学习地图和地图布局

本帖最后由 fxlcoco 于 2010-3-9 09:51 编辑

ESRI BBS GIS大讲堂首发,转载请注明出处

本章是ArcGIS Engine基础开发教程的第三章,主要给大家大致讲解了,地图和地图布局的相关对象。这些对象非

常重要,如果这些基本对象都不熟悉就开始搞ArcGIS Engine开发是不科学的也走不远的。

3地图和地图布局

3.1地图

3.1.1Map对象

Map对象是ArcGIS Engine的主要对象,本章将给大家介绍地图组成结构,如何创建地图,如何对组成地图的对象

进行操作,在介绍这些内容时尽量结合ArcMap学习效果会更好。

在Map对象上能显示的图形有两类一类是地理数据,一类是元素。地理数据包括矢量类型的要素数据,栅格数据,Tin等表面数据等,这些数据都保存在Geodatabase或数据文件如shapefile中,它们是用于GIS分析制图的源数

据; 图1

而元素是另一种可以显示在Map上的对象,它分为两部分一类是图形元素,一类是框架元素,图形元素可以显示出来,而框架元素充当了“容器”的角色。在ArcMap中可以使用“Draw”工具在Map上直接绘制一个矩形,圆形,

文本等对象,在布局视图上也可以给地图添加指北针,图例,比例尺等图形对象,这些都是图形元素。

图2

在ArcMap中Map对象是由MXDocument对象的MapFrame对象管理,MapFrame是一个框架元素。

Map对象有双重身份,一方面是数据的管理容器,可以加载地理数据和图形元素,扮演了数据管理器的角色,另一方面它又可以让用户看到这些数据即扮演了数据显示器的角色,当加载数据到Map对象的时候,Map对象是数据的

管理者;当改变视图范围,刷新地图的时候它是数据的显示者。

Map对象主要实现的接口由IMap, IGraphicContainer, IActiveView, IMapBookmarks等接口。

IMap接口主要用于管理Map对象中的Layer对象,要素选择集对象,MapSourround对象,空间参考等对象。

Map对象可以显示图形元素(Graphics Element),Map对象通过IGraphicsContainer接口来管理这些元素对象,包括图形元素和框架元素。IGraphicsContainer返回的是Map对象中处于活动状态的Graphics layer引用指针,它可以是一个Basicgraphicslayer,也可以是CompositeGraphicsLayer中的一个图层,或者是一个FDOGraphicsLayer

注记图层。

以下代码片段演示如何添加一个Element到Map上:

1. 2. 3.

4.

///

///添加临时元素到地图窗口上 5.

///

6. /// ///

7. 8.

9.

///

public static void AddTempElement(AxMapControl pMapCtrl, IElement pEle, IElementCollection

pEleColl) 10. { 11. try 12. {

13. IMap pMap = pMapCtrl.Map;

14. IGraphicsContainer pGCs = pMap as IGraphicsContainer;

15. if (pEle != null) 16. pGCs.AddElement(pEle, 0);

17.

18. if (pEleColl != null) 19. if (pEleColl.Count > 0)

20. pGCs.AddElements(pEleColl, 0);

21. IActiveView pAV = (IActiveView)pMap;

22. //需要刷新才能即时显示

23. pAV.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, pAV.Extent);

24. }

25. catch (Exception Err)

26. {

27. MessageBox.Show(Err.Message, \提示\

28. } 29. } 复制代码

IActiveView接口定义了Map对象的数据显示功能。使用该接口可以改变视图的范围,刷新视图。

IActiveView的PartialRefresh(esriViewGeography, pLayer, null)用于刷新指定图层: IActiveView的PartialRefresh(esriViewGeography, null, null) 用于刷新刷新所有图层: IActiveView的PartialRefresh(esriViewGeoSelection, null, null) 用于刷新所选择的对象:

IActiveView的PartialRefresh(esriViewGraphics, null, null) 用于刷新图形元素: IActiveView的PartialRefresh(esriViewGraphics, pElement, null) 用于刷新指定图形元素 IActiveView的PartialRefresh(esriViewGraphics, null, null) 用于刷新所有图形元素 IActiveView的PartialRefresh(esriViewGraphicSelection, null, null)用于刷新所选择的图元。

IMapBookmark接口用于管理所有的空间书签对象。

3.1.2图层对象

Map对象可以装载地理数据,这些数据是以图层的形式加载到地图对象上的,图层对象Layer作为一个数据的“中介”存在,它本身没有转载地理数据,而仅仅是获得了数据的引用,用于管理数据源的连接。地理数据始终保存在

Geodatabase或者地理数据文件中。

由于地理数据的类型多样,所以Layer类也拥有众多子类(具体查看帮助),接下来主要给大家介绍要素图层

—FeatureLayer对象。

3.1.3FeatureLayer对象

FeatureLayer类实现的常用主要接口如下图所示:

IFeatureLayer,IFeatureLayerDefinition,FeatureSelection,IGeoDataset,IGeoFeatureLayer,IIdentify,ILaye

rEffects。

图3

IFeatureLayer接口主要用于设置要素图层的数据源(FeatureClass), IFeatureLayer的DataSourceType获取FeatureLayer对象的数据源类型。此外通过IFeatureLayer的Search方法可以查询要素图层上符号某一条件的要

素集。

IGeoFeatureLayer接口继承了ILayer和IFeatureLayer两个接口,用于控制要素图层的符号化和标注等。

IGeoDataset接口仅有两个属性,它们用于管理地理要素集。Extent可以返回当前数据集的范围,是一个IEnvelope

类型的对象;

SpatialReference属性则可以让用户获得这个数据集的空间参考。

IFeatureSelection接口提供管理一个图层中的要素的选择集的方法和属性。

以下代码片段演示如何获取要素图层符合条件获取选择集

1.

2.

3.

/// ///

/// 获取要素图层符合条件获取选择集

4.

///

5. 6.

/// /// 7.

/// 返回选择集

8. private IFeatureSelection SelectLayersFeatures(IFeatureLayer pFeatureLayer, string

WhereClause) 9.

{

10. IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;

11. if (pFeatureSelection == null) return null; 12. IQueryFilter pQueryFilter = new QueryFilterClass();

13. pQueryFilter.WhereClause = WhereClause; 14. pFeatureSelection.SelectFeatures(pQueryFilter,

esriSelectionResultEnum.esriSelectionResultNew, false);

15. return pFeatureSelection;

16. }

复制代码

IFeatureLayerDefinition接口定义了CreateSelectionLayer方法,可以将一个图层选择集中的要素转换为一个单独的要素图层。

3.1.4矢量图层操作实例

以下代码片段演示按行政区加载图层数据的功能,以此理解IFeatureLayerDefinition的CreateSelectionLayer方法的使用。

比方说整个国土资源部有某一类型专题数据有若干图层,每个图层数据的范围是整个中国,这时国土资源部开发一套系统可能有这个需求,每个省只能加载本省的数据。以下函数即为按按行政区范围创建行政区范围的图层。

比方说我想获取河南省的数据只需传入行政区图层中河南省图元的Geometry即可。另外如果如果遇到行政区层必须用esriSpatialRelEnum. esriSpatialRelContains这个参数,否则行政区这个图层就不能只获取河南这个省份,它会把河南省的周围省份,河北,湖北,安徽,陕西,山东,山西等省份的图元也添加创建到新图层。

1.

2.

3.

///

/// 按行政区范围创建行政区范围的图层

4.

///

5. /// /// /// 8.

/// 新创建的图层

6. 7.

9. private IFeatureLayer GetSelectionLayer(IFeatureLayer pFeatureLayer, IGeometry

pGeometry,bool bXZQ) 10. { 11. try 12. {

13. if (pFeatureLayer != null && pGeometry != null)

14. {

15. IQueryFilter pQueryFilter;

16. ISpatialFilter pSpatialFilter = new SpatialFilterClass();

17. IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;

18. pSpatialFilter.GeometryField =

pFeatureLayer.FeatureClass.ShapeFieldName;

19. pFeatureSelection.Clear();

20. if (!bXZQ)

21. {

22. pSpatialFilter.Geometry = pGeometry;

23. pSpatialFilter.SpatialRel =

esriSpatialRelEnum.esriSpatialRelIntersects;

24. pQueryFilter = pSpatialFilter;

25. pFeatureSelection.SelectFeatures(pQueryFilter,

esriSelectionResultEnum.esriSelectionResultNew, false);

26. } 27. else 28. {

29. pSpatialFilter.SpatialRel =

esriSpatialRelEnum.esriSpatialRelContains;

30. pQueryFilter = pSpatialFilter;

31. if(pGeometry is IGeometryCollection)

32. {

33. for (int i = 0; i < (pGeometry as

IGeometryCollection).GeometryCount; i++)

34. {

35. pSpatialFilter.Geometry = (pGeometry as

IGeometryCollection).get_Geometry(i);

36. pFeatureSelection.SelectFeatures(pQueryFilter,

esriSelectionResultEnum.esriSelectionResultAdd, false);

37. } 38. }

39. }

40. IFeatureLayerDefinition pFLDefinition = pFeatureLayer as

IFeatureLayerDefinition;

41. IFeatureLayer pNewFeatureLayer =

pFLDefinition.CreateSelectionLayer(pFeatureLayer.Name ,true, null, null);

42. pNewFeatureLayer.MaximumScale = pFeatureLayer.MaximumScale; 43. pNewFeatureLayer.MinimumScale = pFeatureLayer.MinimumScale;

44. pNewFeatureLayer.Selectable = pFeatureLayer.Selectable;

45. pNewFeatureLayer.Visible = pFeatureLayer.Visible;

46. pNewFeatureLayer.ScaleSymbols = pFeatureLayer.ScaleSymbols;

47. return pNewFeatureLayer;

48. } 49. else 50. {

51. return null;

52. } 53. }

54. catch (Exception Err)

55. {

56. MessageBox.Show(Err.Message, \获取SelectionLayer\

MessageBoxIcon.Information); 57. return null;

58. } 59. }

复制代码

IIdentify接口定义了获得要素图层单个要素的属性的

ILayerFields接口可以直接获取一个要素图层的要素类字段集合

ILayerEffects接口用来设置一个要素图层的透明度,对比度,对比度。以下代码片段演示如何设置要素图层特效:

1.

2. 3. 4.

5.

///

/// 设置图层特效 ///

///

6. 7. 8.

9.

/// ///

///

private static void SetLayerEffects(IFeatureLayer pFeatureLayer, short brightness,

short contrast, short transparency)

10. {

11. ILayerEffects pLayerEffect = pFeatureLayer as ILayerEffects;

12. pLayerEffect.Brightness = brightness; 13. pLayerEffect.Contrast = contrast; 14. pLayerEffect.Transparency = transparency;

15. }

复制代码

3.2地图布局

3.2.1PageLayout对象

PageLayout用以显示地图数据,并通过对地图数据进行整饰以便对地图打印输出满足不同行业对GIS出图功能的需求。PageLayout和Map这两个对象看起来非常相似,它们都是视图对象,可以显示地图;也都是图形元素的容器,可以容纳图形元素(Graphics Element)。但是所能够保存的图形类型却是有差别的。PageLayout除了保存图形元素外,还可以保存诸如MapFrame的框架元素(Frame Element)。PageLayout控件上的Map对象被PageLayout的MapFrame对象所管理的。

PageLayout类主要实现了IPageLayout接口,它定义了用于修改页面版式(layout)的方法和属性。

IPageLayout的方法ZoomToWhole方法可以让PageLayout以最大尺寸显示;

IPageLayout的ZoomToPercent方法可以按照输入的比例显示;

IPageLayout的ZoomToWidth方法可以让视图显示的范围匹配控件对象的宽度。

IPageLayout的Page属性用以获取Page对象

IPageLayout的RulerSettings属性用以获取RulerSettings对象

IPageLayout的HorizontalSnapGuides和VerticalSnapGuides属性用以获取SnapGuides对象 如下图所示:

图4

3.2.2Page对象

Page对象主要来管理PageLayout对象中的页面,它用来加载地理数据,但不提供分析和查询功能。Page类的主要接口是IPage,它除了用于管理Page的颜色,尺寸和方向,还可以管理边框类型和打印区域等。例如通过IPage

的FromID可以直接设置纸张大小类型(A4,A3等)。

3.2.3SnapGrid对象

SnapGrid是PageLayout上用于摆放元素而设置的辅助点如图所示,这些点有规则呈网状排列,便于使用者对其元

素,它可以直接通过IPageLayout的SnapGrid属性获得当前PageLayout使用的SnapGrid对象的引用。

SnapGrid类主要实现了ISnapGrid接口用于设置SnapGrid对象的属性,ISnapGrid的HorizontalSpacing 和VerticalSpacing属性用于设置网点之间的水平距离和垂直距离,ISnapGrid的Visible属性决定了这些网点是否

可见,Draw方法将用于在Page对象上绘制一个SnapGrid对象。

以下代码片段演示如何在设置PageLayout控件上SnapGrid:

1.

2.

///

/// 设置PageLayout上SnapGrid

3.

///

4. 5.

///

private void SetwSnapGridOnPageLayout(IPageLayout pPageLayout)

6. 7.

{

8. if(pPageLayout !=null)

9.

{

10. ISnapGrid pSnapGrid=pPageLayout.SnapGrid;

11. pSnapGrid.VerticalSpacing =2; 12. pSnapGrid.HorizontalSpacing =2;

13. pSnapGrid.IsVisible =true;

14. IActiveView pActiveView=pPageLayout as IActiveView;

15. pActiveView.Refresh();

16. }

17. } 复制代码

3.2.4SnapGuides对象

SnapGuides是为了更好的放置地图而在Pagelayout上设置的辅助线如图所示,SnapGuides分为两种类型一种是水平辅助线通过IPageLayout的HorizontalSnapGuides属性获得,另一种是垂直辅助线通过IPageLayout的VerticalSnapGuides属性获得。每个SnapGuides都管理着一个Guide集合,即不同的类型的辅助线可以同时存在

多条。

SnapGuides都实现了ISnapGuides接口,它定义了管理SnapGuide的属性和方法。

ISnapGuides的AreVisible设定SnapGuides是否可见。

ISnapGuides的GuideCount属性返回一个SnapGuides对象中的Guide的个数。而使用

ISnapGuides的Guide属性可以按索引值获得某个具体的Guide对象。

ISnapGuides的AddGuide方法将一个Guide放在指定位置上。

ISnapGuides的RemoveAllGuides和RemoveGuide方法分别可以清楚所有的Guide和按索引值清除Guide.

以下代码片段演示如何为PageLayout对象添加辅助线:

1.

2.

3.

///

/// 向PageLayout上指定位置添加辅助线

4.

///

5. ///

6.

7. 8.

///

/// private void AddGuideOnPageLayout(IPageLayout pPageLayout,double pPoistion,bool

bHorizontal) 9.

{

10. try 11. {

12. if(pPageLayout !=null)

13. {

14. ISnapGuides pSnapGuides=null;

15. //如果是水平辅助线 16. if(bHorizontal)

17. {

18. pSnapGuides=pPageLayout.HorizontalSnapGuides;

19. }

20. //如果是垂直辅助线

21. else 22. {

23. pSnapGuides=pPageLayout.VerticalSnapGuides;

24. }

25. if(pSnapGuides!=null)

26. {

27. //向PageLayout上添加辅助线 28. pSnapGuides.AddGuide(pPoistion);

29. } 30. } 31. }

32. catch(Exception Err)

33. {

34. MessageBox.Show(Err.Message,\提示\

MessageBoxIcon.Information);

35. }

36. }

复制代码

3.2.5RulerSettings对象

标尺对象是为了辅助图形元素的放置而出现在PageLayout对象上方和左方的辅助尺如图所示,通过IPageLayout

的RulerSettings属性可以获得PageLayout上的RulerSettings对象.

RulerSettings对象主要实现了IRulerSettings接口它只有一个SmallestDivision属性用于设置RulerSettings

对象的最小刻度值属性。

3.2.6Element对象

在Map对象和PageLayout对象显示的数据除了地理数据之外,另一种即为元素数据如下图5所示。

图5

Element是一个非常庞大复杂的对象集合如图6所示,它主要分为两大部分:图形元素(GraphicElement)和框架元

素(Frame Element)。

图形元素(GraphicElement)包LineElement, MarkerElement, TextElement, GroupElement,

FillshapElement,PictureElement,MultiPatchElement等。它们都是作为图形的形式而存在,在地图视图或者PageLayout视图上时可见的。框架元素(Frame Element)包括MapFrameElement, MapSurroundElement等它们是作

为不可见的容器而存在的。

Map对象或者PageLayout对象可以通过IGraphicsContainer接口来管理这些元素,使用IGraphicsContainer接口可以添加,删除和更新位于Map或PageLayout上的元素。使用GroupElement对象还可以将多个元素编组为单个实

体来给用户使用。

IElement是所有图形元素(GraphicElement)和框架元素(Frame Element)都实现的接口,通过IElement接口可以确定Element对象的Geometry属性,同时IElment接口也提供了用于查找和绘制元素的方法。Element是个抽象类,在开发中必须明确指定使用的Element的类型。另外IElement和ILineElement, ITextElement等并不是父子关

系,后者没有Geometry属性。

图6

3.2.7MapGrid对象

MapGrid是布局视图中的一系列参考线和参考点用来帮助地图使用者快速地确定地图要素的位置如图7中的公里格网,MapGridBorder,MapGridLabel。MapGrid等。MapGrid对象由MapGrids来管理,一个MapGrids可以包含多个

MapGrid对象。

图7

MapGrid是一个抽象类,它的子类有MeasuredGrid,IndexGrid,MgrsGrid,Graticule和CustomOverlayGrid五种,

这些子类的对象由MapGridFactory对象创建。MapGride对象模型图如图8和图9所示

图8 图9

1.

2.

3.

///

/// 为PageLayout对象添加格网对象

4.

///

5.

6.

///

private void AddMeasuredGridToPageLayout(IPageLayout pPageLayout)

7. 8. 9.

{

try {

10. //获取MapFrame对象

11. IActiveView pAcitiveView= pPageLayout as IActiveView; 12. IMap pMap = pAcitiveView.FocusMap;

13. IGraphicsContainer pGraphicsContainer=pAcitiveView as IGraphicsContainer; 14. IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame;

15. IMapGrids pMapGrids = pMapFrame as IMapGrids;

16. //创建一个MeasuredGrid对象

17. IMeasuredGrid pMeasureGrid = new MeasuredGridClass();

18. IMapGrid pMapGrid = pMeasureGrid as IMapGrid;

19. pMeasureGrid.FixedOrigin = true; 20. pMeasureGrid.Units = pMap.MapUnits; 21. pMeasureGrid.XIntervalSize = 1000; 22. pMeasureGrid.YIntervalSize = 1000;

23. pMeasureGrid.XOrigin = -180; 24. pMeasureGrid.YOrigin = -90; 25. //设置MeasuredGride投影属性

26. IProjectedGrid pProGrid = pMeasureGrid as IProjectedGrid;

27. pProGrid.SpatialReference = pMap.SpatialReference;

28. pMapGrid.Name = \

29. //创建一个CalibratedMapGridBorder对象并设置为pMapGrid的Border属性

30. ICalibratedMapGridBorder pCalibratedBorder = new

CalibratedMapGridBorderClass();

31. pCalibratedBorder.BackgroundColor = GetRgbColor(255, 255, 255);

32. pCalibratedBorder.ForegroundColor = GetRgbColor(0, 0, 0);

33. pCalibratedBorder.BorderWidth = 0.1; 34. pCalibratedBorder.Interval = 72; 35. pCalibratedBorder.Alternating = true;

36. pMapGrid.Border = pCalibratedBorder as IMapGridBorder;

37. //创建一个FormattedGridLabel对象

38. IFormattedGridLabel pFormattedGridLabel = new FormattedGridLabelClass();

39. IGridLabel pGridLabel = pFormattedGridLabel as IGridLabel;

40. stdole.StdFont pFont = new stdole.StdFont();

41. pFont.Name = \

42. pFont.Size = 6;

43. pGridLabel.Font = pFont as stdole.IFontDisp; ;

44. pGridLabel.Color = GetRgbColor(0, 0, 0);

45. pGridLabel.LabelOffset = 4;

46. pGridLabel.set_LabelAlignment(esriGridAxisEnum.esriGridAxisLeft, false); 47. pGridLabel.set_LabelAlignment(esriGridAxisEnum.esriGridAxisRight, false);

48. INumericFormat pNumericFormat = new NumericFormatClass();

49. pNumericFormat.AlignmentOption = esriNumericAlignmentEnum.esriAlignRight;

50. pNumericFormat.RoundingOption = esriRoundingOptionEnum.esriRoundNumberOfSignificantDigits; 51. pNumericFormat.RoundingValue = 0;

52. pNumericFormat.ShowPlusSign = false;

53. pNumericFormat.ZeroPad = true;

54. pFormattedGridLabel.Format = pNumericFormat as INumberFormat;

55. //设置pMapGrid的LabelFormat属性 56. pMapGrid.LabelFormat = pGridLabel;

57. //添加格网

58. pMapGrids.AddMapGrid(pMapGrid);

59. }

60. catch(Exception Err)

61. {

62. MessageBox.Show(Err.Message,\提示\

MessageBoxIcon.Information);

63. } 64.

65. }

复制代码

3.2.8MapSurround对象

MapSurround对象是与一个地图对象关联的用于修饰地图的辅助图形元素对象如图所示。它的形状或内容会随着Map属性的变化而自动改变。如Map视图范围改变后比例尺(ScaleBar)也会自动调整比例,比例尺文本(ScaleBarText)也会相应改变它的比例值。

MapSurround对象由MapSurroundFrame 对象管理,所有的MapSurround对象添加在布局视图上,每一个MapSurround对象可以通过IMap接口的MapSurrounds属性的索引值所获取。也可以通过IMap接口的MapSurroundCount来遍历布局视图上的所有MapSurround对象。

所有的MapSurround对象都实现IMapSurround接口,使用IMapSurround接口的Name属性可以获得MapSurround对象的名称,通过IMapSurround的FitToBound方法可以设置一个MapSurround对象的大小。

同时MapSurround类也实现了IMapSurroundEvents接口,可以用来触发MapSounrround相关事件如:AfterDraw,BeforeDraw,ContensChanged。

图10

下图是MapSurround对象模型

本文来源:https://www.bwwdw.com/article/g2wf.html

Top