using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;
namespace union
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ListGeometry = new List<IGeometry>();
}
private IFeatureClass pFeatureClass;
private IFeatureWorkspace pFeatureWorkspace;
private List<IGeometry> ListGeometry;
private void button1_Click(object sender, EventArgs e)
{
# region one of the method
//string filePath = @"O:\Experiment Labtory\Data\";
//string fileName = @"unionFeatures.shp";
//axMapControl1.AddShapeFile(filePath, fileName);
# endregion
#region two of the method
IWorkspaceFactory pWF = new ShapefileWorkspaceFactoryClass();
IWorkspace pW = pWF.OpenFromFile("O:\\Experiment Labtory\\Data", 0);
IFeatureWorkspace pFeatureWorkspace = pW as IFeatureWorkspace;
this.pFeatureWorkspace = pFeatureWorkspace;
IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass("newunionFeatrueClass.shp");
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFC;
this.pFeatureClass = pFC;
axMapControl1.AddLayer(pFeatureLayer);
#endregion
}
private void button2_Click(object sender, EventArgs e)
{
int i, j;
//IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
int featureCount = pFeatureClass.FeatureCount(null);
//IFeature pFirstFeature = pFeatureCursor.NextFeature();
IFeature pFirstFeature;
IPolygon pPolygon;
IRelationalOperator pRelationOp;
ITopologicalOperator4 pToplogicalOp;
for (i = 0; i < featureCount - 1; i++)
{
pFirstFeature = pFeatureClass.GetFeature(i);
pPolygon = (IPolygon)pFirstFeature.ShapeCopy;
pRelationOp = (IRelationalOperator)pPolygon;
pToplogicalOp = (ITopologicalOperator4)pPolygon;
pToplogicalOp.Simplify();
for (j = i + 1; j < featureCount; j++)
{
IGeometry pGeometry = pFeatureClass.GetFeature(j).ShapeCopy as IGeometry;
if (pRelationOp.Overlaps(pGeometry))
{
ListGeometry.Add(pToplogicalOp.Union(pGeometry));
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//UID u = null;
//IFeatureClass pFeatureClass=pFeatureWorkspace.CreateFeatureClass
// ("NewUnionFeatureClass", null, u, u, esriFeatureType.esriFTSimple, "NewUnionFeatureClass", "");
IWorkspaceFactory pWF = new ShapefileWorkspaceFactoryClass();
IWorkspace pW = pWF.OpenFromFile("O:\\Experiment Labtory\\Data", 0);
IFeatureWorkspace pFeatureWorkspace = pW as IFeatureWorkspace;
this.pFeatureWorkspace = pFeatureWorkspace;
IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass("newnewFeatureClass.shp");
if (pFC.ShapeType == esriGeometryType.esriGeometryPolygon)
{
IFeature pFeature;
foreach (IGeometry g in ListGeometry)
{
pFeature = pFC.CreateFeature();
pFeature.Shape = g;
pFeature.Store();
}
}
}
}
}