Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename shapely.apply to shapely.transform #1393

Merged
merged 4 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def setup(self):
pygeos.buffer(pygeos.points(np.random.random((500, 2)) * 500), 15)
)
# shift this up and right
self.right = pygeos.apply(self.left, lambda x: x + 50)
self.right = pygeos.transform(self.left, lambda x: x + 50)

def time_difference(self):
pygeos.difference(self.left, self.right)
Expand Down
2 changes: 1 addition & 1 deletion shapely/affinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _affine_coords(coords):
(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)
return shapely.transform(geom, _affine_coords, include_z=ndim == 3)


def interpret_origin(geom, origin, ndim):
Expand Down
4 changes: 2 additions & 2 deletions shapely/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from . import Geometry # NOQA
from . import lib

__all__ = ["apply", "count_coordinates", "get_coordinates", "set_coordinates"]
__all__ = ["transform", "count_coordinates", "get_coordinates", "set_coordinates"]


def apply(geometry, transformation, include_z=False):
def transform(geometry, transformation, include_z=False):
Copy link
Collaborator

@caspervdw caspervdw Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring should also be adjusted. And maybe also change the names all the way down to C?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the docstring

"""Returns a copy of a geometry array with a function applied to its
coordinates.

Expand Down
4 changes: 2 additions & 2 deletions shapely/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def _assert_nan_coords_same(x, y, tolerance, err_msg, 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)
x_no_nan = shapely.transform(x, _replace_nan, include_z=True)
y_no_nan = shapely.transform(y, _replace_nan, include_z=True)

return _equals_exact_with_ndim(x_no_nan, y_no_nan, tolerance=tolerance)

Expand Down
28 changes: 14 additions & 14 deletions shapely/tests/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from numpy.testing import assert_allclose, assert_equal

import shapely
from shapely import apply, count_coordinates, get_coordinates, set_coordinates
from shapely import count_coordinates, get_coordinates, set_coordinates, transform

from .common import (
empty,
Expand Down Expand Up @@ -212,56 +212,56 @@ def test_set_coords_mixed_dimension(include_z):
"geoms",
[[], [empty], [None, point, None], [nested_3], [point, point_z], [line_string_z]],
)
def test_apply(geoms, include_z):
def test_transform(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)
new_geoms = transform(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 test_apply_0dim():
def test_transform_0dim():
# a geometry input returns a geometry
actual = apply(point, lambda x: x + 1)
actual = transform(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)
actual = transform(np.asarray(point), lambda x: x + 1)
assert isinstance(actual, np.ndarray)
assert actual.ndim == 0


def test_apply_check_shape():
def test_transform_check_shape():
def remove_coord(arr):
return arr[:-1]

with pytest.raises(ValueError):
apply(linear_ring, remove_coord)
transform(linear_ring, remove_coord)


def test_apply_correct_coordinate_dimension():
def test_transform_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)
new_geom = transform(geom, lambda x: x + 1, include_z=False)
assert shapely.get_coordinate_dimension(new_geom) == 2


@pytest.mark.parametrize("geom", [
pytest.param(empty_point_z, marks=pytest.mark.skipif(shapely.geos_version < (3, 9, 0), reason="Empty points don't have a dimensionality before GEOS 3.9")),
empty_line_string_z,
])
def test_apply_empty_preserve_z(geom):
def test_transform_empty_preserve_z(geom):
assert shapely.get_coordinate_dimension(geom) == 3
new_geom = apply(geom, lambda x: x + 1, include_z=True)
new_geom = transform(geom, lambda x: x + 1, include_z=True)
assert shapely.get_coordinate_dimension(new_geom) == 3


@pytest.mark.parametrize("geom", [
pytest.param(empty_point_z, marks=pytest.mark.skipif(shapely.geos_version < (3, 9, 0), reason="Empty points don't have a dimensionality before GEOS 3.9")),
empty_line_string_z,
])
def test_apply_remove_z(geom):
def test_transform_remove_z(geom):
assert shapely.get_coordinate_dimension(geom) == 3
new_geom = apply(geom, lambda x: x + 1, include_z=False)
new_geom = transform(geom, lambda x: x + 1, include_z=False)
assert shapely.get_coordinate_dimension(new_geom) == 2
10 changes: 5 additions & 5 deletions shapely/tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,27 @@ def test_adapt_ptr_raises():
"geom", all_types + (shapely.points(np.nan, np.nan), empty_point)
)
def test_hash_same_equal(geom):
assert hash(geom) == hash(shapely.apply(geom, lambda x: x))
assert hash(geom) == hash(shapely.transform(geom, lambda x: x))


@pytest.mark.parametrize("geom", all_types[:-1])
def test_hash_same_not_equal(geom):
assert hash(geom) != hash(shapely.apply(geom, lambda x: x + 1))
assert hash(geom) != hash(shapely.transform(geom, lambda x: x + 1))


@pytest.mark.parametrize("geom", all_types)
def test_eq(geom):
assert geom == shapely.apply(geom, lambda x: x)
assert geom == shapely.transform(geom, lambda x: x)


@pytest.mark.parametrize("geom", all_types[:-1])
def test_neq(geom):
assert geom != shapely.apply(geom, lambda x: x + 1)
assert geom != shapely.transform(geom, lambda x: x + 1)


@pytest.mark.parametrize("geom", all_types)
def test_set_unique(geom):
a = {geom, shapely.apply(geom, lambda x: x)}
a = {geom, shapely.transform(geom, lambda x: x)}
assert len(a) == 1


Expand Down
2 changes: 1 addition & 1 deletion shapely/tests/test_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_shared_paths_non_linestring():
def _prepare_input(geometry, prepare):
"""Prepare without modifying inplace"""
if prepare:
geometry = shapely.apply(geometry, lambda x: x) # makes a copy
geometry = shapely.transform(geometry, lambda x: x) # makes a copy
shapely.prepare(geometry)
return geometry
else:
Expand Down
2 changes: 1 addition & 1 deletion shapely/tests/test_predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def test_is_ccw(geom, expected):

def _prepare_with_copy(geometry):
"""Prepare without modifying inplace"""
geometry = shapely.apply(geometry, lambda x: x) # makes a copy
geometry = shapely.transform(geometry, lambda x: x) # makes a copy
shapely.prepare(geometry)
return geometry

Expand Down