# Finding and snapping nearest point to polyline/point

I'm new to Python programming in ArcGIS, and I'm currently trying to write a script which I think is beyond me right now.

Script should take each point from first layer and:

1. Snap this point to nearest point from second layer (with given
distance)

2. If in given radius there is no point, only line, it should snap to this line

3. If there are two points in radius it should snap to the closest one

4. If distance between points from first and second layer is 0, no snapping is needed

It's hard for me to come up with the right solution. I thought of doing for loop which will iterate through points in first layer and for every point it should check the distance to points/lines in second layer and then snap to it. But the problem may be a huge amount of points in both layers, and maybe there is a faster and easier way of doing it.

Situation where point should snap to nearest point

Situation where point should snap to nearest line if there is no point in distance

EDIT:

I know that I could do it with tools from ArcToolbox but the problem is that it has to be done with a script which can work with hundreds of points in both layers.

So far in my code there is nearly nothing. Just some thoughts of doing it using cursor and for loop.

``````import arcpy

arcpy.env.workspace =
layer1 = path to layer1
layer2 = path to layer1

cursor = arcpy.SearchCursor(layer1)
cursor2 = arcpy.SearchCursor(layer2)

tol = 0,3

for row in cursor:
##getting 1 point from first layer

for row in cursor2:
##  getting all points from layer 2

## measure distance between these two points
dd =
if dd <= tol

## the same again but with other point from layer1
``````
• What have you tried? Please edit your question to include a snippet of your arcpy code that you've tried, and a description of what it is doing (that doesn't suit your requirements)
– Midavalo
Commented Sep 14, 2016 at 18:47
• Have you tried using the Snap tool from ArcToolbox > Editing Tools? I think it will do exactly as you describe without the need for a script
– Midavalo
Commented Sep 14, 2016 at 18:48
• What happens when you run the Snap tool? This should run on hundreds of points without any issue
– Midavalo
Commented Sep 16, 2016 at 0:29
• OK, I've run the Snapp tool in ArcMAP and apparently it worked. And to use it as Arcpy script I need to Copy it As Python Snippet from Results window, right? Commented Sep 16, 2016 at 8:53
• yes that's correct
– Midavalo
Commented Sep 16, 2016 at 8:59

I think you can do all this with the Snap tool in ArcToolbox > Editing Tools.

I have two point layers and one line layer. I want to snap the red points to either the blue points if they're within 10 metres, or the lines if they're within 10 metres but not near points.

Using the snap tool I can specify which layer I want to snap, and all the layers I want to snap to. These are looked at in order so that it will attempt to snap to the points first (since they're listed first) and if it can't find any to snap to it will try the next option.

The result of this snap:

Using the Snap tool will meet all your requirements:

1. Snap this point to nearest point from second layer (with given distance)
2. If in given radius there is no point, only line, it will snap to this line
3. If there are two points in radius it will snap to the closest one
4. If distance between points from first and second layer is 0, no snapping is needed