QGIS – Creating Points Along Geometry with Start and End Offsets

qgis

This question is almost identical to my previous question. However, instead of placing a point at each vertex, I would like to place a point at a specified distance from each vertex (start offset, end offset)

For example, I would like 36 points along a group of lines, each with slightly different lengths. The start point and end point is 1.5 feet inward from each line vertex.

I tried using the Points Along Geometry tool. With the Distance field set to and also $length/37 and the Start Offset and End Offset set to 1.5. All also tried with the Distance set to ($length/37)-0.0001.

Points Along Geometry Output

I tried Geometry by Expression with the expression..

collect_geometries( array_foreach( generate_series(1.5, $length-1.5, $length/37-0.0001), line_interpolate_point( $geometry, @element)))

..but was not able to achieve the desired results.

Best Answer

There are two problems in your approach:

  1. $length calculates ellipsoidal distances. So you might want to use length($geometry) to get planimetric distances. Depending on what CRS you use and what distances you have, that might play a role or not.

  2. Generate a series from 0 to 35 (36 points) and multiply this with the interval. Be aware: there are 35 intervals for 36 points. So the length of the interval is: line's length minus 3 (for 1.5 offset at start and end), divided by 35. Then add to this the start offset of 1.5 and multiply the @element for each point with the interval.

This is the expression to use:

collect_geometries( 
    array_foreach( 
        generate_series(0,35), 
        line_interpolate_point ($geometry,1.5+@element*(length($geometry)-3)/35)
    )
)

Demonstration for 26 points and offset of 15 from start-/endpoints: enter image description here


Variant:

To easily change the no. of points and the offset, use variables.Change the values of 36 and 1.5 on lines 1 and 2 and the expression will automatically adapt:

with_variable('no_of_points',36,  -- change here
with_variable('offset',1.5,  -- change here
collect_geometries( 
    array_foreach( 
        generate_series (0,@no_of_points-1), 
        line_interpolate_point (
            $geometry,
            @offset + @element * (length ($geometry) - 2 * @offset) / (@no_of_points-1))
    )
)))
Related Question