Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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")
Ejemplo n.º 13
0
def test_from_wkb_none():
    # None propagates
    assert shapely.from_wkb(None) is None
Ejemplo n.º 14
0
def shapely_wkb_loads_mock(wkb):
    geom = shapely.from_wkb(wkb)
    return ShapelyGeometryMock(geom)
Ejemplo n.º 15
0
def _unpickle_linearring(wkb):
    linestring = shapely.from_wkb(wkb)
    return shapely.linearrings(shapely.get_coordinates(linestring))
Ejemplo n.º 16
0
 def time_read_from_wkb(self):
     shapely.from_wkb(self.to_read_wkb)
Ejemplo n.º 17
0
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
Ejemplo n.º 18
0
def test_from_wkb():
    expected = shapely.points(1, 1)
    actual = shapely.from_wkb(POINT11_WKB)
    assert_geometries_equal(actual, expected)