Example #1
0
def test_linearring_from_coordinate_sequence():
    expected_coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)]

    ring = LinearRing(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0)))
    assert ring.coords[:] == expected_coords

    ring = LinearRing([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0)])
    assert ring.coords[:] == expected_coords
Example #2
0
def test_linearring_immutable():
    ring = LinearRing([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)])

    with pytest.raises(AttributeError):
        ring.coords = [(1.0, 1.0), (2.0, 2.0), (1.0, 2.0)]

    with pytest.raises(TypeError):
        ring.coords[0] = (1.0, 1.0)
Example #3
0
def test_linearring_from_empty():
    ring = LinearRing()
    assert ring.is_empty
    assert isinstance(ring.coords, CoordinateSequence)
    assert ring.coords[:] == []

    ring = LinearRing([])
    assert ring.is_empty
    assert isinstance(ring.coords, CoordinateSequence)
    assert ring.coords[:] == []
Example #4
0
def test_linearring_from_too_short_linestring():
    # Creation of LinearRing request at least 3 coordinates (unclosed) or
    # 4 coordinates (closed)
    coords = [(0.0, 0.0), (1.0, 1.0)]
    line = LineString(coords)
    with pytest.raises(ValueError, match="requires at least 4 coordinates"):
        LinearRing(line)
Example #5
0
def test_numpy_linearring_coords():
    from numpy.testing import assert_array_equal

    ring = LinearRing(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0)))
    ra = np.asarray(ring.coords)
    expected = np.asarray([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)])
    assert_array_equal(ra, expected)
Example #6
0
def test_linearring_from_unclosed_linestring():
    coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
    line = LineString(coords[:-1])  # Pass in unclosed line
    ring = LinearRing(line)
    assert len(ring.coords) == 4
    assert ring.coords[:] == coords
    assert ring.geom_type == "LinearRing"
Example #7
0
 def test_index_linearring(self):
     shell = LinearRing([(0.0, 0.0), (70.0, 120.0), (140.0, 0.0),
                         (0.0, 0.0)])
     holes = [
         LinearRing([(60.0, 80.0), (80.0, 80.0), (70.0, 60.0),
                     (60.0, 80.0)]),
         LinearRing([(30.0, 10.0), (50.0, 10.0), (40.0, 30.0),
                     (30.0, 10.0)]),
         LinearRing([(90.0, 10), (110.0, 10.0), (100.0, 30.0),
                     (90.0, 10.0)]),
     ]
     g = Polygon(shell, holes)
     for i in range(-3, 3):
         assert g.interiors[i].equals(holes[i])
     with pytest.raises(IndexError):
         g.interiors[3]
     with pytest.raises(IndexError):
         g.interiors[-4]
Example #8
0
    def test_linearring(self):

        # Initialization
        # Linear rings won't usually be created by users, but by polygons
        coords = ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0))
        ring = LinearRing(coords)
        assert len(ring.coords) == 5
        assert ring.coords[0] == ring.coords[4]
        assert ring.coords[0] == ring.coords[-1]
        assert ring.is_ring is True
Example #9
0
def test_polygon_from_linearring():
    coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
    ring = LinearRing(coords)

    polygon = Polygon(ring)
    assert polygon.exterior.coords[:] == coords
    assert len(polygon.interiors) == 0

    # from shell and holes linearrings
    shell = LinearRing([(0.0, 0.0), (70.0, 120.0), (140.0, 0.0), (0.0, 0.0)])
    holes = [
        LinearRing([(60.0, 80.0), (80.0, 80.0), (70.0, 60.0), (60.0, 80.0)]),
        LinearRing([(30.0, 10.0), (50.0, 10.0), (40.0, 30.0), (30.0, 10.0)]),
        LinearRing([(90.0, 10), (110.0, 10.0), (100.0, 30.0), (90.0, 10.0)]),
    ]
    polygon = Polygon(shell, holes)
    assert polygon.exterior.coords[:] == shell.coords[:]
    assert len(polygon.interiors) == 3
    for i in range(3):
        assert polygon.interiors[i].coords[:] == holes[i].coords[:]
Example #10
0
    def test_equals_argument_order(self):
        """
        Test equals predicate functions correctly regardless of the order
        of the inputs. See issue #317.
        """
        coords = ((0, 0), (1, 0), (1, 1), (0, 0))
        ls = LineString(coords)
        lr = LinearRing(coords)

        assert ls.__eq__(lr) is False  # previously incorrectly returned True
        assert lr.__eq__(ls) is False
        assert (ls == lr) is False
        assert (lr == ls) is False

        ls_clone = LineString(coords)
        lr_clone = LinearRing(coords)

        assert ls.__eq__(ls_clone) is True
        assert lr.__eq__(lr_clone) is True
        assert (ls == ls_clone) is True
        assert (lr == lr_clone) is True
Example #11
0
 def test_slice_linearring(self):
     shell = LinearRing([(0.0, 0.0), (70.0, 120.0), (140.0, 0.0),
                         (0.0, 0.0)])
     holes = [
         LinearRing([(60.0, 80.0), (80.0, 80.0), (70.0, 60.0),
                     (60.0, 80.0)]),
         LinearRing([(30.0, 10.0), (50.0, 10.0), (40.0, 30.0),
                     (30.0, 10.0)]),
         LinearRing([(90.0, 10), (110.0, 10.0), (100.0, 30.0),
                     (90.0, 10.0)]),
     ]
     g = Polygon(shell, holes)
     t = [a.equals(b) for (a, b) in zip(g.interiors[1:], holes[1:])]
     assert all(t)
     t = [a.equals(b) for (a, b) in zip(g.interiors[:-1], holes[:-1])]
     assert all(t)
     t = [a.equals(b) for (a, b) in zip(g.interiors[::-1], holes[::-1])]
     assert all(t)
     t = [a.equals(b) for (a, b) in zip(g.interiors[::2], holes[::2])]
     assert all(t)
     t = [a.equals(b) for (a, b) in zip(g.interiors[:3], holes[:3])]
     assert all(t)
     assert g.interiors[3:] == holes[3:] == []
Example #12
0
 def test_empty_linear_ring(self):
     assert LinearRing().is_empty
     assert LinearRing(None).is_empty
     assert LinearRing([]).is_empty
     assert LinearRing(empty_generator()).is_empty
Example #13
0
def test_numpy_empty_linearring_coords():
    ring = LinearRing()
    assert np.asarray(ring.coords).shape == (0, 2)
Example #14
0
def test_linearring_from_numpy():
    # Construct from a numpy array
    coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]

    ring = LinearRing(np.array(coords))
    assert ring.coords[:] == [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
Example #15
0
def test_empty_linearring_coords():
    assert LinearRing().coords[:] == []
Example #16
0
def test_linearring_from_generator():
    coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
    gen = (coord for coord in coords)
    ring = LinearRing(gen)
    assert ring.coords[:] == coords
Example #17
0
def test_linearring_from_invalid():
    coords = [(0.0, 0.0), (0.0, 0.0), (0.0, 0.0)]
    line = LineString(coords)
    assert not line.is_valid
    with pytest.raises(TopologicalError):
        LinearRing(line)
Example #18
0
def test_numpy_object_array():
    geoms = [Point(), GeometryCollection()]
    arr = np.empty(2, object)
    arr[:] = geoms


def test_shape_empty():
    empty_mp = MultiPolygon()
    empty_json = mapping(empty_mp)
    empty_shape = shape(empty_json)
    assert empty_shape.is_empty


@pytest.mark.parametrize(
    "geom",
    [
        Point(),
        LineString(),
        Polygon(),
        MultiPoint(),
        MultiLineString(),
        MultiPolygon(),
        GeometryCollection(),
        LinearRing(),
    ],
)
def test_empty_geometry_bounds(geom):
    """The bounds of an empty geometry is a tuple of NaNs"""
    assert len(geom.bounds) == 4
    assert all(math.isnan(v) for v in geom.bounds)
Example #19
0
 def test_empty_polygon_exterior(self):
     p = Polygon()
     assert p.exterior == LinearRing()
Example #20
0
 def test_linearring_empty(self):
     # Test Non-operability of Null rings
     r_null = LinearRing()
     assert r_null.wkt == "LINEARRING EMPTY"
     assert r_null.length == 0.0
Example #21
0
def test_linearring_from_points():
    # From Points
    expected_coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.0, 0.0)]

    ring = LinearRing([Point(0.0, 0.0), Point(0.0, 1.0), Point(1.0, 1.0)])
    assert ring.coords[:] == expected_coords
Example #22
0
def test_from_linearring():
    coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
    ring = LinearRing(coords)
    copy = LineString(ring)
    assert copy.coords[:] == coords
    assert copy.geom_type == "LineString"
Example #23
0
def test_linestring():
    assert bool(LineString()) is False


def test_point():
    assert bool(Point()) is False


def test_geometry_collection():
    assert bool(GeometryCollection()) is False


geometries_all_types = [
    Point(1, 1),
    LinearRing([(0, 0), (1, 1), (0, 1), (0, 0)]),
    LineString([(0, 0), (1, 1), (0, 1), (0, 0)]),
    Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
    MultiPoint([(1, 1)]),
    MultiLineString([[(0, 0), (1, 1), (0, 1), (0, 0)]]),
    MultiPolygon([Polygon([(0, 0), (1, 1), (0, 1), (0, 0)])]),
    GeometryCollection([Point(1, 1)]),
]


@pytest.mark.parametrize("geom", geometries_all_types)
def test_setattr_disallowed(geom):
    with pytest.raises(AttributeError):
        geom.name = "test"