-
Notifications
You must be signed in to change notification settings - Fork 2
/
地图标注.cs
247 lines (222 loc) · 9.23 KB
/
地图标注.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
namespace WindowsFormsApplication1
{
public partial class 地图标注 : Form
{
IMapControl3 mapcontrol;
IMap pMap;
IRgbColor pRGB = new RgbColorClass();
public 地图标注(IMapControl3 m_mapControl)
{
InitializeComponent();
mapcontrol = m_mapControl;
}
private void 地图标注_Load(object sender, EventArgs e)
{
addlayer();
if (cbxLayer.Items.Count != 0)
{
cbxLayer.SelectedIndex = 0;
addfield();
}
comboBox2.SelectedIndex = 0;
comboBox3.SelectedIndex = 0;
pMap = mapcontrol.Map;
}
private IFeatureLayer getlayerbyname(string layername)
{
ILayer layer = null;
IFeatureLayer flayer = null;
for (int i = 0; i <= mapcontrol.LayerCount - 1; i++)
{
layer = mapcontrol.get_Layer(i);
if (layername == mapcontrol.get_Layer(i).Name.ToString() && layer is IFeatureLayer)
{ flayer = layer as IFeatureLayer; break; }
}
return flayer;
}
private void addlayer()
{
try
{
cbxLayer.Items.Clear();
if (mapcontrol.LayerCount > 0)
{
cbxLayer.Enabled = true;
for (int i = 0; i <= mapcontrol.LayerCount - 1; i++)
{ cbxLayer.Items.Add(mapcontrol.get_Layer(i).Name); }
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void addfield()
{
cbxField.Items.Clear();
string layername = cbxLayer.SelectedItem.ToString();
IFeatureLayer pflayer = getlayerbyname(layername);
for (int i = 0; i <= pflayer.FeatureClass.Fields.FieldCount - 1; i++)
{
cbxField.Items.Add(pflayer.FeatureClass.Fields.get_Field(i).Name.ToString());
}
}
private void cbxLayer_SelectedIndexChanged(object sender, EventArgs e)
{
cbxField.Items.Clear();
addfield();
}
private void cbxField_SelectedIndexChanged(object sender, EventArgs e)
{
string fieldname = cbxField.SelectedItem.ToString();
}
//private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
//{
// string fieldname = cbxField.SelectedItem.ToString();
// IFeatureCursor pfc;
// IFeatureClass featureclass;
// IFeature feature;
// if (fieldname != null)
// {
// featureclass = getlayerbyname(cbxLayer.SelectedItem.ToString()).FeatureClass;
// pfc = featureclass.Search(null, true);
// feature = pfc.NextFeature();
// int index = featureclass.FindField(checkedListBox1.SelectedItem.ToString());
// while (feature != null)
// {
// string value = feature.get_Value(index).ToString();
// listBox1.Items.Add(value);
// feature = pfc.NextFeature();
// }
// }
//}
private void btnA_Click(object sender, EventArgs e)
{
pMap = mapcontrol.Map;
if (cbxLayer.SelectedItem == null)
{
MessageBox.Show("请选择要标注的图层!");
return;
}
if (cbxField.SelectedItem == null)
{
MessageBox.Show("请选择要标注的字段!");
return;
}
if (radioButton1.Checked)
{
LayerLabel();
}
if (radioButton2.Checked)
{
Annotation();
}
this.Dispose();
}
private void Annotation()
{
string fieldname = cbxField.SelectedItem.ToString();
IFeatureLayer featureLayer = getlayerbyname(cbxLayer.SelectedItem.ToString());
IGeoFeatureLayer pGeoFeatLyr = featureLayer as IGeoFeatureLayer;
stdole.IFontDisp pFont = new stdole.StdFontClass() as stdole.IFontDisp;
pFont.Name = comboBox2.SelectedItem.ToString();
ITextSymbol pTextSymbol = new TextSymbolClass();
pTextSymbol.Size = Convert.ToInt16(comboBox3.SelectedItem.ToString());
pTextSymbol.Font = pFont;
if (pRGB.NullColor)
{
pRGB.Red = 255;
pRGB.Blue = 0;
pRGB.Green = 0;
}
pTextSymbol.Color = pRGB;
IAnnotateLayerPropertiesCollection pAnnoProps = pGeoFeatLyr.AnnotationProperties;
pAnnoProps.Clear();
ILabelEngineLayerProperties pLabelEngine = new LabelEngineLayerPropertiesClass();
pLabelEngine.Symbol = pTextSymbol;
pLabelEngine.Expression = "[" + fieldname + "]";
IAnnotateLayerProperties pAnnoLayerProps = pLabelEngine as IAnnotateLayerProperties;
pAnnoLayerProps.DisplayAnnotation = true;
pAnnoLayerProps.LabelWhichFeatures = esriLabelWhichFeatures.esriAllFeatures;
pAnnoProps.Add(pAnnoLayerProps);
pGeoFeatLyr.DisplayField = fieldname;
pGeoFeatLyr.DisplayAnnotation = true;
IActiveView pActiveView = pMap as IActiveView;
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
private void LayerLabel()
{
IFeatureLayer featureLayer = getlayerbyname(cbxLayer.SelectedItem.ToString());
IGeoFeatureLayer geoFeatureLayer = featureLayer as IGeoFeatureLayer;
IFeatureClass featureclass = featureLayer.FeatureClass;
IFields pFields = featureclass.Fields;
int i = pFields.FindField(cbxField.SelectedItem.ToString());
IField field = pFields.get_Field(i);
geoFeatureLayer.DisplayField = field.Name;
geoFeatureLayer.DisplayAnnotation = true;
IActiveView pActiveView = pMap as IActiveView;
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
private void textelementlable()
{
IFeatureLayer pfl = getlayerbyname(cbxLayer.SelectedItem.ToString());
IFeatureClass featureclass = pfl.FeatureClass;
IFeatureCursor pFeatureCursor;
pFeatureCursor = featureclass.Search(null, true);
IFeature pFeat = pFeatureCursor.NextFeature();
while (pFeat != null)
{
IFields pFields = pFeat.Fields;
int i = pFields.FindField(cbxField.SelectedItem.ToString()); ;
IEnvelope pEnv = pFeat.Extent;
IPoint pPoint = new PointClass();
pPoint.PutCoords(pEnv.XMin + pEnv.Width / 2, pEnv.YMin + pEnv.Height / 2);
stdole.IFontDisp pFont = new stdole.StdFontClass() as stdole.IFontDisp;
pFont.Name = comboBox2.SelectedItem.ToString();
ITextSymbol pTextSymbol = new TextSymbolClass();
pTextSymbol.Size = Convert.ToInt16(comboBox3.SelectedItem.ToString());
pTextSymbol.Font = pFont;
if (pRGB.NullColor)
{
pRGB.Red = 110;
pRGB.Blue = 200;
pRGB.Green = 60;
}
pTextSymbol.Color = pRGB;
ITextElement pTextEle = new TextElementClass();
pTextEle.Text = pFeat.get_Value(i).ToString();
pTextEle.ScaleText = true;
pTextEle.Symbol = pTextSymbol;
IElement pEle = pTextEle as IElement;
pEle.Geometry = pPoint;
IActiveView pActiveView = pMap as IActiveView;
IGraphicsContainer pGraphicsContainer = pMap as IGraphicsContainer;
pGraphicsContainer.AddElement(pEle, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
pPoint = null;
pEle = null;
pFeat = pFeatureCursor.NextFeature();
}
}
private void btnSelectColor_Click(object sender, EventArgs e)
{
pRGB = new RgbColorClass();
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
pRGB.Blue = colorDialog1.Color.B;
pRGB.Green = colorDialog1.Color.G;
pRGB.Red = colorDialog1.Color.R;
}
}
}
}