基于C#的ArcEngine二次开发27:点要素连接成线要素、线要素相交代码及说明

目录

1 点要素连接成线要素

1.1 需求说明

1.2 LineClass示例代码

2 线要素反向

3 由点集创建多段线要素

4 获取两条线要素的交点

4.1 创建交集代码

4.2 注意事项

4.3 根据polygon的拓扑关系创建线要素


 

 


1 点要素连接成线要素

1.1 需求说明

将两个点连接起来构成为直线要素

1.2 LineClass示例代码

A 2D straight line between a pair of 2D endpoints; can optionally have height, measure and ID attributes at each endpoint.

从二维点对生成二维直线,高程坐标、测度、ID等属性可选

用到的API:

属性/方法功能
LineClassFromPoint线段起点
 ToPoint线段的终点
        /// <summary>
        /// 创建线要素
        /// </summary>
        /// <returns>线几何图形</returns>
        public static IGeometry GetLineGeometry()
        {
               ILine line = new LineClass();
               line.FromPoint = GetPoint();
               line.ToPoint = GetPoint(); 
              return line as IGeometry;
        }

        /// <summary>
        /// 随机生成三维数据点
        /// </summary>
        /// <returns>三维数据点</returns>
        private static IPoint GetPoint()
        {
            const double Min = -10;
            const double Max = 10;
            Random random = new Random();
            double x = Min + (Max - Min) * random.NextDouble();
            double y = Min + (Max - Min) * random.NextDouble();
            double z = Min + (Max - Min) * random.NextDouble();
            return ConstructPoint3D(x, y, z);
        }

        /// <summary>
        /// 根据输入的x,y,z坐标,生成三维数据点
        /// </summary>
        /// <param name="x">X坐标</param>
        /// <param name="y">Y坐标</param>
        /// <param name="z">Z坐标</param>
        /// <returns>三维坐标点</returns>
        private static IPoint ConstructPoint3D(double x, double y, double z)
        {
            IPoint point = ConstructPoint2D(x, y);
            point.Z = z;
            return point;
        }

        /// <summary>
        /// 构建二维坐标点
        /// </summary>
        /// <param name="x">X坐标</param>
        /// <param name="y">Y坐标</param>
        /// <returns>二维点集合图形</returns>
        private static IPoint ConstructPoint2D(double x, double y)
        {
            IPoint point = new PointClass();
            point.PutCoords(x, y);
            return point;
        }

2 线要素反向

线要素在arcgis中的存储方式为一条有向的路径path,如果想要得到线要素反向,只需要获取线要素节点集合,然后将其逆序得到新的线要素即为反向线要素

        /// <summary>
        /// 线要素反向
        /// </summary>
        /// <param name="polyline">线要素</param>
        /// <returns>反向之后的线要素</returns>
        private IPolyline ChangeDirection(IPolyline polyline)
        {
            IPointCollection pointCollection = polyline as IPointCollection;
            IPointCollection newPC = new PolylineClass();
            for (int i = pointCollection.PointCount - 1; i >= 0; i--)
            {
                newPC.AddPoint(pointCollection.get_Point(i), mis, mis);
            }
            return newPC as IPolyline;
        }

3 由点集创建多段线要素

接口跳转的前提是有类实现了该接口,比如使用PolylineClass实现了IPointCollection 接口,然后添加点之后在跳转到IPolyline;也就是之所以能够实现接口跳转,是因为有一个类同时实现了这两个接口,这样以这个类为跳板,实现接口之间的跳转。

注意:使用点集接口创建PolylineClass类对象,之后添加点集之后,跳转为IPolyline

        /// <summary>
        /// 由点集创建多段线
        /// </summary>
        /// <param name="pts">点集</param>
        /// <returns>多段线</returns>
        public IPolyline AddLineByWrite(List<IPoint> pts)
        {                     
            IPointCollection ptColl = new PolylineClass();
            for (int i = 0; i < pts.Count; i++)
            {
                ptColl.AddPoint(pts[i]);
            }
            return ptColl as IPolyline;         
        }

4 获取两条线要素的交点

4.1 创建交集代码

ArcEngine中相交分析用到接口为 ITopologicalOperator 这里需要用到它里面的 Intersect方法,功能很强大,能根据两个交的几何图形得到对应的各种交集(点,线,面等)现在我要做两个线段的交集,代码如下:

IPolyline tmpLine1 = new PolylineClass();  
tmpLine1.FromPoint = 起点;  
tmpLine1.ToPoint = 终点;  
  
IPolyline tmpLine2 = new PolylineClass();  
tmpLine2.FromPoint = 起点;  
tmpLine2.ToPoint = 终点;  
  
ITopologicalOperator topoOperator = tmpLine1  as ITopologicalOperator;  
  
IGeometry geo = topoOperator.Intersect(tmpLine2 , esriGeometryDimension.esriGeometry0Dimension);  
if (!geo.IsEmpty)  
{  
   IPointCollection Pc = geo as IPointCollection;  
   IPoint Pt = Pc.get_Point(0);  
}  

Intersect(tmpLine2 , esriGeometryDimension.esriGeometry0Dimension)方法的第二个参数,决定的是输出的要素的类型,如果要输出交点就是0D

3D  IPolyline 要打开Z属性。上面的代码需要加上这个:

IZAware zaware = tmpLine1 as IZAware;
zaware.ZAware = true;

两条线都要加,这样再对其做相交分析就能正常得到Z属性了!

4.2 注意事项

以下是官方文档的说明:

Intersection is basically an AND between input geometries.

ITopologicalOperator methods must be applied on high-level geometries only. High-Level geometries are point, multipoint, polyline and polygon. To use this method with low-level geometries such as segments (Line, Circular Arc, Elliptic Arc, Bézier Curve), paths or rings, they must be wrapped into high-level geometries types.

This method does not support GeometryBags.

Since ArcGIS 9.2, Intersect has a larger cost - it takes longer to run the method.  Therefore, it is a better approach to test if the two geometries are disjoint before calling Intersect.

ntersection基本上是输入几何图形之间的AND。ItopologicalOperator方法只能应用于高级几何图形。高级几何图形包括点、多点、多段线和多边形。要将此方法用于低层几何图形,如线段(直线、圆弧、椭圆弧、Bézier曲线)、路径或环,必须将它们包装成高层几何图形类型。此方法不支持GeometryBags。因为ArcGis9.2,Intersect有更大的成本-运行该方法需要更长的时间。因此,在调用Intersect之前测试两个几何图形是否不相交是一种更好的方法。

也就是相交不能针对简单图形,简单图形要做一层高级包装

//The following code shows to wrap a line segment into a polyline in C#
//Assume a line (line1 as ILine) is already created
object obj = Type.Missing;
ISegmentCollection segCollection = new PolylineClass() as ISegmentCollection;
segCollection.AddSegment((ISegment)line1, ref obj, ref obj);

//Set the spatial reference on the new polyline
//The spatial reference is not transfered automatically from the segments
IGeometry geom = segCollection as IGeometry;
geom.SpatialReference = spatialRef;

//Can now be used with ITopologicalOperator methods

4.3 根据polygon的拓扑关系创建线要素

void CreatePolylineFromExistingGeometries(ESRI.ArcGIS.Geometry.IPolygon pPolygon1,
    ESRI.ArcGIS.Geometry.IPolygon pPolygon2)
{
    //Build a new polyline by intersecting two existing polygons.
    ESRI.ArcGIS.Geometry.ITopologicalOperator2 pTopoOp2 = pPolygon1 as
        ITopologicalOperator2;
    //Simplify.
    pTopoOp2.IsKnownSimple_2 = false;
    pTopoOp2.Simplify();

    ESRI.ArcGIS.Geometry.IPolyline pPoly = pTopoOp2.Intersect(pPolygon2,
        ESRI.ArcGIS.Geometry.esriGeometryDimension.esriGeometry1Dimension)as
        IPolyline;
    //pPoly is the new generated polyline.
}

5 IRelationalOperator

Relational Operators compare two geometries and return a boolean indicating whether or not the desired relationship exists.  Some relationships require that the input geometries be of the same dimension while other have more flexible dimensional constraints.  Most of the predefined Relational Operators are mutually exclusive Clementini operators. Please see the "Shape Comparison Language" topic in the Technical Documents section in the help.

关系运算符比较两个几何图形并返回一个布尔值,指示所需关系是否存在。有些关系要求输入几何图形具有相同的尺寸,而另一些关系则具有更灵活的尺寸约束。大多数预定义的关系运算符都是互斥的Clementini运算符。请参阅帮助中技术文档部分的“形状比较语言”主题。

AllPropertiesMethodsDescription
Contains

Indicates if this geometry contains the other geometry.

【前者是否包含后者】

Crosses

Indicates if the two geometries intersect in a geometry of lesser dimension.

【是否在更低的维度相交】

Disjoint

Indicates if the two geometries share no points in common. Negate this result to compute the Intersect relation.

[是否有共同点,求反可验证相交]

Equals

Indicates if the two geometries are of the same type and define the same set of points in the plane.

【指示两个几何图形是否属于同一类型并在平面中定义相同的点集。】

Overlaps

Indicates if the intersection of the two geometries has the same dimension as one of the input geometries.

【指示两个几何图形的交集是否与一个输入几何图形具有相同的尺寸】

Relation

Indicates if the defined relationship exists.

【指示定义的关系是否存在】

Touches

Indicates if the boundaries of the geometries intersect.

【指示几何图形的边界是否相交】

Within

Indicates if this geometry is contained (is within) another geometry.

【指示此几何图形是否包含在另一个几何图形中。】


有兴趣可关注公众号:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小薛引路

喜欢的读者,可以打赏鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值