8

I have two different layers, the pink and the blue one. The green polygon is given by the intersection of the other two layers. I want to calculate the length of the common boundary line between the green and the pink layer.

I have already tried this query but for some reason it doesn't compute anything:

select pink.id as pink_id, 
      st_intersection(pink.geometry, green.geometry) as geometry, 
      st_length(st_intersection(pink.geometry, green.geometry)) as border_length
from pink_layer as pink
    join green_layer as green
        ON st_intersects(pink.geometry, green.geometry)
            AND pink.id = green.id

enter image description here

3 Answers 3

6

The intersection between the pink and green polygons is also a polygon, not a line.

You may want to compute the length of the intersection between the difference between the two polygons and the green (or pink, it shouldn't matter) one.

Beware though, for it to work well the pink polygon should have vertices at the intersection points with the green polygon

select pink.id as pink_id, 
      st_intersection(st_difference(pink.geometry, green.geometry), green.geometry) as geometry, 
      st_length(st_intersection(st_difference(pink.geometry, green.geometry), green.geometry)) as border_length
from pink as pink
    join green as green
        ON st_intersects(pink.geometry, green.geometry)
            AND pink.id = green.id

enter image description here

6

I agree with @JGH about the reason you are not able to compute the length in this case, because your intersections have polygon geometries. However since the previous answer has a caveat related to the position of vertices in the inputs, here is another approach that should be able to handle more complex intersection scenarios:

Original Data: enter image description here

  1. Transform the blue polygons to polylines: enter image description here

  2. Find the Intersection between the blue lines and the pink polygons: enter image description here

  3. Compute the length of the intersected lines (original blue polyline lengths shown for comparison): enter image description here

6

Solution using QGIS expressions:

expression used to calculate intersection length:

round(
length(
intersection(
geometry1:=overlay_intersects('polygonB',$geometry)[0],
geometry2:=boundary($geometry))),2)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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