博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Arcgis engine 指定图层对要素进行创建、删除等操作
阅读量:6583 次
发布时间:2019-06-24

本文共 8814 字,大约阅读时间需要 29 分钟。

 

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创建要素类、图层的方法

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

/// /// 创建要素类/// /// IWorkspace或者IFeatureDataset对象/// 要素类名称/// 空间参考/// 要素类型/// 几何类型/// 字段集/// CLSID值/// EXTCLSID值/// 配置信息关键词/// 
返回IFeatureClass
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;}
创建图层、要素类

 

图层中批量添加点要素

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

/// /// 点坐标 结构体/// public class PointXY {    public double dX;    public double dY;}/// /// 建立 ESRI中的 点类型 并 将其转化为基类接口 IGeometry/// /// 点坐标 结构体/// 
public IGeometry CreatePoint(PointXY point){ IPoint pPoint = new PointClass(); pPoint.X = point.dX; pPoint.Y = point.dY; IGeometry pGeometry = pPoint as IGeometry; return pGeometry;}/// /// 批量加入 点坐标 结构体/// /// 点图层/// 泛型集合【点坐标 结构体】///
public bool AddPointsToLayer(ILayer pLayer, List
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;}
批量添加点要素

 

 

 

 

参考文章

小马哥淡定 ,

  ,

 

转载地址:http://tnxno.baihongyu.com/

你可能感兴趣的文章
js之侧边栏分享
查看>>
IGS_学习笔记09_IREP生成服务后台工具Soagenerate.sh
查看>>
linux内核分析作业4:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用...
查看>>
Ferris教程学习笔记:js示例3.3 累加按钮,自加1
查看>>
springboot秒杀课程学习整理1-4
查看>>
org.eclipse.ui.actionSets扩展点
查看>>
备忘录模式(Memento Pattern)
查看>>
SQL SERVER全面优化-------Expert for SQL Server 诊断系列
查看>>
2018国庆qbxt刷题游记(目录)
查看>>
position:sticky布局
查看>>
简述XSS攻击及其防范措施
查看>>
Perf工具
查看>>
excel 添加换行符,去除换行符:
查看>>
小米5sp手机QQSD卡不可用
查看>>
httpWebRequest获取流和WebClient的文件抓取
查看>>
Linux下常用SVN命令
查看>>
[Leetcode]198. House Robber
查看>>
异步的事件轮询机制
查看>>
数学基础-概率论
查看>>
VALID_FOR in db standby
查看>>