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
nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked May 3, 2011 at 17:45
2
  • 1
    +1 Good question. In the past I've seen problems with IIndexQuery, but have never looked to see if those have been fixed. Instead, I've used ISpatialCacheManager3. Commented May 3, 2011 at 19:02
  • The IProximityOperator.ReturnDistance method is also a way to calculate distance to nearest features. edndoc.esri.com/arcobjects/9.1/ComponentHelp/esriGeometry/… Commented Nov 8, 2013 at 20:21

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

answered May 4, 2011 at 14:25
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?"

answered May 3, 2011 at 20:45
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.