11"""Load/dump geometries using the well-known binary (WKB) format
22"""
33
4- from shapely import geos
4+ from shapely .geos import WKBReader , WKBWriter , lgeos
5+ from shapely .geometry .base import geom_factory
56
67# Pickle-like convenience functions
78
89def loads (data , hex = False ):
910 """Load a geometry from a WKB byte string, or hex-encoded string if
1011 ``hex=True``.
1112 """
12- reader = geos . WKBReader (geos . lgeos )
13+ reader = WKBReader (lgeos )
1314 if hex :
1415 return reader .read_hex (data )
1516 else :
@@ -20,12 +21,29 @@ def load(fp, hex=False):
2021 data = fp .read ()
2122 return loads (data , hex = hex )
2223
23- def dumps (ob , hex = False , ** kw ):
24+ def dumps (ob , hex = False , srid = None , ** kw ):
2425 """Dump a WKB representation of a geometry to a byte string, or a
2526 hex-encoded string if ``hex=True``.
26-
27- See available keyword output settings in ``shapely.geos.WKBWriter``."""
28- writer = geos .WKBWriter (geos .lgeos , ** kw )
27+
28+ Parameters
29+ ----------
30+ ob : geometry
31+ The geometry to export to well-known binary (WKB) representation
32+ hex : bool
33+ If true, export the WKB as a hexidecimal string. The default is to
34+ return a binary string/bytes object.
35+ srid : int
36+ Spatial reference system ID to include in the output. The default value
37+ means no SRID is included.
38+ **kw : kwargs
39+ See available keyword output settings in ``shapely.geos.WKBWriter``."""
40+ if srid is not None :
41+ # clone the object and set the SRID before dumping
42+ geom = lgeos .GEOSGeom_clone (ob ._geom )
43+ lgeos .GEOSSetSRID (geom , srid )
44+ ob = geom_factory (geom )
45+ kw ["include_srid" ] = True
46+ writer = WKBWriter (lgeos , ** kw )
2947 if hex :
3048 return writer .write_hex (ob )
3149 else :
0 commit comments