基于C#的ArcEngine二次开发教程(05):ArcEngine操作矢量数据的常用接口及用法代码示例

目录

1 ArcGIS矢量文件说明

2 AE中与矢量操作相关的接口:

2.1 IFeatureLayer的成员及描述

2.2 IFeatureClass的成员及描述

2.3 IFeature成员及描述

2.4 IField接口成员及说明

2.5 IFields成员及描述

3 利用IFeatureLayer进行矢量图层更名、比例尺及可见性设置

3.1 准备工作

3.2 图层操作

3.2.1 设置图层名称为RenameLayer

3.2.2 设置图层比例尺

3.2.3 设置图层可见性

3.3 字段操作

3.3.1 添加字段

3.3.2 删除字段

3.3.3 查找字段

3.3.4 获取属性值

4 小结


1 ArcGIS矢量文件说明

矢量数据分为点线面三种类型,在ArcGIS中,一个完整的适量文件包括以下几个文件:

文件类型 文件描述 是否必须
dbf 存储属性数据的文件,可以使用Excel打开
sbn、sbx 地理信息索引文件
shx 空间集合特征索引文件
shp 集合特征文件(点,线长度、面积)
prj 投影信息文件

每一个shp的属性表,均默认具有FID和shape字段;如果需要添加新的字段,则点击左上角,选择添加字段,并指定字段名、字段类型和字段属性等信息。

基于C#的ArcEngine二次开发教程(05):ArcEngine操作矢量数据的常用接口及用法代码示例_第1张图片基于C#的ArcEngine二次开发教程(05):ArcEngine操作矢量数据的常用接口及用法代码示例_第2张图片

2 AE中与矢量操作相关的接口:

接口名称 接口说明
IFeatureLayer 矢量图层
IFeatureClass 矢量数据的属性表
IFeature 某一矢量要素的信息
IField 属性表中的某一字段
IFields 属性表中的所有字段集合

2.1 IFeatureLayer的成员及描述

字段 字段名 字段描述 中文解释
AreaOfInterest The default area of interest for the layer.  
Cached Indicates if the layer needs its own display cache.  
DataSourceType Data source type.  
DisplayField Primary display field.  
Draw Draws the layer to the specified display for the given draw phase.  
FeatureClass The layer's feature class.  
MaximumScale Maximum scale (representative fraction) at which the layer will display. 可用于设置图层最大显示比例尺
MinimumScale Minimum scale (representative fraction) at which the layer will display. 可用于设置图层最小显示比例尺
Name Layer name. 可用于设置或获取图层名
ScaleSymbols Indicates if symbols are scaled for the layer.  
Search Creates a cursor based upon the search criteria.  
Selectable Indicates if layer is selectable.  
ShowTips Indicates if the layer shows map tips.  
SpatialReference Spatial reference for the layer.  
SupportedDrawPhases Supported draw phases.  
TipText Map tip text at the specified location.  
Valid Indicates if the layer is currently valid.  
Visible Indicates if the layer is currently visible.  

2.2 IFeatureClass的成员及描述

AddField Adds a field to this object class.
AddIndex Adds an index to this object class.
AliasName The alias name of the object class.
AreaField The geometry area field.
CLSID The GUID for the COM Class (CoClass) corresponding to instances of this object class.
CreateFeature Create a new feature, with a system assigned object ID and null property values.
CreateFeatureBuffer Create a feature buffer that can be used with an insert cursor.
DeleteField Deletes a field from this object class.
DeleteIndex Deletes an index from this object class.
EXTCLSID The GUID for the COM Class (CoClass) corresponding to the class extension for this object class.
Extension The extension for this object class.
ExtensionProperties The extension properties for this object class.
FeatureClassID The unique ID for the Feature Class.
FeatureCount The number of features selected by the specified query.
FeatureDataset The feature dataset that contains the feature class.
FeatureType The type of features in this feature class.
Fields The fields collection for this object class.
FindField The index of the field with the specified name.
GetFeature Get the feature with the specified object ID.
GetFeatures Get a cursor of Rows given a set of object ids.
HasOID Indicates if the class has an object identity (OID) field.
Indexes The indexes collection for this object class.
Insert Returns a cursor that can be used to insert new features.
LengthField The geometry length field.
ObjectClassID The unique ID for the object class.
OIDFieldName The name of the field corresponding to the OID.
RelationshipClasses The relationship classes in which this object class participates in for the specified role.
Search Returns an object cursor that can be used to fetch feature objects selected by the specified query.
Select Returns a selection That contains the object ids selected by the specified query.
ShapeFieldName The name of the default sShape field.
ShapeType The type of the default Shape for the features in this feature class.
Update Returns a cursor that can be used to update features selected by the specified query.

 

2.3 IFeature成员及描述

Class The Object Class for the row.
Delete Deletes the row.
Extent The extent of the feature.
FeatureType The type of the feature.
Fields The fields Collection for this row buffer.
HasOID Indicates if the row has an OID.
OID The OID for the row.
Shape A reference to the default shape for the feature.
ShapeCopy A cloned copy of the default shape for the feature.
Store Stores the row.
Table The Table for the row.
Value The value of the field with the specified index.

2.4 IField接口成员及说明

AliasName The alias name of the field.
CheckValue Indicates if the value is valid given the field definition.
DefaultValue The default value of the field.
Domain The default domain of the field.
DomainFixed Indicates if the field's domain is fixed.
Editable Indicates if the field is editable.
GeometryDef The geometry definition for the field if IsGeometry is TRUE.
IsNullable Indicates if the field can contain null values.
Length The maximum length, in bytes, for values described by the field.
Name The name of the field.
Precision The precision for field values.
Required Indicates if the field is required.
Scale The scale for field values.
Type The type of the field.
VarType The VARTYPE of the field (e.g. VT_I4).

2.5 IFields成员及描述

Field The field at the specified index in the fields collection.
FieldCount The number of fields in the fields collection.
FindField Finds the index of the named field in the fields collection.
FindFieldByAliasName Finds the index of the field with the alias name in the fields collection.

3 利用IFeatureLayer进行矢量图层更名、比例尺及可见性设置

3.1 准备工作

(1)新建winForm应用程序,拖入TocControl、MapControl和ToolBarControl控件,

(2)添加menuStrip控件,设置图层操作和字段操作菜单项如下表:

菜单栏项目信息一览表
项目 name text
图层操作 tsmi_layerRename 图层更名
tsmi_layerRescale 图层比例尺设置
tsmi_layerIsVisible 图层可见性
字段操作 tmsi_addField 添加字段
tmsi_deleteField 删除字段
tmsi_findField 查找字段
tmsi_readPropvalue 读取属性值

(3)添加许可

        public Form1()
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
            InitializeComponent();
        }

3.2 图层操作

引入名称空间,查看帮助文档可知,IFeatureLayer所属名称空间为:ESRI.ArcGIS.Carto

3.2.1 设置图层名称为RenameLayer

        private void tsmi_layerRename_Click(object sender, EventArgs e)
        {
            IFeatureLayer myLayer = axMapControl1.get_Layer(0) as IFeatureLayer;//强制类型转换
            myLayer.Name = "RenameLayer";
            axTOCControl1.Update();
        }

3.2.2 设置图层比例尺

        private void tsmi_layerRescale_Click(object sender, EventArgs e)
        {
            IFeatureLayer myLayer = axMapControl1.get_Layer(0) as IFeatureLayer;//强制类型转换
            myLayer.MaximumScale = 1500000;
            myLayer.MinimumScale = 3000000;
            //大于或小于设置的比例尺范围,矢量数据将不会显示
        }

当图层比例尺在设置的最大、最小范围之外时,图层标记会变成如下图灰色:(此做法可以确保放至大数据量时的处理效率)

3.2.3 设置图层可见性

        private void tsmi_layerIsVisible_Click(object sender, EventArgs e)
        {
            IFeatureLayer myLayer = axMapControl1.get_Layer(0) as IFeatureLayer;//强制类型转换
            myLayer.Visible = false;
            axTOCControl1.Update();//此处不能立即刷新,注意分析原因
        }

3.3 字段操作

样例数据:

基于C#的ArcEngine二次开发教程(05):ArcEngine操作矢量数据的常用接口及用法代码示例_第3张图片

3.3.1 添加字段


        private void tsmi_addFiled_Click(object sender, EventArgs e)
        {
            IFeatureLayer myLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass myClass = myLayer.FeatureClass;
            IFieldEdit field = new FieldClass();
            field.Name_2 = "NewField1";
            field.Type_2 = esriFieldType.esriFieldTypeString;
            field.Length_2 = 200;
            myClass.AddField(field as IField);
            MessageBox.Show("SUCESS");
        }

基于C#的ArcEngine二次开发教程(05):ArcEngine操作矢量数据的常用接口及用法代码示例_第4张图片

//不能添加与已有字段同名的字段,否则会报如下错误

基于C#的ArcEngine二次开发教程(05):ArcEngine操作矢量数据的常用接口及用法代码示例_第5张图片

3.3.2 删除字段

        private void tsmi_deleteFiled_Click_1(object sender, EventArgs e)
        {
            IFeatureLayer myLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass myClass = myLayer.FeatureClass;
            IFields ifds = myClass.Fields;
            IField ifd = ifds.get_Field(4);
            myClass.DeleteField(ifd);
            MessageBox.Show("SUCESS");
        }

基于C#的ArcEngine二次开发教程(05):ArcEngine操作矢量数据的常用接口及用法代码示例_第6张图片

3.3.3 查找字段

        private void tsmi_findField_Click(object sender, EventArgs e)
        {
            IFeatureLayer myLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass myClass = myLayer.FeatureClass;
            int findIndex = myClass.FindField("Shape");
            MessageBox.Show("查找的字段的索引号为"+findIndex.ToString());
        }

基于C#的ArcEngine二次开发教程(05):ArcEngine操作矢量数据的常用接口及用法代码示例_第7张图片

3.3.4 获取属性值

        private void tmsi_readPropvalue_Click(object sender, EventArgs e)
        {
            IFeatureLayer myLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass myClass = myLayer.FeatureClass;
            IFeature ftu = myClass.GetFeature(1);
            int findIndex = myClass.FindField("NAME");
            string name = ftu.get_Value(findIndex).ToString();
            MessageBox.Show("读取到的属性值为" + name);
        }

基于C#的ArcEngine二次开发教程(05):ArcEngine操作矢量数据的常用接口及用法代码示例_第8张图片

4 小结

本文首先回顾ArcGIS矢量数据的基本知识,并介绍了操作矢量数据的5个常用的接口及其成员描述。

然后,从图层和字段属性操作两个层面,讲述具体的代码编写示例:

(1)图层名称变更、图层比例尺的设置和图层可见性

(2)图层字段读取、删除以及字段的索引值的获取、任意属性值的读取操作


更多精彩,欢迎关注个人微信公众账号“学而立行”

你可能感兴趣的:(基于C#的ArcEngine二次开发教程(05):ArcEngine操作矢量数据的常用接口及用法代码示例)