Arcgis engine 指定图层对要素进行创建、删除等操作

 

Arcgis engine 指定图层创建点要素

在指定的图层上创建一个点要素,点要素的位置是通过X,Y坐标指定的,下面是具体的注释 。其中 和IFeatureClassWrite接口有关的代码不要好像也可以实现这个功能,这里是直接通过IFeature添加要素的,不是通过IRow. 

The IFeatureClassWrite interface provides low-level write access to feature class data.  Any associated object behavior is not triggered. In general, IFeatureClassWrite should only be used when implementing custom features that bypass IRow::Store.

 

pLayer = this.axMapControl1.get_Layer(i);//所要加的层  

IFeatureLayer pFeatureLyr = pLayer as IFeatureLayer;//将ILayer转换为IFeaturelayer,为了对图层上的要素进行编辑  
IFeatureClass pFeatCls = pFeatureLyr.FeatureClass;//定义一个要素集合,并获取图层的要素集合  
IFeatureClassWrite fr = (IFeatureClassWrite)pFeatCls;//定义一个实现新增要素的接口实例,并该实例作用于当前图层的要素集  
IWorkspaceEdit w = (pFeatCls as IDataset).Workspace as IWorkspaceEdit;//定义一个工作编辑工作空间,用于开启前图层的编辑状态  
IFeature f;//定义一个IFeature实例,用于添加到当前图层上  
w.StartEditing(true);//开启编辑状态  
w.StartEditOperation();//开启编辑操作  
IPoint p;//定义一个点,用来作为IFeature实例的形状属性,即shape属性  
//下面是设置点的坐标和参考系  
p = new PointClass();  
p.SpatialReference = this.axMapControl1.SpatialReference;  
p.X = 600;  
p.Y = 500;  
  
//将IPoint设置为IFeature的shape属性时,需要通过中间接口IGeometry转换  
IGeometry peo;  
peo = p;  
f = pFeatCls.CreateFeature();//实例化IFeature对象, 这样IFeature对象就具有当前图层上要素的字段信息  
f.Shape = peo;//设置IFeature对象的形状属性  
f.set_Value(3, "house1");//设置IFeature对象的索引是3的字段值  
f.Store();//保存IFeature对象  
fr.WriteFeature(f);//将IFeature对象,添加到当前图层上  
w.StopEditOperation();//停止编辑操作  
w.StopEditing(true);//关闭编辑状态,并保存修改  
this.axMapControl1.Refresh();//刷新地图  

 

删除指定图层的全部要素

private void DeleteAll()
{
    ILayer pLayer = getLayerByName(axMapControl1, "Train");
    //getLayerByName()方法为自定义函数,获取名称为“Train”的图层
    IFeatureLayer pFeatureLyr = pLayer as IFeatureLayer;//将ILayer转换为IFeaturelayer,为了对图层上的要素进行编辑
    IFeatureClass pFeatCls = pFeatureLyr.FeatureClass;//定义一个要素集合,并获取图层的要素集合
    ITable pTable = (ITable)pFeatCls;
    pTable.DeleteSearchedRows(null);
    axMapControl1.ActiveView.Refresh();
}

 

arcengine创建要素类、图层的方法

对图层的创建除了可以参考下面的代码,也可以参考本博客的这篇文章: shp图层创建

/// <summary>
/// 创建要素类
/// </summary>
/// <param name="pObject">IWorkspace或者IFeatureDataset对象</param>
/// <param name="pName">要素类名称</param>
/// <param name="pSpatialReference">空间参考</param>
/// <param name="pFeatureType">要素类型</param>
/// <param name="pGeometryType">几何类型</param>
/// <param name="pFields">字段集</param>
/// <param name="pUidClsId">CLSID值</param>
/// <param name="pUidClsExt">EXTCLSID值</param>
/// <param name="pConfigWord">配置信息关键词</param>
/// <returns>返回IFeatureClass</returns>
public static IFeatureClass CreateFeatureClass(object pObject, string pName, ISpatialReference pSpatialReference, esriFeatureType pFeatureType,
              esriGeometryType pGeometryType, IFields pFields, UID pUidClsId, UID pUidClsExt, string pConfigWord)
{
    #region 错误检测
    if (pObject == null)
    {
        throw (new Exception("[pObject] 不能为空!"));
    }
    if (!((pObject is IFeatureWorkspace) || (pObject is IFeatureDataset)))
    {
        throw (new Exception("[pObject] 必须为IFeatureWorkspace 或者 IFeatureDataset"));
    }
    if (pName.Length == 0)
    {
        throw (new Exception("[pName] 不能为空!"));
    }
    if ((pObject is IWorkspace) && (pSpatialReference == null))
    {
        throw (new Exception("[pSpatialReference] 不能为空(对于单独的要素类)"));
    }
    #endregion

    // pUidClsID字段为空时

    if (pUidClsId == null)
    {
        pUidClsId = new UIDClass();
        switch (pFeatureType)
        {
            case (esriFeatureType.esriFTSimple):
                if (pGeometryType == esriGeometryType.esriGeometryLine)
                    pGeometryType = esriGeometryType.esriGeometryPolyline;
                pUidClsId.Value = "{52353152-891A-11D0-BEC6-00805F7C4268}";
                break;
            case (esriFeatureType.esriFTSimpleJunction):
                pGeometryType = esriGeometryType.esriGeometryPoint;
                pUidClsId.Value = "{CEE8D6B8-55FE-11D1-AE55-0000F80372B4}";
                break;
            case (esriFeatureType.esriFTComplexJunction):
                pUidClsId.Value = "{DF9D71F4-DA32-11D1-AEBA-0000F80372B4}";
                break;
            case (esriFeatureType.esriFTSimpleEdge):
                pGeometryType = esriGeometryType.esriGeometryPolyline;
                pUidClsId.Value = "{E7031C90-55FE-11D1-AE55-0000F80372B4}";
                break;
            case (esriFeatureType.esriFTComplexEdge):
                pGeometryType = esriGeometryType.esriGeometryPolyline;
                pUidClsId.Value = "{A30E8A2A-C50B-11D1-AEA9-0000F80372B4}";
                break;
            case (esriFeatureType.esriFTAnnotation):
                pGeometryType = esriGeometryType.esriGeometryPolygon;
                pUidClsId.Value = "{E3676993-C682-11D2-8A2A-006097AFF44E}";
                break;
            case (esriFeatureType.esriFTDimension):
                pGeometryType = esriGeometryType.esriGeometryPolygon;
                pUidClsId.Value = "{496764FC-E0C9-11D3-80CE-00C04F601565}";
                break;
        }
    }


    //pUidClsExt字段为空时
    if (pUidClsExt == null)
    {
        switch (pFeatureType)
        {
            case esriFeatureType.esriFTAnnotation:
                pUidClsExt = new UIDClass();
                pUidClsExt.Value = "{24429589-D711-11D2-9F41-00C04F6BC6A5}";
                break;
            case esriFeatureType.esriFTDimension:
                pUidClsExt = new UIDClass();
                pUidClsExt.Value = "{48F935E2-DA66-11D3-80CE-00C04F601565}";
                break;
        }
    }


    //字段集合为空时
    if (pFields == null)
    {
        //实倒化字段集合对象
        pFields = new FieldsClass();
        IFieldsEdit tFieldsEdit = (IFieldsEdit)pFields;

        //创建几何对象字段定义
        IGeometryDef tGeometryDef = new GeometryDefClass();
        IGeometryDefEdit tGeometryDefEdit = tGeometryDef as IGeometryDefEdit;

        //指定几何对象字段属性值
        tGeometryDefEdit.GeometryType_2 = pGeometryType;
        tGeometryDefEdit.GridCount_2 = 1;
        tGeometryDefEdit.set_GridSize(0, 1000);
        if (pObject is IWorkspace)
        {
            tGeometryDefEdit.SpatialReference_2 = pSpatialReference;
        }

        //创建OID字段
        IField fieldOID = new FieldClass();
        IFieldEdit fieldEditOID = fieldOID as IFieldEdit;
        fieldEditOID.Name_2 = "OBJECTID";
        fieldEditOID.AliasName_2 = "OBJECTID";
        fieldEditOID.Type_2 = esriFieldType.esriFieldTypeOID;
        tFieldsEdit.AddField(fieldOID);

        //创建几何字段
        IField fieldShape = new FieldClass();
        IFieldEdit fieldEditShape = fieldShape as IFieldEdit;
        fieldEditShape.Name_2 = "SHAPE";
        fieldEditShape.AliasName_2 = "SHAPE";
        fieldEditShape.Type_2 = esriFieldType.esriFieldTypeGeometry;
        fieldEditShape.GeometryDef_2 = tGeometryDef;
        tFieldsEdit.AddField(fieldShape);
    }



    //几何对象字段名称
    string strShapeFieldName = "";
    for (int i = 0; i < pFields.FieldCount; i++)
    {
        if (pFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
        {
            strShapeFieldName = pFields.get_Field(i).Name;
            break;
        }
    }

    if (strShapeFieldName.Length == 0)
    {
        throw (new Exception("字段集中找不到几何对象定义"));
    }

    IFeatureClass tFeatureClass = null;
    if (pObject is IWorkspace)
    {
        //创建独立的FeatureClass
        IWorkspace tWorkspace = pObject as IWorkspace;
        IFeatureWorkspace tFeatureWorkspace = tWorkspace as IFeatureWorkspace;
        tFeatureClass = tFeatureWorkspace.CreateFeatureClass(pName, pFields, pUidClsId, pUidClsExt, pFeatureType, strShapeFieldName, pConfigWord);
    }
    else if (pObject is IFeatureDataset)
    {
        //在要素集中创建FeatureClass
        IFeatureDataset tFeatureDataset = (IFeatureDataset)pObject;
        tFeatureClass = tFeatureDataset.CreateFeatureClass(pName, pFields, pUidClsId, pUidClsExt, pFeatureType, strShapeFieldName, pConfigWord);
    }

    return tFeatureClass;
}
创建图层、要素类

 

图层中批量添加点要素

创建好要素图层后,需要对要素图层添加要素。本部分以点要素的添加为例进行讲解。

/// <summary>
/// 点坐标 结构体
/// </summary>
public class PointXY 
{
    public double dX;
    public double dY;
}

/// <summary>
/// 建立 ESRI中的 点类型 并 将其转化为基类接口 IGeometry
/// </summary>
/// <param name="point">点坐标 结构体</param>
/// <returns></returns>
public IGeometry CreatePoint(PointXY point)
{
    IPoint pPoint = new PointClass();
    pPoint.X = point.dX;
    pPoint.Y = point.dY;
    IGeometry pGeometry = pPoint as IGeometry;
    return pGeometry;
}

/// <summary>
/// 批量加入 点坐标 结构体
/// </summary>
/// <param name="pLayer">点图层</param>
/// <param name="pointCol">泛型集合【点坐标 结构体】</param>
/// <returns></returns>
public bool AddPointsToLayer(ILayer pLayer, List<PointXY> pointCol)
{
    IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
    if (pFeatureLayer == null) 
    {
        MessageBox.Show(pLayer.Name + "不是矢量图层!"); 
        return false; 
    }
    //
    IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
    if (pFeatureClass.ShapeType  !=  esriGeometryType.esriGeometryPoint) 
    {
        System.Windows.Forms.MessageBox.Show(pLayer.Name + "不是点图层!"); 
         return false;
    }
    //
    IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);
    IFeatureBuffer pFeatureBuffer = null;
    foreach(PointXY one in pointCol)
    {                
        pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
        IFeature pNewFeature = pFeatureBuffer as IFeature;
        pNewFeature.Shape = BuildPoint(one);
        //
        pFeatureCursor.InsertFeature(pFeatureBuffer);
    }
    pFeatureCursor.Flush();

    return true;
}
批量添加点要素

 

 

 

 

参考文章

小马哥淡定 , Arcgis engine 指定图层创建点要素

删除指定图层的全部要素

arcengine创建要素类、图层的方法

GIS_LS  ,图层中批量添加点要素

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值