6

I have two point layers, one with telecom towers and another with secondary schools. I have used the NNJoin 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
  • 2
    Do the pairs of points have a feature in common? (for example an ID) It can used as a key to link them... May 11, 2019 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, 2019 at 12:20

4 Answers 4

10

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

enter image description here

https://docs.qgis.org/testing/en/docs/user_manual/processing_algs/qgis/vectoranalysis.html#qgisdistancetonearesthublinetohub

enter image description here

3

There is a possibility 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.

Example

With the following Query, it is possible to draw lines between the pairs of points.

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
GROUP BY s.Name
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".

Result


References:

3

Nowadays, exactly since QGIS version 3.16, there is another possibility: use the overlay functions of QGIS expressions, either with Geometry Generator or Geometry by expression. Use this expression:

make_line (
    $geometry,
    overlay_nearest (
        'towers',
        $geometry
    )
)

Screenshot: red= schools, white=towers; the black lines (arrows) are created with the expression from above: enter image description here

2

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 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.