[GIS] Creating “oblique bounding box” with maximum width/height ratio

extentsfmepostgisqgis

I would like to create a surrounding rectangle with maxiumum width/height ratio ("oblique bounding box") from a set of input features as shown in the image below:

enter image description here

E.g. the PostGIS functions ST_Envelope and ST_Box2D as well as FME Transformers BoundingBoxReplacer and BoundingBoxAccumulator generate axis parallel bounding boxes.

Any suggestions using approaches with PostGIS, QGIS or FME greatly welcome!

Best Answer

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.