3

I'm trying to add points along lines every 3 metres but I'd like the starting/ending point at certain non fixed distance. When I use the tool 'Points along geometry' inside Graphical Modeler. I don't want a fixed distance for 'start/end offset", the distance is variable. I want to use a field called "offset" containing the pre-calculated distance for every line.

example

When I select the column "offset" with the distances in it, QGIS doesn't recognise or ignores the values. Instead of adding points at the predefined value, It's adding the double of distance at the starting point and adding the endpoint at the end of the line which I don't want.

Am I missing something?

Is there any other way to do this?

With QGIS or PyQGIS?

Any ideas?


As far as I can see, there is no option to use this tool above to solve my problem. Someone suggested using Python within QGIS. I'm a beginner so I'm not quite confident about the code. I only have a few lines but I know that it needs to add a lot more.

I have a layer with lines and I want to add points along the lines with 3 metres equidistant. In addition, I have a column on the attribute table with the distances for start and end offset for each line which I would like to use.

What I have:

from qgis.core import (QgsFeature, QgsGeometry, QgsVectorLayer, QgsMapLayerRegistry, QgsField)

inputLayer = iface.activeLayer()

feat = layer[0].getFeatures().next()

for feat in inputLayer.getFeatures():
     dist = feat['offset'] # this is the field name with distances
     geom = feat.geometry()
     l = 0
     while l < geom.length():
        p = geom.interpolate(l)
        print(p)
        l += dist
  • I just want to double check what values you put in Start and End offset. They should be zero (0); if you put in three (3) for both, I'd guess you'd get your result. Please update the original question. – Richard Morgan Jun 10 at 14:45
3

It is not yet the ultimate solution in terms of using entirely a Virtual Layer. However, it is possible to overcome the issue of creating offset for the starting/ending points. And it works well within a Processing Modeller, see Phase 3.

Tested on QGIS 3.4

The full solution will probably include something as ST_Line_Interpolate_Point() and using some sort of a loop.


Phase 1. Starting/ending point at certain non fixed distance.

I can suggest using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer...

Let's assume there is a layer called "lines" (red), see image below.

input

With the following Query, it is possible to achieve an offset of 1.000 meter at the beginning and 2.000 at the end of the line object.

WITH start_offset AS (
SELECT make_line(start_point(geometry),
       ST_Translate(start_point(geometry),
                    sin(ST_Azimuth(ST_PointN(geometry,2), start_point(geometry)))*1000,
                    cos(ST_Azimuth(ST_PointN(geometry,2), start_point(geometry)))*1000,
                    0)
       ) AS start_offset_geom
FROM lines
),

end_offset AS (
SELECT make_line(end_point(geometry),
       ST_Translate(end_point(geometry),
                    sin(ST_Azimuth(ST_PointN(geometry,ST_NumPoints(geometry)-1), end_point(geometry)))*2000,
                    cos(ST_Azimuth(ST_PointN(geometry,ST_NumPoints(geometry)-1), end_point(geometry)))*2000,
                    0)
       ) AS end_offset_geom 
FROM lines
)

SELECT ST_LineMerge(ST_Union(ST_Union(geometry, start_offset_geom), end_offset_geom)) AS new_line_geom,
       ST_Length(ST_LineMerge(ST_Union(ST_Union(geometry, start_offset_geom), end_offset_geom))) AS length
FROM lines, start_offset, end_offset

The output Virtual Layer will generate a line (blue) consisting of the initial line geometry and two additional parts created by means of the offset parameter and line directions.

output


Phase 2. Adding points along the line every 3 metres using 'Points along geometry' geoalgorithm.

On this stage, the simple adjustment of the desired distance is required and considering 'virtual_layer' as the input layer.

window_1

window_2

The output. Because of the scale points over the line look too dense.

result_1

Nevertheless, when zoom in is applied, created points will look as follows.

result_2


Phase 3. Implementing the workflow in QGIS 3 Processing Modeller with usage of the algorithm from Vector General > Execute SQL in which the query must be copied&pasted.

processing_modeller

  • 1
    Sorry for the delay and thanks for your answer @Taras. I haven't had a chance to try your solution but I'll definitely do it. – Senda Sep 10 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.