8

I have a postgis table containing polygons. I want to transform my polygons to 4 points polygons : retrieve the smallest MBR.

Here comes an illustration of what I want :

alt text

My original polygon is in pink color. I guess, it will be quite easy to get the MBR (with ST_Envelope and BBox function) But is there an easy way to get what I want?

Any ideas? Many thanks

4 Answers 4

3

Some 11 years late to the party, but I think this does what you want (well, from v2.5.0 which was probably after you asked!)

https://postgis.net/docs/ST_OrientedEnvelope.html

1
  • 1
    It seems to be that, indeed ! The algorithm is now also available in Processing in QGIS. Eleven years have passed ... ;-))
    – simo
    Commented Apr 29, 2022 at 6:02
3

For that simple example you have there, the ST_ConvexHull would give you that answer. For the more complex case, haven't thought much about it, but my guess is that you can

1) Take the ST_ConvexHull - http://www.postgis.org/documentation/manual-1.5SVN/ST_ConvexHull.html

If you are using PostGIS 1.5+ 2) Then get the ST_LongestLine of the convexhull ST_LongestLine(ST_ConvexHull(geom), ST_ConvexHull(geom)) That will give you two corners of the rectangle (though there is another part -- I'm missing) since those two corners may not be the diagonal) http://www.postgis.org/documentation/manual-1.5SVN/ST_LongestLine.html

You may also want to take a look at the code for ST_MinimumBoundingCircle -- its currently just implemented as a plpgsql function -- but what you are doing I think is a simplification of that.

http://www.postgis.org/documentation/manual-1.5SVN/ST_MinimumBoundingCircle.html

3

You can find it described here: http://www.cgafaq.info/wiki/Minimum_Rectangle_Enclosing_Points

Which leads to: http://cgm.cs.mcgill.ca/~orm/maer.html

3

A complete answer can be found here however in summary:

  • [S] rotate the polygon to match one of its edges with X axis
  • compute minmax of X and Y for vertices
  • this gives you a candidate for minimum area rectangle (MAR) or MBR
  • store the area of (x2-x1)*(y2-y1)
  • repeat from [S] for all edges
  • find the minimum area which corresponds to MBR
  • rotate found [[x1,y1],[x2,y2]] by -angle applied in [S]

Done.

Your Answer

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

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