爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 36138|回复: 89

MeteoInfo二次开发教程(三)

[复制链接]

新浪微博达人勋

发表于 2012-4-7 22:22:57 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
这一讲说说添加图层以及简单的图层图例、标注功能。

开发一个气象方面的专业软件通常在软件启动时会自动加载、设置好相应的地理底图,可以在主窗体的Load事件中加入添加图层的代码。添加图层可以利用MapFrame类的AddLayer函数,LayersLegend类中可以有多个MapFrame,但只有一个ActiveMapFrame,每个MapFrame对象中包含一个MapView对象(它才是真正显示图形的控件),给MapFrame添加图层也就是给MapFrame中的MapView添加图层,MapView中也有AddLayer函数,大家可以试试。

图层的基类是MapLayer类,有三个图层类继承自MapLayer类,分别是VectorLayer(矢量图层)类、RasterLayer(栅格图层)类和ImageLayer(图像图层)类。先来说所VectorLayer,它包含了很多图元(Shape),PointShape、PolylineShape和PolygonShape是三个基本的图元类,每个VectorLayer只能有一种图元类型(ShapeType)。图层是按照其图例(LegendScheme)来渲染的,LegendScheme又分为三种类型(LegendType):SingleSymbol(单一符号)、GraduatedColor(按照某个字段进行分级,不同级别用不同符号)和UniqueValue(按照某个字段的值,不同的值用不同的符号)。LegendScheme是有一些LegendBreak构成的,LegendBreak也有几种不同的类型:PointBreak、PolylineBreak、PolygonBreak等。有点复杂,以后会慢慢理解的,先看一个简单的Demo吧。

写一个LoadLayers函数来添加几个图层:世界各国行政区域图层、河流图层和城市图层,相应的shape文件可以在MeteoInfo软件中找到。先从shape文件中创建一个图层(其实shape文件创建的都是矢量图层VectorLayer),然后对图层的LegendScheme中的LegendBreak进行简单的修改以达到自己想要的渲染效果。对于城市图层还显示如何添加标注,先设置VectorLayer中LabelSet的各种参数,然后用VectorLayer的AddLabels方法添加标注。里面还有设置MapLayout中ActiveLayoutMap的位置、大小的代码。
  1.         private void LoadLayers()
  2.         {            
  3.             layersLegend1.ActiveMapFrame.MapView.LockViewUpdate = true;

  4.             //Load country layer
  5.             string aFile = Application.StartupPath + "\\Map\\country1.shp";
  6.             MapLayer aLayer = MapDataManage.OpenLayer(aFile);
  7.             aLayer.LegendScheme.breakList[0].Color = Color.WhiteSmoke;
  8.             layersLegend1.ActiveMapFrame.AddLayer(aLayer);

  9.             //Load river layer
  10.             aFile = Application.StartupPath + "\\Map\\rivers.shp";
  11.             aLayer = MapDataManage.OpenLayer(aFile);
  12.             aLayer.LegendScheme.breakList[0].Color = Color.Aqua;
  13.             ((PolyLineBreak)aLayer.LegendScheme.breakList[0]).Size = 2;
  14.             layersLegend1.ActiveMapFrame.AddLayer(aLayer);

  15.             //Load city layer
  16.             aFile = Application.StartupPath + "\\Map\\CITIES.shp";
  17.             aLayer = MapDataManage.OpenLayer(aFile);
  18.             ((PointBreak)aLayer.LegendScheme.breakList[0]).Color = Color.Red;
  19.             aLayer.Expanded = true;
  20.             layersLegend1.ActiveMapFrame.AddLayer(aLayer);
  21.             //Label city name
  22.             VectorLayer cityLayer = (VectorLayer)aLayer;
  23.             cityLayer.LabelSet.FieldName = "NAME";
  24.             cityLayer.LabelSet.AvoidCollision = true;
  25.             cityLayer.LabelSet.LabelAlignType = MeteoInfoC.Legend.AlignType.Center;
  26.             cityLayer.LabelSet.Offset = 0;
  27.             cityLayer.LabelSet.LabelFont = new Font("Arial", 8);
  28.             cityLayer.LabelSet.LabelColor = Color.Red;
  29.             cityLayer.LabelSet.DrawShadow = false;
  30.             cityLayer.LabelSet.ShadowColor = Color.White;
  31.             cityLayer.LabelSet.ColorByLegend = false;
  32.             cityLayer.AddLabels();

  33.             //Set layout map size
  34.             mapLayout1.ActiveLayoutMap.Left = 40;
  35.             mapLayout1.ActiveLayoutMap.Top = 40;
  36.             mapLayout1.ActiveLayoutMap.Width = 600;
  37.             mapLayout1.ActiveLayoutMap.Height = 400;

  38.             //Refresh
  39.             layersLegend1.ActiveMapFrame.MapView.LockViewUpdate = false;
  40.             layersLegend1.ActiveMapFrame.MapView.ZoomToExtent(70, 140, 10, 60);
  41.             layersLegend1.Refresh();
  42.         }
复制代码

在主窗体的Load事件中加入LoadLayers函数:
  1.         private void frmMain_Load(object sender, EventArgs e)
  2.         {
  3.             //Set width and height
  4.             this.Width = 1000;
  5.             this.Height = 625;

  6.             //Load layers
  7.             LoadLayers();

  8.             //Set initial tool
  9.             TSB_Pan.PerformClick();            
  10.         }
复制代码

运行效果如下:
Image00862.png
Image00863.png



本帖被以下淘专辑推荐:

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-4-7 22:28:27 | 显示全部楼层
沙发,继续等待王老师出教程~~~
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-4-7 22:29:28 | 显示全部楼层
楼主真棒!
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-4-7 22:55:15 | 显示全部楼层
无私贡献的人最高尚啊!
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-4-7 23:08:59 | 显示全部楼层
需要在代码中添加一句using MeteoInfoC.Legend;
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-4-7 23:10:23 | 显示全部楼层
另外如果地图路径是Application.StartupPath+………………的话,需要把map文件夹放在debug里。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 成长值: 19710
发表于 2012-4-7 23:15:26 | 显示全部楼层

哇哇,笨笨什么时候变得如此强悍。。。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-4-7 23:20:47 | 显示全部楼层
交作业

无标题.png
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2012-4-7 23:22:18 | 显示全部楼层
兰溪之水 发表于 2012-4-7 23:15
哇哇,笨笨什么时候变得如此强悍。。。

依样画葫芦
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 成长值: 19710
发表于 2012-4-7 23:38:34 | 显示全部楼层
传说中的谁 发表于 2012-4-7 23:22
依样画葫芦

兰溪的电脑硬盘只剩787M。。。VS装不了。。。
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表