最近做项目,遇到一个问题:去除面要素的图斑,即在shp面要素中,去除面积小于XX的内环,保留面积大于XX的内环。上网搜索没有现成的代码,于是自己写了一个,现在分享给大家,希望对大家有帮助。
关键代码:
private IGeometryCollection ConsoleRing(IFeatureLayer featureLayer)
{
IFeatureClass featureClass = featureLayer.FeatureClass;
IFeatureCursor featureCursor = featureClass.Update(null, false);// featureLayer.Search(null, false);
IFeature feature = featureCursor.NextFeature();
while (feature != null)
{
double dArea = 0.0;
IPolygon4 polygon = feature.ShapeCopy as IPolygon4;
IGeometryCollection pGeometryCollection = new PolygonClass();
IGeometryBag bag = polygon.ExteriorRingBag; //获取多边形的所有外环
IGeometryCollection newPolygonGeo = new PolygonClass();
IEnumGeometry geo = bag as IEnumGeometry;
geo.Reset();
IRing exRing = geo.Next() as IRing;
while (exRing != null)
{
IGeometryBag bags = polygon.get_InteriorRingBag(exRing); //获取当前外环所包含的内环
//第一步 把外环加到newPolygonGeo
addExt2Poly(newPolygonGeo, exRing);
IEnumGeometry geos = bags as IEnumGeometry;
geos.Reset();
IRing inRing = geos.Next() as IRing;
while (inRing != null)
{
IArea pArea = inRing as IGeometry as IArea;
dArea = pArea.Area;
if (Math.Abs(dArea) >= int.Parse(txtArea.Text))
{
//第二步 把面积大于的加到newPolygonGeo
newPolygonGeo.AddGeometry(inRing as IGeometry);
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(inRing); //释放资源
inRing = geos.Next() as IRing;
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(geos);
exRing = geo.Next() as IRing;
}
feature.Shape = newPolygonGeo as IGeometry;
featureCursor.UpdateFeature(feature);
System.Runtime.InteropServices.Marshal.ReleaseComObject(geo);
feature = featureCursor.NextFeature();
if (feature == null)
{
featureCursor.Flush();
System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
featureCursor = null;
}
return newPolygonGeo;
}
return null;
}