{"id":720,"date":"2025-12-23T09:58:14","date_gmt":"2025-12-23T15:58:14","guid":{"rendered":"https:\/\/thenoyes.com\/littlenoise\/?p=720"},"modified":"2025-12-24T15:33:43","modified_gmt":"2025-12-24T21:33:43","slug":"reverse-linestring-in-mysql","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=720","title":{"rendered":"Reverse LineString in MySQL"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted has-small-font-size\">SET @linestring = LineString(Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0), Point(0, 0));<br><br>SELECT ST_AsText( CAST(@linestring AS POLYGON) );<br>-- ERROR 4033 (22S04): Invalid cast from LINESTRING to POLYGON. A polygon ring is in the wrong direction.<br><br>WITH RECURSIVE <br>    nums (n) AS (SELECT ST_NumPoints(@linestring) <br>                 UNION ALL SELECT n - 1 FROM nums WHERE n > 1),<br>    points (p) AS (SELECT ST_PointN(@linestring, n) FROM nums)<br>SELECT CAST(ST_COLLECT(p) AS LineString) INTO @linestring FROM points;<br><br>SELECT ST_AsText( CAST(@linestring AS POLYGON) );<br>+-------------------------------------------+<br>| ST_AsText( CAST(@linestring AS POLYGON) ) |<br>+-------------------------------------------+<br>| POLYGON((0 0,1 0,1 1,0 1,0 0))            |<br>+-------------------------------------------+<br><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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) );&#8211; 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 &#8211; 1 FROM nums WHERE n > 1), points (p) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4],"tags":[],"class_list":["post-720","post","type-post","status-publish","format-standard","hentry","category-mysql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-bC","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=720"}],"version-history":[{"count":3,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/720\/revisions"}],"predecessor-version":[{"id":741,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/720\/revisions\/741"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}