ArcEngine编辑模块——创建要素

59 篇文章 75 订阅

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,如下图所示,可以发现刚刚创建的要素可以正常显示。

在这里插入图片描述

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值