I have a GIS application with multiple polygons where I'd like to get the bounding box of the polygons.
The ST_Envelope function works as expected. Because the map is rotated the polygons always point north:
To get the angle right I was expecting the ST_OrientedEnvelope to do the trick, however this function is returning parallelograms instead of rectangles:
I thought it could be a projection issue so I tried ST_OrientedEnvelope(st_setsrid(location,4326)), ST_OrientedEnvelope(st_setsrid(location,3857)).
But got the same result. Any ideas on what might be going on?
Edit: Adding a simple way to reproduce the issue
CREATE TABLE geometries (
geometry geometry
);
INSERT INTO geometries(geometry) values(ST_GeomFromText('POLYGON((-71.25961696926942 42.470522973159675,-71.25978846747648 42.470624424600906,-71.25973918365264 42.47066975346566,-71.25956768540266 42.47056830210077,-71.25961696926942 42.470522973159675))'))
SELECT ST_AsGeoJson(st_collect(ST_OrientedEnvelope(geometry), geometry)) FROM geometries
Edit: Adding a geojson with the result of ST_Envelope, ST_OrientedEnvelope and the original geometry:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-71.259930308,
42.470242534
],
[
-71.259826779,
42.470100589
],
[
-71.259359953,
42.470441072
],
[
-71.259463482,
42.470583018
],
[
-71.259930308,
42.470242534
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-71.259926479,
42.470143762
],
[
-71.259926479,
42.470557235
],
[
-71.25940358,
42.470557235
],
[
-71.25940358,
42.470143762
],
[
-71.259926479,
42.470143762
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-71.259767585,
42.470143762
],
[
-71.259926479,
42.470237284
],
[
-71.259883325,
42.470276801
],
[
-71.259856807,
42.470261501
],
[
-71.259826746,
42.470288812
],
[
-71.259789051,
42.470266698
],
[
-71.259768993,
42.470284974
],
[
-71.259785003,
42.470294028
],
[
-71.259498831,
42.470557235
],
[
-71.25940358,
42.470500889
],
[
-71.259728887,
42.470201686
],
[
-71.259713898,
42.470192639
],
[
-71.259767585,
42.470143762
]
]
]
}
}
]
}
Geojson.io
ST_OrientedEnvelope
is not meant to be axis aligned in any way, so it is unlikely that their bounds have any collinearity with the projected North.