def test_to_wkb_3D(): point_z = shapely.points(1, 1, 1) actual = shapely.to_wkb(point_z, byte_order=1) # fmt: off assert actual == b"\x01\x01\x00\x00\x80\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?" # noqa # fmt: on actual = shapely.to_wkb(point_z, output_dimension=2, byte_order=1) assert actual == POINT11_WKB
def test_to_wkb_byte_order(): point = shapely.points(1.0, 1.0) be = b"\x00" le = b"\x01" point_type = b"\x01\x00\x00\x00" # 1 as 32-bit uint (LE) coord = b"\x00\x00\x00\x00\x00\x00\xf0?" # 1.0 as double (LE) assert shapely.to_wkb(point, byte_order=1) == le + point_type + 2 * coord assert (shapely.to_wkb(point, byte_order=0) == be + point_type[::-1] + 2 * coord[::-1])
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_to_wkb_hex(): point = shapely.points(1, 1) actual = shapely.to_wkb(point, hex=True, byte_order=1) le = "01" point_type = "01000000" coord = "000000000000F03F" # 1.0 as double (LE) assert actual == le + point_type + 2 * coord
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 test_to_wkb_point_empty_2d_output_dim_3(geom, expected): actual = shapely.to_wkb(geom, output_dimension=3, byte_order=1) # Split 'actual' into header and coordinates coordinate_length = 16 header_length = len(expected) - coordinate_length # Check the total length (this checks the correct dimensionality) assert len(actual) == header_length + coordinate_length # Check the header assert actual[:header_length] == expected[:header_length] # Check the coordinates (using numpy.isnan; there are many byte representations for NaN) assert np.isnan(struct.unpack("<2d", actual[header_length:])).all()
def wkb(self): return shapely.to_wkb(self.g)
def test_to_wkb_exceptions(): with pytest.raises(TypeError): shapely.to_wkb(1) with pytest.raises(shapely.GEOSException): shapely.to_wkb(point, output_dimension=4)
def test_to_wkb_none(): # None propagates assert shapely.to_wkb(None) is None
def wkb(self): """WKB representation of the geometry""" return shapely.to_wkb(self)
def test_to_wkb(): point = shapely.points(1, 1) actual = shapely.to_wkb(point, byte_order=1) assert actual == POINT11_WKB
def test_from_wkb_empty(wkt): wkb = shapely.to_wkb(shapely.Geometry(wkt)) geom = shapely.from_wkb(wkb) assert shapely.is_geometry(geom).all() assert shapely.is_empty(geom).all() assert shapely.to_wkb(geom) == wkb
def __reduce__(self): return (shapely.from_wkb, (shapely.to_wkb(self, include_srid=True), ))
def time_write_to_wkb(self): shapely.to_wkb(self.to_write)
def setup(self): self.to_write = shapely.polygons(np.random.random((10000, 100, 2))) self.to_read_wkt = shapely.to_wkt(self.to_write) self.to_read_wkb = shapely.to_wkb(self.to_write)
def __reduce__(self): """WKB doesn't differentiate between LineString and LinearRing so we need to move the coordinate sequence into the correct geometry type""" return (_unpickle_linearring, (shapely.to_wkb(self, include_srid=True), ))
def wkb_hex(self): """WKB hex representation of the geometry""" return shapely.to_wkb(self, hex=True)
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_from_wkb_all_types(geom, use_hex, byte_order): if shapely.get_type_id(geom) == shapely.GeometryType.LINEARRING: pytest.skip("Linearrings are not preserved in WKB") wkb = shapely.to_wkb(geom, hex=use_hex, byte_order=byte_order) actual = shapely.from_wkb(wkb) assert_geometries_equal(actual, geom)