ArcEngine中多边形内外环的处理(转)
ArcEngine中多边形内外环的处理
Polylgon对象是由一个或多个Ring对象的有序集合,它可以是由单个Ring对象构成,也可以使用多个Ring组成如下图所示。其中Ring可以分为Outer Ring(外环)和Inner Ring(内环)之分。外环和内环都是有方向的,它们的区别是外环的方向是顺时针的,内环的方向是逆时针。如果多边形只有外环,没有内环,很好处理,难得是如何将内环和外环区分开来,并且保证正确的内外环包含关系。
前段时间做MDB数据库的坐标转换功能,由于针对的是地方坐标系,所以无法用7参数构建投影坐标统一转换。我采用的是将featureLayer中的Feature一个一个取出来,将每个点的起始点坐标取出来进行转换,然后再组合回去。现在将多边形部分代码贴出来:
IPolygon4 polygon = pFeature.ShapeCopy as IPolygon4;
IGeometryBag bag = polygon.ExteriorRingBag; //获取多边形的所有外环
IEnumGeometry geo = bag as IEnumGeometry;
geo.Reset();
IRing exRing = geo.Next() as IRing;
while (exRing != null)
{
IGeometryBag bags = polygon.get_InteriorRingBag(exRing); //获取当前外环所包含的内环
IEnumGeometry geos = bags as IEnumGeometry;
geos.Reset();
IRing inRing = geos.Next() as IRing;
while (inRing != null) //先处理内环
{
ISegmentCollection segs = inRing as ISegmentCollection;
ISegmentCollection ring = new RingClass(); //创建新的ring对象,以重新组合polygon
for (int i = 0; i < segs.SegmentCount; i++)
{
object o = Type.Missing;
ISegment s = segs.get_Segment(i);
IPoint fromPoint = s.FromPoint;
IPoint toPoint = s.ToPoint;
x = fromPoint.X;
y = fromPoint.Y;
fromPoint.PutCoords(ConvertTo2000X(x, y), ConvertTo2000Y(x, y)); //将X、Y坐标转换
s.FromPoint = fromPoint;
x1 = toPoint.X;
y1 = toPoint.Y;
toPoint.PutCoords(ConvertTo2000X(x1, y1), ConvertTo2000Y(x1, y1));
s.ToPoint = toPoint;
ring.AddSegment(s, ref o, ref o);
}
IRing ir = ring as IRing;
ir.Close();
ppolygon.AddGeometry(ir);
inRing = geos.Next() as IRing;
}
ISegmentCollection seg = exRing as ISegmentCollection;
ISegmentCollection rings = new RingClass();
for (int i = 0; i < seg.SegmentCount; i++) //处理外环
{
object o = Type.Missing;
ISegment s = seg.get_Segment(i);
IPoint fromPoint = s.FromPoint;
IPoint toPoint = s.ToPoint;
x = fromPoint.X;
y = fromPoint.Y;
fromPoint.PutCoords(ConvertTo2000X(x, y), ConvertTo2000Y(x, y));
s.FromPoint = fromPoint;
x1 = toPoint.X;
y1 = toPoint.Y;
toPoint.PutCoords(ConvertTo2000X(x1, y1), ConvertTo2000Y(x1, y1));
s.ToPoint = toPoint;
rings.AddSegment(s, ref o, ref o);
}
IRing irr = rings as IRing;
irr.Close();
ppolygon.AddGeometry(irr);
exRing = geo.Next() as IRing;
}
IPolygon p = ppolygon as IPolygon;
p.SimplifyPreserveFromTo(); //保证多边形封闭
pFeature.Shape = p;
pFeature.Store();
通过以上的方法能够将polygon取出来,实现自定义的坐标转换过程,由于先取出来然后再重新组合,所以效率不是很高,但是经过大数据量的验证,表明效率能够接受。