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.

2

1 Answer 1

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

Your Answer

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

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