geos icon indicating copy to clipboard operation
geos copied to clipboard

Collectionextract function in C api

Open theroggy opened this issue 2 years ago • 9 comments

Maybe I looked over it, but I don't seem to find an ST_CollectionExtract equivalent in the C API.

If it is indeed not available, it would be great if it could be added...

Regards, Pieter

theroggy avatar Dec 12 '23 21:12 theroggy

The C++ API has a geos::geom::util::GeometryExtracter class (example use with GeometryExtracterTest.cpp), but this does appear to be absent from the C API.

mwtoews avatar Dec 13 '23 00:12 mwtoews

If it is indeed not available, it would be great if it could be added...

What is your use case for this? Is it to deal with mixed-dimension results from overlay operations, or something else?

dr-jts avatar Dec 13 '23 18:12 dr-jts

Related: https://trac.osgeo.org/geos/ticket/1117

dbaston avatar Dec 13 '23 18:12 dbaston

Here's a possible design, for comment:

Geometry*    GEOSGeom_extractCollection(Geometry* collection, unsigned int dimension)

Should this have simple copy semantics? Or should it minimize allocations via:

  • adds the elements of required dimension to a new Multi geometry (should this be returned as an atomic geometry if only one element is found?)
  • ?? any elements not returned are destroyed automatically
  • ?? caller must destroy original geometry

dr-jts avatar Dec 13 '23 18:12 dr-jts

Actually the question of "return POLYGON() or return MULTIPOLYGON()" for single-item collections seems a little fraught... doing so over a set could result in a heterogeneous set of POLYGON/MULTIPOLYGON instead of a nice homogeneous set of MULTIPOLYGON.

pramsey avatar Dec 13 '23 18:12 pramsey

Actually the question of "return POLYGON() or return MULTIPOLYGON()" for single-item collections seems a little fraught... doing so over a set could result in a heterogeneous set of POLYGON/MULTIPOLYGON instead of a nice homogeneous set of MULTIPOLYGON.

Fraught indeed... also, extracting an arbitrary set of polygons into a MULTIPOLYGON might produce an invalid geometry, if they happen to overlap.

I suggest taking the low road and just banging all polygons into a single MULTIPOLYGON, caveat usor. For more refined semantics the user will have to write their own code.

dr-jts avatar Dec 13 '23 18:12 dr-jts

If it is indeed not available, it would be great if it could be added...

What is your use case for this? Is it to deal with mixed-dimension results from overlay operations, or something else?

Yes indeed. Remove eg. lines and points from collections returned from overlay operations (e.g. intersection,...) between polygons, or cleanup the result of makevalid if linework algorithm is used, as it doesn't support keepcollapsed=False.

theroggy avatar Dec 13 '23 18:12 theroggy

I suppose the GeometryExtracter mentioned above already takes this in account, but to be sure: geometrycollections can be nested, so possibly the extraction needs to be recursive (or maybe/hopefully there is some limit on the number of nestings?).

E.g. GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(LINESTRING(0 0, 1 1)),LINESTRING(2 2, 3 3))

theroggy avatar Dec 14 '23 07:12 theroggy

geometrycollections can be nested, so possibly the extraction needs to be recursive (or maybe/hopefully there is some limit on the number of nestings?).

Yes, this function needs to extract all elements from nested GeometryCollections.

dr-jts avatar Dec 18 '23 01:12 dr-jts