Calculating max and min height of route segments with different lenghts based on DEM in QGIS

maxheightqgisrastersegmentworkflow

I do have a line layer containing route segments (input layer). The routes as well as the segments have different lengths. I also have a DEM for the height information.

Now I need to know the minimum and maximum height of each segment to calculate the slope of the segment.

What I've tried so far:

  1. converted my line layer to point layer using plugin "QChainage", distance set to 10m
  2. Used the "Drape" tool from processing toolbox to calculate height (z value)
  3. Added geometry info using "Add geometry attributes" (now I do have my zcoord)
  4. "Join attributes by nearest" to get the route and segment info on each point ("Join attribute by location" did not work, I don't know why)
  5. maximum("zcoord", 'segment_id') did not work to get the maximum height value for each segment

What I want to have as a result: I'd like to add an attribute "max" and "min" to my input layer.

Does someone have a simple and working solution?

Best Answer

Preparations:

  • Make sure each segment is a feature on its own with a unique id field (use Explode line if that is not the case and create a new field with field calculator and the expression $id).
  • Raster and line layers should be in the same, projected CRS. Otherwise, reproject.

Workflow:

  1. Run Menu Processing / Toolbox / Points along geometry with a distance of 10 m. The resulting points will contain the unique id from your line layer.

  2. Create a new attribute called elevation with field calculator on the resulting point layer with this expression: raster_value( 'raster_dem', 1,$geometry) and replace raster_dem with the name of your raster (DEM) layer.

  3. Create two new attributes min_elev and max_elev for min and max values, grouped by the unique id, representing the segments the points belong to: minimum( elevation, id) and maximum( elevation, id).

  4. Go to the layer with the line segments and create a new attribute max for the maximum elevation for each segment, using this expression (and repeat it replacing max_elev with min_elev on line 3 to create an attribute for minimum elevation):

    attribute (
       get_feature('Interpolated', 'id', "id"), 
       'max_elev'
    )
    

    Remark: end of line 3, the first 'id' is the name of the unique field in the layer Interpolated (the layer containing the points created in step 1), the second "id" is the name of the (corresponding) attribute in the initial line layer. They should normally be the same (as set in the initial line layer).

enter image description here