[postgis-tickets] [PostGIS] #4856: ST_Intersection error
PostGIS
trac at osgeo.org
Thu Feb 18 11:19:41 PST 2021
#4856: ST_Intersection error
-----------------------+---------------------------
Reporter: ezimanyi | Owner: pramsey
Type: defect | Status: new
Priority: medium | Milestone: PostGIS 2.5.6
Component: postgis | Version: 2.5.x
Resolution: | Keywords:
-----------------------+---------------------------
Comment (by mdavis):
This is not a bug, it is actually expected behaviour. The reason is
because the floating point representation used internally for numbers is
not able to precisely represent certain decimal numbers (even apparently
simple ones like 2.6). The usual term for this is "roundoff error".
Simplifying the example makes this more clear:
{{{
SELECT ST_AsTExt(ST_Intersection('LINESTRING(1 1,3 3.5)','LINESTRING(1
1,2.6 3)'));
-- POINT(1 1)
}}}
This is actually the correct result, since the point (2.6 3) cannot be
represented exactly internally, and so it does not lie exactly on the
first line. This can be seen from:
{{{
SELECT ST_Intersects('LINESTRING (1 1, 3 3.5)', 'POINT (2.6 3)');
-- f
}}}
In this case the `intersection` and `intersects` operations are
consistent. They just don't match what you think they should be. But you
are computing in exact decimal arithmetic!
One way to mitigate this issue is to use the new ST_Intersection function
that takes a gridsize. Using a small gridsize will cause this operation to
return the "expected" result, since it causes the point to snap to the
line.
Incidentally, there are also situations where the operations themeselves
are internally inconsistent, also due to roundoff error. This is a
classic example:
{{{
WITH a AS (
SELECT 'LINESTRING(0.2 0.2, 1.2 1.2)'::geometry AS l1,
'LINESTRING(1.2 0.2, 0.2 1.2)'::geometry AS l2
)
SELECT ST_AsText(ST_Intersection(l1, l2)) AS intersection_l1_l2,
ST_Intersects(ST_Intersection(l1, l2), l1) AS
l1_intersects_intersection,
ST_Intersects(ST_Intersection(l1, l2), l2) AS
l2_intersects_intersection
FROM a;
---
-[ RECORD 1 ]--------------+---------------
intersection_l1_l2 | POINT(0.7 0.7)
l1_intersects_intersection | t
l2_intersects_intersection | f
}}}
--
Ticket URL: <https://trac.osgeo.org/postgis/ticket/4856#comment:1>
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