def test_from_wkb_hex(): # HEX form expected = shapely.points(1, 1) actual = shapely.from_wkb("0101000000000000000000F03F000000000000F03F") assert_geometries_equal(actual, expected) actual = shapely.from_wkb(b"0101000000000000000000F03F000000000000F03F") assert_geometries_equal(actual, expected)
def test_from_wkb_warn_on_invalid_warn(): # invalid WKB with pytest.warns(Warning, match="Invalid WKB"): result = shapely.from_wkb(b"\x01\x01\x00\x00\x00\x00", on_invalid="warn") assert result is None # invalid ring in WKB with pytest.warns(Warning, match="Invalid WKB"): result = shapely.from_wkb(INVALID_WKB, on_invalid="warn") assert result is None
def test_from_wkb_ignore_on_invalid_ignore(): # invalid WKB with pytest.warns(None) as w: result = shapely.from_wkb(b"\x01\x01\x00\x00\x00\x00", on_invalid="ignore") assert result is None assert len(w) == 0 # no warning # invalid ring in WKB with pytest.warns(None) as w: result = shapely.from_wkb(INVALID_WKB, on_invalid="ignore") assert result is None assert len(w) == 0 # no warning
def test_from_wkb_ignore_on_invalid_ignore(): # invalid WKB with warnings.catch_warnings(): warnings.simplefilter("error") # no warning result = shapely.from_wkb(b"\x01\x01\x00\x00\x00\x00", on_invalid="ignore") assert result is None # invalid ring in WKB with warnings.catch_warnings(): warnings.simplefilter("error") # no warning result = shapely.from_wkb(INVALID_WKB, on_invalid="ignore") assert result is None
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_from_wkb_exceptions(): with pytest.raises(TypeError, match="Expected bytes or string, got int"): shapely.from_wkb(1) # invalid WKB with pytest.raises(shapely.GEOSException, match="Unexpected EOF parsing WKB"): result = shapely.from_wkb(b"\x01\x01\x00\x00\x00\x00") assert result is None # invalid ring in WKB with pytest.raises( shapely.GEOSException, match="Points of LinearRing do not form a closed linestring", ): result = shapely.from_wkb(INVALID_WKB) assert result is None
def test_from_wkb_point_empty(wkb, expected_type, expected_dim): geom = shapely.from_wkb(wkb) # POINT (nan nan) transforms to an empty point assert shapely.is_empty(geom) assert shapely.get_type_id(geom) == expected_type # The dimensionality (2D/3D) is only read correctly for GEOS >= 3.9.0 if shapely.geos_version >= (3, 9, 0): assert shapely.get_coordinate_dimension(geom) == expected_dim
def loads(data, hex=False): """Load a geometry from a WKB byte string, or hex-encoded string if ``hex=True``. Raises ------ WKBReadingError, UnicodeDecodeError If ``data`` contains an invalid geometry. """ return shapely.from_wkb(data)
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_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 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)
def test_from_wkb_on_invalid_unsupported_option(): with pytest.raises(ValueError, match="not a valid option"): shapely.from_wkb(b"\x01\x01\x00\x00\x00\x00", on_invalid="unsupported_option")
def test_from_wkb_none(): # None propagates assert shapely.from_wkb(None) is None
def shapely_wkb_loads_mock(wkb): geom = shapely.from_wkb(wkb) return ShapelyGeometryMock(geom)
def _unpickle_linearring(wkb): linestring = shapely.from_wkb(wkb) return shapely.linearrings(shapely.get_coordinates(linestring))
def time_read_from_wkb(self): shapely.from_wkb(self.to_read_wkb)
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(): expected = shapely.points(1, 1) actual = shapely.from_wkb(POINT11_WKB) assert_geometries_equal(actual, expected)