[GIS] Aligning multiple points to line in QGIS

bufferpoints-to-linepyqgisqgissnapping

I would like to align multiple points to a line, or lines within a layer, using a specified tolerance or buffer around the line objects. Please refer to the sample sketch attached.

For the sake of this example, the points closest to the line in the BEFORE picture are within 5 map units of the line, while the outermost points are over 10 map units away. I would like to snap the closest points onto the nearest line, using a tolerance of 5 map units to achieve the result in the AFTER picture.

enter image description here

Best Answer

There's a built-in tool to do this in the (unreleased) QGIS 3.0 version. You can get a nightly snapshot from the QGIS website to test this in advance.

To do this:

  1. Run the "Snap geometries to layer" processing algorithm
  2. Select your points layer as the "input layer"
  3. Select the line layer as the "reference layer"
  4. Enter a suitable tolerance (maximum distance to move points while snapping)
  5. Change the behavior to "Prefer closest point"

enter image description here

Here's the result, showing the original points as "x", and the snapped points as green dots. I've used a tolerance here so that only some of the input points are snapped.

enter image description here

Related Question