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?

enter image description here

  • One way could be to copy the line shapefile and then clip the line based on the polygons. Then you will get segments for each line-polygon pair and can easily calculate the distance for each line segment by adding another column in the attribute table and store distances in it – JoeBe Mar 4 at 1:11
  • @JoeBe hi. Thanks. My two layers are now the original Polyline, and now the segmented Polyline. Could you explain assign the start and end distances to the segments that correlate with the distance along the original Polyline? – Mardymonkey Mar 4 at 7:48
  • I am sorry. I understood your question wrongly. What you want is not a seperate layer of the line segments, but a point layer that stores the touch (not intersect) positions between the line and the polygons. Once you have this you need to calculate the distances of the points along the line (see here) – JoeBe Mar 4 at 8:32

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.

  1. in the line layer add a new field called "id" and populate it with the Field Calculator and the expression $id

  2. 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:

round(line_locate_point( geometry:=geometry(get_feature('line', 'id', "line_id")), 
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:

enter image description here

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.