I have two point layers, one with telecom towers and another with secondary schools. I have used the NN-join plugin to find the nearest tower from each school, and calculated the distance between. The result is in a vector layer, with separate columns for the tower and school names.

How can one draw lines between the pairs of points?

  • 2
    Do the pairs of points have a feature in common? (for example an ID) It can used as a key to link them... – gianfranco di pietro May 11 '19 at 10:04
  • No, they have separate IDs, i.e. each school and tower has a separate name, and (obviously) separate lat/long. But since each pair is uniquely defined on each row in the table, perhaps I can create a unique ID by concatenating school name and tower name? – olofhess May 11 '19 at 12:20

This should be possible without any plugin using the default Processing tools, particularly Distance to nearest hub:

enter image description here


enter image description here


Tested on QGIS 2.18 and QGIS 3.4

I can suggest using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer...

Let's assume we have eight features in "school" (blue squares) and five in "tower" (orange circles) accordingly, see image below.


With the following Query, it is possible to achieve the result

SELECT s.Name AS SName, t.Name AS TName,
       make_line(s.geometry, t.geometry),
       ROUND(MIN(ST_Distance(s.geometry, t.geometry)),2) AS distance,
       s.id || '_' || t.id AS uniqueid
FROM school AS s, tower AS t
ORDER BY distance DESC

The output Virtual Layer will generate shortest lines between schools and towers including the following attributes 'school name', 'closest tower name', 'distance to the closest tower' and 'unique id'.




There are several ways to accomplish this. I used the following method.

  1. Use the NN_join plugin to find the shortest distance between a school and a tower.
  2. Use the vector/geometry tools/add geometry attributes to add coordinate values for the point pairs to the file.
  3. Export as .csv, and open in Excel. Sort the file and add a row number as "ID".
  4. Split the file in two, one with school points and another with the associated tower. Keep the sorting sequence, and add row number as "ID" in the second file.
  5. Now search for the following plugin https://plugins.qgis.org/plugins/connect_points/
    Note: This plugin is not updated for QGIS 3.x, so I used QGIS 2.8 for this step.
  6. Import the schools and towers layers and run the plugin.

The result is a shp-layer with lines from each school to the nearest tower.

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.