| 20 | | def polygonize(iterator): |
| 21 | | """Creates polygons from a list of LineString objects. |
| 22 | | """ |
| 23 | | lines = [shapeup(ob) for ob in iterator] |
| 24 | | geom_array_type = c_void_p * len(lines) |
| 25 | | geom_array = geom_array_type() |
| 26 | | for i, line in enumerate(lines): |
| 27 | | geom_array[i] = line._geom |
| 28 | | product = lgeos.GEOSPolygonize(byref(geom_array), len(lines)) |
| 29 | | collection = geom_factory(product) |
| 30 | | for g in collection.geoms: |
| 31 | | clone = lgeos.GEOSGeom_clone(g._geom) |
| 32 | | g = geom_factory(clone) |
| 33 | | g._owned = False |
| 34 | | yield g |
| | 12 | class CollectionOperator(object): |
| 40 | | Parameters |
| 41 | | ---------- |
| 42 | | lines : MultiLineString, sequence of lines, or sequence of coordinates |
| 43 | | """ |
| 44 | | multilinestring = None |
| 45 | | if hasattr(lines, 'type') and lines.type == 'MultiLineString': |
| 46 | | multilinestring = lines |
| 47 | | elif hasattr(lines, '__iter__'): |
| 48 | | try: |
| 49 | | multilinestring = asMultiLineString([ls.coords for ls in lines]) |
| 50 | | except AttributeError: |
| 51 | | multilinestring = asMultiLineString(lines) |
| 52 | | if multilinestring is None: |
| 53 | | raise ValueError("Cannot linemerge %s" % lines) |
| 54 | | result = lgeos.GEOSLineMerge(multilinestring._geom) |
| 55 | | return geom_factory(result) |
| | 26 | The source may be a MultiLineString, a sequence of LineString objects, |
| | 27 | or a sequence of objects than can be adapted to LineStrings. |
| | 28 | """ |
| | 29 | source = getattr(lines, 'geoms', None) or lines |
| | 30 | obs = [self.shapeup(l) for l in source] |
| | 31 | geom_array_type = c_void_p * len(obs) |
| | 32 | geom_array = geom_array_type() |
| | 33 | for i, line in enumerate(obs): |
| | 34 | geom_array[i] = line._geom |
| | 35 | product = lgeos.GEOSPolygonize(byref(geom_array), len(obs)) |
| | 36 | collection = geom_factory(product) |
| | 37 | for g in collection.geoms: |
| | 38 | clone = lgeos.GEOSGeom_clone(g._geom) |
| | 39 | g = geom_factory(clone) |
| | 40 | g._owned = False |
| | 41 | yield g |
| 57 | | def cascaded_union(geoms): |
| 58 | | """Returns the union of a sequence of geometries |
| 59 | | |
| 60 | | This is the most efficient method of dissolving many polygons. |
| 61 | | """ |
| 62 | | L = len(geoms) |
| 63 | | subs = (c_void_p * L)() |
| 64 | | for i, g in enumerate(geoms): |
| 65 | | subs[i] = g._geom |
| 66 | | collection = lgeos.GEOSGeom_createCollection(6, subs, L) |
| 67 | | return geom_factory(lgeos.GEOSUnionCascaded(collection)) |
| | 43 | def linemerge(self, lines): |
| | 44 | """Merges all connected lines from a source |
| | 45 | |
| | 46 | The source may be a MultiLineString, a sequence of LineString objects, |
| | 47 | or a sequence of objects than can be adapted to LineStrings. Returns a |
| | 48 | LineString or MultiLineString when lines are not contiguous. |
| | 49 | """ |
| | 50 | source = None |
| | 51 | if hasattr(lines, 'type') and lines.type == 'MultiLineString': |
| | 52 | source = lines |
| | 53 | elif hasattr(lines, '__iter__'): |
| | 54 | try: |
| | 55 | source = asMultiLineString([ls.coords for ls in lines]) |
| | 56 | except AttributeError: |
| | 57 | source = asMultiLineString(lines) |
| | 58 | if source is None: |
| | 59 | raise ValueError("Cannot linemerge %s" % lines) |
| | 60 | result = lgeos.GEOSLineMerge(source._geom) |
| | 61 | return geom_factory(result) |
| | 63 | def cascaded_union(geoms): |
| | 64 | """Returns the union of a sequence of geometries |
| | 65 | |
| | 66 | This is the most efficient method of dissolving many polygons. |
| | 67 | """ |
| | 68 | L = len(geoms) |
| | 69 | subs = (c_void_p * L)() |
| | 70 | for i, g in enumerate(geoms): |
| | 71 | subs[i] = g._geom |
| | 72 | collection = lgeos.GEOSGeom_createCollection(6, subs, L) |
| | 73 | return geom_factory(lgeos.GEOSUnionCascaded(collection)) |
| | 74 | |
| | 75 | operator = CollectionOperator() |
| | 76 | polygonize = operator.polygonize |
| | 77 | linemerge = operator.linemerge |
| | 78 | cascaded_union = operator.cascaded_union |
| | 79 | |