Split Line To Two Parts using an Point

Help
2009-04-27
2013-05-30
  • i am trying to find a way to split a line by using an intersection
    point already known. So the function must return two line segments.
    The code doen't work. seperationOnIndex  is always -1.
    Any ideas?
    I am using sharpmap v0.9

    Private Function SplitGeometryLineToTwoParts( lineToSplit As
    GisSharpBlog.NetTopologySuite.Geometries.Geometry  , splitPoint As
    GisSharpBlog.NetTopologySuite.Geometries.Geometry )

    Dim splitPointCoordinate As ICoordinate = splitPoint.Coordinate

    Dim seperationOnIndex As Integer = -1
            For CoordIndex As Integer = 0 To
    lineToSplit.Coordinates.Length - 2
                Dim coord1 As ICoordinate = lineToSplit.Coordinates
    (CoordIndex)
                Dim coord2 As ICoordinate = lineToSplit.Coordinates
    (CoordIndex + 1)

                Dim rli As New
    GisSharpBlog.NetTopologySuite.Algorithm.RobustLineIntersector
    'NonRobustLineIntersector()
                rli.ComputeIntersection(splitPointCoordinate, coord1,
    coord2)
                Dim intNum As Integer = rli.IntersectionNum
                Dim check As Boolean = rli.IsIntersection
    (splitPointCoordinate)

                If rli.IsInteriorIntersection Then
                    seperationOnIndex = CoordIndex
                End If
                If intNum <> 0 Then
                    seperationOnIndex = CoordIndex
                End If
                If check Then
                    seperationOnIndex = CoordIndex
                End If
                If rli.HasIntersection Then
                    seperationOnIndex = CoordIndex
                End If
            Next

            If seperationOnIndex = -1 Then
                ' MsgBox("What's the error?")
                Exit Sub
            End If

    '-------------------------------------------------------------
            Dim myLeftCoords As New List(Of GeoAPI.Geometries.ICoordinate)
            Dim myRightCoords As New List(Of
    GeoAPI.Geometries.ICoordinate)
            'add the intersection point at  the beginning of myRightCoords
            myRightCoords.Add(splitPointCoordinate)
            For CoordIndex As Integer = 0 To
    lineToSplit.Coordinates.Length - 1
                Dim coord As GeoAPI.Geometries.ICoordinate =
    lineToSplit.Coordinates(CoordIndex)

                'myLeftCoords should include the coordinates from 0 up to
    and including seperationIndex
                If CoordIndex <= seperationOnIndex Then
                    myLeftCoords.Add(coord)
                End If

                'myRightCoords should include all the coordinates Greater
    than separationIndex
                If CoordIndex > seperationOnIndex Then
                    myRightCoords.Add(coord)
                End If
            Next
            '-------------------------------------------------------------

            'add the intersection point at
            'the end of myLeftCoords
            myLeftCoords.Add(splitPointCoordinate)
            Dim LeftSegment As New
    GisSharpBlog.NetTopologySuite.Geometries.LineString
    (myLeftCoords.ToArray)
            Dim RightSegment As New
    GisSharpBlog.NetTopologySuite.Geometries.LineString
    (myRightCoords.ToArray)

    '------------------------------------------------------------------------------

    Return Nothing
    End Function