def test_linearrings_invalid_shape(shape): coords = np.ones(shape) with pytest.raises(ValueError): shapely.linearrings(coords) # make sure the first coordinate != second coordinate coords[..., 1] += 1 with pytest.raises(ValueError): shapely.linearrings(coords)
def test_polygon_with_none_hole(): actual = shapely.polygons( shapely.linearrings(box_tpl(0, 0, 10, 10)), [ shapely.linearrings(box_tpl(1, 1, 2, 2)), None, shapely.linearrings(box_tpl(3, 3, 4, 4)), ], ) assert shapely.area(actual) == 98.0
def test_linearrings_buffer(dim, order): coords = np.random.randn(10, 4, dim) coords1 = np.asarray(coords.reshape(10 * 4, dim), order=order) indices1 = np.repeat(range(10), 4) result1 = shapely.linearrings(coords1, indices=indices1) # with manual closure -> can directly copy from buffer if C order coords2 = np.hstack((coords, coords[:, [0], :])) coords2 = np.asarray(coords2.reshape(10 * 5, dim), order=order) indices2 = np.repeat(range(10), 5) result2 = shapely.linearrings(coords2, indices=indices2) assert_geometries_equal(result1, result2)
def test_linearrings_buffer(dim, order): coords1 = np.random.randn(10, 4, dim) coords1 = np.asarray(coords1, order=order) result1 = shapely.linearrings(coords1) # with manual closure -> can directly copy from buffer if C order coords2 = np.hstack((coords1, coords1[:, [0], :])) coords2 = np.asarray(coords2, order=order) result2 = shapely.linearrings(coords2) assert_geometries_equal(result1, result2) # create scalar -> can also directly copy from buffer if F order coords3 = np.asarray(coords2[0], order=order) result3 = shapely.linearrings(coords3) assert_geometries_equal(result3, result1[0])
def test_linearrings_invalid_ndim(): msg = r"The ordinate \(last\) dimension should be 2 or 3, got {}" coords1 = np.random.randn(10, 3, 4) with pytest.raises(ValueError, match=msg.format(4)): shapely.linearrings(coords1) coords2 = np.hstack((coords1, coords1[:, [0], :])) with pytest.raises(ValueError, match=msg.format(4)): shapely.linearrings(coords2) # too few ordinates coords3 = np.random.randn(10, 3, 1) with pytest.raises(ValueError, match=msg.format(1)): shapely.linestrings(coords3)
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 __new__(self, coordinates=None): if coordinates is None: # empty geometry # TODO better way? return shapely.from_wkt("LINEARRING EMPTY") elif isinstance(coordinates, LineString): if type(coordinates) == LinearRing: # return original objects since geometries are immutable return coordinates elif not coordinates.is_valid: raise TopologicalError("An input LineString must be valid.") else: # LineString # TODO convert LineString to LinearRing more directly? coordinates = coordinates.coords else: # check coordinates on points def _coords(o): if isinstance(o, Point): return o.coords[0] else: return o coordinates = [_coords(o) for o in coordinates] if len(coordinates) == 0: # empty geometry # TODO better constructor + should shapely.linearrings handle this? return shapely.from_wkt("LINEARRING EMPTY") geom = shapely.linearrings(coordinates) if not isinstance(geom, LinearRing): raise ValueError("Invalid values passed to LinearRing constructor") return geom
def test_linearrings_out(indices, expected): out = np.empty(4, dtype=object) out[3] = empty_point actual = shapely.linearrings( [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], indices=indices, out=out, ) assert_geometries_equal(out, expected) assert actual is out
def __new__(self, coordinates=None): """ Parameters ---------- coordinates : sequence A sequence of (x, y [,z]) numeric coordinate pairs or triples. Also can be a sequence of Point objects. Rings are implicitly closed. There is no need to specific a final coordinate pair identical to the first. Example ------- Construct a square ring. >>> ring = LinearRing( ((0, 0), (0, 1), (1 ,1 ), (1 , 0)) ) >>> ring.is_closed True >>> ring.length 4.0 """ if coordinates is None: # empty geometry # TODO better way? return shapely.from_wkt("LINEARRING EMPTY") elif isinstance(coordinates, LineString): if type(coordinates) == LinearRing: # return original objects since geometries are immutable return coordinates elif not coordinates.is_valid: raise TopologicalError("An input LineString must be valid.") else: # LineString # TODO convert LineString to LinearRing more directly? coordinates = coordinates.coords else: # check coordinates on points def _coords(o): if isinstance(o, Point): return o.coords[0] else: return o coordinates = [_coords(o) for o in coordinates] if len(coordinates) == 0: # empty geometry # TODO better constructor + should shapely.linearrings handle this? return shapely.from_wkt("LINEARRING EMPTY") geom = shapely.linearrings(coordinates) if not isinstance(geom, LinearRing): raise ValueError("Invalid values passed to LinearRing constructor") return geom
def _unpickle_linearring(wkb): linestring = shapely.from_wkb(wkb) return shapely.linearrings(shapely.get_coordinates(linestring))
@pytest.mark.parametrize("order", ["C", "F"]) def test_linearrings_buffer(dim, order): coords = np.random.randn(10, 4, dim) coords1 = np.asarray(coords.reshape(10 * 4, dim), order=order) indices1 = np.repeat(range(10), 4) result1 = shapely.linearrings(coords1, indices=indices1) # with manual closure -> can directly copy from buffer if C order coords2 = np.hstack((coords, coords[:, [0], :])) coords2 = np.asarray(coords2.reshape(10 * 5, dim), order=order) indices2 = np.repeat(range(10), 5) result2 = shapely.linearrings(coords2, indices=indices2) assert_geometries_equal(result1, result2) hole_1 = shapely.linearrings([(0.2, 0.2), (0.2, 0.4), (0.4, 0.4)]) hole_2 = shapely.linearrings([(0.6, 0.6), (0.6, 0.8), (0.8, 0.8)]) poly = shapely.polygons(linear_ring) poly_empty = shapely.Geometry("POLYGON EMPTY") poly_hole_1 = shapely.polygons(linear_ring, holes=[hole_1]) poly_hole_2 = shapely.polygons(linear_ring, holes=[hole_2]) poly_hole_1_2 = shapely.polygons(linear_ring, holes=[hole_1, hole_2]) @pytest.mark.parametrize( "rings,indices,expected", [ ([linear_ring, linear_ring], [0, 1], [poly, poly]), ([None, linear_ring], [0, 1], [poly_empty, poly]), ([None, linear_ring, None, None], [0, 0, 1, 1], [poly, poly_empty]), ([linear_ring, hole_1, linear_ring], [0, 0, 1], [poly_hole_1, poly]),
def test_linearrings_unclosed(): actual = shapely.linearrings(box_tpl(0, 0, 1, 1)[:-1]) assert_geometries_equal( actual, shapely.Geometry("LINEARRING (1 0, 1 1, 0 1, 0 0, 1 0)"))
import numpy as np import pytest import shapely shapely20_todo = pytest.mark.xfail( strict=False, reason="Not yet implemented for Shapely 2.0" ) point_polygon_testdata = ( shapely.points(np.arange(6), np.arange(6)), shapely.box(2, 2, 4, 4), ) point = shapely.points(2, 3) line_string = shapely.linestrings([(0, 0), (1, 0), (1, 1)]) linear_ring = shapely.linearrings([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) polygon = shapely.polygons([(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)]) multi_point = shapely.multipoints([(0, 0), (1, 2)]) multi_line_string = shapely.multilinestrings([[(0, 0), (1, 2)]]) multi_polygon = shapely.multipolygons( [ [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], [(2.1, 2.1), (2.2, 2.1), (2.2, 2.2), (2.1, 2.2), (2.1, 2.1)], ] ) geometry_collection = shapely.geometrycollections( [shapely.points(51, -1), shapely.linestrings([(52, -1), (49, 2)])] ) point_z = shapely.points(2, 3, 4) line_string_z = shapely.linestrings([(0, 0, 4), (1, 0, 4), (1, 1, 4)]) polygon_z = shapely.polygons([(0, 0, 4), (2, 0, 4), (2, 2, 4), (0, 2, 4), (0, 0, 4)])
def test_linearrings_all_nan(): coords = np.full((4, 2), np.nan) with pytest.raises(shapely.GEOSException): shapely.linearrings(coords)
def test_linearrings_invalid_shape_scalar(): with pytest.raises(ValueError): shapely.linearrings((1, 1))
def test_linearrings_unclosed_all_coords_equal(): actual = shapely.linearrings([(0, 0), (0, 0), (0, 0)], indices=np.zeros(3)) assert_geometries_equal( actual, shapely.Geometry("LINEARRING (0 0, 0 0, 0 0, 0 0)"))
def test_linearrings_invalid(coordinates): # attempt to construct linestrings with 1 coordinate with pytest.raises((shapely.GEOSException, ValueError)): shapely.linearrings(coordinates, indices=np.zeros(len(coordinates)))
def test_linearrings(coordinates): actual = shapely.linearrings( np.array(coordinates, dtype=np.float64), indices=np.zeros(len(coordinates), dtype=np.intp), ) assert_geometries_equal(actual, shapely.linearrings(coordinates))
def test_linearrings_from_xy(): actual = shapely.linearrings([0, 1, 2, 0], [3, 4, 5, 3]) assert_geometries_equal( actual, shapely.Geometry("LINEARRING (0 3, 1 4, 2 5, 0 3)"))
def test_polygon_from_linearring(): actual = shapely.polygons(shapely.linearrings(box_tpl(0, 0, 1, 1))) assert_geometries_equal( actual, shapely.Geometry("POLYGON ((1 0, 1 1, 0 1, 0 0, 1 0))"))