# Measuring average, min and max of vertex height of linear shape using QGIS?

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?

• 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:

``````z(\$geometry)
``````

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:

start_height:

``````z(start_point(\$geometry))
``````

end_height:

`````` z(end_point(\$geometry))
``````

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

mean:

``````("start_height" + "end_height") / 2
``````

min:

``````min("start_height", "end_height")
``````

max:

``````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:

``````example:
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.