4

I am using the geometry generator to learn how to generate vectors using expressions. Currently, I have a point vector, and I would like to create horizontal lines (shown in red: manually drawn) connecting the points, as illustrated in the image below:

Requires Lines

I am aware that the make_line function allows us to create lines. How should I modify my code to generate a line that passes through these points horizontally?

The condition I intend to use is to connect lines with the same Y coordinates.

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

1 Answer 1

7

Easiest is probably

make_line (array_agg ($geometry, group_by:=$y))

Or, extending your version, you can add a geometries_to_array() function: it is available since QGIS 3.28, so for older versions use the expression above:

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

enter image description here

3
  • Is there any way to extract this generated line in any vector format? or is this only useful for visualization purpose?
    – unknown123
    Nov 29, 2023 at 15:02
  • I tried using 'Geometry by expression' but there is error 'Evaluation error: Cannot use aggregate function in this context.'
    – unknown123
    Nov 29, 2023 at 15:07
  • Yes, Geoemtry by expression would be the way to do so. Unfortunately, as you realized, it does not work with aggregate functions. Maybe post this as a separate question.
    – Babel
    Nov 29, 2023 at 15:12

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.