How to efficiently create a polyline

SummaryA 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.

Sub CreatePolylineByPoints()

'Build polyline from a sequence of vertices (points). 
'At ArcGIS 9.2, the recommended way to add arrays of points to a geometry is to use
'the IGeometryBridge2 interface on the GeometryEnvironment singleton object.
Dim pGeoBrg As ESRI.ArcGIS.Geometry.IGeometryBridge2
pGeoBrg = New ESRI.ArcGIS.Geometry.GeometryEnvironment

'pPointColl is the new polyline.
Dim pPointColl As ESRI.ArcGIS.Geometry.IPointCollection4
pPointColl = New ESRI.ArcGIS.Geometry.Polyline
'Set pPointColl.SpatialReference = 'Define the spatial reference of the new polyline.

Dim aWKSPointBuffer() As ESRI.ArcGIS.esriSystem.WKSPoint
Dim cPoints As Long
cPoints = 4 'The number of points in the first part.
ReDim aWKSPointBuffer(0 To cPoints - 1)
'aWKSPointBuffer = 'Read cPoints into the point buffer.

pGeoBrg.SetWKSPoints(pPointColl, aWKSPointBuffer)
'pPointColl has now been defined.

End Sub

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).

Sub CreatePolylineBySegments()

'Build a polyline segment by segment.
Dim pSegPoly As ESRI.ArcGIS.Geometry.IPolyline
pSegPoly = New ESRI.ArcGIS.Geometry.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 you could add
'segments directly to the polyline.
'You cannot reuse 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.
Dim cArc As ESRI.ArcGIS.Geometry.ICircularArc
cArc = New ESRI.ArcGIS.Geometry.CircularArc
Dim bCur As ESRI.ArcGIS.Geometry.IBezierCurve
bCur = New ESRI.ArcGIS.Geometry.BezierCurve
Dim line As ESRI.ArcGIS.Geometry.ILine
line = New ESRI.ArcGIS.Geometry.Line
Dim path1 As ESRI.ArcGIS.Geometry.ISegmentCollection, path2 As ESRI.ArcGIS.Geometry.ISegmentCollection
path1 = New ESRI.ArcGIS.Geometry.Path
path2 = New ESRI.ArcGIS.Geometry.Path
'The first part of the polyline contains a half circle arc segment.
'The second part of the polyline contains a Bézier curve segment and a line segment.
Dim pGeoColl As ESRI.ArcGIS.Geometry.IGeometryCollection
pGeoColl = pSegPoly

'At this point, a _shell_ geometry has been constructed. It consists of one
'polyline containing two paths, each of which contains one segment.
'However, the coordinates of those segments have not been defined.
'Because you still have references to those segments, you can define their
'coordinates now.
Dim pPnt As ESRI.ArcGIS.Geometry.IPoint
pPnt = New ESRI.ArcGIS.Geometry.Point
pPnt.X = -10
pPnt.Y = 0
cArc.PutCoordsByAngle(pPnt, 0, 3.14159265358979, 10.0#) 'A half circle arc.
Dim pntArray(0 To 3) As ESRI.ArcGIS.Geometry.IPoint
Dim i As Long
For i = 0 To 3
pntArray(i) = New ESRI.ArcGIS.Geometry.Point
Next i
pntArray(0).X = 10
pntArray(0).Y = 0
pntArray(1).X = 10
pntArray(1).Y = 10
pntArray(2).X = 20
pntArray(2).Y = 10
pntArray(3).X = 20
pntArray(3).Y = 0
bCur.PutCoords(4, pntArray(0))
line.FromPoint.PutCoords(20, 0)
line.ToPoint.PutCoords(30, 0)

'pPolyline has now been defined. When changing segment coordinates directly
'like this, 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, and so on.
'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.

End Sub

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:

Sub CreatePolylineFromExistingGeometries(ByRef pPolygon1 As ESRI.ArcGIS.Geometry.IPolygon, ByRef pPolygon2 As ESRI.ArcGIS.Geometry.IPolygon)

'Build a new polyline by intersecting two existing polygons.
Dim pTopoOp2 As ESRI.ArcGIS.Geometry.ITopologicalOperator2
pTopoOp2 = pPolygon1
pTopoOp2.IsKnownSimple_2 = False

Dim pPoly As ESRI.ArcGIS.Geometry.IPolyline
pPoly = pTopoOp2.Intersect(pPolygon2, ESRI.ArcGIS.Geometry.esriGeometryDimension.esriGeometry1Dimension)
'pPoly is the new generated polyline.

End Sub