Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#4127 closed defect (fixed)

ST_Dimension returns 0 for empty GeometryCollection

Reported by: esparano Owned by: pramsey
Priority: medium Milestone: PostGIS 2.5.0
Component: postgis Version: 2.4.x
Keywords: Cc:

Description

The documentation says that ST_Dimension should return 0 for Points and NULL for empty geometries. However, there appears to be a bug in the code where ST_Dimension return 0 for empty collections.

I would suggest changing line 1033 of lwgeom.c from:

int maxdim = 0, i;

to:

int maxdim = -1, i;

Change History (7)

comment:1 by pramsey, 6 years ago

The documentation seems to be out of date, as "null on empty" behaviour has been commented out…

postgis25=# select st_dimension('POINT EMPTY');
 st_dimension 
--------------
            0
(1 row)

postgis25=# select st_dimension('LINESTRING EMPTY');
 st_dimension 
--------------
            1
(1 row)

postgis25=# select st_dimension('POLYGON EMPTY');
 st_dimension 
--------------
            2
(1 row)

postgis25=# select st_dimension('GEOMETRYCOLLECTION EMPTY');
 st_dimension 
--------------
            0
(1 row)

comment:2 by pramsey, 6 years ago

#1441 is where the behaviour was changed.

comment:3 by pramsey, 6 years ago

Resolution: fixed
Status: newclosed

In 16663:

Harmonize doco with existing behaviour, closes #4127

comment:4 by esparano, 6 years ago

I think there may have been a misunderstanding and I'd like understand the intended behavior of the code. The dimension of a Point is 0 because, mathematically, it is a 0-dimensional object. The docs also say that the dimensionality of an array of geometries is the highest dimensionality of any of the subgeometries. So are you saying that, if there aren't any subgeometries in the first place, its dimensionality is actually 0? I was thinking it makes more sense to return -1, like the function returns for NULL.

Last edited 6 years ago by esparano (previous) (diff)

comment:5 by esparano, 6 years ago

Resolution: fixed
Status: closedreopened

Could I get some input on this? Reopening temporarily while we discuss this.

Last edited 6 years ago by esparano (previous) (diff)

comment:6 by robe, 6 years ago

Resolution: fixed
Status: reopenedclosed

Here is the way I see it

ST_Dimension is a concept of a geometry's geometric type and is defined to be the maximum dimension of any geometry in the set.

So that said

POINT EMPTY → 0

LINESTRING EMPTY → 1

MULTIPOLYGON EMPTY → 2

SELECT ST_Dimension(ST_Collect('POLYGON EMPTY'::geometry, 'LINESTRING EMPTY'::geometry)) → 2

So the only question left is what to do about GEOMETRYCOLLECTION EMPTY. Does an empty geometrycollection imply we should consider it being the highest dimension of emptiness or the lowest dimension of emptiness?

I frankly don't care - doesn't seem to be worth changing the code to address a questionably ambiguous thing and making it return -1 is liable to break someone's code somewhere, so would have to wait til PostGIS 3.0 since it is a policy change.

That said, I'm reclosing this bug. If you want to discuss further, about why this is all wrong, use our dev mailing list for that:

https://lists.osgeo.org/mailman/listinfo/postgis-devel

Last edited 6 years ago by robe (previous) (diff)

comment:7 by robe, 6 years ago

Milestone: PostGIS 2.4.5PostGIS 2.5.0
Note: See TracTickets for help on using tickets.