SET @linestring = LineString(Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0), Point(0, 0));
SELECT ST_AsText( CAST(@linestring AS POLYGON) );
-- ERROR 4033 (22S04): Invalid cast from LINESTRING to POLYGON. A polygon ring is in the wrong direction.
WITH RECURSIVE
nums (n) AS (SELECT ST_NumPoints(@linestring)
UNION ALL SELECT n - 1 FROM nums WHERE n > 1),
points (p) AS (SELECT ST_PointN(@linestring, n) FROM nums)
SELECT CAST(ST_COLLECT(p) AS LineString) INTO @linestring FROM points;
SELECT ST_AsText( CAST(@linestring AS POLYGON) );
+-------------------------------------------+
| ST_AsText( CAST(@linestring AS POLYGON) ) |
+-------------------------------------------+
| POLYGON((0 0,1 0,1 1,0 1,0 0)) |
+-------------------------------------------+
Leave a Reply