ArcEngine合并要素
开发环境:VS2013 + ArcEngine 10.4
在数据量较大时,请用ITopologicalOperator的ConstructUnion方法,而不是一个一个Union
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace WindowsFormsApplication2
{
public class MergeTool
{
/// <summary>
/// 输入要素
/// </summary>
private IFeatureClass in_FeatureClass;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="in_FeatureClass"></param>
public MergeTool(IFeatureClass in_FeatureClass)
{
this.in_FeatureClass = in_FeatureClass;
}
/// <summary>
/// 获取空间参考
/// </summary>
/// <returns></returns>
private ISpatialReference GetSpatialReference()
{
IGeoDataset pGeoDataset = in_FeatureClass as IGeoDataset;
ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;
return pSpatialReference;
}
/// <summary>
/// 合并几何体
/// </summary>
/// <returns></returns>
private IGeometry GetMergeGeometry()
{
IGeometryBag pGeometryBag = new GeometryBag() as IGeometryBag;
pGeometryBag.SpatialReference = GetSpatialReference();
IGeometryCollection pGeometryCollection = pGeometryBag as IGeometryCollection;
// 属性过滤
IQueryFilter pQueryFilter = new QueryFilter();
pQueryFilter.AddField("Shape");
// 要素游标
IFeatureCursor pFeatureCursor = in_FeatureClass.Search(pQueryFilter, true);
IFeature pFeature = pFeatureCursor.NextFeature();
if (pFeature == null)
{
return null;
}
// 遍历游标
object missing = Type.Missing;
while (pFeature != null)
{
pGeometryCollection.AddGeometry(pFeature.ShapeCopy, ref missing, ref missing);
pFeature = pFeatureCursor.NextFeature();
}
Marshal.ReleaseComObject(pFeatureCursor);
// 合并要素
ITopologicalOperator pTopologicalOperator = null;
if (in_FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
{
pTopologicalOperator = new Multipoint() as ITopologicalOperator;
pTopologicalOperator.ConstructUnion(pGeometryCollection as IEnumGeometry);
}
else if (in_FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{
pTopologicalOperator = new Polyline() as ITopologicalOperator;
pTopologicalOperator.ConstructUnion(pGeometryCollection as IEnumGeometry);
}
else
{
pTopologicalOperator = new Polygon() as ITopologicalOperator;
pTopologicalOperator.ConstructUnion(pGeometryCollection as IEnumGeometry);
}
return pTopologicalOperator as IGeometry;
}
/// <summary>
/// 执行工具
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public IFeatureClass ExecuteTool(string filePath)
{
IGeometryDef pGeometryDef = new GeometryDef();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
if (in_FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
{
pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryMultipoint;
pGeometryDefEdit.HasM_2 = false;
pGeometryDefEdit.HasZ_2 = false;
pGeometryDefEdit.SpatialReference_2 = GetSpatialReference();
}
else if (in_FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{
pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
pGeometryDefEdit.HasM_2 = false;
pGeometryDefEdit.HasZ_2 = false;
pGeometryDefEdit.SpatialReference_2 = GetSpatialReference();
}
else
{
pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
pGeometryDefEdit.HasM_2 = false;
pGeometryDefEdit.HasZ_2 = false;
pGeometryDefEdit.SpatialReference_2 = GetSpatialReference();
}## 标题
// 字段集合
IFields pFields = new Fields();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
// Shape
IField pField = new Field();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
pFieldEdit.GeometryDef_2 = pGeometryDef;
pFieldEdit.AliasName_2 = "Shape";
pFieldEdit.Name_2 = "Shape";
pFieldEdit.IsNullable_2 = false;
pFieldEdit.Required_2 = true;
pFieldsEdit.AddField(pField);
// 创建要素类
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(filePath), 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(System.IO.Path.GetFileName(filePath), pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
// 插入要素
IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);
pFeatureBuffer.Shape = GetMergeGeometry();
pFeatureCursor.InsertFeature(pFeatureBuffer);
pFeatureCursor.Flush();
return pFeatureClass;
}
}
}
运行结果:
初始数据如下图:
合并数据如下图:
转载自:https://blog.csdn.net/HerryDong/article/details/82751410