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 hexadecimal 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 ob = shapely.set_srid(ob, srid) kw["include_srid"] = True if "big_endian" in kw: # translate big_endian=True/False into byte_order=0/1 # but if not specified, keep the default of byte_order=-1 (native) big_endian = kw.pop("big_endian") byte_order = 0 if big_endian else 1 kw.update(byte_order=byte_order) return shapely.to_wkb(ob, hex=hex, **kw)
def _unpickle_linearring(wkb): linestring = shapely.from_wkb(wkb) srid = shapely.get_srid(linestring) linearring = shapely.linearrings(shapely.get_coordinates(linestring)) if srid: linearring = shapely.set_srid(linearring, srid) return linearring
def test_to_wkb_srid(): # hex representation of POINT (0 0) with SRID=4 ewkb = "01010000200400000000000000000000000000000000000000" wkb = "010100000000000000000000000000000000000000" actual = shapely.from_wkb(ewkb) assert shapely.to_wkt(actual, trim=True) == "POINT (0 0)" assert shapely.to_wkb(actual, hex=True, byte_order=1) == wkb assert shapely.to_wkb(actual, hex=True, include_srid=True, byte_order=1) == ewkb point = shapely.points(1, 1) point_with_srid = shapely.set_srid(point, np.int32(4326)) result = shapely.to_wkb(point_with_srid, include_srid=True, byte_order=1) assert np.frombuffer(result[5:9], "<u4").item() == 4326
def test_pickle_with_srid(geom): geom = shapely.set_srid(geom, 4326) pickled = pickle.dumps(geom) assert shapely.get_srid(pickle.loads(pickled)) == 4326
def test_to_wkb_point_empty_srid(): expected = shapely.set_srid(empty_point, 4236) wkb = shapely.to_wkb(expected, include_srid=True) actual = shapely.from_wkb(wkb) assert shapely.get_srid(actual) == 4236
def test_get_set_srid(): actual = shapely.set_srid(point, 4326) assert shapely.get_srid(point) == 0 assert shapely.get_srid(actual) == 4326