I have calculated the centroids from NUTS1 information. Now for every neighbor centroids, I would like to calculate the distance. I want to connect only the neighbor's Centroids regarding the NUTS1. I tried the formula join attributes by nearest but as the title suggests joins the nearest and not the neighbors.

Here is a screenshot of the problem

enter image description here

Any suggestions?

Furthermore, after tries, I manage to solve the problem. First I created a distance matrix with k nearest equal to 7. Then I duplicate the distance matrix and by using "Join by lines" I connected all the relevant centroids. At this point, I start manually deleting the lines that were not correct according to the neighbor's idea. After that, I checked the attribute table (in order to save in a csv) but there were multiple duplicates for the same connected line. Perhaps the command Join by lines caused the duplicates problem. I export it in excel. Where I could erase easily all the duplicates with a command.

The map looks like that right now

enter image description here

  • 1
    I susgest you provide a screenshot indicating, how nearest and neighbours differ.
    – Erik
    Commented Mar 9, 2021 at 10:07
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works
    – Ian Turton
    Commented Mar 9, 2021 at 10:09
  • Hi, thank you for your comment and suggestions prnt.sc/10gye5d here is a screenshot. So the main problem is that I want to connect every centroid with the neighbor one. I Commented Mar 9, 2021 at 10:26
  • By "neighbours" do you mean two regions that share a land border?
    – Spacedman
    Commented Mar 9, 2021 at 11:47
  • @Spacedman yes :) Commented Mar 9, 2021 at 12:34

2 Answers 2


Use "Join Attributes By Location" with the "touches" predicate:

enter image description here

This will return a polygon layer with lots of polygons in it. Here's a chunk of the attribute table:

enter image description here

showing (look at the fid and fid_2 columns) region 1 neighbours regions 2 and 3, and region 10 neighbours regions 9, 22, and 20. So the geometry for region 1 is repeated twice, and the geometry for region 10 is repeated 3 times. Depending on what you want to do with the adjacency you could drop the geometry and have this is a non-geometric table and then join it to you polygons via QGIS 'Join' on the fid.

The X and Y attributes here were the start of an attempt to draw the connecting lines for this neighbourhood network, but I've not done that yet.

  • thank you @spacedman for your suggestion. Commented Mar 9, 2021 at 13:38
  • I manage to create my distance table, the problem is that I have too many duplicates do you know how can I eliminate duplicates. I am attaching a screenshot. Commented Mar 9, 2021 at 13:39
  • prnt.sc/10h55bk Commented Mar 9, 2021 at 13:41
  • Have you done some more processing to get that attribute table - looks like the output from "hub lines", in which case how did you make that and were there duplicates in the "join attributes by location" step - which you've not shown us. This might be another question.
    – Spacedman
    Commented Mar 9, 2021 at 13:58
  • Thank you a lot for your comments. Do you have a suggestion how can I clean the duplicated from this attribute table. prnt.sc/10h6c06 Commented Mar 9, 2021 at 14:18

You can create a Virtual Layer that will query the polygon layer twice and compute the distance. It is actually simpler to compute the centroids on the fly

go to the menu Layer > Add Layer > Add/Edit Virtual Layer... and enter the following query. Feel free to add more fields from both layers.

SELECT a.id, b.id, st_distance(st_centroid(a.geometry),st_centroid(b.geometry)) dist
FROM myPolygonLayer a
  JOIN myPolygonLayer b ON ST_INTERSECTS(a.geometry,b.geometry)
WHERE a.id <> b.id 

The line WHERE a.id <> b.id will returns the same distance twice (from A to B, and from B to A). If you want a single distance, change it to WHERE a.id > b.id 1

If you do need/want to use the already computed centroid layer, you would need to add two more joins to the query. You can add any field from p1 (the source polygon), c1 (the centroid from the source polygon), p2 (the neighbor polygon) and c2(the centroid from the neighbor polygon)

SELECT p1.id, p2.id, st_distance(c1.geometry,c2.geometry) dist
FROM myPolygonLayer p1
 JOIN myPointLayer c1 ON ST_INTERSECTS(p1.geometry,c1.geometry)
 JOIN myPolygonLayer p2 ON ST_INTERSECTS(p1.geometry,p2.geometry)
 JOIN myPointLayer c2 ON ST_INTERSECTS(p2.geometry,c2.geometry)
WHERE c1.id <> c2.id 
  • Thank you a lot for your suggestion. Perhaps do you know how to clean the duplicates from the following attribute table prnt.sc/10h6c06 ? Commented Mar 9, 2021 at 14:21
  • duplicates here are likely caused by duplicates in your input(s). Pick one ID and check if you have multiple polygons/points for it
    – JGH
    Commented Mar 9, 2021 at 14:24

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.