I am encountering an issue while trying to use the make_line function along with geometries_to_array in the Geometry by Expression tool in QGIS. The expression I'm using is as follows:

    geometries_to_array (
        collect ($geometry, group_by:=$y)

However, when I run this expression in the Geometry by Expression tool, I receive the following error:

Evaluation error: Cannot use aggregate function in this context

I understand that this error is related to the use of aggregate functions within the make_line expression. Is there a workaround for this issue? I'm trying to create lines based on the y-coordinate in my dataset.


1 Answer 1


The aggregate function that cause the error is collect(), and the context in which it can't be used is the Geometry by Expression tool.

You can't aggregate features from the same input layer because it is being used to create the features of the output layer.

But you can use the aggregate() function, which aggregate features from other layer. There is no need to create a new layer to do this, you can point to the same layer by its name and it will be taken as a copy of the input layer, so the following expression works:

   layer:='Layer name',

Remember that all expressions are evaluated once for each feature, and output features will be the same as input (with another geometry).

You will have features with the same (aggregated) geometry in the output layer.

  • to limit the number of geometries created and to avoid re-running the same expression for each feature, one could add a case when $id=1 then [...] end, so it will run just for the 1st feature.
    – Babel
    Nov 30, 2023 at 1:56
  • 2
    In that case I think all other features will have a null geometry. Nov 30, 2023 at 1:57
  • 1
    It shouldn't be a problem as long as the user knows what is happening, in both cases of duplicate or null geometries. Nov 30, 2023 at 2:06
  • 1
    else $geometry could raise an error, because I understand that the user wants to create a line layer from a point layer, so the output should be lines. Nov 30, 2023 at 2:08
  • 2
    In my opinion, this operation should be avoided and the features should be aggregated through an SQL query, which returns only the number of features that result from the grouping. But it wasn't my intention to answer that either :-) Nov 30, 2023 at 2:21

Your Answer

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

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