def test_polygons_not_enough_points_in_holes(shape): coords = np.ones(shape) with pytest.raises(ValueError): shapely.polygons(np.ones((1, 4, 2)), coords) # make sure the first coordinate != second coordinate coords[..., 1] += 1 with pytest.raises(ValueError): shapely.polygons(np.ones((1, 4, 2)), coords)
def test_polygons_out(indices, expected): out = np.empty(4, dtype=object) out[2] = empty_point actual = shapely.polygons([linear_ring, linear_ring], indices=indices, out=out) assert_geometries_equal(out, expected) assert actual is out
def setup(self): self.multipolygons = np.array( [ shapely.multipolygons( shapely.polygons(np.random.random((2, 100, 2)))) for i in range(10000) ], dtype=object, )
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 __new__(self, shell=None, holes=None): if shell is None: # empty geometry # TODO better way? return shapely.from_wkt("POLYGON EMPTY") elif isinstance(shell, Polygon): # return original objects since geometries are immutable return shell # else: # geom_shell = LinearRing(shell) # if holes is not None: # geom_holes = [LinearRing(h) for h in holes] if holes is not None: if len(holes) == 0: # shapely constructor cannot handle holes=[] holes = None else: holes = [LinearRing(ring) for ring in holes] if not isinstance(shell, BaseGeometry): if not isinstance(shell, (list, np.ndarray)): # eg emtpy generator not handled well by np.asarray shell = list(shell) shell = np.asarray(shell) if len(shell) == 0: # empty geometry # TODO better constructor + should shapely.polygons handle this? return shapely.from_wkt("POLYGON EMPTY") if not np.issubdtype(shell.dtype, np.number): # conversion of coords to 2D array failed, this might be due # to inconsistent coordinate dimensionality raise ValueError("Inconsistent coordinate dimensionality") elif not isinstance(shell, LinearRing): shell = LinearRing(shell) geom = shapely.polygons(shell, holes=holes) if not isinstance(geom, Polygon): raise ValueError("Invalid values passed to Polygon constructor") return geom
def test_polygons(rings, indices, expected): actual = shapely.polygons(np.array(rings, dtype=object), indices=np.array(indices, dtype=np.intp)) assert_geometries_equal(actual, expected)
def test_2_polygons_with_same_hole(): actual = shapely.polygons( [box_tpl(0, 0, 10, 10), box_tpl(0, 0, 5, 5)], [box_tpl(1, 1, 2, 2)]) assert shapely.area(actual).tolist() == [99.0, 24.0]
def test_polygons(): actual = shapely.polygons(box_tpl(0, 0, 1, 1)) assert_geometries_equal( actual, shapely.Geometry("POLYGON ((1 0, 1 1, 0 1, 0 0, 1 0))"))
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]), ([linear_ring, linear_ring, hole_1], [0, 1, 1], [poly, poly_hole_1]), ([None, linear_ring, linear_ring, hole_1], [0, 0, 1, 1
def test_polygons_none(): assert_geometries_equal(shapely.polygons(None), empty_polygon) assert_geometries_equal(shapely.polygons(None, holes=[linear_ring]), empty_polygon)
def test_polygons_not_enough_points_in_holes_scalar(): with pytest.raises(ValueError): shapely.polygons(np.ones((1, 4, 2)), (1, 1))
def test_polygon_no_hole_list_raises(): with pytest.raises(ValueError): shapely.polygons(box_tpl(0, 0, 10, 10), box_tpl(1, 1, 2, 2))
def __new__(self, shell=None, holes=None): """ Parameters ---------- shell : sequence A sequence of (x, y [,z]) numeric coordinate pairs or triples. Also can be a sequence of Point objects. holes : sequence A sequence of objects which satisfy the same requirements as the shell parameters above Example ------- Create a square polygon with no holes >>> coords = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.)) >>> polygon = Polygon(coords) >>> polygon.area 1.0 """ if shell is None: # empty geometry # TODO better way? return shapely.from_wkt("POLYGON EMPTY") elif isinstance(shell, Polygon): # return original objects since geometries are immutable return shell # else: # geom_shell = LinearRing(shell) # if holes is not None: # geom_holes = [LinearRing(h) for h in holes] if holes is not None: if len(holes) == 0: # shapely constructor cannot handle holes=[] holes = None else: holes = [LinearRing(ring) for ring in holes] if not isinstance(shell, BaseGeometry): if not isinstance(shell, (list, np.ndarray)): # eg emtpy generator not handled well by np.asarray shell = list(shell) shell = np.asarray(shell) if len(shell) == 0: # empty geometry # TODO better constructor + should shapely.polygons handle this? return shapely.from_wkt("POLYGON EMPTY") if not np.issubdtype(shell.dtype, np.number): # conversion of coords to 2D array failed, this might be due # to inconsistent coordinate dimensionality raise ValueError("Inconsistent coordinate dimensionality") elif not isinstance(shell, LinearRing): shell = LinearRing(shell) geom = shapely.polygons(shell, holes=holes) if not isinstance(geom, Polygon): raise ValueError("Invalid values passed to Polygon constructor") return geom
def setup(self): self.points = shapely.points(np.random.random((100000, 2))) self.polygon = shapely.polygons(np.random.random((3, 2)))
def test_polygon_no_hole_wrong_type(): with pytest.raises((TypeError, shapely.GEOSException)): shapely.polygons(point)
def test_polygon_with_2_holes(): actual = shapely.polygons( box_tpl(0, 0, 10, 10), [box_tpl(1, 1, 2, 2), box_tpl(3, 3, 4, 4)]) assert shapely.area(actual) == 98.0
def test_polygon_with_1_hole(): actual = shapely.polygons(box_tpl(0, 0, 10, 10), [box_tpl(1, 1, 2, 2)]) assert shapely.area(actual) == 99.0
def test_polygon_wrong_hole_type(): with pytest.raises((TypeError, shapely.GEOSException)): shapely.polygons(linear_ring, [point])
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 test_2_polygons_with_2_same_holes(): actual = shapely.polygons( [box_tpl(0, 0, 10, 10), box_tpl(0, 0, 5, 5)], [box_tpl(1, 1, 2, 2), box_tpl(3, 3, 4, 4)], ) assert shapely.area(actual).tolist() == [98.0, 23.0]
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)]) geometry_collection_z = shapely.geometrycollections([point_z, line_string_z])
def test_2_polygons_with_different_holes(): actual = shapely.polygons( [box_tpl(0, 0, 10, 10), box_tpl(0, 0, 5, 5)], [[box_tpl(1, 1, 3, 3)], [box_tpl(1, 1, 2, 2)]], ) assert shapely.area(actual).tolist() == [96.0, 24.0]
"this": "that" }, "prop0": "value0" }, }, ], }, indent=4, ) GEOJSON_GEOMETRY_EXPECTED = shapely.points(125.6, 10.1) GEOJSON_COLLECTION_EXPECTED = [ shapely.points([102.0, 0.6]), shapely.linestrings([[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]]), shapely.polygons([[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]), ] class ShapelyGeometryMock: def __init__(self, g): self.g = g self.__geom__ = g._ptr if hasattr(g, "_ptr") else g @property def __array_interface__(self): # this should not be called # (starting with numpy 1.20 it is called, but not used) return np.array([1.0, 2.0]).__array_interface__ @property
def test_polygons_not_enough_points_in_shell_scalar(): with pytest.raises(ValueError): shapely.polygons((1, 1))