SharpMap开发教程——图层标注
在GIS开发中,根据图层属性字段对要素进⾏标注(图层标注)是⼀项常规的、必备的功能。在基于SharpMap开发GIS应⽤时,也可以⽅便的实现该功能。
1、加载Shapefile图层数据
SharpMap.Layers.VectorLayer vLayer = new SharpMap.Layers.VectorLayer("States");
vLayer.DataSource = new SharpMap.Data.Providers.ShapeFile("ShpData\\Provinces_R.shp", true);
mapBox1.Map.Layers.Add(vLayer);
添加图层、设置图层显⽰样式,详细步骤可参考
2、添加图层标注
与⼀般GIS软件不同,SharpMap的图层标注是以⼀类特殊的图层(LabelLayer)⽽存在的。
SharpMap.Layers.LabelLayer lLayer = new SharpMap.Layers.LabelLayer("labels");
lLayer.DataSource = vLayer.DataSource;
lLayer.Enabled = true;
lLayer.LabelColumn = "name";
mapBox1.Map.Layers.Add(lLayer);
创建⼀个标注图层lLayer,然后指定标注图层的数据源,也就是指定对哪⼀个⽮量图层做标注,再指定标注字段即可。
3、设置各种图层标注风格
lLayer.Style = new SharpMap.Styles.LabelStyle();
lLayer.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;安卓开发教程 pdf
lLayer.SmoothingMode = SmoothingMode.AntiAlias;
lLayer.Style.CollisionDetection = true;
lLayer.Style.ForeColor = Color.DarkBlue;
lLayer.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
lLayer.Style.Offset = new PointF(1f,1f);
lLayer.Style.BackColor = Brushes.White;
有⾮常多的参数和⽅法都可以来设置图层标注风格,参见下图
图层渲染效果如下
4、图标标注的旋转⾓度问题
有两个参数可以控制图层标注的旋转⾓度,⼀是设置LabelLayer的RotationColumn,即指定⼀个数值型[0,360]的字段,根据字段数值来旋转标注内容的⾓度,该⾓度是以正右⽅向为起始顺时针旋转的夹⾓;⼆是设置LabelStyle的Rotation值[0,360],也是以正右⽅向为起始顺时针旋转的夹⾓。
⼆者在使⽤效果上有所不同,设置LabelLayer的RotationColumn,可以为每⼀个要素标注定制⼀个合适的旋转⾓度。
lLayer.RotationColumn = "labelangle";
其中Shapefile属性表中,内蒙古的labelangle为330,其余为0,效果如下
⽽设置设置LabelStyle的Rotation值,则是对图层中的所有标注同步起作⽤。
lLayer.Style.Rotation = 30;
可以看到,所有要素标注都被旋转了30度。其中内蒙古要素的标注,因为两个旋转因素的作⽤(330+30)⽽恢复为⽔平状态。