How to: Snap points to lines in ArcGIS with Python
I’ve recently starting scripting ArcGIS using Python as part of one of my academic projects (about which I’ll post more later) and needed to be able to snap points to polylines – that is, move points so that they lie on the nearest polyline. An example would help to explain this:
In the image above, the black line is the relevant polyline, and the blue point is the original point. I wanted this point to lie on the line itself, which it did after processing (the red point).
A number of forum posts I read on the internet about this directed me to Hawth’s Tools for ArcGIS, but sadly these don’t work with ArcGIS 9.3, which is the version I am using. Instead, I wrote the Python code below which does the job using the ArcGIS Near (Analysis) command. The Python code is shown below, and is well commented so that it should be relatively easy to understand.
# ------ Snap Point to Line ------ # Author: Robin Wilson (firstname.lastname@example.org) # Description: Move the points in points so that they # lie on top of the nearest line # -------------------------------- def snap_point_to_line(points, lines): # Load the Analysis toolbox so that the Near tool is available gp.toolbox = "analysis" # Perform the Near operation looking for the nearest line # (from the lines Feature Class) to each point (from the # points Feature Class). The third argument is the search # radius - blank means to search as far as is needed. The # fourth argument instructs the command to output the # X and Y co-ordinates of the nearest point found to the # NEAR_X and NEAR_Y fields of the points Feature Class gp.near(points, lines, "", "LOCATION" # Create an update cursor for the points Feature Class # making sure that the NEAR_X and NEAR_Y fields are included # in the return data rows = gp.UpdateCursor(points, "", "", "NEAR_X, NEAR_Y") row = rows.Next() # For each row while row: # Get the location of the nearest point on one of the lines # (added to the file as fields by the Near operation above new_x = row.GetValue("NEAR_X") new_y = row.GetValue("NEAR_Y") # Create a new point object with the new x and y values point = gp.CreateObject("Point") point.x = new_x point.y = new_y # Assign it to the shape field row.shape = point # Update the row data and move to the next row rows.UpdateRow(row) row = rows.Next()
This script is included inÂ RTWTools for ArcGIS, where it is integrated into an ArcGIS Toolbox.
Categorised as: Academic, GIS, How To
Searching for this for some time now – i guess luck is more advanced than search engines 🙂
I served a lot of your audience, but still not solve a problmema I have.
Let me explain:
I have a Polyline Feature Class and want to move only the endpoint or the startpoint of each of the lines. You know how I can do this?.
Just great. It could be improve it, but I dont know how to 🙁 , if you make that a new point shapefile must be created (as result of the tool operation) instead of moving the original shapefile (just in case you need to have both of them, the original shapefile point and the snapped one). Greetings from Costa Rica and thank you very much!
Hi Sandra – Glad you found it useful. I was thinking of extending it to be able to do that too sometime – I’ll put it on my todo list! I originally implemented it so that it moved the original shapefile because that was the easiest way for me to do it for my project, but I think having the option would be good. i’ll drop you an email when I’ve done that (may be a while though). Cheers, Robin.
I came across this script and I am curious if you have explored writing one that would snap a point to a line based on attribute information? Specifically I am interested in moving collision location points that were geocoded and offset (based on distance and direction from an intersection) back onto my centerline file. The problem is that sometimes when I do the snapping tool the points snap to other centerlines that are not the correct road (sometimes a perpendicular overhead freeway). Would this be hard to do? I think it would be something like snap to nearest ‘Collisions.PrimaryRd’ = ‘Streets.St_Name’ but I don’t know much about Python.
San Francisco, Department of Public Health
I’ll have a look into this for you over the next few days and see what I can do.
Thanks a lot for this script! Works very well in ArcGIS 9.3.1.
Did you ever get chance to look into Sarah Bergquist’s question regarding a ‘snap by attribute’ tool?
Have you had a a chance to look at Sarah’s request to snap by attribute?
Unfortunately I haven’t had a chance, and I’m unlikely to be able to find time. Sorry about that – my PhD has to take priority at the moment.