1

I have a QGIS point layer with several points with no attributes except the Id field. I want to make a line from each point to the rest of the points. In addition, I would like to get two new fields, with the source ID and the destination ID.

I need the length field of the lines. I would need a new field to classify the lines, for each source point, according to their length: shortest length = 1, second shortest length = 2, third shortest length = 3...

ID   ID_source  ID_target  Length Length_class
1        1         1         0,5      1
2        1         2         0,8      2
3        1         3         1,2      3
4        2         1         0,6      1
5        2         2         0,7      2
6        2         3         0,9      3...  

Lines and a table similar to this should be the result. The original table only has the ID field. The points are the result of some previous operations. Maybe 1000 or 2000 source points.

david
  • 103
  • 4
  • 1
    How many points do you have? What have you tried so far? Are there already value ranges for "classes" ? – Taras May 26 '22 at 19:29
  • 1
    You can start here : https://gis.stackexchange.com/questions/141078/generating-line-segments-between-all-points-using-qgis and https://gis.stackexchange.com/questions/366235/creating-all-possible-line-segments-between-all-points-using-qgis?noredirect=1&lq=1 – Taras May 26 '22 at 19:33

1 Answers1

0

An extension to @user30184's answer, using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer....

Let's assume there is a point layer called 'Centroids', see image below.

input

With the following query, it is possible to make a line from each point to the rest of the points.

SELECT
    make_line(a.geometry, b.geometry) AS geometry, 
    a.id AS "ID_Sourse",
    b.id AS "ID_Target",
    round(st_length(make_line(a.geometry, b.geometry)), 4) AS "Length",
FROM
    "Centroids" AS a,
    "Centroids" AS b 
WHERE
    a.id < b.id;

After apply two times the "Add autoincremental field" tool to (1) create an autoincremental field "ID" and (2) a new field to classify the lines "L_Class".

The output polyline layer with its attribute table will look like this:

result

Taras
  • 32,823
  • 4
  • 66
  • 137