6

So I have a line that is basically drawn from two points. A starting point and an end point. It looks like this:

enter image description here

I then densify the line by count and set the value to e.g. 50

enter image description here

Then I explode the line so that I get one feature (row) per vertex (I am not sure if that step is a good idea)

enter image description here

And then I do not know how to continue. My first Idea was to use the profiles from lines tool from SAGA. It does work and creates a point for each cell (actually the densifying might be unnecessary too) which holds the raster value. I also added the field rn for the row number. enter image description here

However, when I then run the points to path tool, the height information gets lost.

Another idea was to simply use the sample raster tool for the exploded line. However, I cannot use the line as vector layer input for sampling the raster values.

Anybody has an idea on how I can output a line that for each vertex has the height information from the raster?

1
  • Would you like to add z value to the line's vertices or to get z values as a column? In your example, points to path tool makes a line from points, so you get one line (one row) and loose all table values for points. This is an expected result. Mar 5 at 7:22

2 Answers 2

10

You can use "Drape (set Z value from raster)" tool to set the z value of every vertex in the densified line to a value sampled from a raster.

enter image description here

enter image description here

1
  • Ah cool, did not know about this!:) Thanks a lot. But what I actually wanted was a column in the line-layer with all the z-values. Is that possible too?
    – Lenn
    Mar 5 at 11:43
9

Using QGIS expressions, use this expression:

raster_value( 'DEM', 1, start_point($geometry))

This is to get the elevation from the raster called DEM, raster band 1 for the line's start point. To get the elevation of the line's end point, simply change start_point to end_point.

enter image description here


Variant:

If you want to calculate the elevation along a line with just two vertices at a regular interval, you can use the following expression that generates an array of elevation values - for each point on the line in a regular interval - an interval that can be defined on line 3 (here: 100):

array_foreach(
    generate_series(
        0, length($geometry), 100  -- change interval here
    ),
    raster_value (
        'DEM',
        1,
        line_interpolate_point( 
            $geometry,
            @element
        )
    )
)

enter image description here

1
  • that is it! Thank you so much:):)
    – Lenn
    Mar 5 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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