Lady_Jane

Iterating through ITopologicalOperator.Union Method - VBA

Discussion created by Lady_Jane on Aug 4, 2011
Latest reply on Aug 5, 2011 by Lady_Jane
Hi everyone,
I haven't been able to find very useful documentation or examples of the ITopologicalOperator.Union method.
I have a line feature (pRiver) and its to-point (pToPoint).  I am looping through all of the line features that intersect with pToPoint in order to find the one that is downstream (pToPoint has the same coordinates as the downstream segment's from-point).  Once I find the downstream segment (pNextRiver) I want to use ITopologicalOperator.Union to add it to pRiver, creating a cumulative line feature (pRiverAdd).
Then the loops starts over, pToPoint becomes the to-point of the last segment added, I loop through all the line features that intersect with it until I find the next downstream one.  Then I add that segment to pRiverAdd, using ITopologicalOperator.Union.
The following line:
Set pRiverAdd = pTopoOpUnion.Union(pNextRiver)

is giving me an automation error the first time through the loop.

Here is a larger code snippet:
Do Until DamDone = True
            LoopCount1 = LoopCount1 + 1
            Dim pNextRiverF As IFeature
            'If this is the first time through the loop...
            If LoopCount1 = 1 Then
                '...the to-point is that of the dam's segment
                Set pToPoint = pRiver.ToPoint
                '...and we are joining downstream rivers to the dam's segment
                Set pTopoOpUnion = pRiver
            'If it's not...
            ElseIf LoopCount1 > 1 Then
                '...we are joining downstream rivers to the cumulative rivers
                Set pTopoOpUnion = pRiverAdd
            End If
            
            'Find segments that intersect with the to-point
            Set pTopoOp = pToPoint
            Set BufferPoly = pTopoOp.Buffer(1)
        
            With pSF
                Set .Geometry = BufferPoly
                .GeometryField = "Shape"
                .SpatialRel = esriSpatialRelIntersects
            End With
            'Create a feature cursor to hold potential downstream segments
            Dim pNextCursor As IFeatureCursor
            Set pNextCursor = pRiversFC.Search(pSF, False)

            'Find the one that is actually downstream
            '           ## Loop through all potential downstream rivers ##
            DownstreamSegment = False
            Debug.Print "Looping through potential downstream segments..."
            tsOut.WriteLine "Looping through potential downstream segments..."
            Do Until DownstreamSegment = True
                Set pNextRiverF = pNextCursor.NextFeature
                Set pNextRiver = pNextRiverF.Shape
                Set pNextFromPoint = pNextRiver.FromPoint
                '   ##  Are there any remaining potential downstream segments?   ##
                '--> No
                Debug.Print "Are there any remaining potential downstream segments?"
                tsOut.WriteLine "Are there any remaining potential downstream segments?"
                If pNextRiverF Is Nothing Then
                    Debug.Print "No"
                    tsOut.WriteLine "No"
                    Debug.Print "Is the length greater than or equal to the user distance?"
                    tsOut.WriteLine "Is the length greater than or equal to the user distance?"
                    If Length >= UserDist Then
                        Debug.Print "Yes"
                        tsOut.WriteLine "Yes"
                        DamDone = True
                        SubcurveType = "Normal"
                    Else:
                        Debug.Print "No"
                        tsOut.WriteLine "No"
                        DamDone = True
                        Flag = True
                        FlagLength = Length
                        SubcurveType = "Flag"
                    End If
                '--> Yes
                Else:
                    Debug.Print "Yes"
                    tsOut.WriteLine "Yes"
                        '   ##  Does the to-point have the same x coordinate as the from-point?    ##
                    '--> Yes
                    Debug.Print "Does the to-point have the same x coordinate as the from-point?"
                    tsOut.WriteLine "Does the to-point have the same x coordinate as the from-point?"
                    If Round(pToPoint.X, 5) = Round(pNextFromPoint.X, 5) Then
                        '   ##  Does the to-point have the same y coordinate as the from point? ##
                        '--> Yes
                        Debug.Print "Yes"
                        tsOut.WriteLine "Yes"
                        Debug.Print "Does the to-point have the same y-coordinate as the from point?"
                        tsOut.WriteLine "Does the to-point have the same Y-coordinates as the from-point?"
                        If Round(pToPoint.Y, 5) = Round(pNextFromPoint.Y, 5) Then
                            Debug.Print "Yes"
                            tsOut.WriteLine "Yes"
                            'This is the next downstream segment
                            Length = Length + pNextRiver.Length
                            'Add this segment to pRiverAdd
                            Set pRiverAdd = pTopoOpUnion.Union(pNextRiver)  'Automation Error
                            'Update the to point
                            Set pToPoint = pNextRiver.ToPoint
                            'Set the flag to true
                            DownstreamSegment = True
                            Debug.Print "Next downstream segment has been found, OID: " & pNextRiverF.OID
                            tsOut.WriteLine "Next downstream segment has been found, OID: " & pNextRiverF.OID
                            'Check to see if the target length has been reached
                            Debug.Print "Has the target length been reached?"
                            tsOut.WriteLine "Has the target length been reached?"
                            If Length >= UserDist Then
                                Debug.Print "Yes...we are finished with this dam"
                                tsOut.WriteLine "Yes... we are finished with this dam"
                                'If it has, we are finished with this particular dam
                                DamDone = True
                                SubcurveType = "Normal"
                            Else:
                                Debug.Print "No...keep looking downstream"
                                tsOut.WriteLine "No... keep looking downstream"
                            End If
                        '--> No
                        Else:
                        Debug.Print "No"
                        tsOut.WriteLine "No"
                        '   Do nothing, this is not the next downstream segment
                        End If
                    '--> No
                    Else:
                        Debug.Print "No...this is not the next downstream segment"
                        tsOut.WriteLine "No... this is not the next downstream segment"
                        'Do nothing, this is not the next downstream segment
                    End If
                ' If there are no more potential downstream segments...
                End If
            Loop
        Loop

Does anyone know what might be causing the error?
Thank-you!

ps. anyone else have trouble typing "topological"? :p

Outcomes