def _assert_nan_coords_same(x, y, tolerance, err_msg, verbose): x, y = np.broadcast_arrays(x, y) x_coords = shapely.get_coordinates(x, include_z=True) y_coords = shapely.get_coordinates(y, include_z=True) # Check the shapes (condition is copied from numpy test_array_equal) if x_coords.shape != y_coords.shape: return False # Check NaN positional equality x_id = np.isnan(x_coords) y_id = np.isnan(y_coords) if not (x_id == y_id).all(): msg = build_err_msg( [x, y], err_msg + "\nx and y nan coordinate location mismatch:", verbose=verbose, ) raise AssertionError(msg) # If this passed, replace NaN with a number to be able to use equals_exact x_no_nan = shapely.apply(x, _replace_nan, include_z=True) y_no_nan = shapely.apply(y, _replace_nan, include_z=True) return _equals_exact_with_ndim(x_no_nan, y_no_nan, tolerance=tolerance)
def test_apply_0dim(): # a geometry input returns a geometry actual = apply(point, lambda x: x + 1) assert isinstance(actual, shapely.Geometry) # a 0-dim array input returns a 0-dim array actual = apply(np.asarray(point), lambda x: x + 1) assert isinstance(actual, np.ndarray) assert actual.ndim == 0
def test_apply(geoms, include_z): geoms = np.array(geoms, np.object_) coordinates_before = get_coordinates(geoms, include_z=include_z) new_geoms = apply(geoms, lambda x: x + 1, include_z=include_z) assert new_geoms is not geoms coordinates_after = get_coordinates(new_geoms, include_z=include_z) assert_allclose(coordinates_before + 1, coordinates_after, equal_nan=True)
def _prepare_input(geometry, prepare): """Prepare without modifying inplace""" if prepare: geometry = shapely.apply(geometry, lambda x: x) # makes a copy shapely.prepare(geometry) return geometry else: return geometry
def _prepare_with_copy(geometry): """Prepare without modifying inplace""" geometry = shapely.apply(geometry, lambda x: x) # makes a copy shapely.prepare(geometry) return geometry
def test_apply_remove_z(geom): assert shapely.get_coordinate_dimension(geom) == 3 new_geom = apply(geom, lambda x: x + 1, include_z=False) assert shapely.get_coordinate_dimension(new_geom) == 2
def test_apply_empty_preserve_z(geom): assert shapely.get_coordinate_dimension(geom) == 3 new_geom = apply(geom, lambda x: x + 1, include_z=True) assert shapely.get_coordinate_dimension(new_geom) == 3
def test_apply_correct_coordinate_dimension(): # ensure that new geometry is 2D with include_z=False geom = line_string_z assert shapely.get_coordinate_dimension(geom) == 3 new_geom = apply(geom, lambda x: x + 1, include_z=False) assert shapely.get_coordinate_dimension(new_geom) == 2
def test_apply_check_shape(): def remove_coord(arr): return arr[:-1] with pytest.raises(ValueError): apply(linear_ring, remove_coord)
def affine_transform(geom, matrix): r"""Returns a transformed geometry using an affine transformation matrix. The coefficient matrix is provided as a list or tuple with 6 or 12 items for 2D or 3D transformations, respectively. For 2D affine transformations, the 6 parameter matrix is:: [a, b, d, e, xoff, yoff] which represents the augmented matrix:: [x'] / a b xoff \ [x] [y'] = | d e yoff | [y] [1 ] \ 0 0 1 / [1] or the equations for the transformed coordinates:: x' = a * x + b * y + xoff y' = d * x + e * y + yoff For 3D affine transformations, the 12 parameter matrix is:: [a, b, c, d, e, f, g, h, i, xoff, yoff, zoff] which represents the augmented matrix:: [x'] / a b c xoff \ [x] [y'] = | d e f yoff | [y] [z'] | g h i zoff | [z] [1 ] \ 0 0 0 1 / [1] or the equations for the transformed coordinates:: x' = a * x + b * y + c * z + xoff y' = d * x + e * y + f * z + yoff z' = g * x + h * y + i * z + zoff """ if len(matrix) == 6: ndim = 2 a, b, d, e, xoff, yoff = matrix if geom.has_z: ndim = 3 i = 1.0 c = f = g = h = zoff = 0.0 matrix = a, b, c, d, e, f, g, h, i, xoff, yoff, zoff elif len(matrix) == 12: ndim = 3 a, b, c, d, e, f, g, h, i, xoff, yoff, zoff = matrix if not geom.has_z: ndim = 2 matrix = a, b, d, e, xoff, yoff else: raise ValueError("'matrix' expects either 6 or 12 coefficients") def _affine_coords(coords): """Internal function to yield affine transform of coordinate tuples""" x = coords[:, 0] y = coords[:, 1] if ndim == 2: xp = a * x + b * y + xoff yp = d * x + e * y + yoff return np.hstack((np.atleast_2d(xp).T, np.atleast_2d(yp).T)) elif ndim == 3: z = coords[:, 2] xp = a * x + b * y + c * z + xoff yp = d * x + e * y + f * z + yoff zp = g * x + h * y + i * z + zoff return np.hstack( (np.atleast_2d(xp).T, np.atleast_2d(yp).T, np.atleast_2d(zp).T) ) return shapely.apply(geom, _affine_coords, include_z=ndim == 3)
def test_set_unique(geom): a = {geom, shapely.apply(geom, lambda x: x)} assert len(a) == 1
def test_neq(geom): assert geom != shapely.apply(geom, lambda x: x + 1)
def test_eq(geom): assert geom == shapely.apply(geom, lambda x: x)
def test_hash_same_not_equal(geom): assert hash(geom) != hash(shapely.apply(geom, lambda x: x + 1))
def test_hash_same_equal(geom): assert hash(geom) == hash(shapely.apply(geom, lambda x: x))