[postgis-devel] ST_Reverse issues

Sandro Santilli strk at kbt.io
Thu Jun 24 07:59:18 PDT 2021


On Thu, Jun 24, 2021 at 04:53:14PM +0200, Sandro Santilli wrote:
> I'm facing dragons with this query:
> 
> 
>   WITH inp AS (
>     SELECT 'LINESTRING(0 0,10 0,5 5,0 0)'::geometry g
>     UNION ALL
>     SELECT 'LINESTRING(-1.8 -6,-1.8 -7,-1.2 -7,-1.2 -6,-1.8
>   -6)'::geometry
>   )
>   SELECT
>     -- We expect both fields being true
>     ST_AsText(g) straight,
>     ST_AsText(ST_Reverse(g)) reverse,
>     ST_IsPolygonCW(ST_MakePolygon(ST_Normalize(g))) cw,
>     ST_IsPolygonCW(ST_MakePolygon(ST_Normalize(ST_Reverse(g)))) cw_when_reverted
>   FROM inp;
> 
> 
> The result I'm getting:
> 
>     -[ RECORD 1 ]----+----------------------------------------------------
>     straight         | LINESTRING(0 0,10 0,5 5,0 0)
>     reverse          | LINESTRING(0 0,5 5,10 0,0 0)
>     cw               | t
>     cw_when_reverted | t
>     -[ RECORD 2 ]----+----------------------------------------------------
>     straight         | LINESTRING(-1.8 -6,-1.8 -7,-1.2 -7,-1.2 -6,-1.8 -6)
>     reverse          | LINESTRING(-1.8 -6,-1.2 -6,-1.2 -7,-1.8 -7,-1.8 -6)
>     cw               | f
>     cw_when_reverted | f
> 
> Note how  "cw" and "cw_when_reverted" have the same value.

Sorry, I just realized I'm calling ST_Normalize after ST_Reverse
so it is expected that we get the same orientation, what is
unexpected is that ST_Normalize turns the ring into a Clockwise
ring in the first case and in a Counterclockwise
ring in the second case. Should a closed linestring be normalized
to be in a specific winding order or not ? The documentation isn't
clear on the matter, but I think there are differences there between
postgis (or GEOS) versions.

--strk;


More information about the postgis-devel mailing list