7

I have:

(a) point feature class (b) polyline feature class

I am trying to loop through the (a) point feature class to find the nearest feature from the (b) polyline feature class.

Just as a start to more complex command, I'd like to find and select the nearest (b) polyline, and create a message box that tells me its ObjectID, just so I know that the method for finding the nearest line is working.

I've come across two options, both of which I'm having issues with.

(1) The IIndexQuery.NearestFeature method, which I could use in combination with the IFeature.GetFeature method to select the polyline feature of interest.

I've successfully entered the parameters (I think), but the NearestFeature method returns a "-1" for each of the features FIDs. Maybe this is because my features have an 'ObjectID' field, but no 'FID' field, though, I tried the same code with a polyline feature that did have an FID field, and it still gave me -1's across the board. Could anybody speak to this?

(2) The IHitTest.HitTest method, which returns a segment index of the nearest feature.

The problem here is that I'm looking for a value in a field for the whole line, not just a segment of the line. Is there a way to return the whole feature from the segment index?

Any other suggestions on finding the nearest line from a given point?

I couldn't figure out how to get the coloring right on the code below...

Private Sub NHD_Prep_Click()

'Set general variables  

Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pMap As IMap
Set pMap = pMxDoc.FocusMap

'Set variables for the line layer of interest

Dim pNhdLayer As ILayer
Set pNhdLayer = pMap.Layer(0)
Dim pNhdFLayer As IFeatureLayer
Set pNhdFLayer = pNhdLayer
Dim pNhdFClass As IFeatureClass
Set pNhdFClass = pNhdFLayer.FeatureClass
Dim pNhdFeatureIndex As IFeatureIndex
Set pNhdFeatureIndex = New FeatureIndex
Set pNhdFeatureIndex.FeatureClass = pNhdFClass
Dim pNhdIndexQuery As IIndexQuery2
Set pNhdIndexQuery = pNhdFeatureIndex

'Set variables for the point layer of interest

Dim pLayer As ILayer
Set pLayer = pMap.Layer(1)
Dim pFLayer As IFeatureLayer
Set pFLayer = pLayer
Dim pFClass As IFeatureClass
Set pFClass = pFLayer.FeatureClass

'Set number variables

Dim nearReachFID As Long
Dim distance As Double
Dim strReach As String
Dim dblmeas As Double

'Check to make sure the layer of interest is on top

If pMxDoc.FocusMap.Layer(0).Name <> "NHD Flowline" Then
    MsgBox "Please place NHD Flowline on the top spot of the TOC..."
    Exit Sub
End If

'Start Cursor on point layer features

Dim pFCursor As IFeatureCursor
Set pFCursor = pFClass.Search(Nothing, True)
Dim pFeature As IFeature
Set pFeature = pFCursor.NextFeature

Do Until pFeature Is Nothing

    'Use nearest point method to find the line FID and distance to it

    Dim pPointFeature As IPoint
    Set pPointFeature = pFeature.Shape

    pNhdIndexQuery.NearestFeature pPointFeature, nearReachFID, distance

    MsgBox "This is the " & pFeature.Value(pFeature.Fields.FindField("Name")) & " feature. " _
    & "The distance from this point to the nearest line, " & nearReachFID & ", is " & distance

    Set pFeature = pFCursor.NextFeature
Loop

End Sub
2

2 Answers 2

4

OK so my co-worker helped me figure this out...

Turns out I hadn't declared an envelope for my featureIndex, which in turn, left my indexQuery empty.

Underneath:

    Dim pNhdIndexQuery As IIndexQuery2

I added the following lines of code:

    Dim pEnvelope As IEnvelope
    Set pEnvelope = New Envelope
    pNhdFeatureIndex.Index Nothing, pEnvelope

This comes before:

    Set pNhdIndexQuery = pNhdFeatureIndex

You also need to ensure that the projections of your data frame, sourceindex, and inpoints all match, lest you catch an error.

Hope this helps,

DR

1

Here are some alternative non arcobject options for finding nearest feature(s). This was a previous question posted on this site, "For every point, distance to nearest polygon of type X?"

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.