1、前言
在ArcEngine
中,创建要素一般分为两部分,即:设置空间信息、设置属性信息。下面给出实现代码。
2、定义实体类
首先在ArcMap
中创建一个点
文件,其属性字段如下表所示:
字段名称 | 字段类型 |
---|---|
FID | 整型 |
Shape | 几何 |
Longitude(经度) | 双精度 |
Latitude(纬度) | 双精度 |
SchoolName(学校名称) | 文本 |
SchoolType(学校类型) | 文本 |
根据该属性表创建一个实体类——SchoolModel
,代码如下:
using System.ComponentModel;
namespace WindowsFormsApplication1
{
public class SchoolModel
{
[Category("只读属性")]
[Description("FID")]
[ReadOnly(true)]
public int FID { get; set; }
[Category("只读属性")]
[Description("Shape")]
[ReadOnly(true)]
public string Shape { get; set; }
[Category("只读属性")]
[Description("经度")]
[ReadOnly(true)]
public double Longitude { get; set; }
[Category("只读属性")]
[Description("纬度")]
[ReadOnly(true)]
public double Latitude { get; set; }
[Category("可编辑属性")]
[Description("名称")]
[ReadOnly(false)]
public string SchoolName { get; set; }
[Category("可编辑属性")]
[Description("类型")]
[ReadOnly(false)]
[TypeConverter(typeof(SchoolTypeItem))]
public string SchoolType { get; set; }
}
public class SchoolTypeItem : StringConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[] { "幼儿园", "小学", "初中", "高中", "大学" });
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
}
}
3、创建要素
搭建一个如下图所示的界面:
代码如下所示:
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
private IWorkspaceEdit pWorkspaceEdit;
private IFeature pFeature;
// 构造函数
public MainForm()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
axMapControl1.Extent = axMapControl1.FullExtent;
btnStartEditing.Enabled = true;
btnStopEditing.Enabled = false;
}
// 开始编辑
private void btnStartEditing_Click(object sender, EventArgs e)
{
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IDataset pDataset = pFeatureClass as IDataset;
IWorkspace pWorkspace = pDataset.Workspace;
pWorkspaceEdit = pWorkspace as IWorkspaceEdit;
pWorkspaceEdit.StartEditing(true);
btnStartEditing.Enabled = false;
btnStopEditing.Enabled = true;
}
// 结束编辑
private void btnStopEditing_Click(object sender, EventArgs e)
{
pWorkspaceEdit.StopEditing(true);
pWorkspaceEdit = null;
pFeature = null;
// 清除选择
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
axMapControl1.Map.ClearSelection();
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
// 设置控件
btnStartEditing.Enabled = true;
btnStopEditing.Enabled = false;
prgFeatureAttributes.SelectedObject = null;
}
// 点击地图创建要素
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (pWorkspaceEdit != null)
{
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
pPoint.PutCoords(e.mapX, e.mapY);
// 获取字段索引
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFields pFields = pFeatureClass.Fields;
int fieldIndex_Longitude = pFields.FindField("Longitude");
int fieldIndex_Latitude = pFields.FindField("Latitude");
// 创建新要素
pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = pPoint;
pFeature.set_Value(fieldIndex_Longitude, pPoint.X);
pFeature.set_Value(fieldIndex_Latitude, pPoint.Y);
pFeature.Store();
// 清除选择
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
axMapControl1.Map.ClearSelection();
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
// 选中该要素
IQueryFilter pQueryFilter = new QueryFilter();
pQueryFilter.WhereClause = "FID=" + pFeature.OID.ToString();
IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
axMapControl1.ActiveView.Refresh();
// 创建实体类
SchoolModel schoolModel = new SchoolModel
{
FID = pFeature.OID,
Shape = "点",
Longitude = Math.Round(pPoint.X, 3),
Latitude = Math.Round(pPoint.Y, 3),
SchoolName = "",
SchoolType = ""
};
prgFeatureAttributes.SelectedObject = schoolModel;
}
}
// 设置要素属性值
private void prgFeatureAttributes_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
if (pFeature != null)
{
int fieldIndex = pFeature.Fields.FindField(e.ChangedItem.Label);
pFeature.set_Value(fieldIndex, e.ChangedItem.Value);
pFeature.Store();
}
}
}
}
运行结果如下图所示:
打开ArcMap
,如下图所示,可以发现刚刚创建的要素可以正常显示。