How to make programmatically split polyline at many points (not at one point)

3964
5
05-12-2012 11:23 PM
MarkMindlin
Occasional Contributor III
Hi there,

I am searching a method that can do a split polyline at many points. (originally make split at polygon(s) intersect polyline(s)).
I though that IFeatureEdit2.Split() or IFeatureEdit2.SplitWithUpdate() can manage with it. Instead these two methods have argument like IGeometry  - after I take as a parameter IGeometry (Multipoint) - I get an error "Splitting a polyline requires a point splitter." (meaning must be IPoint).

Any suggestion, please. I look forward to hearing about ArcObjects, not workaround.
0 Kudos
5 Replies
MarkMindlin
Occasional Contributor III
May be an interface IPolycurve2 could be usable. It has a SplitAtPoints method.
0 Kudos
KenBuja
MVP Esteemed Contributor
Here's how I split a polyline using points.

Private Sub SplitFeature(ByRef pfeature As ESRI.ArcGIS.Geodatabase.IFeature, ByRef pSplitPoints As ESRI.ArcGIS.Geometry.IPointCollection)

    Dim pEnumVertex As ESRI.ArcGIS.Geometry.IEnumVertex
    Dim pPolyCurve As ESRI.ArcGIS.Geometry.IPolycurve2
    Dim pEnumSplitPoint As ESRI.ArcGIS.Geometry.IEnumSplitPoint

    pEnumVertex = pSplitPoints.EnumVertices
    pPolyCurve = pfeature.Shape
    pEnumSplitPoint = pPolyCurve.SplitAtPoints(pEnumVertex, True, True, -1)

    'You can check to see if the line was actually split if you need to do more things with the resultant polylines.
    'If Not pEnumSplitPoint.SplitHappened Then Exit Sub

End Sub
0 Kudos
MarkMindlin
Occasional Contributor III
Thank you Ken,

Could you provide a suggestion how can I get new parts of the feature after split? Meaning pointers to the new polylines.
0 Kudos
KenBuja
MVP Esteemed Contributor
Sure. Here's one way to do it in an edit session as an Add-in. And don't forget to mark the question as answered.

  Private pEditLayers As ESRI.ArcGIS.Editor.IEditLayers = My.ArcMap.Editor
  Private pEditor As ESRI.ArcGIS.Editor.IEditor  = My.ArcMap.Editor

  Private Sub SplitFeature(ByRef pfeature As ESRI.ArcGIS.Geodatabase.IFeature, ByRef pSplitPoints As ESRI.ArcGIS.Geometry.IPointCollection)

    Dim pEnumVertex As ESRI.ArcGIS.Geometry.IEnumVertex
    Dim pGeoColl As ESRI.ArcGIS.Geometry.IGeometryCollection
    Dim pPolyCurve As ESRI.ArcGIS.Geometry.IPolycurve2
    Dim pEnumSplitPoint As ESRI.ArcGIS.Geometry.IEnumSplitPoint
    Dim pNewFeature As ESRI.ArcGIS.Geodatabase.IFeature

    Try
      pfeature.Shape.Project(pEditor.Map.SpatialReference)

      pEnumVertex = pSplitPoints.EnumVertices
      pPolyCurve = pfeature.Shape
      pEnumSplitPoint = pPolyCurve.SplitAtPoints(pEnumVertex, True, True, -1)

      If Not pEnumSplitPoint.SplitHappened Then Exit Sub

      pGeoColl = pPolyCurve
      pfeature.Shape = BuildPolyline(pGeoColl.Geometry(0))
      pfeature.Store()

      For i As Integer = 1 To pGeoColl.GeometryCount - 1
        pNewFeature = pEditLayers.CurrentLayer.FeatureClass.CreateFeature
        pNewFeature.Shape = BuildPolyline(pGeoColl.Geometry(i))
        CopyAttributes(pfeature, pNewFeature)
        pNewFeature.Store()
      Next

    Catch ex As Exception
      System.Windows.Forms.MessageBox.Show(ex.ToString, "SplitLine: SplitFeatures")
    End Try

  End Sub

  Private Function BuildPolyline(ByRef pSegColl As ESRI.ArcGIS.Geometry.ISegmentCollection) As ESRI.ArcGIS.Geometry.IPolyline

    Dim pPolyline As ESRI.ArcGIS.Geometry.IGeometryCollection = New ESRI.ArcGIS.Geometry.Polyline
    Dim pGeometry As ESRI.ArcGIS.Geometry.IGeometry

    Try
      pGeometry = pPolyline
      pGeometry.SpatialReference = pEditor.Map.SpatialReference
      pPolyline.AddGeometries(1, pSegColl)
      Return pPolyline

    Catch ex As Exception
      System.Windows.Forms.MessageBox.Show(ex.ToString, "SplitLine: BuildPolyline")
      Return Nothing
    End Try

  End Function

  Private Sub CopyAttributes(ByRef pInFeature As ESRI.ArcGIS.Geodatabase.IFeature, ByRef pOutFeature As ESRI.ArcGIS.Geodatabase.IFeature)

    Dim pField As ESRI.ArcGIS.Geodatabase.IField
    Dim pFields As ESRI.ArcGIS.Geodatabase.IFields

    Try
      pFields = pInFeature.Fields
      For i As Integer = 0 To pFields.FieldCount - 1
        pField = pFields.Field(i)
        If pField.Editable Then
          If Not pField.Type = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeOID And Not pField.Type = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry Then
            pOutFeature.Value(i) = pInFeature.Value(i)
          End If
        End If
      Next

    Catch ex As Exception
      System.Windows.Forms.MessageBox.Show(ex.ToString, "SplitLine: CopyAttributes")
    End Try

  End Sub


MarkMindlin
Occasional Contributor III
Thank you a lot, Ken!
0 Kudos