Creating "oblique bounding box" with maximum width/height ratio?

This is probably overkill on the processing front and there is likely to be a better mathematical solution, but as an example of a way that it could be done rather simply as a query

SELECT 
   id, rotated_by, oblique_bound
  FROM 
     (
     SELECT 
        m.id,
        r rotated_by, 
        ST_Rotate(ST_Envelope(ST_Rotate(m.geom, r)),-r) oblique_bound,
        row_number OVER (PARTITION BY id) 
                   ORDER BY ST_Area(ST_Rotate(ST_Envelope(ST_Rotate(m.geom, r)),-r))) N
       FROM 
          generate_series(0, 90, 0.1) N(r), my_table m
    ) s
WHERE N = 1;

This rotates the geometry, creates the bounds, reverses the rotation for each tenth of a degree between 0 and 90. The result is then the bounding box with the least area. Of course this is not an entirely accurate way of doing it and may need tweaking of the increment value in the series depending on your requirements.


QGIS has a "minimum oriented bounding box" algorithm which does exactly this.


The bounding box replacer transformer, which you mentioned should be able to do this. According to the documentation it,

Replaces the geometry of the feature with either its two-dimensional bounding box or its two-dimensional minimum oriented bounding box.

The parameter allows you to choose either axis-algined or rectilinear bounding box.

Apparently, there is a suggested enhancement to include the angle of the longer side of the oriented bounding box. This is Safe Number PR#53924.