TransWikia.com

PostGIS ST_Covers doesn't match polygon after ST_Union

Geographic Information Systems Asked by Viktor Kurchenko on June 8, 2021

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?

One Answer

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

Correct answer by dr_jts on June 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP