一个完备的要素SHAPE裁剪函数

要素裁剪的问题,即如何裁剪要素的SHAPE,还有许多网友在询问,其实这并没有什么特别之处。除了AO自带的Clip函数以外,我一般都是使用ITopologicalOperator接口提供的拓扑方法来自己搞定,原因很简单,每一条要素的裁剪情况都能自己掌握。

要素裁剪无非两种情况,一是不处理裁剪面边缘上的要素,统统删除,这个没有什么问题,空间关系选择Intersect即可;另一个是裁剪边缘上的要素,如Polygon如果与裁剪面相交,则保留不想交的部分,如果Polyline与裁剪面相交,也是保留面外面的一部分线段。

这几个拓扑方法在很多时候会爆出异常,其实解决的方法在网上早就有了,关键是两个Geoemtry的SpatialReference需要一致才行。下面给出一个完备的函数:
        public static IGeometry getUpdateOuterGeometry2(IGeometry srcGeo, IGeometry refPolygon)
        {
            if (srcGeo.SpatialReference != null && refPolygon.SpatialReference == null)
                refPolygon.Project(srcGeo.SpatialReference);
            if (srcGeo.SpatialReference == null && refPolygon.SpatialReference != null)
                srcGeo.Project(refPolygon.SpatialReference);

            IGeometry pGeo = null;
            ITopologicalOperator2 pTopo;
            ITopologicalOperator2 pTopo2;
            switch (srcGeo.GeometryType)
            {
                case esriGeometryType.esriGeometryPolyline:
                    pTopo = (ITopologicalOperator2)refPolygon ;
                    pTopo.IsKnownSimple_2 = false;
                    pTopo.Simplify();
                    pTopo2 = (ITopologicalOperator2)srcGeo;
                    pTopo2.IsKnownSimple_2 = false;
                    pTopo2.Simplify();
                    pGeo = pTopo2.Difference(pTopo.Intersect(srcGeo, esriGeometryDimension.esriGeometry1Dimension));
                    break;
                case esriGeometryType.esriGeometryPolygon:
                    pTopo = (ITopologicalOperator2)srcGeo;
                    pTopo.IsKnownSimple_2 = false;
                    pTopo.Simplify();
                    pGeo = pTopo.Difference(refPolygon);
                    break;
                default:
                    pGeo = srcGeo;
                    break;
            }
            return pGeo;
        }

posted on 2008-10-20 10:18  浩淼  阅读(1234)  评论(0编辑  收藏  举报

导航