图形合并
/// <summary>
/// 计算合并面积(去掉岛)
/// </summary>
/// <param name="pArrayPolygon">图形集合</param>
/// <param name="isShowError"></param>
/// <returns></returns>
public static IPolygon UnionOrRemoveRingFromPolygon(ArrayList pArrayPolygon, bool isShowError)
{
IPolygon pPolygon_Each = null;
IPolygon pPolygon_Result = null;
IGeometryCollection pGeometryCollection = null;
ISegmentCollection pSegmentCollection_Ring = null;
object oMissing = Type.Missing;
try
{
if (pArrayPolygon == null)
return null;
if (pArrayPolygon.Count == 0)
return null;
pGeometryCollection = new PolygonClass();
pPolygon_Result = pGeometryCollection as IPolygon;
for (int i = 0; i < pArrayPolygon.Count; i++)
{
pPolygon_Each = pArrayPolygon[i] as IPolygon;
IGeometryCollection pGeometryCollection_Temp = null;
pGeometryCollection_Temp = pPolygon_Each as IGeometryCollection;
for (int j = 0; j < pGeometryCollection_Temp.GeometryCount; j++)
{
pSegmentCollection_Ring = new RingClass();
pSegmentCollection_Ring.AddSegmentCollection(pGeometryCollection_Temp.get_Geometry(j) as ISegmentCollection);
pGeometryCollection.AddGeometry(pSegmentCollection_Ring as IGeometry, ref oMissing, ref oMissing);
}
}
pPolygon_Result.SimplifyPreserveFromTo();
return pPolygon_Result;
}
catch (Exception ex)
{
//if (isShowError)
// MessageBox.Show("合并(去岛)多边形发生错误,错误信息为:->\r\n" + ex.Message,
// "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
-----------------------------------------------
ITopologicalOperator 接口实现了union和ConstructUnion两种合并要素的方法,union只能实现两种要素的合并,不能实现两种以上的要素合 并,ConstructUnion方法可以实现两种以上的要素合并。这两种合并方法合并后,原来的要素还存在。这与merge方法有所区别。举个例子。有 两个面,面A和面B,这两个面不一定要相邻。选中两个面后,如果用Union,则产生一个新的面要素,即为面A和面B的集合,而原来的面A和面B依然保 留。如果用Merge也产生一个新的要素,几何形体依然为面A和面B的集合,即面C,但它的属性只能从A和B中选择一个,也就是说属性合并。而且原来的面 A和面B也就不存在了。另外,无论是Union还是Merge在合并的时间,必须选择同一种几何类型,如面与面合并,线与线合并。
具体代码如下:
public void UnionFeatures()
{
IMap pMap = MapControl.Map;
IActiveView pActiveView = pMap as IActiveView;
IGeometryCollection Geometrybag = new GeometryBagClass();//装geometry的袋子
IEnumFeature pEnumFeat = (IEnumFeature)pMap.FeatureSelection;
IFeature pFeat = pEnumFeat.Next();
object oMissing = Type.Missing;
while (pFeat != null)
{
IGeometry pGeometry = pFeat.Shape as IGeometry;
Geometrybag.AddGeometry(pGeometry, ref oMissing, ref oMissing);//将geometry装进袋子
pFeat.Delete();
pFeat = pEnumFeat.Next();
}
ITopologicalOperator unionedpolygon = new PolygonClass();
unionedpolygon.ConstructUnion(Geometrybag as IEnumGeometry);//
IGeometry pGeo = unionedpolygon as IGeometry;
CreateUnionFeature(pGeo);
pActiveView.Refresh();
}