Skip to content

Add gdal vector combine, gdal vector dissolve (round 2) - #13985

Merged
rouault merged 7 commits into
OSGeo:masterfrom
dbaston:collect-dissolve
Feb 23, 2026
Merged

rouault merged 7 commits into
OSGeo:masterfrom
dbaston:collect-dissolve

Conversation

@dbaston

@dbaston dbaston commented Feb 21, 2026 •

Copy link
Copy Markdown
Member

This PR resurrects #13330 but makes some significant changes.

gdal vector combine

For gdal vector collect, the default behavior is now to combine input multipart geometries rather than putting them into a nested collection. For example, POINT (3 8) and MULTIPOINT (2 6) become MULTIPOINT (3 8, 2 6) rather than the PostGIS result of

# select st_astext(st_collect('point (3 8)', 'multipoint ((2 1))'));
                    st_astext                     
--------------------------------------------------
 GEOMETRYCOLLECTION(POINT(3 8),MULTIPOINT((2 1)))

The behavior change is motivated by usability -- without the change, for example, I cannot run:

gdal vector pipeline ! read ne_10m_admin_0_countries.shp ! combine --group-by CONTINENT ! dissolve !  write /tmp/out.shp --overwrite

because Shapefile does not support GeometryCollections. If I instead write to a GeoPackage the pipeline completes but the result does not display in QGIS.

A flag (--keep-nested) is added to get PostGIS-style nested output. The name could probably be improved.

gdal vector dissolve

The previously-discussed behavior about running "LineMerge" after "Union" is implemented.

Side notes

I don't love the use of OGRGeometry::Clone() in GDALVectorCollectOutputLayer. It would be nice to figure out a pattern where GetNextFeature can return a moved feature in a context where the layer will only be consumed once. Maybe it's not worth the complexity though.

Even though it is very simple, the implementation of OGRGeometryCollection::addGeometryComponents felt painful with the manual management of papoGeoms. I think it is worth replacing this and other similar cases with either a std::vector (just wrap methods that modify it in a try/catch for std::bad_alloc ?) or adding a CPLVector template that wraps std::vector and converts the exceptions to error codes. In addition to being easier on the eyes this would remove the inefficient pattern of N reallocations to add N elements to a container. Curious if there's any input on that.

@dbaston dbaston added the gdal_cli Anything related to the new 3.11 "gdal" CLI frontend label Feb 21, 2026
Comment thread apps/gdalalg_vector_combine.cpp Resolved
Comment thread apps/gdalalg_vector_collect.cpp Resolved
Comment thread apps/gdalalg_vector_collect.cpp Resolved
Comment thread apps/gdalalg_vector_collect.cpp Resolved
Comment thread apps/gdalalg_vector_collect.cpp Resolved
Comment thread apps/gdalalg_vector_dissolve.cpp Resolved
Comment thread apps/gdalalg_vector_dissolve.cpp Resolved
@rouault

rouault commented Feb 21, 2026

Copy link
Copy Markdown
Member

I don't love the use of OGRGeometry::Clone() in GDALVectorCollectOutputLayer. It would be nice to figure out a pattern where GetNextFeature can return a moved feature in a context where the layer will only be consumed once. Maybe it's not worth the complexity though.

didn't you mean OGRFeature::Clone() at https://github.com/OSGeo/gdal/pull/13985/changes#diff-797ee50b80770e22f9baa077b51445205e94c696b84d9ad34e791f7463a4dae3R168 (can't see a OGRGeometry::Clone() use) . As far as I can see the GetNextProcessedFeature() method can be called several times on the same source feature in case of multiple iteration loops.

I think it is worth replacing this and other similar cases with either a std::vector (just wrap methods that modify it in a try/catch for std::bad_alloc ?) or adding a CPLVector template that wraps std::vector and converts the exceptions to error codes.

yes, could be worth removing that raw pointer array. I'm undecided if we need a CPLVector, and which pattern it should follow: similar to CPLString that inherits from std::string (but that causes some pains on Windows as it is CPL_DLL because it re-exports std::string symbols), or using a decorator/proxy design pattern, that is have a std::vector member variable and mimic std::vector API. Probably the later

@msmitherdc

Copy link
Copy Markdown
Member

I like combine better than collect

@rouault

rouault commented Feb 22, 2026

Copy link
Copy Markdown
Member

combine sounds good

@jratike80

Copy link
Copy Markdown
Collaborator

Combine feels good.

Maybe worth mentioning that --keep-nestedoption is reversible and it is possible to extract the source geometries unaltered from the result, that is generally not possible with the default option. Not sure if reversibillity is true when some complex GeometryCollections are combined, though.

@rouault rouault added this to the 3.13.0 milestone Feb 23, 2026
Comment thread doc/source/conf.py Resolved
@dbaston

dbaston commented Feb 23, 2026

Copy link
Copy Markdown
Member Author

As far as I can see the GetNextProcessedFeature() method can be called several times on the same source feature in case of multiple iteration loops.

Yes, I was wondering if we could identify contexts where there is guaranteed to be only one iteration loop (intermediate step of a pipeline?) and steal the constructed feature. But a simpler approach is probably to rework this (later) such that GetNextProcessedFeature always moves the returned feature, and ResetReading causes the source (uncombined) features to be re-read. Then just make sure that iteration is only performed once in an intermediate pipeline step with proper implementation of GetFeatureCount etc.

@rouault rouault changed the title Add gdal vector collect, gdal vector dissolve (round 2) Add gdal vector combine, gdal vector dissolve (round 2) Feb 23, 2026
@rouault

rouault commented Feb 23, 2026

Copy link
Copy Markdown
Member

I was wondering if we could identify contexts where there is guaranteed to be only one iteration loop (intermediate step of a pipeline?)

that guarantee only holds if the combine step is followed by things like another non-streaming algorithm, tee or write. In other scenarios, the user could potentially rewind the layer. We cannot also exclude that other algorithms down the stream may rewind the stream, but there would be non-streaming algorithms

@rouault
rouault merged commit d494448 into OSGeo:master Feb 23, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gdal_cli Anything related to the new 3.11 "gdal" CLI frontend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants