3

Is there a way to calculate the width and height of a rectangular polygon such as the following?

POLYGON((530934.26 161504.25,
         530941.99 161515.92,
         530945.36 161513.76,
         530937.66 161502.06,
         530934.26 161504.25))

I was trying to go on the root of calculating the diagonal ST_MaxDistance(geom, geom) and area st_area(geom) and their by get to the dimensions in a mathematical way but doesn't look like it is straight forward.

2
  • So do you want to get the lengths of the sides of postgis.net/docs/ST_OrientedEnvelope.html?
    – user30184
    Commented Jul 4, 2020 at 17:05
  • @user30184 not sure the direction. I tried ST_AsText(ST_OrientedEnvelope(geom)) and get the output as POLYGON((530934.266697057 161504.258440467,530937.63710994 161502.0254556,530945.395827638 161513.736274812,530942.025414755 161515.96925968,530934.266697057 161504.258440467))
    – Shana
    Commented Jul 4, 2020 at 17:38

1 Answer 1

5

If you want the lengths of the perpendicular sides you can just extract the first three coordinates and find the distances between them, as shown here:

WITH data AS (
  SELECT 'POLYGON((530934.26 161504.25, 530941.99 161515.92,  530945.36 161513.76,   530937.66 161502.06, 530934.26 161504.25))'::geometry 
         AS geom
)
SELECT  ST_Distance(ST_PointN(ST_ExteriorRing(geom), 1),
                    ST_PointN(ST_ExteriorRing(geom), 2)) AS d1, 
        ST_Distance(ST_PointN(ST_ExteriorRing(geom), 2),
                    ST_PointN(ST_ExteriorRing(geom), 3)) AS d2 
  FROM data;
0

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.