The following code demonstrates how to create valid polylines efficiently.
- createMultipartPolylinePathSegmentCollection: Create a multipart polyline using path via ISegmentCollection.
- createMultipartPolylinePathPointCollection: Create a multipart polyline using path via IPointCollection.
How to use
- Use this code in VBA.
'************************************************************************************************
'* GEOMETRY TYPE : POLYLINE
'* NOTE :In the following samples the geometries are simple without having to use ITopologicalOpeartor::Simplify.
'* However if the data creation process cannot insure simple geometries
'* the geometries have to be simplified before storing or using those in geometry operations.
'************************************************************************************************
'*************************************************************************
'* NAME : createMultipartPolylinePathSegmentCollection
'* DESCRIPTION : Create a multipart polyline using path via ISegmentCollection.
'* This sub is demonstrating it by creating a branching polyline.
'* NOTE : This is the approach to use if non-linear segments (Circular Arc, Elliptical Arc and Bezier Curve) have to be created.
'*************************************************************************
Sub createMultipartPolylinePathSegmentCollection()
Dim pPointsPath0(1) As IPoint, pPointsPath1(1) As IPoint, pPointsPath2(1) As IPoint
Dim pPointsPath3(1) As IPoint, pPointsPath4(1) As IPoint, pPointsPath5(1) As IPoint, pPointsPath6(1) As IPoint
Dim pPathColl(6) As ISegmentCollection, pLine0 As ILine, pLine1 As ILine, pLine2 As ILine, pLine3 As ILine
Dim pLine4 As ILine, pLine5 As ILine, pLine6 As ILine, i As Long, pspref As ISpatialReference, pGeoSpRef As IGeometry
Dim pGeometry(6) As IGeometry, pPolColl As IGeometryCollection
'Create a new polyline the IGeometryCollection interface
Set pPolColl = New Polyline
'*********************************************************
'THE SPATIAL REFERENCE SHOULD BE SET HERE ON THE POLYLINE
'Here the spatial reference is created in memory but could also come from various sources:
'IMap, IGeodataset, IGeometry etc...
Set pspref = New UnknownCoordinateSystem
pspref.SetFalseOriginAndUnits -10000, -10000, 100000 'Set the false origin and units.
'The XYUnits value is equivalent to the precision specified when creating a feature class
Set pGeoSpRef = pPolColl
Set pGeoSpRef.SpatialReference = pspref
'*********************************************************
'Initialize points
For i = 0 To 1
Set pPointsPath0(i) = New EsriGeometry.Point
Set pPointsPath1(i) = New EsriGeometry.Point
Set pPointsPath2(i) = New EsriGeometry.Point
Set pPointsPath3(i) = New EsriGeometry.Point
Set pPointsPath4(i) = New EsriGeometry.Point
Set pPointsPath5(i) = New EsriGeometry.Point
Set pPointsPath6(i) = New EsriGeometry.Point
Next
'Initialize path
For i = 0 To 6
Set pPathColl(i) = New Path
Set pGeometry(i) = pPathColl(i) 'QI to IGeometry to be able to use AddGeometries later on
Next
'Initialize lines, putcoords of points and add each line to a separate path
Set pLine0 = New EsriGeometry.Line
pPointsPath0(0).PutCoords 100, 100
pPointsPath0(1).PutCoords 100, 150
pLine0.PutCoords pPointsPath0(0), pPointsPath0(1)
pPathColl(0).AddSegment pLine0
Set pLine1 = New EsriGeometry.Line
pPointsPath1(0).PutCoords 100, 150
pPointsPath1(1).PutCoords 100, 250
pLine1.PutCoords pPointsPath1(0), pPointsPath1(1)
pPathColl(1).AddSegment pLine1
Set pLine2 = New EsriGeometry.Line
pPointsPath2(0).PutCoords 100, 250
pPointsPath2(1).PutCoords 75, 300
pLine2.PutCoords pPointsPath2(0), pPointsPath2(1)
pPathColl(2).AddSegment pLine2
Set pLine3 = New EsriGeometry.Line
pPointsPath3(0).PutCoords 100, 250
pPointsPath3(1).PutCoords 150, 300
pLine3.PutCoords pPointsPath3(0), pPointsPath3(1)
pPathColl(3).AddSegment pLine3
Set pLine4 = New EsriGeometry.Line
pPointsPath4(0).PutCoords 100, 150
pPointsPath4(1).PutCoords 100, 200
pLine4.PutCoords pPointsPath4(0), pPointsPath4(1)
pPathColl(4).AddSegment pLine4
Set pLine5 = New EsriGeometry.Line
pPointsPath5(0).PutCoords 100, 200
pPointsPath5(1).PutCoords 150, 250
pLine5.PutCoords pPointsPath5(0), pPointsPath5(1)
pPathColl(5).AddSegment pLine5
Set pLine6 = New EsriGeometry.Line
pPointsPath6(0).PutCoords 100, 200
pPointsPath6(1).PutCoords 150, 175
pLine6.PutCoords pPointsPath6(0), pPointsPath6(1)
pPathColl(6).AddSegment pLine6
'Add all the paths to the polyline using AddGeometries method
pPolColl.AddGeometries 7, pGeometry(0)
'You can draw, store or use the polygon (pPolColl) in other geometry operations at this point
End Sub
'*************************************************************************
'* NAME : createMultipartPolylinePathPointCollection
'* DESCRIPTION : Create a multipart polyline using path via IPointCollection.
'* This sub is demonstrating it by creating a branching polyline.
'*************************************************************************
Sub createMultipartPolylinePathPointCollection()
Dim pPointsPath0(1) As IPoint, pPointsPath1(1) As IPoint, pPointsPath2(1) As IPoint
Dim pPointsPath3(1) As IPoint, pPointsPath4(1) As IPoint, pPointsPath5(1) As IPoint, pPointsPath6(1) As IPoint
Dim pPathColl(6) As IPointCollection, i As Long, pspref As ISpatialReference, pGeoSpRef As IGeometry
Dim pGeometry(6) As IGeometry, pPolColl As IGeometryCollection
'Create a new polyline the IGeometryCollection interface
Set pPolColl = New Polyline
'*********************************************************
'THE SPATIAL REFERENCE SHOULD BE SET HERE ON THE POLYLINE
'Here the spatial reference is created in memory but could also come from various sources:
'IMap, IGeodataset, IGeometry etc...
Set pspref = New UnknownCoordinateSystem
pspref.SetFalseOriginAndUnits -10000, -10000, 100000 'Set the false origin and units.
'The XYUnits value is equivalent to the precision specified when creating a feature class
Set pGeoSpRef = pPolColl
Set pGeoSpRef.SpatialReference = pspref
'*********************************************************
'Initialize points
For i = 0 To 1
Set pPointsPath0(i) = New EsriGeometry.Point
Set pPointsPath1(i) = New EsriGeometry.Point
Set pPointsPath2(i) = New EsriGeometry.Point
Set pPointsPath3(i) = New EsriGeometry.Point
Set pPointsPath4(i) = New EsriGeometry.Point
Set pPointsPath5(i) = New EsriGeometry.Point
Set pPointsPath6(i) = New EsriGeometry.Point
Next
'Initialize path
For i = 0 To 6
Set pPathColl(i) = New Path
Set pGeometry(i) = pPathColl(i) 'QI to IGeometry to be able to use AddGeometries later on
Next
'Putcoords on the points
pPointsPath0(0).PutCoords 100, 100
pPointsPath0(1).PutCoords 100, 150
pPointsPath1(0).PutCoords 100, 150
pPointsPath1(1).PutCoords 100, 250
pPointsPath2(0).PutCoords 100, 250
pPointsPath2(1).PutCoords 75, 300
pPointsPath3(0).PutCoords 100, 250
pPointsPath3(1).PutCoords 150, 300
pPointsPath4(0).PutCoords 100, 150
pPointsPath4(1).PutCoords 100, 200
pPointsPath5(0).PutCoords 100, 200
pPointsPath5(1).PutCoords 150, 250
pPointsPath6(0).PutCoords 100, 200
pPointsPath6(1).PutCoords 150, 175
'Add points to separate paths
pPathColl(0).AddPoints 2, pPointsPath0(0)
pPathColl(1).AddPoints 2, pPointsPath1(0)
pPathColl(2).AddPoints 2, pPointsPath2(0)
pPathColl(3).AddPoints 2, pPointsPath3(0)
pPathColl(4).AddPoints 2, pPointsPath4(0)
pPathColl(5).AddPoints 2, pPointsPath5(0)
pPathColl(6).AddPoints 2, pPointsPath6(0)
'Add all the paths to the polyline using AddGeometries method
pPolColl.AddGeometries 7, pGeometry(0)
'You can draw, store or use the polygon (pPolColl) in other geometry operations at this point
End Sub