[gdal-dev] Adding multipolygons to an existing geometry collection

Frank Warmerdam warmerdam at pobox.com
Wed Mar 24 15:07:48 EDT 2010

danotchki wrote:
> Hello List,
> I hope you will be able to help me. I'm stuck on a problem.
> I'm trying to reconstruct within my Python script polygons from a 
> shapefile that belong together.
> So I naturally perfomed:
> geom_tmp = ogr.CreateGeometryFromWkb(ftr.GetGeometryRef().ExportToWkb())
> geom.AddGeometryDirectly(geom_tmp)
> However this works only when the geometry to add is of type POLYGON. 
> This _doesn't_ work with type MULTIPOLYGON (exit code 3).


What type is "geom"?  If it is a MULTIPOLYGON then it can only
have polygons as children, not multipolygons.

> My question: is there a way to add MULTIPOLYGONs to an existing Geometry?
> What I don't understand is that when CreateGeometryFromWkb returns a 
> MULTIPOLYGON it actually returns a GEOMETRY instead of a 
> GEOMETRYCOLLECTION, thus preventing looping on each individual geometry 
> with GetNumGeometries. Would there be a way to cast the polygon to a 
> multipolygon?

In C/C++ there are methods on OGRGeometryFactory like
forceToPolygon and forceToMultiPolygon that might be helpful
but I see these are not exposed in Python.  Are you willing to
build from SVN trunk source code?  If so, I could add these methods
for you.

Best regards,
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

More information about the gdal-dev mailing list