PIE属性表多字段的文本绘制
最近研究了PIE SDK文本元素的绘制相关内容,因为在我们的开发中,希望可以做到在打开一个Shp文件后,读取到属性表的所有字段,然后可以选择一些需要的字段,将这些字段的所有要素值的文本,绘制到shp图中相应的要素位置上。
我主要是通过PIE的官方博文(https://www.cnblogs.com/PIESat/p/10172166.html)进行学习的,并在技术员姐姐的帮助下,最后成功的将文本绘制于图上。
结果如下图所示:
先打开一张属性表字段不为空Shp文件
读取shp的属性表中的所有字段,选取需要绘制在图上的字段
将所选的字段的值绘制到相应的要素的位置上
开发环境:vs2013 framework4
代码如下:
在子窗口的构造函数中,遍历当前shp文件属性表的所有字段,并设置默认好默认字体样式
1 #region 构造函数 2 public FeaturesShow(IFeatureDataset featureDataset) 3 { 4 InitializeComponent(); 5 m_featureDataset = featureDataset; 6 IFeature feature = m_featureDataset.GetNextFeature(); 7 for (int i = 1; i < feature.GetFieldCount(); i++) 8 { 9 string fieldname = feature.GetFieldName(i); 10 this.listBox1.Items.Add(fieldname); 11 } 12 //默认字体 13 FontDlg.Font = new Font("Calibri", 15, FontStyle.Bold); 14 FontDlg.Color = Color.Chartreuse; 15 } 16 #endregion
点击了子窗体的确定后,返回得到已选择的字段名,和设置的字体样式,并在图上进行绘制
1 private void 文本绘制ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 if (mapControl1.ActiveView.CurrentLayer == null) 4 return; 5 IFeatureLayer featurelayer = mapControl1.ActiveView.CurrentLayer as IFeatureLayer; 6 if (featurelayer == null) 7 return; 8 9 //清除图上已绘制的元素 10 mapControl1.ActiveView.GraphicsContainer.DeleteAllElements(); 11 //获得要读取的数据,传入子窗体 12 IFeatureDataset featureDataset = featurelayer.FeatureClass.GetFeatureDataset(); 13 FeaturesShow frm = new FeaturesShow(featureDataset); 14 frm.ShowDialog(); //通过窗口来选择需要展示在图上的特征值 15 if (frm.DialogResult == DialogResult.OK) 16 { 17 //符号设置 18 FontDialog m_FontDlg = frm.m_FontDlg; 19 ITextSymbol textSymbol = new TextSymbol(); 20 textSymbol.Color = m_FontDlg.Color; 21 textSymbol.Font = m_FontDlg.Font; 22 textSymbol.Size = m_FontDlg.Font.Size; 23 24 int m_FeatureCount = frm.m_FeatureCount; 25 string[] m_Fieldname=frm.m_Fieldname; 26 int[] index = new int[m_FeatureCount+1]; 27 IFields fields = featureDataset.GetFields(); 28 index[0] = 0; 29 for (int i = 1; i < m_FeatureCount+1;i++ ) 30 { 31 index[i] = fields.GetFieldIndex(m_Fieldname[i-1]); 32 } 33 IFeature feature = featureDataset.GetFeature(0); 34 while (feature != null) 35 { 36 ////取左上角坐标值 37 IPolygon polygon = new PIE.Geometry.Polygon(); 38 polygon = feature.Geometry as IPolygon; 39 IPointCollection poly = polygon as IPointCollection; 40 IPoint point = poly.GetPoint(0); 41 //创建文本元素实例对象 42 ITextElement textElement = new PIE.Carto.TextElement(); 43 textElement.Geometry = point as IGeometry; 44 textElement.Symbol = textSymbol; 45 46 for(int i=1;i<m_FeatureCount+1;i++) 47 { 48 string strValue = ""; 49 if (feature.IsValueNull(index[i]) == false) 50 { 51 strValue = m_Fieldname[i-1] + ":" + feature.GetValue(index[i]).ToString() + "\r\n"; 52 textElement.Text = textElement.Text+strValue; 53 } 54 } 55 if(textElement.Text!="") 56 { 57 string strID = fields.GetFieldName(0) + ":" + feature.GetValue(index[0]).ToString() + "\r\n"; 58 textElement.Text = strID + textElement.Text; 59 mapControl1.ActiveView.GraphicsContainer.AddElement(textElement); 60 } 61 feature = featureDataset.GetNextFeature(); 62 } 63 mapControl1.PartialRefresh(ViewDrawPhaseType.ViewAll); 64 } 65 }
有帮助的话,记得点个赞支持一下哦~
也欢迎各位评论,指点,交流