QGIS – Creating Connection Lines Between 2 Layers with Geometry Generator

arrayexpressiongeometry-generatorqgis

I would like to know if there is any way to generate a connecting line between 2 points layers (blocks-points representing a watershed, concpoints-points representing the concentration location of the flow from that watershed) through the geometry generator in the style panel of QGIS, with the specification that in some cases there is a relationship of several basins concentrated in a single point.

In these specific cases, the attribute "ID_UC" that indicates which basins contribute to a concentration point is a string that separates the basin ID's ("ID_QE" attribute of the layer blocks) using a comma, as in the following image:

enter image description here

As you can see in the following image, I was able to accomplish the task for simple cases where there is a ratio of 1 basin to 1 concentration point, using the expression

make_line(
   start_point($geometry),
   geometry(
       get_feature(
           'concpoints',
           'ID_UC',
           "ID_QE" 
       )
   )
)

enter image description here

I imagine that there is a way within the geometry generator to transform the string field "ID_UC" into an array, where each element would be the ID of a different basin, and thus be able to make this connection between the blocks layer and the concpoints layer, or another way to generate these connection lines.

Download example project and layers

Best Answer

You can do this using array_foreach() function

collect_geometries(array_foreach(string_to_array( "ID_UC" ),

Use this expression on the 'concpoints' layer.

collect_geometries(array_foreach(string_to_array( "ID_UC" ),
make_line(
    start_point($geometry),
    geometry(
        get_feature(
                   'blocks',
            'ID_QE',
            @element
        )
    )
)))

enter image description here

Related Question