Skip to content

Instantly share code, notes, and snippets.

@kgjenkins
Last active February 21, 2023 18:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kgjenkins/308e214f4c29d257a8dada7463aa7f8c to your computer and use it in GitHub Desktop.
Save kgjenkins/308e214f4c29d257a8dada7463aa7f8c to your computer and use it in GitHub Desktop.
Interpolate points along lines using QGIS virtual layers

Interpolate points along lines using QGIS virtual layers

Suppose we want to interpolate a number of points along each line in a layer:

image

We have a column called stations that specifies how many points to interpolate on each line:

image

We can use QGIS virtual layers to create a new layer containing the interpolated points:

select
  line_interpolate_equidistant_points(geometry, st_length(geometry)/ (stations+1) ) as geometry,
  * 
from origlines

Note that we first calculate the new geometry, then select all the remaining columns with *.

To use this query:

  • Layer menu > Add layer > Add/edit virtual layer
  • Specify the "Layer name" for the new layer
  • Paste the code above into the "Query" box
  • Click "Add"

image

Note that the new layer is virtual, and is dynamically generated from the "origlines" layer. To save it permanently, right-click and "Save as..."

Also, all the points along a single line are multipoints, and part of a single multipoint feature. If you want separate records for each point, you can use the "Multipart to singleparts" tool, found in the Processing Toolbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment