1

I have a simple table:

create table test (
    id serial,
    polygon geometry(Polygon, 3857)
);

And I want to insert two polygons in this table which should be created as points expand with intersection area between them:

insert into test(polygon)
values(ST_Transform(ST_Expand(ST_Transform(ST_SetSRID(ST_MakePoint(30.2833756, 50.4419441), 4326), 31997), 100), 3857));
insert into test(polygon)
values(ST_Transform(ST_Expand(ST_Transform(ST_SetSRID(ST_MakePoint(30.2841370, 50.4419441), 4326), 31997), 100), 3857));

After that I have a query to check if point is covered by polygons union:

with point_polygon as (
    select ST_Transform(ST_Expand(ST_Transform(ST_SetSRID(ST_MakePoint(30.2841370, 50.4419441), 4326), 31997), 100), 3857)
)
select ST_Covers(ST_Union(polygon), (select * from point_polygon))
from test
where ST_Intersects(polygon, (select * from point_polygon));

I can't understand why above query returns true for the first point and false for the second...

If I decrease the expand radius in select from 100 to 99.999 it returns true:

with point_polygon as (
        select ST_Transform(ST_Expand(ST_Transform(ST_SetSRID(ST_MakePoint(30.2841370, 50.4419441), 4326), 31997), 99.999), 3857)
    )
    select ST_Covers(ST_Union(polygon), (select * from point_polygon))
    from test
    where ST_Intersects(polygon, (select * from point_polygon));

Is it a rounding issue?

2

Yes, this is a rounding (numerical precision) issue. In general, it's not possible to compute the intersection point of two line segments in finite-precision arithmetic exactly so that it lies on both of the original line segments.

The union contains computed intersection points, and it just happens that they cause one of the original polygons to lie inside the union and one slightly outside.

Here's a picture of the situation:

enter image description here

4
  • Hi dr_jts, thank you! What is the best way to fix it? decrease expand radius? – Viktor Kurchenko Oct 15 '20 at 21:22
  • Well, sure, if you can tolerate decreasing the expand distance slightly for the query polygons that will work (as you saw). But whether that is a solution depends on your actual use case. Alternatively you can use a test which provides a tolerance (such as intersecting the two polygons and comparing the area of the result to the original). – dr_jts Oct 15 '20 at 21:43
  • dr_jts, thanks a ton! BTW, which software did you use to visualize this issue? – Viktor Kurchenko Oct 15 '20 at 21:47
  • That is the JTS TestBuilder (github.com/locationtech/jts/blob/master/doc/JTSTestBuilder.md). It has a Reveal Topology mode that shows vertices and segments which are very close. – dr_jts Oct 16 '20 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.