How would you solve the problem:
- Points with km values every kilometer
- A route (f.e. railroad) als polyline
- An area where you would like to know at what kilometers it intersects with the route (railroad km_min and km_max)
You can use the field calculator on your area
layer with the following expression:
Where:
-- 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)
)
)
)
)
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.
QGIS expression - overlay function, using overlay function as filter