6

I have two linear layers like you see below:

enter image description here

I would like to select just the nearest black lines to the red ones, which represent the other layer.

I found similar solutions, but they refer to the point layer instead:

Find nearest line feature from point in QGIS

Is there some way of making a selection of just these lines, which lie in the direct vicinity of the red line (another layer)?

UPDATE I:

The answer below brought me to the following approach:

enter image description here

enter image description here

the formula works, but I guess something is wrong with the Join by nearest option. I've set a unique id for both layers, one of them had already fid as the unique one. When the formula is applied, some parts of the layer are selected regardless of their vicinity to the other layer.

How should I do the Join by nearest option with respect to this situation?

1
  • Yes, indeed I just need the road lines, the very first lines which are visible next to the red line.
    – Geographos
    Commented May 5, 2023 at 12:28

3 Answers 3

5

You need a unique id field in your black line layer, mine is called id.

Join (by nearest) this id to the centerline so each centerline get the attribute of the closest black line. First split the centerline into short segments to be sure each nearest black line is found.

In the output you will have only the black ids closest to the centerline, the ids you want to select.

My output layer is called 'joined'. Select by expression on your black line layer with the expression:

array_contains(array:=
aggregate(layer:='joined', aggregate:='array_agg', expression:="id"), value:="id")

enter image description here

1
  • It doesn't work in my case, as all lines are being selected. I provided a unique ID both for the main layer as well as the other, but I am not sure how to execute the Join by nearest option.
    – Geographos
    Commented May 15, 2023 at 16:36
3
+50

Use QGIS expressions with function overlay_nerarest(), available since QGIS 3.16, to identify the nearest line from another layer (here called layer_2). On the first layer (with the red line in your screenshot), run Geometry by expression with the following expression to create a layer containing the closest line:

overlay_nearest('layer_2', $geometry)[0]

Like this, you already have a layer with the lines you want. If you indeed want to have a selection on your initial layer_2, proceed as follows: On layer layer_2 use Select by Expression with this expression:

overlay_within('Modified geometry')
8
  • I used to select by expression, but it looks like your formula is not known. Is it a correct or incorrect way I did it?
    – Geographos
    Commented May 19, 2023 at 14:20
  • What QGIS version do you use?
    – Babel
    Commented May 19, 2023 at 14:46
  • I can't see what you did, so difficicult to say what went wrong. Please add details (e.g. what steps you did, exact error message etc) and a screenshot, if possible, to see what doesn't work
    – Babel
    Commented May 19, 2023 at 14:55
  • I use QGIS 3.12
    – Geographos
    Commented May 19, 2023 at 15:17
  • OK, that explains why the expression doesn't work: overlay_ functions were introduced in QGIS 3.16 (see link in updated answer). You should update to a newer version. If you use an older version, this should be stated in the question, normally we assume you use the current version if not otherwise stated. With QGIS 3.12, you could use refFunctions plugin to achieve the same: plugins.qgis.org/plugins/refFunctions
    – Babel
    Commented May 19, 2023 at 15:24
2

As you made clear in your comment, you're using QGIS 3.12, so you can't use the very handy overlay_nearest() function, introduced in QGIS 3.16. So for older versions (tested with QGIS 3.12), you have to use a more complicated approach (that sayed: I heavily recommend updating to the current version!). Use this expression, replacing line2 with the name of your line layer from where you want to find the closest line feature:

array_filter (
     array_foreach(
        aggregate ('line2', 'array_agg', $geometry),
        if (
            intersects(
                buffer (@element, 0.1), 
                closest_point (
                    aggregate ('line2', 'collect', $geometry),
                    $geometry
                )
            ),
            @element,
            NULL
        )
    ),
    @element is not NULL
) [0]

Screenshot from QGIS 3.12: red line=layer line1, black lines= layer line2; yellow highlighted line is created with the expression from above, applied on line1 (red line) to find the closest one of the black lines:

enter image description here


If you need a selection on the layer with the black lines, use the expression from above with Geometry by expression to create the line as a separate layer. Than go the the initial black line layer, use Select by expression with this expression, where Modified the the name of the layer created with Geometry by expression:

intersects ($geometry, aggregate ('Modified', 'collect', $geometry))
6
  • Ok, will it work also as Select by expression? Instead of styling layer
    – Geographos
    Commented May 24, 2023 at 10:34
  • Sorry, that is a complete different case. For select by expression, you must work on the layer you make the selection, thus on the black layer. If you work on the red layer, you can relatively easy find the the closest black line. It's more diffiult to work on the black layer to find... what? First the nearest red line, then from there go back to find the nearest black line... and repeat this for each feature of the black line. Seems too cumbersome to try. Why don't you want to update your installation? Much less headache, probably.
    – Babel
    Commented May 24, 2023 at 12:53
  • See updated answer how to make the selection.
    – Babel
    Commented May 24, 2023 at 12:57
  • 1
    I have no admin rights for the upgrade, that's he case. Tried your solution, but QGIS goes frozen, probably due to the size of my layer.
    – Geographos
    Commented May 26, 2023 at 9:22
  • 1
    It doesn't work as my QGIS is constantly thrown away, but I will accept your answer as I believe it works correctly in normal conditions.
    – Geographos
    Commented May 26, 2023 at 14:25

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.