Skip to content

Commit 89bd078

Browse files
Added srid keyword to wkb.dumps. Closes #592.
1 parent ad53583 commit 89bd078

2 files changed

Lines changed: 75 additions & 6 deletions

File tree

shapely/wkb.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
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

89
def 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:

tests/test_wkb.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from shapely.wkb import dumps, loads
2+
from shapely.geometry import Point
3+
import binascii
4+
5+
6+
def bin2hex(value):
7+
return binascii.b2a_hex(value).upper().decode("utf-8")
8+
9+
10+
def hex2bin(value):
11+
return binascii.a2b_hex(value)
12+
13+
14+
def test_dumps_srid():
15+
p1 = Point(1.2, 3.4)
16+
result = dumps(p1)
17+
assert bin2hex(result) == "0101000000333333333333F33F3333333333330B40"
18+
result = dumps(p1, srid=4326)
19+
assert bin2hex(result) == "0101000020E6100000333333333333F33F3333333333330B40"
20+
21+
22+
def test_dumps_endianness():
23+
p1 = Point(1.2, 3.4)
24+
result = dumps(p1)
25+
assert bin2hex(result) == "0101000000333333333333F33F3333333333330B40"
26+
result = dumps(p1, big_endian=False)
27+
assert bin2hex(result) == "0101000000333333333333F33F3333333333330B40"
28+
result = dumps(p1, big_endian=True)
29+
assert bin2hex(result) == "00000000013FF3333333333333400B333333333333"
30+
31+
32+
def test_dumps_hex():
33+
p1 = Point(1.2, 3.4)
34+
result = dumps(p1, hex=True)
35+
assert result == "0101000000333333333333F33F3333333333330B40"
36+
37+
38+
def test_loads_srid():
39+
# load a geometry which includes an srid
40+
geom = loads(hex2bin("0101000020E6100000333333333333F33F3333333333330B40"))
41+
assert isinstance(geom, Point)
42+
assert geom.coords[:] == [(1.2, 3.4)]
43+
# by default srid is not exported
44+
result = dumps(geom)
45+
assert bin2hex(result) == "0101000000333333333333F33F3333333333330B40"
46+
# include the srid in the output
47+
result = dumps(geom, include_srid=True)
48+
assert bin2hex(result) == "0101000020E6100000333333333333F33F3333333333330B40"
49+
# replace geometry srid with another
50+
result = dumps(geom, srid=27700)
51+
assert bin2hex(result) == "0101000020346C0000333333333333F33F3333333333330B40"

0 commit comments

Comments
 (0)