5

I want to connect all points in a point feature with every possible line segment using QGIS.

For example, how a pentagon has five points with ten line segments connecting them all to form a pentagram inside.

Alternatively phrased: How do I make spokes when all the points are hubs?

I don't care about the order, I just want every connection between two points as one line each. Is there a tool or script that can do this in QGIS?

This topic (How to generate line segments between all points) is the closest to what I want but the plugins are no longer supported.

12

You can achieve this using the Field Calculator.

Using the Geometry Generator, with Linestring geometry type, use this expression

collect_geometries( 
 array_foreach(
  array_agg($geometry)
 ,make_line($geometry,@element))
)

It will create an array of lines that connect any point with all the others point on the same layer.

You can create a new layer using the same expression in the Processing tool Vector Geometry > Geometry by expression

Here a screenshot that shows the result using the expression in the Geometry Generator

enter image description here

| improve this answer | |
  • Thanks, got it to work – qu4ntumrush Jun 28 at 23:28
  • 1
    Great! Please consider accepting the answer (green checkmark to the left) so others know it is a working solution – Val P Jun 30 at 21:36
10

You can make use of the virtual layers.

Go to Layer/ Add layer/ add-edit virtual layer and enter the following query. Feel free to add as many field as you want. The trick is to do a cross-join on the same table, generating every combination between the two layers.

select a.id, b.id, makeline(a.geometry, b.geometry) as geometry
from myLayer a, myLayer b
where a.id <> b.id
| improve this answer | |
  • Looks like my answer here Creating lines connecting each possible pair of points in PostGIS?. – Taras Jun 29 at 8:35
  • @Taras If you believe Qgis and Postgis are the same, feel free to close the question as a duplicate. I hope you are not expecting anyone to know about all off-topic answers on this site, and the concept of a cross-join is not really a new concept either – JGH Jun 29 at 11:33
  • 1
    They are not :-) it is just a funny and at the same time an interesting thing that the same answer suits both softwares – Taras Jun 29 at 11:35
2

Let's assume there is a point layer "Layer" with its corresponding attribute table accordingly, see image below. Besides a new attribute might be created that will group points together, e.g. "id" on the icon below.

input

Step 1. Duplicate your initial point layer with 'RMC > Duplicate Layer...'.

Step 2. Make use of "Join by lines (hub lines)" (Spoke ID Field is "id"). Afterwards the application of "Fix geometries" and "Remove null geometries" is essential.

result

Step 3. If lines should be unique (only one connection between two points), then proceed additionally with "Delete duplicate geometries".

result_unique


To check whether the result is correct use the initial number of points, e.g. n.

  • when lines are doubled doubled
  • when lines are unique unique
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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