I am working with two shapefile layers: lines and points. These shapefiles do not overlap and have different attribute values.


The point layer contains data that I want to join to the beginning and end of the line layer. If a can´t join this data by field value or location, which vector tool should I use in QGIS?

  • imgur.com/0jAO7wb – Anelsio Sep 16 '19 at 14:00
  • 2
    Are the layers close enough to each other you could buffer the points and then join attributes by location? Please edit your post so it contains the images. – Erik Sep 16 '19 at 14:01
  • Hello Erik. The distances between the layers vary throughout the project. In this image the distance is approximately 40 meters. Is buffering still a viable alternative? – Anelsio Sep 16 '19 at 14:05
  • How do you know, which point exactly belongs to a line? – Erik Sep 16 '19 at 14:13
  • The point value that I want to add to the beginning and end of each line is the perpendicular of the point (please consider all the points as values from a previous line layer). Each point has a metric value, so there is no problem if different lines have the same value. – Anelsio Sep 16 '19 at 14:30

I have tried to set up the example above. I hope I correctly noticed that your point layer consists of points located very close to each other. Looks like the output after using the 'Create points along lines' tool.

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 the following layers "Layer_Points" and "Layer_Lines", see image below. The data that has to be transferred from the points layer called "Data".


With the following query, it is possible to join points' attributes to lines appropriated through the beginning and the end of the line layer.

WITH start_points AS (
SELECT l.id, p."data"
FROM "Layer_Lines" AS l, "Layer_Points" AS p
ORDER BY MIN(ST_Length(ST_ShortestLine(start_point(l.geometry), p.geometry))) DESC),

end_points AS (
SELECT l.id, p."data"
FROM "Layer_Lines" AS l, "Layer_Points" AS p
ORDER BY MIN(ST_Length(ST_ShortestLine(end_point(l.geometry), p.geometry))) DESC)

SELECT l.*, sp."data" AS "start_data", ep."data" AS "end_data"
FROM "Layer_Lines" AS l
JOIN start_points AS sp ON sp.id = l.id
JOIN end_points AS ep ON ep.id = l.id

The output Virtual Layer with its Attribute table will look as following


ST_ShortestLine()is the shortest line between two geometries. ST_StartPoint() defines the lines' starting points for connecting with points layer, see image below.


The same principle was applied for ST_EndPoint()


Use the extract specific vertices tool to extract the endpoints of each line as a separate point layer, as explained here.

Use the NNJoin plugin to perform a nearest-neighbor join between the new endpoints layer and your other point layer. Or use the geomnearest function from the refFunctions plugin in the Field Calculator.

Use the Field Calculator to copy the joined values to attributes in your original line layer, with expressions like this:

aggregate('endpoints_joined', 'min', "joined_value", $geometry = start_point(geometry(@parent)))

aggregate('endpoints_joined', 'min', "joined_value", $geometry = end_point(geometry(@parent)))

To apply the example expressions to your data, make the following substitutions:

  • Use the actual name of the joined endpoint layer where the example expressions say 'endpoints_joined'.
  • Use the actual name of the field copied from the original point layer where the example expressions say "joined_value".

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.