def test_line_interpolate_point_empty(geom, normalized): # These geometries segfault in some versions of GEOS (in 3.8.0, still # some of them segfault). Instead, we patched this to return POINT EMPTY. # This matches GEOS 3.8.0 behavior on simple empty geometries. assert_geometries_equal( shapely.line_interpolate_point(geom, 0.2, normalized=normalized), empty_point)
def test_from_wkt(): expected = shapely.points(1, 1) actual = shapely.from_wkt("POINT (1 1)") assert_geometries_equal(actual, expected) # also accept bytes actual = shapely.from_wkt(b"POINT (1 1)") assert_geometries_equal(actual, expected)
def test_set_operation_reduce_two_none(func, related_func, none_position): test_data = reduce_test_data[:2] test_data.insert(none_position, None) test_data.insert(none_position, None) actual = func(test_data) expected = related_func(reduce_test_data[0], reduce_test_data[1]) assert_geometries_equal(actual, expected)
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_geometrycollections_out(indices, expected): out = np.empty(4, dtype=object) out[3] = empty_point actual = shapely.geometrycollections([point, line_string], indices=indices, out=out) assert_geometries_equal(out, expected) assert actual is out
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 test_set_operation_prec_reduce_one_none(func, related_func, none_position): test_data = reduce_test_data[:2] test_data.insert(none_position, None) actual = func(test_data, grid_size=1) expected = related_func(reduce_test_data[0], reduce_test_data[1], grid_size=1) assert_geometries_equal(actual, expected)
def test_pickle(geom): if shapely.get_type_id(geom) == 2: # Linearrings get converted to linestrings expected = shapely.linestrings(shapely.get_coordinates(geom)) else: expected = geom pickled = pickle.dumps(geom) assert_geometries_equal(pickle.loads(pickled), expected, tolerance=0)
def test_reverse_none(): assert shapely.reverse(None) is None assert shapely.reverse([None]).tolist() == [None] geometry = shapely.Geometry("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))") expected = shapely.Geometry("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))") result = shapely.reverse([None, geometry]) assert result[0] is None assert_geometries_equal(result[1], expected)
def test_set_operation_reduce_one_none(func, related_func, none_position): # API change: before, intersection_all and symmetric_difference_all returned # None if any input geometry was None. # The new behaviour is to ignore None values. test_data = reduce_test_data[:2] test_data.insert(none_position, None) actual = func(test_data) expected = related_func(reduce_test_data[0], reduce_test_data[1]) assert_geometries_equal(actual, expected)
def test_set_precision_z(mode): with warnings.catch_warnings(): warnings.simplefilter( "ignore") # GEOS <= 3.9 emits warning for 'pointwise' geometry = shapely.set_precision( shapely.Geometry("POINT Z (0.9 0.9 0.9)"), 1, mode=mode) assert shapely.get_precision(geometry) == 1 assert_geometries_equal(geometry, shapely.Geometry("POINT Z (1 1 0.9)"))
def test_linestrings_from_coords(): actual = shapely.linestrings([[[0, 0], [1, 1]], [[0, 0], [2, 2]]]) assert_geometries_equal( actual, [ shapely.Geometry("LINESTRING (0 0, 1 1)"), shapely.Geometry("LINESTRING (0 0, 2 2)"), ], )
def set_precision_preserve_topology(preserve_topology): # the preserve_topology kwarg is deprecated (ignored) with pytest.warns(UserWarning): actual = shapely.set_precision( shapely.Geometry("LINESTRING (0 0, 0.1 0.1)"), 1.0, preserve_topology=preserve_topology, ) assert_geometries_equal(shapely.force_2d(actual), shapely.Geometry("LINESTRING EMPTY"))
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 set_precision_pointwise_pre_310(): # using 'pointwise' emits a warning with pytest.warns(UserWarning): actual = shapely.set_precision( shapely.Geometry("LINESTRING (0 0, 0.1 0.1)"), 1.0, mode="pointwise", ) assert_geometries_equal(shapely.force_2d(actual), shapely.Geometry("LINESTRING EMPTY"))
def test_points_out(indices, expected): out = np.empty(4, dtype=object) out[2] = empty_point actual = shapely.points( [[2, 3], [2, 3]], indices=indices, out=out, ) assert_geometries_equal(out, expected) assert actual is out
def test_set_precision_drop_coords(): # setting precision of 0 will not drop duplicated points in original geometry = shapely.set_precision( shapely.Geometry("LINESTRING (0 0, 0 0, 0 1, 1 1)"), 0) assert_geometries_equal( geometry, shapely.Geometry("LINESTRING (0 0, 0 0, 0 1, 1 1)")) # setting precision will remove duplicated points geometry = shapely.set_precision(geometry, 1) assert_geometries_equal(geometry, shapely.Geometry("LINESTRING (0 0, 0 1, 1 1)"))
def test_linestrings_from_xy_broadcast(): x = [0, 1] # the same X coordinates for both linestrings y = [2, 3], [4, 5] # each linestring has a different set of Y coordinates actual = shapely.linestrings(x, y) assert_geometries_equal( actual, [ shapely.Geometry("LINESTRING (0 2, 1 3)"), shapely.Geometry("LINESTRING (0 4, 1 5)"), ], )
def test_get_parts(geom): expected_num_parts = shapely.get_num_geometries(geom) if expected_num_parts == 0: expected_parts = [] else: expected_parts = shapely.get_geometry(geom, range(0, expected_num_parts)) parts = shapely.get_parts(geom) assert len(parts) == expected_num_parts assert_geometries_equal(parts, expected_parts)
def test_set_precision_collapse(geometry, mode, expected): """Lines and polygons collapse to empty geometries if vertices are too close""" actual = shapely.set_precision(geometry, 1, mode=mode) if shapely.geos_version < (3, 9, 0): # pre GEOS 3.9 has difficulty comparing empty geometries exactly # normalize and compare by WKT instead assert shapely.to_wkt(shapely.normalize(actual)) == shapely.to_wkt( shapely.normalize(expected)) else: # force to 2D because GEOS 3.10 yields 3D geometries when they are empty. assert_geometries_equal(shapely.force_2d(actual), expected)
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_get_parts_return_index(): geom = np.array([multi_point, point, multi_polygon]) expected_parts = [] expected_index = [] for i, g in enumerate(geom): for j in range(0, shapely.get_num_geometries(g)): expected_parts.append(shapely.get_geometry(g, j)) expected_index.append(i) parts, index = shapely.get_parts(geom, return_index=True) assert len(parts) == len(expected_parts) assert_geometries_equal(parts, expected_parts) assert np.array_equal(index, expected_index)
def test_get_parts_array(): # note: this also verifies that None is handled correctly # in the mix; internally it returns -1 for count of geometries geom = np.array( [None, empty_line_string, multi_point, point, multi_polygon]) expected_parts = [] for g in geom: for i in range(0, shapely.get_num_geometries(g)): expected_parts.append(shapely.get_geometry(g, i)) parts = shapely.get_parts(geom) assert len(parts) == len(expected_parts) assert_geometries_equal(parts, expected_parts)
def test_set_precision_intersection(): """Operations should use the most precise presision grid size of the inputs""" box1 = shapely.normalize(shapely.box(0, 0, 0.9, 0.9)) box2 = shapely.normalize(shapely.box(0.75, 0, 1.75, 0.75)) assert shapely.get_precision(shapely.intersection(box1, box2)) == 0 # GEOS will use and keep the most precise precision grid size box1 = shapely.set_precision(box1, 0.5) box2 = shapely.set_precision(box2, 1) out = shapely.intersection(box1, box2) assert shapely.get_precision(out) == 0.5 assert_geometries_equal(out, shapely.Geometry("LINESTRING (1 1, 1 0)"))
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_linestrings_buffer(dim): coords = np.random.randn(10, 3, dim) coords1 = np.asarray(coords, order="C") result1 = shapely.linestrings(coords1) coords2 = np.asarray(coords1, order="F") result2 = shapely.linestrings(coords2) assert_geometries_equal(result1, result2) # creating (.., 8, 8*3) strided array so it uses copyFromArrays coords3 = np.asarray(np.swapaxes(np.swapaxes(coords, 0, 2), 1, 0), order="F") coords3 = np.swapaxes(np.swapaxes(coords3, 0, 2), 1, 2) result3 = shapely.linestrings(coords3) assert_geometries_equal(result1, result3)
def test_coverage_union_reduce_1dim(n): """ This is tested seperately from other set operations as it differs in two ways: 1. It expects only non-overlapping polygons 2. It expects GEOS 3.8.0+ """ test_data = [ shapely.box(0, 0, 1, 1), shapely.box(1, 0, 2, 1), shapely.box(2, 0, 3, 1), ] actual = shapely.coverage_union_all(test_data[:n]) # perform the reduction in a python loop and compare expected = test_data[0] for i in range(1, n): expected = shapely.coverage_union(expected, test_data[i]) assert_geometries_equal(actual, expected, normalize=True)
def test_get_rings_return_index(): geom = np.array([polygon, None, empty_polygon, polygon_with_hole]) expected_parts = [] expected_index = [] for i, g in enumerate(geom): if g is None or shapely.is_empty(g): continue expected_parts.append(shapely.get_exterior_ring(g)) expected_index.append(i) for j in range(0, shapely.get_num_interior_rings(g)): expected_parts.append(shapely.get_interior_ring(g, j)) expected_index.append(i) parts, index = shapely.get_rings(geom, return_index=True) assert len(parts) == len(expected_parts) assert_geometries_equal(parts, expected_parts) assert np.array_equal(index, expected_index)
def test_get_parts_geometry_collection_multi(): """On the first pass, the individual Multi* geometry objects are returned from the collection. On the second pass, the individual singular geometry objects within those are returned. """ geom = shapely.geometrycollections( [multi_point, multi_line_string, multi_polygon]) expected_num_parts = shapely.get_num_geometries(geom) expected_parts = shapely.get_geometry(geom, range(0, expected_num_parts)) parts = shapely.get_parts(geom) assert len(parts) == expected_num_parts assert_geometries_equal(parts, expected_parts) expected_subparts = [] for g in np.asarray(expected_parts): for i in range(0, shapely.get_num_geometries(g)): expected_subparts.append(shapely.get_geometry(g, i)) subparts = shapely.get_parts(parts) assert len(subparts) == len(expected_subparts) assert_geometries_equal(subparts, expected_subparts)
def test_line_interpolate_point_geom_array(): actual = shapely.line_interpolate_point( [line_string, linear_ring, multi_line_string], -1) assert_geometries_equal(actual[0], shapely.Geometry("POINT (1 0)")) assert_geometries_equal(actual[1], shapely.Geometry("POINT (0 1)")) assert_geometries_equal(actual[2], shapely.Geometry("POINT (0.5528 1.1056)"), tolerance=0.001)