1

How would you solve the problem:

  1. Points with km values every kilometer
  2. A route (f.e. railroad) als polyline
  3. An area where you would like to know at what kilometers it intersects with the route (railroad km_min and km_max)

enter image description here

1 Answer 1

2

You can use the field calculator on your area layer with the following expression:

Where:

  • route: is your line layer
  • area: is your polygon layer
  • points: is your points layer (with a field called distance - I use meters - to use km you will have to convert in the expression)
-- make variable from array of all distances of intersecting points
with_variable(
    'distarray',
    overlay_intersects(
        'points', 
        "distance"
    )
    ,
    -- make variable of the geometry of the point with minimum distance
    with_variable(
        'start',
        array_filter(
            overlay_intersects(
                'points', 
                array("distance", $geometry)
            ),
            @element[0] = array_min(@distarray)
        )[0][1],
        
        -- make variable of the geometry of the point with maximum distance
        with_variable(
            'end',
            array_filter(
                overlay_intersects(
                    'points', 
                    array("distance", $geometry)
                ),
                @element[0] = array_max(@distarray)
            )[0][1],
            
            -- make variable of the route intersecting the area polygon
            with_variable(
                'intersectline',
                intersection($geometry, aggregate('route', 'collect', $geometry)),
            
                -- get length of line between the start of the intersecting route and the first point and subtract it from the minimum point distance
                round(array_min(@distarray) - line_locate_point(@intersectline, @start), 1) 
                
                -- concatenate the two resulting values into a string, separated by a comma
                -- of course, the two values can be calculated separately into two different fields if you prefer
                || ', ' || 
                
                -- get length of line between the end of the intersecting route and the last point and add it to the maximum point distance
                -- the line is reversed so that `line_locate_point` starts at the other end of the intersecting route
                round(array_max(@distarray) + line_locate_point(reverse(@intersectline), @end), 1)
            
            )
        )
    )
)

enter image description here

enter image description here

The blue labels in the polygon show the length of lines I drew as a sanity check between the ends of the intersecting route and the first/last points.

Note: this will have limitations if the route is very twisty and intersects the area more than once.

Reference

QGIS expression - overlay function, using overlay function as filter

4
  • the route kilometers are predetermined, so i can't start at 1 or 0. And the information about where the polygone of the area intersects (in the example picture 86.850km to 91.655km) needs to be precise... but thank you!
    – Marek
    Commented Dec 6, 2022 at 15:36
  • I see. My example starts at 0 for convenience, but the concept would remain the same for any value of distance. I'll update my answer shortly, unless someone else answers in the meantime.
    – Matt
    Commented Dec 6, 2022 at 15:39
  • just wanted to ask if you could help me with the update?
    – Marek
    Commented Dec 9, 2022 at 1:09
  • 1
    Yes, sorry, I had to ask my own question in order to solve it (see referenced question), and yesterday I was away from my computer. The expression looks complicated but the logic is reasonably simple. Let me know if I should break it down / provide more clarification. To other readers or my answer, please feel free to suggest refinements if you know a simpler way).
    – Matt
    Commented Dec 9, 2022 at 11:15

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.