def geos_multilinestring_from_py(ob): # ob must be either a MultiLineString, a sequence, or # array of sequences or arrays if isinstance(ob, MultiLineString): return geos_geom_from_py(ob) obs = getattr(ob, 'geoms', ob) L = len(obs) assert L >= 1 exemplar = obs[0] try: N = len(exemplar[0]) except TypeError: N = exemplar._ndim if N not in (2, 3): raise ValueError("Invalid coordinate dimensionality") # Array of pointers to point geometries subs = (c_void_p * L)() # add to coordinate sequence for l in range(L): geom, ndims = linestring.geos_linestring_from_py(obs[l]) if lgeos.GEOSisEmpty(geom): raise EmptyPartError( "Can't create MultiLineString with empty component") subs[l] = cast(geom, c_void_p) return (lgeos.GEOSGeom_createCollection(5, subs, L), N)
def geos_multipoint_from_py(ob): if isinstance(ob, MultiPoint): return geos_geom_from_py(ob) m = len(ob) try: n = len(ob[0]) except TypeError: n = ob[0]._ndim assert n == 2 or n == 3 # Array of pointers to point geometries subs = (c_void_p * m)() # add to coordinate sequence for i in range(m): coords = ob[i] geom, ndims = point.geos_point_from_py(coords) if lgeos.GEOSisEmpty(geom): raise EmptyPartError("Can't create MultiPoint with empty component") subs[i] = cast(geom, c_void_p) return lgeos.GEOSGeom_createCollection(4, subs, m), n
def polygonize_full(self, lines): """Creates polygons from a source of lines, returning the polygons and leftover geometries. The source may be a MultiLineString, a sequence of LineString objects, or a sequence of objects than can be adapted to LineStrings. Returns a tuple of objects: (polygons, dangles, cut edges, invalid ring lines). Each are a geometry collection. Dangles are edges which have one or both ends which are not incident on another edge endpoint. Cut edges are connected at both ends but do not form part of polygon. Invalid ring lines form rings which are invalid (bowties, etc). """ source = getattr(lines, 'geoms', None) or lines try: source = iter(source) except TypeError: source = [source] finally: obs = [self.shapeup(l) for l in source] L = len(obs) subs = (c_void_p * L)() for i, g in enumerate(obs): subs[i] = g._geom collection = lgeos.GEOSGeom_createCollection(5, subs, L) dangles = c_void_p() cuts = c_void_p() invalids = c_void_p() product = lgeos.GEOSPolygonize_full(collection, byref(dangles), byref(cuts), byref(invalids)) return (geom_factory(product), geom_factory(dangles), geom_factory(cuts), geom_factory(invalids))
def geos_multipolygon_from_polygons(ob): """ob must be either a sequence or array of sequences or arrays.""" obs = getattr(ob, 'geoms', None) or ob L = len(obs) assert L >= 1 exemplar = obs[0] try: N = len(exemplar[0][0]) except TypeError: N = exemplar._ndim assert N == 2 or N == 3 subs = (c_void_p * L)() for l in range(L): shell = getattr(obs[l], 'exterior', None) if shell is None: shell = obs[l][0] holes = getattr(obs[l], 'interiors', None) if holes is None: holes = obs[l][1] geom, ndims = geos_polygon_from_py(shell, holes) subs[l] = cast(geom, c_void_p) return (lgeos.GEOSGeom_createCollection(6, subs, L), N)
def cascaded_union(self, geoms): """Returns the union of a sequence of geometries This is the most efficient method of dissolving many polygons. """ L = len(geoms) subs = (c_void_p * L)() for i, g in enumerate(geoms): subs[i] = g._geom collection = lgeos.GEOSGeom_createCollection(6, subs, L) return geom_factory(lgeos.methods['cascaded_union'](collection))
def geos_multipolygon_from_polygons(arg): """Creates a GEOS multipolygon from a sequence of polygon-like objects. Parameters ---------- arg : sequence or MultiPolygon Returns ------- int Pointer to a GEOS multipolygon. """ if isinstance(arg, MultiPolygon): return geos_geom_from_py(arg) obs = getattr(arg, 'geoms', arg) obs = [ ob for ob in obs if ob and not (isinstance(ob, polygon.Polygon) and ob.is_empty) ] L = len(obs) # Bail immediately if we have no input points. if L <= 0: return (lgeos.GEOSGeom_createEmptyCollection(6), 3) # This function does not accept sequences of MultiPolygons: there is # no implicit flattening. if isinstance(obs[0], MultiPolygon): raise ValueError("Sequences of multi-polygons are not valid arguments") exemplar = obs[0] try: N = len(exemplar[0][0]) except TypeError: N = exemplar._ndim assert N == 2 or N == 3 subs = (c_void_p * L)() for i, ob in enumerate(obs): if isinstance(ob, polygon.Polygon): shell = ob.exterior holes = ob.interiors else: shell = ob[0] holes = ob[1] geom, ndims = polygon.geos_polygon_from_py(shell, holes) subs[i] = cast(geom, c_void_p) return (lgeos.GEOSGeom_createCollection(6, subs, L), N)
def unary_union(self, geoms): """Returns the union of a sequence of geometries This method replaces :meth:`cascaded_union` as the prefered method for dissolving many polygons. """ L = len(geoms) subs = (c_void_p * L)() for i, g in enumerate(geoms): subs[i] = g._geom collection = lgeos.GEOSGeom_createCollection(6, subs, L) return geom_factory(lgeos.methods['unary_union'](collection))
def geos_geometrycollection_from_py(ob): """Creates a GEOS GeometryCollection from a list of geometries""" L = len(ob) N = 2 subs = (c_void_p * L)() for l in range(L): assert (isinstance(ob[l], BaseGeometry)) if ob[l].has_z: N = 3 geom, n = geos_geom_from_py(ob[l]) subs[l] = geom return (lgeos.GEOSGeom_createCollection(7, subs, L), N)
def geos_multipolygon_from_py(ob): """ob must provide Python geo interface coordinates.""" L = len(ob) N = len(ob[0][0][0]) assert L >= 1 assert N == 2 or N == 3 subs = (c_void_p * L)() for l in xrange(L): geom, ndims = geos_polygon_from_py(ob[l][0], ob[l][1:]) subs[l] = cast(geom, c_void_p) return (lgeos.GEOSGeom_createCollection(6, subs, L), N)
def geos_multipoint_from_py(ob): if isinstance(ob, MultiPoint): return geos_geom_from_py(ob) # If numpy is present, we use numpy.require to ensure that we have a # C-continguous array that owns its data. View data will be copied. ob = required(ob) try: # From array protocol array = ob.__array_interface__ assert len(array['shape']) == 2 m = array['shape'][0] n = array['shape'][1] assert m >= 1 assert n == 2 or n == 3 # Make pointer to the coordinate array if isinstance(array['data'], tuple): # numpy tuple (addr, read-only) cp = cast(array['data'][0], POINTER(c_double)) else: cp = array['data'] # Array of pointers to sub-geometries subs = (c_void_p * m)() for i in range(m): geom, ndims = point.geos_point_from_py(cp[n * i:n * i + 2]) subs[i] = cast(geom, c_void_p) except AttributeError: # Fall back on list m = len(ob) try: n = len(ob[0]) except TypeError: n = ob[0]._ndim assert n == 2 or n == 3 # Array of pointers to point geometries subs = (c_void_p * m)() # add to coordinate sequence for i in range(m): coords = ob[i] geom, ndims = point.geos_point_from_py(coords) subs[i] = cast(geom, c_void_p) return lgeos.GEOSGeom_createCollection(4, subs, m), n
def cascaded_union(self, geoms): """Returns the union of a sequence of geometries This method was superseded by :meth:`unary_union`. """ try: L = len(geoms) except TypeError: geoms = [geoms] L = 1 subs = (c_void_p * L)() for i, g in enumerate(geoms): subs[i] = g._geom collection = lgeos.GEOSGeom_createCollection(6, subs, L) return geom_factory(lgeos.methods['cascaded_union'](collection))
def unary_union(self, geoms): """Returns the union of a sequence of geometries This method replaces :meth:`cascaded_union` as the prefered method for dissolving many polygons. """ try: if isinstance(geoms, BaseMultipartGeometry): geoms = geoms.geoms L = len(geoms) except TypeError: geoms = [geoms] L = 1 subs = (c_void_p * L)() for i, g in enumerate(geoms): subs[i] = g._geom collection = lgeos.GEOSGeom_createCollection(6, subs, L) return geom_factory(lgeos.methods['unary_union'](collection))
def geos_multipolygon_from_polygons(arg): """ ob must be either a MultiPolygon, sequence or array of sequences or arrays. """ if isinstance(arg, MultiPolygon): return geos_geom_from_py(arg) obs = getattr(arg, 'geoms', arg) obs = [ ob for ob in obs if ob and not (isinstance(ob, polygon.Polygon) and ob.is_empty) ] L = len(obs) # Bail immediately if we have no input points. if L <= 0: return (lgeos.GEOSGeom_createEmptyCollection(6), 3) exemplar = obs[0] try: N = len(exemplar[0][0]) except TypeError: N = exemplar._ndim assert N == 2 or N == 3 subs = (c_void_p * L)() for i, ob in enumerate(obs): if isinstance(ob, polygon.Polygon): shell = ob.exterior holes = ob.interiors else: shell = ob[0] holes = ob[1] geom, ndims = polygon.geos_polygon_from_py(shell, holes) subs[i] = cast(geom, c_void_p) return (lgeos.GEOSGeom_createCollection(6, subs, L), N)
def geos_multipoint_from_py(ob): try: # From array protocol array = ob.__array_interface__ assert len(array['shape']) == 2 m = array['shape'][0] n = array['shape'][1] assert m >= 1 assert n == 2 or n == 3 # Make pointer to the coordinate array try: cp = cast(array['data'][0], POINTER(c_double)) except ArgumentError: cp = array['data'] # Array of pointers to sub-geometries subs = (c_void_p * m)() for i in xrange(m): geom, ndims = geos_point_from_py(cp[n * i:n * i + 2]) subs[i] = cast(geom, c_void_p) except AttributeError: # Fall back on list m = len(ob) try: n = len(ob[0]) except TypeError: n = ob[0]._ndim assert n == 2 or n == 3 # Array of pointers to point geometries subs = (c_void_p * m)() # add to coordinate sequence for i in xrange(m): coords = ob[i] geom, ndims = geos_point_from_py(coords) subs[i] = cast(geom, c_void_p) return lgeos.GEOSGeom_createCollection(4, subs, m), n
def geos_multilinestring_from_py(ob): # ob must be either a MultiLineString, a sequence, or # array of sequences or arrays if isinstance(ob, MultiLineString): return geos_geom_from_py(ob) try: # From array protocol array = ob.__array_interface__ assert len(array['shape']) == 1 L = array['shape'][0] assert L >= 1 # Array of pointers to sub-geometries subs = (c_void_p * L)() for l in range(L): geom, ndims = linestring.geos_linestring_from_py(array['data'][l]) subs[i] = cast(geom, c_void_p) N = lgeos.GEOSGeom_getCoordinateDimension(subs[0]) except (NotImplementedError, AttributeError): obs = getattr(ob, 'geoms', ob) L = len(obs) exemplar = obs[0] try: N = len(exemplar[0]) except TypeError: N = exemplar._ndim assert L >= 1 assert N == 2 or N == 3 # Array of pointers to point geometries subs = (c_void_p * L)() # add to coordinate sequence for l in range(L): geom, ndims = linestring.geos_linestring_from_py(obs[l]) subs[l] = cast(geom, c_void_p) return (lgeos.GEOSGeom_createCollection(5, subs, L), N)
def geos_multilinestring_from_py(ob): # ob must be either a sequence or array of sequences or arrays try: # From array protocol array = ob.__array_interface__ assert len(array['shape']) == 1 L = array['shape'][0] assert L >= 1 # Make pointer to the coordinate array cp = cast(array['data'][0], POINTER(c_double)) # Array of pointers to sub-geometries subs = (c_void_p * L)() for l in xrange(L): geom, ndims = geos_linestring_from_py(array['data'][l]) subs[i] = cast(geom, c_void_p) N = lgeos.GEOSGeom_getDimensions(subs[0]) except (NotImplementedError, AttributeError): obs = getattr(ob, 'geoms', ob) L = len(obs) exemplar = obs[0] try: N = len(exemplar[0]) except TypeError: N = exemplar._ndim assert L >= 1 assert N == 2 or N == 3 # Array of pointers to point geometries subs = (c_void_p * L)() # add to coordinate sequence for l in xrange(L): geom, ndims = geos_linestring_from_py(obs[l]) subs[l] = cast(geom, c_void_p) return (lgeos.GEOSGeom_createCollection(5, subs, L), N)
def cascaded_union(self, geoms): """Returns the union of a sequence of geometries This function is deprecated, as it was superseded by :meth:`unary_union`. """ warn( "The 'cascaded_union()' function is deprecated. " "Use 'unary_union()' instead.", ShapelyDeprecationWarning, stacklevel=2) try: if isinstance(geoms, BaseMultipartGeometry): geoms = geoms.geoms L = len(geoms) except TypeError: geoms = [geoms] L = 1 subs = (c_void_p * L)() for i, g in enumerate(geoms): subs[i] = g._geom collection = lgeos.GEOSGeom_createCollection(6, subs, L) return geom_factory(lgeos.methods['cascaded_union'](collection))