7

In PostGIS, I want to calculate the length of different sides of a polygon. I know how to calculate a perimeter but how to calculate length side by side?

Perimeter is as a sum of side's length so I think PostGIS do this operation.

For example a basic polygon like this :

WITH data AS (
    SELECT
        1 AS gid,
        'SRID=2154;POLYGON((0 1, 1 10, 20 30, 30 1, 0 1))'::geometry AS geom
)

SELECT
    gid,
    geom,
    ST_Perimeter(geom) AS perimeter
FROM
    data;

polygon_sides

What is the length between A and B, B and C, C and D, D and A?

8

See: https://stackoverflow.com/questions/7595635/how-to-convert-polygon-data-into-line-segments-using-postgis :

SELECT st_length(ST_MakeLine(sp,ep))
FROM
   -- extract the endpoints for every 2-point line segment for each linestring
   (SELECT
      ST_PointN(geom, generate_series(1, ST_NPoints(geom)-1)) as sp,
      ST_PointN(geom, generate_series(2, ST_NPoints(geom)  )) as ep
    FROM
       -- extract the individual linestrings
      (SELECT (ST_Dump(ST_Boundary(geom))).geom
       FROM yourtable
       ) AS linestrings
    ) AS segments;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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