[PostGIS] #5754: ST_ForcePolygonCCW unexpectedly modifies line geometries
PostGIS
trac at osgeo.org
Tue Oct 14 13:17:36 PDT 2025
#5754: ST_ForcePolygonCCW unexpectedly modifies line geometries
----------------------+---------------------------
Reporter: nti | Owner: pramsey
Type: defect | Status: new
Priority: high | Milestone: PostGIS 3.4.5
Component: postgis | Version: 3.4.x
Resolution: | Keywords:
----------------------+---------------------------
Comment (by pramsey):
This seems to be very long-standing, and can be traced to the fact that,
while ST_Reverse() reverses all geometry types, the ST_ForcePolygonCW and
ST_ForcePolygonCCW functions only reverse polygonal types. And there's
logic in the pipe that checks "is this CCW? then reverse it". So polygonal
types get handled correctly, and lines get reversed every time. Which is
what you're seeing.
A quick in-place fix is
{{{
CREATE OR REPLACE FUNCTION ST_ForcePolygonCCW(geometry)
RETURNS geometry
AS $$ SELECT
CASE WHEN ST_GeometryType($1) IN ('ST_Polygon',
'ST_MultiPolygon')
THEN ST_Reverse(ST_ForcePolygonCW($1))
ELSE $1 END $$
LANGUAGE 'sql' IMMUTABLE STRICT PARALLEL SAFE;
}}}
A better fix would involve trying to centralize the "should I try and
orient this" logic into a single place, as it's currently in 5 places (and
more with this temporary fix).
Unfortunately it seems there's no way to fix this without changing the SQL
definition for ST_ForcePolygonCCW which makes a patch-level fix a "no go",
so this will have to be for the 3.7 milestone.
--
Ticket URL: <https://trac.osgeo.org/postgis/ticket/5754#comment:3>
PostGIS <http://trac.osgeo.org/postgis/>
The PostGIS Trac is used for bug, enhancement & task tracking, a user and developer wiki, and a view into the subversion code repository of PostGIS project.
More information about the postgis-tickets
mailing list