From an answer in the below question, I can generate a polygon around a LineString input to generate an Atlas. The polygon represents the Atlas view area, given the map's scale, width and height.
Creating an overview polygon from a Atlas Print Layout map extent in canvas?
I am trying to expand on this and rotate the polygon to match the map rotation in print composer. The map rotation is defined as:
90-"Rotation"
where "Rotation" is an attribute in the LineString.
--layout map scale
with_variable('print_scale',1000,(
--layout map height
with_variable('H_map',606.787,(
--layout map width
with_variable('W_map',964.142,(
with_variable('ex', centroid(bounds($geometry)),(
with_variable('H_coverage',((@H_map*0.0005)*@print_scale),(
with_variable('W_coverage',((@W_map*0.0005)*@print_scale),(
rotate(
(geom_from_wkt( 'POLYGON(('||
(x(@ex) + @W_coverage)||' '||(y(@ex) + @H_coverage)||','||
(x(@ex) + @W_coverage)||' '||(y(@ex) - @H_coverage)||','||
(x(@ex) - @W_coverage)||' '||(y(@ex) - @H_coverage)||','||
(x(@ex) - @W_coverage)||' '||(y(@ex) + @H_coverage)||','||
(x(@ex) + @W_coverage)||' '||(y(@ex) + @H_coverage)||'))')
),
-1*(90-"Rotation"),center:=centroid($geometry), per_part:=False
)
))))))))))))
The problem I'm facing is the rotation offsets the polygon slightly (see red box in below image). In theory, when the polygon is displayed in print composer it shouldn't be visible. It appears to work when there is no rotation.
I suspect it could be due to the centre of rotation? In addition to the above I have tried not defining the centre point leaving it as default (NULL) and used the x(@ex) and y(@ex) to define the centre. This sets it further off. Does anyone have any ideas?