10

I have two layers:

  • myPoints: a spatial point layer with an attribute field "ID" (entries are unique).
  • connectivityList: a non-spatial attribute table with two fields (idFrom, idTo). Here, the entries are not unique (i.e, a single point from myPoints can appear multiple times in the list, both as start and end point).

My goal is to draw one line for each row in the connectivity list that goes from one point (with ID == idFrom) to another point (with ID == idTo). How can I achieve this?

I would love a solution that just links the layers instead of baking them together, as the point coordinates will change in the future and I would like the lines to automatically adapt. I guess I am looking for a virtual layer, but I could not figure out how to set it up. This question seems very related, but I don't know how to select both of my points and connect them by a line.

2 Answers 2

14

General query to relate your layers in a Virtual Layer:

SELECT
  c.*,
  MakeLine(f.geometry, t.geometry) AS geometry
FROM
  "connectivityList" AS c
  JOIN
  "myPoints" AS f
    ON c."idFrom" = f."ID"
  JOIN
  "myPoints" AS t
    ON c."idTo" = t."ID"
;
1
  • Perfect, thank you very much! All lines are displayed and even the Identification tool works.
    – jonas
    Oct 18, 2022 at 9:38
10

You can also use an expression using geometry generator.

  1. Make an new style layer for the point layer.
  2. Choose "Geometry Generator", "LineString/MultiLineString" for "Symbol layer type" and "Geometry type" respectively.
  3. Paste the following expression.
    make_line(
      $geometry,
      geometry(
        get_feature(
          @layer,
          'ID',
          attribute(
            get_feature(
              'connectivityList',
              'idFrom',
              ID
            ),
            'idTo'))))
    

enter image description here

Then the lines automatically adapt.

enter image description here

1
  • It works partly, but thank you very much already. Using the geometry generator is a good idea. The remaining problem is: if one point occurs multiple times as a "fromID" in the connectivity list, only the first row in the list will be drawn. I have edited the question to make it clear.
    – jonas
    Oct 18, 2022 at 8:57

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.