Skip to content

Commit

Permalink
introduced is_coordinate function
Browse files Browse the repository at this point in the history
  • Loading branch information
whisk committed Nov 4, 2023
1 parent 1a68c4b commit c56fbb4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
24 changes: 18 additions & 6 deletions pygeoif/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,35 @@ def move_coordinates(
Move the coordinates recursively by the given vector.
This forcefully changes the dimension of each of the coordinate to match
the dimension of the vector.
the dimensionality of the vector.
>>> move_coordinates(((0, 0), (-1, 1)), (-1, 1))
((-1, 1), (-2, 2))
>>> move_coordinates(((0, 0, 0), (-1, 1, 0)), (-1, 1))
((-1, 1), (-2, 2))
>>> move_coordinates(((0, 0), (-1, 1)), (-1, 1, 0))
((-1, 1, 0), (-2, 2, 0))
"""
if isinstance(coordinates, (tuple, list)) and isinstance(
coordinates[0],
(int, float),
):
# coordinates is just a list of numbers, i.e. represents a single coordinate
if is_coordinate(coordinates):
# a single coordinate
return move_coordinate(coordinates, move_by, z)
# a list of coordinates
return tuple(move_coordinates(c, move_by, z) for c in coordinates)


def is_coordinate(val: Any) -> bool:
"""
Check if given value is a coordinate i.e. vector of generic dimensionality.
>>> is_coordinate((1, 0))
True
>>> is_coordinate(1)
False
>>> is_coordinate([(1, 2), (3, 4)])
False
"""
return isinstance(val, tuple) and all(isinstance(x, (int, float)) for x in val)


__all__ = [
"centroid",
"compare_coordinates",
Expand Down
15 changes: 15 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pygeoif.functions import convex_hull
from pygeoif.functions import dedupe
from pygeoif.functions import signed_area
from pygeoif.functions import is_coordinate


def circle_ish(x, y, r, steps):
Expand Down Expand Up @@ -452,3 +453,17 @@ def test_compare_neq_empty_geo_interface() -> None:
}

assert compare_geo_interface(geo_if, {}) is False


def test_is_coordinate() -> None:
assert is_coordinate((1, 2)) is True
assert is_coordinate((1,)) is True


def test_is_coordinate_neq_composite_coords() -> None:
assert is_coordinate([(1, 2)]) is False
assert is_coordinate(((1, 2), )) is False


def test_is_coordinate_neq_primitive() -> None:
assert is_coordinate(1) is False

0 comments on commit c56fbb4

Please sign in to comment.