# Calculate length of common boundary

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
`````` 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
`````` 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:

1. Transform the blue polygons to polylines: 2. Find the Intersection between the blue lines and the pink polygons: 3. Compute the length of the intersected lines (original blue polyline lengths shown for comparison): Solution using QGIS expressions:

expression used to calculate intersection length:

``````round(
length(
intersection(
geometry1:=overlay_intersects('polygonB',\$geometry),
geometry2:=boundary(\$geometry))),2)
`````` 