Discussion:
[Community] GeometryCollections with multiple polygons
Harasty, Daniel J
2012-02-29 11:47:20 UTC
Permalink
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
dharasty at appcomsci.com<mailto:dharasty at appcomsci.com>




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20120229/f7993ae2/attachment.htm>
Sean Gillies
2012-03-01 15:56:23 UTC
Permalink
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.
Post by Harasty, Daniel J
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
Post by Harasty, Daniel J
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
Post by Harasty, Daniel J
"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
Post by Harasty, Daniel J
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.
????????? 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
????????? 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
????????? Allow GeometryCollection() to be created with a list of initial
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
dharasty at appcomsci.com
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
--
Sean Gillies
Harasty, Daniel J
2012-03-05 16:44:52 UTC
Permalink
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....
"GEOMETRYCOLLECTION (%s)" % ", ".join(g.wkt for g in geometries)
Will that get you by?
Thanks! The second tip (doing the "GEOMETRYCOLLECTION".join([...])) was my "fallback plan".

I'll investigate the "intentional invalid Multipolygon" trick, too.

I do need to be able to read and reconstitute whatever serialization I choose (in Python with Shapely), so I'll have to make sure that Shapely can "round trip" either of those options. Also, I do have the additional requirements that whatever I create has to be readable/bufferable by our system's SQL server.

If either of the above work, then I'm good.

Thanks again,

Dan
Aron Bierbaum
2012-04-06 13:55:57 UTC
Permalink
We have run into the same issue before. I can completely see where Sean is
coming from when saying it is probably not a good idea to make
GeometryCollection's first class citizens. With that said, we ran into a
situation where we had no choice but to create the geometry collections.
The attached code is not pretty and is probably considered a complete hack,
but it accomplished what we needed.

-Aron

On Mon, Mar 5, 2012 at 10:44 AM, Harasty, Daniel J
Post by Sean Gillies
Post by Sean Gillies
I'm reluctant to make heterogeneous geometry collections first-class
objects in Shapely,
Post by Sean Gillies
mostly because GEOS (the lib under Shapely) generally disallows any
operations on them.
Post by Sean Gillies
For what it's worth, Shapely will happily write invalid multipolygons to
WKT....
Post by Sean Gillies
Thanks to the design of WKT, it's trivial to serialize a list of
"GEOMETRYCOLLECTION (%s)" % ", ".join(g.wkt for g in geometries)
Will that get you by?
Thanks! The second tip (doing the "GEOMETRYCOLLECTION".join([...])) was
my "fallback plan".
I'll investigate the "intentional invalid Multipolygon" trick, too.
I do need to be able to read and reconstitute whatever serialization I
choose (in Python with Shapely), so I'll have to make sure that Shapely can
"round trip" either of those options. Also, I do have the additional
requirements that whatever I create has to be readable/bufferable by our
system's SQL server.
If either of the above work, then I'm good.
Thanks again,
Dan
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20120406/ede03af3/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: create_geom_collection.py
Type: application/octet-stream
Size: 1070 bytes
Desc: not available
URL: <http://lists.gispython.org/pipermail/community/attachments/20120406/ede03af3/attachment.obj>
Loading...