Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#5117 closed defect (invalid)

aggregate st_union ignores linestring of zero length

Reported by: anneb Owned by: pramsey
Priority: medium Milestone: PostGIS 3.3.0
Component: postgis Version: 3.2.x
Keywords: Cc:

Description

The aggregate version of st_union ignores zero length linestrings while similar functions do not.

The following ignores the first linestring:

with lines as (
  select st_geomfromewkt('srid=3857;LINESTRING(5 5,5 5)') as geom
  union all
  select st_geomfromewkt('srid=3857;LINESTRING(1 1, 2 2)') as geom
  )
select st_union(geom) from lines;

However, the following examples include the first linestring

-- non-aggregate version of st_union:
select st_union(st_geomfromewkt('srid=3857;LINESTRING(5 5,5 5)'),
 st_geomfromewkt('srid=3857;LINESTRING(1 1, 2 2)'));

-- aggregate versions of st_extent and st_collect:
with lines as (
  select st_geomfromewkt('srid=3857;LINESTRING(5 5,5 5)') as geom
  union all
  select st_geomfromewkt('srid=3857;LINESTRING(1 1, 2 2)') as geom
  )
select st_extent(geom) from lines;

with lines as (
  select st_geomfromewkt('srid=3857;LINESTRING(5 5,5 5)') as geom
  union all
  select st_geomfromewkt('srid=3857;LINESTRING(1 1, 2 2)') as geom
  )
select st_collect(geom) from lines;

Change History (2)

comment:1 by komzpa, 3 years ago

Resolution: invalid
Status: newclosed

Zero-length lines are not a valid geometry per OGC spec, the end points of line are its boundary and boundary isn't inside the geometry. Some interpretations of it will mean "there are no points in line". Any is correct as the geometry is not valid.

07:01:59 [kom] > select ST_IsValid(st_geomfromewkt('srid=3857;LINESTRING(5 5,5 5)'));
NOTICE:  00000: Too few points in geometry component at or near point 5 5
LOCATION:  pg_notice, lwgeom_pg.c:364
┌────────────┐
│ st_isvalid │
├────────────┤
│ f          │
└────────────┘
(1 row)

You can get result you expect if you make sure your geom is valid.

07:03:22 [kom] > with lines as (
  select ST_MakeValid(st_geomfromewkt('srid=3857;LINESTRING(5 5,5 5)')) as geom
  union all
  select ST_MakeValid(st_geomfromewkt('srid=3857;LINESTRING(1 1, 2 2)')) as geom
  )
select ST_AsText(st_union(geom)) from lines;
┌────────────────────────────────────────────────────┐
│                     st_astext                      │
├────────────────────────────────────────────────────┤
│ GEOMETRYCOLLECTION(POINT(5 5),LINESTRING(1 1,2 2)) │
└────────────────────────────────────────────────────┘
(1 row)

Time: 2,638 ms

comment:2 by anneb, 3 years ago

ST_ShortestLine(A, B) produces a zero length line if A and B intersect. As per the OGC spec ST_ShortestLine does not work correctly? Should the problem be reported there?

Last edited 3 years ago by anneb (previous) (diff)
Note: See TracTickets for help on using tickets.