How to efficiently create a polyline


Summary
A polyline can be built in several ways according to the user's input. In this article, some of the most commonly used methods are listed for efficiently creating a polyline.

In this topic


Building a polyline using points

The following code example shows how to build a polyline using a collection of points. This approach is preferred when the user has a sequence of vertices (points) as input. The resulting polyline path will only contain straight line segments.
[Java]
static void createPolylineByPoints()throws Exception{
    //Build polyline from a sequential of points. 
    //At 9.2, the recommended way to add arrays of points to a geometry is to use
    //the IGeometryBridge2 interface on the GeometryEnvironment singleton object.
    IGeometryBridge2 geomBridge = new GeometryEnvironment();

    //pPointColl is the new polyline
    IPointCollection4 polyline = new Polyline();
    //TODO:Set the spatial reference of the new polyline


    int count = ...;
    _WKSPoint[] points = new _WKSPoint[count];
    //TODO: Initialize the points array with proper coordinates


    geomBridge.setWKSPoints(polyline, points);
    //polyline has now been defined
}

Building a polyline using segments

In the following code example, the multipart polyline is built segment by segment. It gives you the most control if you are using advanced construction techniques or curved segments (circular arcs, Bézier curves, and so on).
[Java]
static void createPolylineBySegments()throws Exception{

    //Build a polyline segment-by-segment
    IPolyline polyline = new Polyline();
    //Set pSegPoly.SpatialReference = //Always define the spatial reference of new top level geometries

    //Create the segments and paths – if this were a single part polyline we could add
    //segments directly to the polyline.
    //You cannot re-use the same path object; also, when paths are added to the polyline
    //it takes ownership of them. You cannot then reuse a path for building another polyline.
    //These same restrictions also apply to segments.
    ICircularArc circularArc = new CircularArc();
    IBezierCurveGEN bezCurve = new BezierCurve();
    ILine line = new Line();
    ISegmentCollection path1 = new Path();
    ISegmentCollection path2 = new Path();
    //The first part of the polyline contains a half circle arc segment
    path1.addSegment((ISegment)circularArc, null, null);
    //The second part of the polyline contains a bezier curve segment and a line segment
    path2.addSegment((ISegment)bezCurve, null, null);
    path2.addSegment((ISegment)line, null, null);
    IGeometryCollection geomCollection = (IGeometryCollection)polyline;
    geomCollection.addGeometry((IGeometry)path1, null, null);
    geomCollection.addGeometry((IGeometry)path2, null, null);

    //At this point, we have constructed a _shell_ geometry. It consists of one
    //polyline containing two paths, each of which contains one segment.
    //However, the coordinates of those segments have not yet been defined.
    //Because we still have references to those segments, we can define their
    //coordinates now.
    IPoint point = new Point();
    point.setX( - 10);
    point.setY(0);
    circularArc.putCoordsByAngle(point, 0, 3.14159265358979, 10.0); 
        //a half circle arc
    IPoint[] points = new IPoint[]{
        new Point(), new Point(), new Point(), new Point()
    };
    points[0].setX(10);
    points[0].setY(0);
    points[1].setX(10);
    points[1].setY(10);
    points[2].setX(20);
    points[2].setY(10);
    points[3].setX(20);
    points[3].setY(0);
    bezCurve.putCoords(points);
    line.getFromPoint().putCoords(20, 0);
    line.getToPoint().putCoords(30, 0);

    //pPolyline has now been defined. When changing segment coordinates directly
    //like this, we need to be careful to let the top level geometry know that
    //things have changed underneath it, so that it can delete any cached properties
    //that it might be maintaining, such as envelope, length, area, etc.
    //Note that when you use certain methods on the top-level geometry implementation
    //of IGeometryCollection interface, like AddGeometry, it will automatically
    //invalidate any cached properties.
    geomCollection.geometriesChanged();

}

Creating a polyline using existing geometries

A polyline can be created based on topological relationships between existing geometries. In the following code example, a polyline is generated by intersecting two existing polygons:
[Java]
static void createPolylineFromExistingGeometries(IPolygon pPolygon1, IPolygon
    pPolygon2)throws Exception{

    //Build a new polyline by intersect two existing polygons
    ITopologicalOperator2 topoOp = (ITopologicalOperator2)pPolygon1;
    //Simplify
    topoOp.setIsKnownSimple(false);
    topoOp.simplify();

    IPolyline newPolyline = (IPolyline)topoOp.intersect(pPolygon2,
        esriGeometryDimension.esriGeometry1Dimension);

}