Skip to main content

ST_IsClosed

Tests whether a LineString or MultiLineString is closed (start point equals end point).

Syntax

ST_IsClosed(geometry)

Parameters

ParameterTypeDescription
geometryGeometryLineString or MultiLineString to test

Returns

TypeDescription
BooleanTrue if geometry is closed, false otherwise

Examples

Closed LineString (Ring)

SELECT ST_IsClosed(ST_GeomFromText('LINESTRING(0 0, 4 0, 4 4, 0 4, 0 0)'));
-- Returns: true

Open LineString

SELECT ST_IsClosed(ST_GeomFromText('LINESTRING(0 0, 4 0, 4 4)'));
-- Returns: false

Find Unclosed Boundaries

SELECT id FROM boundaries
WHERE NOT ST_IsClosed(geom);

Convert to Polygon

SELECT id, ST_GeomFromText('POLYGON((' || coords || '))')
FROM (
SELECT id, coords FROM line_features
WHERE ST_IsClosed(geom)
);

Notes

  • Only applies to LineStrings and MultiLineStrings
  • For MultiLineString, all component lines must be closed
  • Points and Polygons return NULL or error
  • A closed LineString is not necessarily a valid ring (could self-intersect)

See Also