4

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

example

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?

5
  • imgur.com/0jAO7wb
    – Anelsio
    Sep 16, 2019 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, 2019 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, 2019 at 14:05
  • How do you know, which point exactly belongs to a line?
    – Erik
    Sep 16, 2019 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, 2019 at 14:30

2 Answers 2

8

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.


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 placed in the field "Data".

input

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
    GROUP BY
        l.id
    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
    GROUP BY
        l.id
    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

output


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

start_point

The same principle was applied for ST_EndPoint()

6

Use the "Extract specific vertices" tool (see documentation) 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))
    )

or

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