I have three vector layers two points and one polygon:

  1. Centroids (POINTS FROM)
  1. Sorting_plants_2020 (POINTS TO)
postcode | name | where | capacity | id
  1. National_BC / countries (POLYGON)
CNTR_CODE | NUTS_NAME | BC_Balance | id

I want to calculate the minimum distance of each point to another point layer if they are located within the same country (i.e. the distance from the closest point of the closest point).

Is there any available plugin in QGIS for doing this? Or maybe some existing PyQGIS code?

enter image description here

  • So for each country you only want one distance, the minimum distance between one point table and another point table within a country? Or do you want several minimum distances for each country?
    – Jakob
    Oct 20, 2020 at 7:50
  • I would like one distance from one point table to another point table if these (distances are within a country). So the first option you state.. Oct 20, 2020 at 7:54
  • 2
    Did you check this thread gis.stackexchange.com/questions/328406/… ? What happens when you try to implement my solution?
    – Taras
    Oct 20, 2020 at 7:57
  • That's nice of you to mention! I tried this solution already. Somehow I when I try to create this virtual layer I get an empty layer with only headers.. But this is exactly what I want yes. Oct 20, 2020 at 7:59
  • Yes they all have the same CRS. The code now looks like this: SELECT ct.name AS city, c.CNTR_CODE AS conflict, cnt.NUTS_NAME AS country, make_line(ct.geometry, c.geometry), ROUND(MIN(ST_Distance(ct.geometry, c.geometry)),1) AS distance, ct.id || '_' || c.id AS uniqueid FROM Sorting_Plants_2020 AS ct, Centroids AS c, National_BC AS cnt WHERE st_within(c.geometry, cnt.geometry) AND st_within(ct.geometry, cnt.geometry) GROUP BY ct.name ORDER BY distance DESC And when I test this I results in no error.. Oct 20, 2020 at 8:13

1 Answer 1


Additionally to my answer here, I may suggest the following workflow with less work with "Virtual Layer".

Let's assume there are two point layers "poi"(pink) and "end"(dark green), and one polygon layer "poly"(blue) with its corresponding attribute tables accordingly, see image below.


Step 1. Deploy a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer... with the following query to create all possible lines connected between two points layers.

SELECT "poi".id AS pfrom,"end".id AS pto,
make_line("poi".geometry, "end".geometry) AS geom
FROM "poi", "end"


As an alternative to create all possible connections between points from two layesr you may try using the solution provided in this answer.

Step 2. Proceed with "Extract by location" with ticked within geometric predicate.


Note that the algorithm will be applied for lines that are entirely within each single polygon, i.e. no connections with points from neighbor polygons, see image below.


Step 3. Use "Extract by expression"with the following expression
$length = minimum($length, "pfrom"). Afterwards the application of "Join attributes by location" might be relevant.


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.