2

I have a GIS application with multiple polygons where I'd like to get the bounding box of the polygons.

Polygons

The ST_Envelope function works as expected. Because the map is rotated the polygons always point north: ST_Envelope

To get the angle right I was expecting the ST_OrientedEnvelope to do the trick, however this function is returning parallelograms instead of rectangles:

ST_OrientedEnvelope

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

enter image description here

2
  • 2
    That is indeed likely a projection issue, and your attempts are unsuitable to solve that - setting the SRID is quite the opposite of transforming them! Please provide more info about the used projection (which hopefully clarifies why the geometries are rotated). Note that the 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.
    – geozelot
    Mar 6 at 13:30
  • Thanks for the response! Basically I have lat/long coordinates that are rendered in a webmercator projection on the client. I don't expect the orientedenvelope to be axis aligned, quite the opposite, but a rectangle that encloses the polygon using the polygon angle. I've posted the return of the st_asgeojson, if that helps. Thanks. Mar 6 at 21:16

1 Answer 1

1

Your data is not projected (4326) when computing the oriented bounding box and you are being tricked by the shape of the projected data, in 3857.

Angles are to be handled with much caution in 4326. A degree of longitude near the pole doesn't have the same ground length, in meters, as a degree of longitude near the equator. Inversely, a degree of latitude always have the same ground length in meters. Think of a T shape near the pole and near the equator. The vertical bar always have the same length while the horizontal bar is getting smaller and smaller as you move away from the equator. The angles of the encompassing triangles will be the same in 4326, but not once projected in 3857.

So, the shapes that you have look squared in 3857, but not once considered in 4326. Compute the oriented bounding box is 4326 and display it in 4326, it will be a rectangle.

enter image description here

Change the display to 3857 and the angles looks quiet differently now:

enter image description here

So the solution is to compute the oriented bounding box using a suitable projection that preserve angles

select ST_OrientedEnvelope(st_transform(st_setsrid(geometry,4326),3857)) from ...`

enter image description here

1
  • Thanks! I managed to get the expected GeoJSON by converting back to 4326 at the end: SELECT st_transform(ST_OrientedEnvelope(st_transform(st_setsrid(geometry,4326),3857)),4326) FROM geometries Mar 8 at 18:29

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.