Opened 4 years ago

Closed 3 years ago

#4738 closed defect (fixed)

ST_Union creates wrong result

Reported by: Lars Aksel Opsahl Owned by: pramsey
Priority: medium Milestone: PostGIS GEOS
Component: postgis Version: 3.0.x
Keywords: Cc:

Description

I have a table with a set off linestrings (found in test_input_snap.zip) (image found in test_input_snap.png)

If i run the SQL below I get a very strange results , because the lines seems to snap to a grid off some kind, then the lines are totally changed.

(image of result found in test_output_union_fail.png)

create table test_output_union_fail as (
select (ST_Dump(ST_Union(i.geom))).geom from 
( select geom from test_input_snap ) as i
where st_isValid(geom) = true 
);

If I pick out some of the failing and just run ST_Union on this lines the result is ok. (image of result found in test_output_union_ok.png)

create table test_output_union_ok as (
select (ST_Dump(ST_Union(i.geom))).geom from 
( select geom from test_input_snap where id in (760,310,760,1511,792) ) as i
where st_isValid(geom) = true 
);

I am testing on

POSTGIS="3.1.0dev 3.1.0alpha1-144-g50796cc" [EXTENSION] PGSQL="120" GEOS="3.8.1-CAPI-1.13.3" PROJ="7.0.1" LIBXML="2.9.1" LIBJSON="0.11" TOPOLOGY (1 row)

Attachments (4)

test_input_snap.zip (3.4 MB ) - added by Lars Aksel Opsahl 4 years ago.
Dump of the input table
test_input_snap.png (144.0 KB ) - added by Lars Aksel Opsahl 4 years ago.
Image of input
test_output_union_ok.png (13.6 KB ) - added by Lars Aksel Opsahl 4 years ago.
Image of ok
test_output_union_fail.png (17.5 KB ) - added by Lars Aksel Opsahl 4 years ago.
Image of failed result

Change History (10)

by Lars Aksel Opsahl, 4 years ago

Attachment: test_input_snap.zip added

Dump of the input table

by Lars Aksel Opsahl, 4 years ago

Attachment: test_input_snap.png added

Image of input

by Lars Aksel Opsahl, 4 years ago

Attachment: test_output_union_ok.png added

Image of ok

by Lars Aksel Opsahl, 4 years ago

Attachment: test_output_union_fail.png added

Image of failed result

comment:1 by Lars Aksel Opsahl, 4 years ago

I have testing some more and I see this behaviour

If test all lines with id less than 500 it seem to be OK.

select (ST_Dump(ST_Union(i.geom))).geom from 
( select geom from test_input_snap where id < 500 ) as i

It also seem ok with all between 200 and 600.

select (ST_Dump(ST_Union(i.geom))).geom from 
( select geom from test_input_snap where id > 200 and id < 600 ) as i

But if I try to out lines between 100 and 600 it starts to snap.

select (ST_Dump(ST_Union(i.geom))).geom from 
( select geom from test_input_snap where id > 100 and id < 600 ) as i

I now tested on POSTGIS="3.0.0 r17983" [EXTENSION] PGSQL="120" GEOS="3.8.0-CAPI-1.13.1 " PROJ="6.3.0" LIBXML="2.9.10" LIBJSON="0.13.1" LIBPROTOBUF="1.3.2" WAGYU="0.4.3 (Internal)" TOPOLOGY

comment:2 by Lars Aksel Opsahl, 4 years ago

If you look at the last query and change that to use ST_AsText, the lines seems not to collapse. And in addition to get a more correct result it's also much faster with ST_AsText.

[local] lop@sl=# select count(*) as ST_AsBinary from (
select (ST_Dump(ST_Union(i.geom))).geom from 
( select ST_AsBinary(geom) as geom from test_input_snap where id > 100 and id < 600 ) as i
) as r;
 st_asbinary 
-------------
         343
(1 row)

Time: 2808.473 ms (00:02.808)
[local] lop@sl=# select count(*) as ST_AsText from (
select (ST_Dump(ST_Union(i.geom))).geom from 
( select ST_AsText(geom) as geom from test_input_snap where id > 100 and id < 600 ) as i
) as r;
 st_astext 
-----------
     12091
(1 row)

Time: 336.900 ms

comment:3 by strk, 4 years ago

This is most likely entering the precision reduction heuristic in GEOS and reducing precision too much. I guess using the new (GEOS-3.9) fixed-precision overlay would fix this case, it's to be tested.

comment:4 by mdavis, 4 years ago

This works with JTS OverlayNG. Hopefully will with GEOS as well. Have added it as a test case.

comment:5 by Algunenano, 4 years ago

Milestone: PostGIS 3.1.0PostGIS GEOS

comment:6 by pramsey, 3 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.