Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getNumGeometries with empty geometries #949

Closed
mwtoews opened this issue Aug 27, 2023 · 3 comments · Fixed by #950
Closed

getNumGeometries with empty geometries #949

mwtoews opened this issue Aug 27, 2023 · 3 comments · Fixed by #950
Assignees

Comments

@mwtoews
Copy link
Contributor

mwtoews commented Aug 27, 2023

GEOS' getNumGeometries is inconsistent with empty geometry types. Investigating via Shapely:

import shapely
import numpy as np

shapely.geos_version_string  # 3.12.0
geom_types = [
    "POINT", "LINESTRING", "POLYGON", "LINEARRING",
    "GEOMETRYCOLLECTION", "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON"]
empties = shapely.from_wkt(np.char.add(geom_types, " EMPTY"))
dict(zip(geom_types, shapely.get_num_geometries(empties)))
shapely.get_num_geometries(empties)  # array([1, 1, 1, 1, 0, 0, 0, 0], dtype=int32)
dict(zip(geom_types, shapely.get_num_geometries(empties)))
# shows
{'POINT': 1,
 'LINESTRING': 1,
 'POLYGON': 1,
 'LINEARRING': 1,
 'GEOMETRYCOLLECTION': 0,
 'MULTIPOINT': 0,
 'MULTILINESTRING': 0,
 'MULTIPOLYGON': 0}

PostGIS's documentation for ST_NumGeometries does not document the behaviour with EMPTY geometries, but these counts are all zero:

SELECT ST_AsText(postgis_typmod_type(i) || ' EMPTY') AS empty_geom,
    ST_NumGeometries((postgis_typmod_type(i) || ' EMPTY')::geometry)
FROM generate_series(4, 63, 4) AS i;
        empty_geom        | st_numgeometries 
--------------------------+------------------
 POINT EMPTY              |                0
 LINESTRING EMPTY         |                0
 POLYGON EMPTY            |                0
 MULTIPOINT EMPTY         |                0
 MULTILINESTRING EMPTY    |                0
 MULTIPOLYGON EMPTY       |                0
 GEOMETRYCOLLECTION EMPTY |                0
 CIRCULARSTRING EMPTY     |                0
 COMPOUNDCURVE EMPTY      |                0
 CURVEPOLYGON EMPTY       |                0
 MULTICURVE EMPTY         |                0
 MULTISURFACE EMPTY       |                0
 POLYHEDRALSURFACE EMPTY  |                0
 TRIANGLE EMPTY           |                0
 TIN EMPTY                |                0
(15 rows)
@dr-jts
Copy link
Contributor

dr-jts commented Aug 27, 2023

Why do you say this is inconsistent? An empty atomic geometry is still a geometry, so can argue that getNumGeometries == 1 is correct.

I'm not sure there is a "right" answer to this. It just has to be documented behaviour.

@mwtoews
Copy link
Contributor Author

mwtoews commented Aug 27, 2023

Agree the best fix is to document the differences. I forgot there is a similar discussion for PostGIS ~2 years ago https://trac.osgeo.org/postgis/ticket/4736

More importantly, the number of geometries needs to be correct when they are unpacked, which is OK with GEOS.

See also https://trac.osgeo.org/postgis/ticket/5483 to improve docs for PostGIS.

@mwtoews
Copy link
Contributor Author

mwtoews commented Aug 27, 2023

An analogy that I can think of is that you can count one empty bottle, or zero bottles in an empty crate. But I'll probably document it differently than that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants