Eg. A line maybe 100m long. Polygon A intersects the line at 0m and 50m, Polgon B intersects the line at 50m and 75m, Polygon C intersects the line at 75m and 100m.
How do I retrieve these distances and attach them to the polygon layer?
Eg. A line maybe 100m long. Polygon A intersects the line at 0m and 50m, Polgon B intersects the line at 50m and 75m, Polygon C intersects the line at 75m and 100m.
How do I retrieve these distances and attach them to the polygon layer?
This is a solution created using the QGIS Field Calculator.
Assuming that your layers are called 'line' and 'polygon" you need first of all add two new fields in the attribute table of your layers.
in the line layer add a new field called "id" and populate it with the Field Calculator and the expression $id
in the polygon layer add a new field called "line_id" and populate it with the id of the intersecting lines. You can use the expression
aggregate(layer:='line', aggregate:='concatenate', expression:=to_string($id),
filter:=intersects( $geometry, geometry(@parent)))
With this two new fields that connect the layers you can use the following expression:
concat(
round(line_locate_point( geometry:=geometry(get_feature('line', 'id', "line_id")),
point:=start_point(
intersection($geometry, (geometry(get_feature('line', 'id', "line_id"))))))),'m - ',
round(line_locate_point( geometry:=geometry(get_feature('line', 'id', "line_id")), point:=end_point(
intersection($geometry, (geometry(get_feature('line', 'id', "line_id"))))))),'m')
This expression can be used in the polygon layer as Label > Value > expression... or used in the same layer in the Field Calculator to create a new field.
This is the result:
Addendum
When a polygon is intersected by two lines, only one of the lines will be used to calculate position and relation with the line.
To create double information, as a first step, before creating the field "line_id", just duplicate the geometry and attribute to one of the polygons the id of one line and to the other the id of the second line.
This will create later two different lines and calculate the measure in relation to different lines.