I have a linear shape, all of the vertexes of each segment has a z-value, stored in the geometry.

I need for every record, segment the average, min and max height value aggregated from their vertex heights.

How would I get this information using the field calculator?

I'm using QGIS 3.4 Madeira.

  • 1
    When you say the vertices have z-values, do you mean that you have a point layer whose points lie exactly on the vertices of a line layer? – TeddyTedTed Jan 10 at 12:15
  • No, I have a linear shape, I used the drape function to add height information to all the vertexes, and these are stored in the geometry itself. – Gyenes Márton Jan 10 at 14:06

For entire line features:

Use the 'Extract Vertices' tool to turn all vertices into points.

In the new points layer create a new field for the height value and populate it using:


Now use 'Join Attributes By Location (Summary)' and join the line feature to the vertices (you may want to delete some of the fields automatically created by extracting the vertices first).

The new Joined Layer will be automatically populated with statistics about the joined attributes, including mean, min, and max.

This will maintain the original IDs, it will result in a lot of unwanted fields which you may want to delete.

For individual segments:

Use 'Explode Lines' to convert the whole line feature into individual segments.

You might have to 'Drape' the result again to restore the z-values.

Create two new columns in the segments layer for the start and end height, in field calculator use the following:





from those values you can create new columns for average, min, and max using the following field calculations:


("start_height" + "end_height") / 2


min("start_height", "end_height")


max("start_height", "end_height")
  • My problems with this solution. 1. I have to keep the original segments unexploded. 2. I have to keep the original segment IDs of the segments. 3. I need the mean, max and min z values derived from of the whole segments' vertexes. On the other hand it may help. I will try to extract the vertexes into point shape, transfer the original ID-s to each object, and try to collect the information from this layer. Thanks – Gyenes Márton Jan 10 at 16:00
  • Ah I see, edited answer. – TeddyTedTed Jan 10 at 16:15

After suggestions, I was able to create the dataset. The steps:

  1. I used drape function from processing toolbox to add z value to each vertex of the linear objects
  2. from vector/geometry tools I used extract vertexes function and got a point layer
  3. from vector/geometry tools I used add geometry attribute function on the new point layer
  4. in DB manager I ran 3 queries on the resulting layer of the geometry attribute generation to select the min, max and avg values for each unique id:

select unique_id, max(zcoord) from 'added geom info'
where unique_id=unique_id
group by unique_id 

I added the query results to the project, and with a simple join, based on unique_id I was able to add the good values to each records.

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.