1

I have two sets of lines: Layer A (a planned network) and Layer B (how the network was realized). The course of the lines on those two layers is similar, but not the same and they do not have the same fields. (A has more lines and also lots of attributes I want to keep, B does not) I now need to move the lines on A, so they follow the path of the lines on Layer B as closely as possible, in the areas where they are nearest. I want to achieve this as automated as possible.

I have tried "snap geometries to layer", but this only snaps nodes within a certain distance, without moving the entire line.

I think I may need to create a new layer with "Geometry by expression", and use "overlay_nearest" as an argument, but cannot get the expression right.

So far I tried this with Layer A:

case
when overlay_nearest( 'Layer B')
then $geometry
end

Which only results in a copy of Layer A. What do I have to replace $geometry with?

And could I set some kind of buffer, in which Lines from A are aloud to move toward B?

Am I completely off and need to try something different?

1 Answer 1

0

Snap all the vertices of Layer_A to Layer_B and then create the line_substring() of Layer_B from one to the next of these snapped points/vertices. To do so, use the expression below on Layer_A with Geometry by Expression to create a new line layer.

Result, here using Geometry Generator (for visualization purpose): Layer A in different colors and with vertices (white dots); thick line in light blue: layer_B; black line created by the expression; as you can see, the selected part on layer_A on the right side is selected (yellow) for the black line as well. Attributes are preserved: enter image description here

collect_geometries(
    with_variable (
    'ln',
    array_foreach(
        geometries_to_array(nodes_to_points ($geometry)),
        line_locate_point (
            overlay_nearest('layer_B',$geometry)[0],
            closest_point (
                overlay_nearest('layer_B',$geometry)[0],
                @element
            )
        )
    ),
    array_filter (
        array_foreach (
            generate_series (0,array_length(@ln)-1),
            line_substring (
                overlay_nearest('layer_B',$geometry)[0],
                @ln[@element],
                @ln[@element+1])
            ),
            @element is not NULL
        )
    )
)
2
  • Hi Babel, it looks the way it is supposed to now, but as far as I know, the Geometrygenerator does not create a new set of lines, only changes how vectors are rendered, correct? Therefore your answer does sadly not help, because I need a new Shapefile as the result.
    – Jackie
    Nov 30, 2023 at 9:15
  • You can use the same expression with Geometry by expression to create actual geometries. Geometry Generator is just for visualization purpose (screenshot).
    – Babel
    Nov 30, 2023 at 11:12

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.