Hi Dan, I'm reluctant to make heterogeneous geometry collections first-class objects in Shapely, mostly because GEOS (the lib under Shapely) generally disallows any operations on them.
For what it's worth, Shapely will happily write invalid multipolygons to WKT. >>> from shapely.geometry import MultiPolygon >>> from shapely.geometry import box >>> m = MultiPolygon([box(0, 0, 2, 2), box(1, 1, 3, 3)]) >>> m.is_valid False >>> m.wkt 'MULTIPOLYGON (((2.0000000000000000 0.0000000000000000, 2.0000000000000000 2.0000000000000000, 0.0000000000000000 2.0000000000000000, 0.0000000000000000 0.0000000000000000, 2.0000000000000000 0.0000000000000000)), ((3.0000000000000000 1.0000000000000000, 3.0000000000000000 3.0000000000000000, 1.0000000000000000 3.0000000000000000, 1.0000000000000000 1.0000000000000000, 3.0000000000000000 1.0000000000000000)))' Thanks to the design of WKT, it's trivial to serialize a list of geometries to WKT like this: >>> "GEOMETRYCOLLECTION (%s)" % ", ".join(g.wkt for g in geometries) 'GEOMETRYCOLLECTION (...)' Will that get you by? On Wed, Feb 29, 2012 at 4:47 AM, Harasty, Daniel J <[email protected]> wrote: > Hi, all. > > > > I’m an experienced Python user, and pretty comfortable with GIS concepts, > too. I’m using Shapely for several tasks; a hearty “thank you” to Sean > Gillies and the team that develops and supports that library! > > > > In my current application, I’m using Shapely mainly for its ability to > marshal to/from WKT. > > > > I’m having a bit of trouble using Shapely for one task. I want to: > > · Put a few (potentially overlapping) polygons in a single Shapely > object > > · I want the collection to preserve the “winding” of the polygons > that I have carefully set. > > > > A “MultiPolygon” won’t work for me since my constituent polygons may > overlap. > > > > A “GeometryCollection” seems like the right object, but here are my two > difficulties as I’m trying to use it: > > · It doesn’t seem to have a way to create it directly with > constituents of my choice > > o It only “comes about” after certain operations, like union() and > cascaded_union() > > o But I don’t want a union – it would merge my polygons’ borders. I wan’t > them to remain distinct. > > · Plus, the union() operations don’t necessarily preserve the > “winding” that I have set. > > > > Can anyone think of a way to use the current API to accomplish what I want? > > > > If Shapely would require an update to support this new feature, what do you > think a good API would be for it? I can think of two candidates; either: > > · Allow GeometryCollection() to be created with a list of initial > constituents: > > o poly1 = Polygon(….); poly2 = Polygon(….); gcoll = > GeometryCollection([poly1, poly2]) > > · Introduce a new operation like “.add()” or “.superpose()” which > adds another thing to the collection without performing a “union()” > operation. > > o gcoll = GeometryCollection(); gcoll = gcoll.add(poly1); gcoll = > gcoll.add(poly2) > > > > Either approach also solves something else on my “wish list”: I’d also > sometimes like to collect a bunch of points in a GeometryCollection() -- > even if they would “fit” just fine in a MultiPoint() object. > > > > Thanks for your input and consideration. > > > > -- Dan Harasty > > [email protected] > > > > > > > > > > > _______________________________________________ > Community mailing list > [email protected] > http://lists.gispython.org/mailman/listinfo/community > -- Sean Gillies _______________________________________________ Community mailing list [email protected] http://lists.gispython.org/mailman/listinfo/community
