1,布局
连接要素的字段映射控件使用的是列表框ListBox
2,获取目标要素和连接要素
使用两种方式,一是自动获取当前地图加载的图层
#region 获取主窗口图层并添加到控件中
IMap myMap = form.getMapControl().Map;
IFeatureLayer pFeatLayer;
IFeatureClass pFeatureClass;
ICompositeLayer pCompLayer;
//遍历地图
for (int i = 0; i < myMap.LayerCount; i++)
{
if (myMap.get_Layer(i) is IFeatureLayer)
{
//获得图层要素
pFeatLayer = myMap.get_Layer(i) as IFeatureLayer;
pFeatureClass = pFeatLayer.FeatureClass;
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPoint || pFeatureClass.ShapeType == esriGeometryType.esriGeometryLine || pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{
ComboxTarget.Items.Add(pFeatLayer.Name);
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{
ComboxJoin.Items.Add(pFeatLayer.Name);
}
}
else if (myMap.get_Layer(i) is IGroupLayer)
{
string name = myMap.get_Layer(i).Name;
//遍历图层组
pCompLayer = myMap.get_Layer(i) as ICompositeLayer;
for (int j = 0; j < pCompLayer.Count; j++)
{
if (pCompLayer.get_Layer(j) is IFeatureLayer)
{
pFeatLayer = pCompLayer.get_Layer(j) as IFeatureLayer;
pFeatureClass = pFeatLayer.FeatureClass;
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPoint || pFeatureClass.ShapeType == esriGeometryType.esriGeometryLine || pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{
ComboxTarget.Items.Add(name + @"\" + pFeatLayer.Name);
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{
ComboxJoin.Items.Add(name + @"\" + pFeatLayer.Name);
}
}
}
}
}
#endregion
二是,从文件中选择图层并添加
#region 从文件中选择输入图层添加到控件中
openFileDialog1.FileName = "";
openFileDialog1.Filter = "要素类|*.shp;*.dwg";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (System.IO.Path.GetExtension(openFileDialog1.FileName) == ".DWG" || System.IO.Path.GetExtension(openFileDialog1.FileName) == ".dwg")
{
FileInfo fileInfo = new FileInfo(openFileDialog1.FileName);
string path = fileInfo.Directory.ToString();
string fileName = fileInfo.Name;
IWorkspaceFactory pCadWorkspaceFactory = new CadWorkspaceFactoryClass();
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pCadWorkspaceFactory.OpenFromFile(path, 0);
IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName);
IFeatureClassContainer pFeatureClassContainer = pFeatureDataset as IFeatureClassContainer;
//遍历CAD文件中的每个要素
for (int i = 0; i < pFeatureClassContainer.ClassCount; i++)
{
IFeatureClass pFeatureClass = pFeatureClassContainer.get_Class(i);
if (pFeatureClass.FeatureType != esriFeatureType.esriFTCoverageAnnotation)
{
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.Name = pFeatureClass.AliasName;
pFeatureLayer.FeatureClass = pFeatureClass;
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPoint || pFeatureClass.ShapeType == esriGeometryType.esriGeometryLine || pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{
this.ComboxTarget.Items.Add(openFileDialog1.FileName + @"\" + pFeatureLayer.Name);
}
}
}
}
else
{
this.ComboxTarget.Items.Add(openFileDialog1.FileName);
}
}
#endregion
3,关于字段映射
返回输入图层的完整路径
#region 返回输入图层路径
public string getInputLayerValue(string textValue)
{
#region 参数定义
string InputValue = null;
string mValue = textValue;
IMap myMap = form.getMapControl().Map;
IFeatureLayer[] pFeatureLayers = MyFeatureLayer.GetMyFeatureLayers(myMap);
#endregion
#region 获取输入点图层
if (mValue.Contains(":"))//来自文件选择
{
InputValue = mValue;
}
else //来自主窗口加载图层
{
if (mValue.Contains(@"\")) //CAD要素类
{
int m = mValue.LastIndexOf(@"\");
string pValue = mValue.Substring(m + 1);
for (int i = 0; i < pFeatureLayers.Length; i++)//选择地图控件中与下拉框同名的地图图层
{
if (pFeatureLayers[i].Name == pValue)
{
ILayer mLayer = pFeatureLayers[i];
IDataLayer dataLayer = mLayer as IDataLayer;
IDatasetName pDsName = (IDatasetName)(dataLayer.DataSourceName);
IWorkspaceName wsName = pDsName.WorkspaceName;
string pathStr = wsName.PathName;
string fileName = mValue;
string fullName = System.IO.Path.Combine(pathStr, fileName);
InputValue = fullName;
break;
}
}
}
else //shp图层
{
for (int i = 0; i < pFeatureLayers.Length; i++)//选择地图控件中与下拉框同名的地图图层
{
if (pFeatureLayers[i].Name == mValue)
{
ILayer mLayer = pFeatureLayers[i];
IDataLayer dataLayer = mLayer as IDataLayer;
IDatasetName pDsName = (IDatasetName)(dataLayer.DataSourceName);
IWorkspaceName wsName = pDsName.WorkspaceName;
string pathStr = wsName.PathName;
string fileName = pDsName.Name + ".shp";
string fullName = System.IO.Path.Combine(pathStr, fileName);
InputValue = fullName;
break;
}
}
}
}
return InputValue;
#endregion
}
#endregion
添加字段映射,显示的模式是:图层名.字段名(字段类型)
IGPUtilities gputilities = new GPUtilitiesClass();//gp应用程序类
IGPFieldMapping mFieldMapping = new GPFieldMappingClass();
IArray Tables = new ArrayClass();
string pathA = "";
string pathB = "";
string TargetValue = null;
string JoinValue = null;
List TargetFNlist = new List();
private void ComboxTarget_TextChanged(object sender, EventArgs e)
{
TargetFNlist.Clear();
IDETable TargetTable;
if (ComboxTarget.Text.Length > 0)
{
TargetValue = getInputLayerValue(ComboxTarget.Text);
string tName = System.IO.Path.GetFileName(ComboxTarget.Text);
//添加字段映射
pathA = TargetValue;
TargetTable = (IDETable)gputilities.MakeDataElement(pathA, null, null);
Tables.Add(TargetTable);
mFieldMapping.Initialize(Tables, null);
for (int i = 0; i < mFieldMapping.Fields.FieldCount; i++)
{
IField pField = mFieldMapping.Fields.get_Field(i);
IFieldEdit pEditField = (IFieldEdit)pField;
TargetFNlist.Add(tName + "." + pField.Name + "(" + MyFieldType.SetMyFieldType(pEditField.Type) + ")");
}
}
else
{
MessageBox.Show("还没有选择目标要素,请选择!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
关于MyFieldType.SetMyFieldType(pEditField.Type),自定义的一个类,用来将英文的字段类型转为中文显示
public static string SetMyFieldType(esriFieldType fieldType)//将EsriType 转换为String
{
switch (fieldType)
{
case esriFieldType.esriFieldTypeSmallInteger:
return "短整型";
case esriFieldType.esriFieldTypeInteger:
return "长整型";
case esriFieldType.esriFieldTypeSingle:
return "浮点型";
case esriFieldType.esriFieldTypeDouble:
return "双精度";
case esriFieldType.esriFieldTypeString:
return "文本";
case esriFieldType.esriFieldTypeDate:
return "日期";
case esriFieldType.esriFieldTypeOID:
return "对象ID";
case esriFieldType.esriFieldTypeGeometry:
return "几何";
default:
return "文本";
}
}
重命名映射字段
//定义一个文本框
TextBox txtEdit = new TextBox();
public SpatialJoinForm()
{
InitializeComponent();
txtEdit.KeyDown += new KeyEventHandler(txtEdit_KeyDown);
}
///
/// 点击其它项 隐藏编辑框
///
private void ListboxFields_MouseClick(object sender, MouseEventArgs e)
{
txtEdit.Visible = false;
}
///
/// 双击项时显示编辑框
///
private void ListboxFields_DoubleClick(object sender, EventArgs e)
{
if (ListboxFields.Items.Count > 0)
{
int itemSelected = ListboxFields.SelectedIndex;
string itemText = ListboxFields.Items[itemSelected].ToString();
string TXT = mFieldMapping.Fields.get_Field(ListboxFields.SelectedIndex).Name;
Rectangle rect = ListboxFields.GetItemRectangle(itemSelected);
txtEdit.Parent = ListboxFields;
txtEdit.Bounds = rect;
txtEdit.Multiline = true;
txtEdit.Visible = true;
txtEdit.Text = TXT;
txtEdit.Focus();
txtEdit.SelectAll();
}
}
///
/// KeyDown事件定义 重命名字段
///
private void txtEdit_KeyDown(object sender, KeyEventArgs e)
{
//Enter键 更新项并隐藏编辑框
if (e.KeyCode == Keys.Enter)
{
IField ifd = mFieldMapping.Fields.get_Field(ListboxFields.SelectedIndex);
IFieldEdit ife = (IFieldEdit)ifd;
ife.Name_2 = txtEdit.Text;
IGPFieldMap mFieldMap = mFieldMapping.GetFieldMap(ListboxFields.SelectedIndex);
mFieldMap.OutputField = ife;
mFieldMapping.ReplaceFieldMap(ListboxFields.SelectedIndex, mFieldMap);//修改字段名
string path = mFieldMapping.GetFieldMap(ListboxFields.SelectedIndex).Fields.get_Field(0).Name.ToString();
string name = System.IO.Path.GetFileName(path);
int r = name.LastIndexOf(".shp");
string filename = name.Substring(0, r);
string type = MyFieldType.SetMyFieldType(mFieldMapping.Fields.get_Field(ListboxFields.SelectedIndex).Type);
ListboxFields.Items[ListboxFields.SelectedIndex] = filename + "." + txtEdit.Text + "(" + type + ")";
txtEdit.Visible = false;
}
//Esc键 直接隐藏编辑框
if (e.KeyCode == Keys.Escape)
txtEdit.Visible = false;
}
删除字段
//删除字段
private void BtnDelete_Click(object sender, EventArgs e)
{
if (ListboxFields.SelectedIndex > -1)
{
mFieldMapping.RemoveFieldMap(ListboxFields.SelectedIndex);
ListboxFields.Items.RemoveAt(ListboxFields.SelectedIndex);
}
}
字段映射中所选字段的合并规则,使用了列表框的右键菜单contextMenuStrip1
//右键点击ListBox的Item选中并弹出菜单
private void ListboxFields_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
System.Drawing.Point p = e.Location;//获取点击的位置
int currentIndex = ListboxFields.IndexFromPoint(p);//根据位置获取右键点击项的索引
if (ListboxFields.Items.Count > 0)
{
this.ListboxFields.SetSelected(currentIndex, true);
if (currentIndex == ListboxFields.SelectedIndex)
{
this.ListboxFields.ContextMenuStrip = contextMenuStrip1;
}
else
{
this.ListboxFields.ContextMenuStrip = null;
}
}
else
{
this.ListboxFields.ContextMenuStrip = null;
}
}
}
关于选中一个item时,前面显示对号,使用了最死板的方法,代码重复性很高,有待改进。关于图片,使用的是资源文件,解决方案右击,选择属性,打开后按图选择,添加现有文件,添加下载的PNG之类的图片(16 * 16)即可
#region 字段映射中所选字段的合并规则
IGPFieldMap mrFieldmap;
private void firstToolStripMenuItem_Click(object sender, EventArgs e)
{
firstToolStripMenuItem.Image = Properties.Resources.对号;
lastToolStripMenuItem.Image = null;
joinToolStripMenuItem.Image = null;
sumToolStripMenuItem.Image = null;
meanToolStripMenuItem.Image = null;
medianToolStripMenuItem.Image = null;
modeToolStripMenuItem.Image = null;
minToolStripMenuItem.Image = null;
maxToolStripMenuItem.Image = null;
standardDeviationToolStripMenuItem.Image = null;
countToolStripMenuItem.Image = null;
//MessageBox.Show(ListboxFields.SelectedIndex.ToString());//所选行的Index
//MessageBox.Show(ListboxFields.SelectedItem.ToString());//所选行的值
mrFieldmap= mFieldMapping.GetFieldMap(ListboxFields.SelectedIndex);
mrFieldmap.MergeRule =esriGPFieldMapMergeRule.esriGPFieldMapMergeRuleFirst;
mFieldMapping.ReplaceFieldMap(ListboxFields.SelectedIndex, mrFieldmap);
}
依次响应点击事件,修改Image和MergeRule,代码省略……
……………………………………………………………………………………………………………………………………………………
#endregion
重置字段映射,即重新获取图层的全部字段,发生在不小心删除了不该删除的字段之后
//重置字段
private void BtnReset_Click(object sender, EventArgs e)
{
ListboxFields.Items.Clear();
mFieldMapping.RemoveAll();
mFieldMapping.Initialize(Tables, null);
for (int i = 0; i < TargetFNlist.Count; i++)
{
ListboxFields.Items.Add(TargetFNlist[i]);
}
}
执行空间连接,并返回执行进度或错误提示
Geoprocessor gp = new Geoprocessor();
ESRI.ArcGIS.AnalysisTools.SpatialJoin sj = new ESRI.ArcGIS.AnalysisTools.SpatialJoin();
IGeoProcessorResult2 JSresult = null;
//关于获取输入图层和判断输出图层名称是否有误代码省略……
……………………………………………………
sj.target_features = TargetValue;
sj.join_features = JoinValue;
sj.out_feature_class = TextBoxOutput.Text;
sj.join_operation = JoinOperation.Text;
sj.join_type = "KEEP_ALL";
sj.match_option = ComboxMatchOption.Text;
sj.field_mapping = mFieldMapping;
//执行并返回错误信息
try
{
JSresult = gp.Execute(sj, null) as IGeoProcessorResult2;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "GP Error");
}
finally
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < gp.MessageCount; i++)
sb.AppendLine(gp.GetMessage(i));
if (sb.Capacity > 0) MessageBox.Show(sb.ToString(), "GP Messages");
}
总结到此为止。还有添加字段到字段映射中,不想写了,等到需要用的时候再写吧。