def dumps(ob, hex=False, srid=None, **kw): """Dump a WKB representation of a geometry to a byte string, or a hex-encoded string if ``hex=True``. Parameters ---------- ob : geometry The geometry to export to well-known binary (WKB) representation hex : bool If true, export the WKB as a hexidecimal string. The default is to return a binary string/bytes object. srid : int Spatial reference system ID to include in the output. The default value means no SRID is included. **kw : kwargs See available keyword output settings in ``shapely.geos.WKBWriter``.""" if srid is not None: # clone the object and set the SRID before dumping geom = lgeos.GEOSGeom_clone(ob._geom) lgeos.GEOSSetSRID(geom, srid) ob = geom_factory(geom) kw["include_srid"] = True writer = WKBWriter(lgeos, **kw) if hex: return writer.write_hex(ob) else: return writer.write(ob)
def _shapely_normalize(geom): """ Small helper function for now because it is not yet available in Shapely. """ from shapely.geos import lgeos from shapely.geometry.base import geom_factory from ctypes import c_void_p, c_int lgeos._lgeos.GEOSNormalize_r.restype = c_int lgeos._lgeos.GEOSNormalize_r.argtypes = [c_void_p, c_void_p] geom_cloned = lgeos.GEOSGeom_clone(geom._geom) lgeos._lgeos.GEOSNormalize_r(lgeos.geos_handle, geom_cloned) return geom_factory(geom_cloned)
def geos_geom_from_py(ob, create_func=None): """Helper function for geos_*_from_py functions in each geom type. If a create_func is specified the coodinate sequence is cloned and a new geometry is created with it, otherwise the geometry is cloned directly. This behaviour is useful for converting between LineString and LinearRing objects. """ if create_func is None: geom = lgeos.GEOSGeom_clone(ob._geom) else: cs = lgeos.GEOSGeom_getCoordSeq(ob._geom) cs = lgeos.GEOSCoordSeq_clone(cs) geom = create_func(cs) N = lgeos.GEOSGeom_getCoordinateDimension(geom) return geom, N
def polygonize(self, lines): """Creates polygons from a source of lines The source may be a MultiLineString, a sequence of LineString objects, or a sequence of objects than can be adapted to LineStrings. """ source = getattr(lines, 'geoms', None) or lines obs = [self.shapeup(l) for l in source] geom_array_type = c_void_p * len(obs) geom_array = geom_array_type() for i, line in enumerate(obs): geom_array[i] = line._geom product = lgeos.GEOSPolygonize(byref(geom_array), len(obs)) collection = geom_factory(product) for g in collection.geoms: clone = lgeos.GEOSGeom_clone(g._geom) g = geom_factory(clone) g._owned = False yield g
def to_shapely(geometry): """ Converts PyGEOS geometries to Shapely. Parameters ---------- geometry : shapely Geometry object or array_like Examples -------- >>> to_shapely(Geometry("POINT (1 1)")) # doctest: +SKIP <shapely.geometry.point.Point at 0x7f0c3d737908> Notes ----- If PyGEOS and Shapely do not use the same GEOS version, the conversion happens through the WKB format and will thus be slower. """ check_shapely_version() if shapely_compatible is None: raise ImportError("This function requires shapely") unpack = geometry is None or isinstance(geometry, Geometry) if unpack: geometry = (geometry, ) if shapely_compatible: geometry = [ None if g is None else shapely_geom_factory( shapely_lgeos.GEOSGeom_clone(g._ptr)) for g in geometry ] else: geometry = to_wkb(geometry) geometry = [ None if g is None else shapely_wkb_loads(g) for g in geometry ] if unpack: return geometry[0] else: arr = np.empty(len(geometry), dtype=object) arr[:] = geometry return arr
def normalize(self): """Converts geometry to normal form (or canonical form). This method orders the coordinates, rings of a polygon and parts of multi geometries consistently. Typically useful for testing purposes (for example in combination with `equals_exact`). Examples -------- >>> from shapely.wkt import loads >>> p = loads("MULTILINESTRING((0 0, 1 1), (3 3, 2 2))") >>> p.normalize().wkt 'MULTILINESTRING ((2 2, 3 3), (0 0, 1 1))' """ # self.impl['normalize'](self) if self._geom is None: raise ValueError("Null geometry supports no operations") geom_cloned = lgeos.GEOSGeom_clone(self._geom) lgeos.GEOSNormalize(geom_cloned) return geom_factory(geom_cloned)