AnsweredAssumed Answered

Snap Points to Lines Based on Distance and Attributes Code help

Question asked by clintoncooper on Feb 4, 2013
Latest reply on Feb 5, 2013 by Wayne_Whitley
I found this piece of code through my searches and need a few pointers on how to get this to work:

Private Sub MovePoint()

Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

'''''''''''''''''''''''''''''''''''''''''''''''
Dim pFLayerA As IFeatureLayer
Set pFLayerA = pMxDoc.FocusMap.Layer(0) 'my point layer

Dim pFClassA As IFeatureClass
Set pFClassA = pFLayerA.FeatureClass

Dim FCursorA As IFeatureCursor
Dim FeatureA As IFeature
'''''''''''''''''''''''''''''''''''''''''''''''''

Dim FCursorB As IFeatureCursor
Dim FeatureB As IFeature

Dim ThePoint As IPoint
Dim TheCurve As ICurve

Dim pFLayerB As IFeatureLayer
Set pFLayerB = pMxDoc.FocusMap.Layer(1) 'my line layer

Dim pfClassB As IFeatureClass
Set pfClassB = pFLayerB.FeatureClass

Dim SpFLayerB As IFeatureSelection
Set SpFLayerB = pMxDoc.FocusMap.Layer(1)

'''''''''''''''''''''''''''''''''''''''''''''''''''

Dim K As String 'K is the Line ID
Dim Y As Integer 'Y is the position of the LN_ID field i.e the line "NOMBRE" field

'Define a query to be use in the cursor to select only the line associated with the point
Dim pfilter As IQueryFilter
Dim strQuery As String


Dim pNearPoint As IPoint
Dim DistOnCurve As Double
Dim NearDist As Double
Dim bRight As Boolean

''''''''''''''''''''''''''''''''''''''''''''''''''''
'Find position of the LN_ID field in the point layer
Y = pFClassA.FindField("NOMBRE")

'Open point cursor
Set FCursorA = pFClassA.Update(Nothing, True)
Set FeatureA = FCursorA.NextFeature

'Loop through all records in point layer
Do Until FeatureA Is Nothing

Set ThePoint = FeatureA.Shape

'Set up the filter for the line cursor
K = FeatureA.Value(Y) 'Find the line id in the point layer
strQuery = "NOMBRE = '" & K & "'"
Set pfilter = New QueryFilter
pfilter.WhereClause = strQuery

'Open line cursor as select the line based of pfilter value
Set FCursorB = pfClassB.Search(pfilter, False)
Set FeatureB = FCursorB.NextFeature
Set TheCurve = FeatureB.Shape
Set pNearPoint = New Point

'Find nearest point on the line to the point
TheCurve.QueryPointAndDistance esriNoExtension, ThePoint, False, _
pNearPoint, DistOnCurve, NearDist, bRight

'Update the old point to the new point
Set FeatureA.Shape = pNearPoint
FCursorA.UpdateFeature FeatureA

'Move to the next record in the point feature
Set FeatureA = FCursorA.NextFeature

Loop

pMxDoc.ActiveView.Refresh

MsgBox "Done"

End Sub



I am a newbie when it comes to running scripts.  I do understand that I need to have the point feature class at the top of my toc and the line feature class second right under it.  But I am not sure what I need to change within the script above to make it work in my mxd.  For example what lines of code do I change to find the fields that need to match?  Thanks in advance!

I am running ARCGIS 10.1 SP1

Outcomes