I have a shapefile containing several thousand GPS locations located in multiple countries and another shapefile with country polygons (or alternatively also one with country border polylines).

How can I calculate the shortest distance from each GPS location (point) to the borders of all countries adjacent to the country the point is located in?

Is there even a way to achieve this in QGIS?

Example of what I would like to calculate for every point:

enter image description here

I managed to get the distance to the (single) nearest border using the NNJoin plugin, but I have trouble identifying all adjacent borders and calculating all distances.

Ideally, the data structure would look like this, assuming that the distance is only reported for adjacent countries:

enter image description here

If this is difficult to achieve for adjacent countries only, a simple distance matrix with the distances to all countries in the sample would also be fine, but I have not found a way to create one for polylines rather than points.

  • 1
    I think you may need to write some code - the process will be something like - select a polygon and then select all of the neighbours. Then for each point in the 1st polygon, calculate the distance to each of the neighbours in turn, repeat for each polygon in the data set
    – Ian Turton
    Commented Apr 28, 2020 at 10:04
  • @IanTurton unfortunately, I am not very familiar with coding GIS processes. Would you recommend simply using the PyQGIS console to do this? Or some other software?
    – tAlbert
    Commented Apr 28, 2020 at 11:46

2 Answers 2


You can achieve this using a virtual layer.

The idea is to read the points, join them to the polygon layer to get the polygons where the points are located, then join again these polygons to the polygon layer to get the adjacent ones and finally compute the distance between the point and the adjacent polygons.

Go to the menu Layer > Add Layer > Add/Edit Virtual Layer... and enter the following query.

SELECT  gps.id,neighbors_a.CSDUID, 
        st_distance(neighbors_a.geometry,gps.geometry) as dist
FROM gps
JOIN a gps_in_a 
 ON st_intersects(gps.geometry, gps_in_a.geometry)
JOIN a neighbors_a
 ON st_intersects(gps_in_a.geometry,neighbors_a.geometry)
  AND gps_in_a.csduid != neighbors_a.csduid

enter image description here

In this example my point layer is called gps and my area layer is called a -> replace with your own. Also replace with the proper id column name.

It is important to use a CRS whose unit is in meters, and that is suitable for distance computation.

enter image description here

  • sorry to ask but ... what is the role played by the two a after the two JOIN ? I don't understand very well ... thanks
    – Snaileater
    Commented Apr 28, 2020 at 14:36
  • @snaileater the 1st one is to select the polgyon under the point, the 2nd one is to select the polygons adjacent to the 1st one
    – JGH
    Commented Apr 28, 2020 at 14:38
  • i do understand your aim but i don't understand the syntax ... what's the purpose of these two 'a', they are not aliases since they aren't used anywhere else, what are they ?
    – Snaileater
    Commented Apr 28, 2020 at 14:57
  • @snaileater Ah sorry, it's the layer name :-), which are then aliased as gps_in_a and neighbors_a
    – JGH
    Commented Apr 28, 2020 at 14:58
  • @JGH Thank you for the solution. I tried it on a small subsample and it works perfectly fine, although the query takes a lot of time. I estimate that querying the whole data will take me several hours, at least. Do you know if there is a way to speed this up other than creating a spatial index?
    – tAlbert
    Commented Apr 28, 2020 at 15:31

This is not very elegant but you could try something like this:

  • Convert each border polyline to a raster with a small resolution
  • for each raster country outline run QGIS > Raster > Proximity (you'll end up with multiple proximity outputs, one for each outline
  • for your location point file use the Point Sampling Tool (from plugins) to extract the distances from the proximity outputs. This tool allows batch processing and on-the-fly renaming of the fields that it creates in your point file so you should be able to create a table as in your original post.

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.